Mercurial > hg > isophonics-drupal-site
comparison core/modules/media/src/Access/MediaRevisionAccessCheck.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\media\Access; | |
4 | |
5 use Drupal\Core\Access\AccessResult; | |
6 use Drupal\Core\Entity\EntityTypeManagerInterface; | |
7 use Drupal\Core\Routing\Access\AccessInterface; | |
8 use Drupal\Core\Session\AccountInterface; | |
9 use Drupal\media\MediaInterface; | |
10 use Symfony\Component\Routing\Route; | |
11 | |
12 /** | |
13 * Provides an access checker for media item revisions. | |
14 * | |
15 * @ingroup media_access | |
16 */ | |
17 class MediaRevisionAccessCheck implements AccessInterface { | |
18 | |
19 /** | |
20 * The media storage. | |
21 * | |
22 * @var \Drupal\Core\Entity\ContentEntityStorageInterface | |
23 */ | |
24 protected $mediaStorage; | |
25 | |
26 /** | |
27 * The media access control handler. | |
28 * | |
29 * @var \Drupal\Core\Entity\EntityAccessControlHandlerInterface | |
30 */ | |
31 protected $mediaAccess; | |
32 | |
33 /** | |
34 * A static cache of access checks. | |
35 * | |
36 * @var array | |
37 */ | |
38 protected $access = []; | |
39 | |
40 /** | |
41 * Constructs a new MediaRevisionAccessCheck. | |
42 * | |
43 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager | |
44 * The entity type manager. | |
45 */ | |
46 public function __construct(EntityTypeManagerInterface $entity_type_manager) { | |
47 $this->mediaStorage = $entity_type_manager->getStorage('media'); | |
48 $this->mediaAccess = $entity_type_manager->getAccessControlHandler('media'); | |
49 } | |
50 | |
51 /** | |
52 * Checks routing access for the media item revision. | |
53 * | |
54 * @param \Symfony\Component\Routing\Route $route | |
55 * The route to check against. | |
56 * @param \Drupal\Core\Session\AccountInterface $account | |
57 * The currently logged in account. | |
58 * @param int $media_revision | |
59 * (optional) The media item revision ID. If not specified, but $media is, | |
60 * access is checked for that object's revision. | |
61 * @param \Drupal\media\MediaInterface $media | |
62 * (optional) A media item. Used for checking access to a media items | |
63 * default revision when $media_revision is unspecified. Ignored when | |
64 * $media_revision is specified. If neither $media_revision nor $media are | |
65 * specified, then access is denied. | |
66 * | |
67 * @return \Drupal\Core\Access\AccessResultInterface | |
68 * The access result. | |
69 */ | |
70 public function access(Route $route, AccountInterface $account, $media_revision = NULL, MediaInterface $media = NULL) { | |
71 if ($media_revision) { | |
72 $media = $this->mediaStorage->loadRevision($media_revision); | |
73 } | |
74 $operation = $route->getRequirement('_access_media_revision'); | |
75 return AccessResult::allowedIf($media && $this->checkAccess($media, $account, $operation))->cachePerPermissions()->addCacheableDependency($media); | |
76 } | |
77 | |
78 /** | |
79 * Checks media item revision access. | |
80 * | |
81 * @param \Drupal\media\MediaInterface $media | |
82 * The media item to check. | |
83 * @param \Drupal\Core\Session\AccountInterface $account | |
84 * A user object representing the user for whom the operation is to be | |
85 * performed. | |
86 * @param string $op | |
87 * (optional) The specific operation being checked. Defaults to 'view'. | |
88 * | |
89 * @return bool | |
90 * TRUE if the operation may be performed, FALSE otherwise. | |
91 */ | |
92 public function checkAccess(MediaInterface $media, AccountInterface $account, $op = 'view') { | |
93 if (!$media || $op !== 'view') { | |
94 // If there was no media to check against, or the $op was not one of the | |
95 // supported ones, we return access denied. | |
96 return FALSE; | |
97 } | |
98 | |
99 // Statically cache access by revision ID, language code, user account ID, | |
100 // and operation. | |
101 $langcode = $media->language()->getId(); | |
102 $cid = $media->getRevisionId() . ':' . $langcode . ':' . $account->id() . ':' . $op; | |
103 | |
104 if (!isset($this->access[$cid])) { | |
105 // Perform basic permission checks first. | |
106 if (!$account->hasPermission('view all media revisions') && !$account->hasPermission('administer media')) { | |
107 $this->access[$cid] = FALSE; | |
108 return FALSE; | |
109 } | |
110 | |
111 // There should be at least two revisions. If the revision ID of the | |
112 // given media item and the revision ID of the default revision differ, | |
113 // then we already have two different revisions so there is no need for a | |
114 // separate database check. | |
115 if ($media->isDefaultRevision() && ($this->countDefaultLanguageRevisions($media) == 1)) { | |
116 $this->access[$cid] = FALSE; | |
117 } | |
118 elseif ($account->hasPermission('administer media')) { | |
119 $this->access[$cid] = TRUE; | |
120 } | |
121 else { | |
122 // First check the access to the default revision and finally, if the | |
123 // media passed in is not the default revision then access to that, too. | |
124 $this->access[$cid] = $this->mediaAccess->access($this->mediaStorage->load($media->id()), $op, $account) && ($media->isDefaultRevision() || $this->mediaAccess->access($media, $op, $account)); | |
125 } | |
126 } | |
127 | |
128 return $this->access[$cid]; | |
129 } | |
130 | |
131 /** | |
132 * Counts the number of revisions in the default language. | |
133 * | |
134 * @param \Drupal\media\MediaInterface $media | |
135 * The media item for which to to count the revisions. | |
136 * | |
137 * @return int | |
138 * The number of revisions in the default language. | |
139 */ | |
140 protected function countDefaultLanguageRevisions(MediaInterface $media) { | |
141 $entity_type = $media->getEntityType(); | |
142 $count = $this->mediaStorage->getQuery() | |
143 ->allRevisions() | |
144 ->condition($entity_type->getKey('id'), $media->id()) | |
145 ->condition($entity_type->getKey('default_langcode'), 1) | |
146 ->count() | |
147 ->execute(); | |
148 return $count; | |
149 } | |
150 | |
151 } |