Added Entity and Entity Reference Revisions which got dropped somewhere along the...
[yaffs-website] / web / modules / contrib / entity / src / Access / EntityRevisionRouteAccessChecker.php
1 <?php
2
3 namespace Drupal\entity\Access;
4
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Entity\ContentEntityInterface;
7 use Drupal\Core\Entity\EntityStorageInterface;
8 use Drupal\Core\Entity\EntityTypeManagerInterface;
9 use Drupal\Core\Routing\Access\AccessInterface;
10 use Drupal\Core\Routing\RouteMatchInterface;
11 use Drupal\Core\Session\AccountInterface;
12 use Symfony\Component\Routing\Route;
13
14 /**
15  * Checks access to a entity revision.
16  */
17 class EntityRevisionRouteAccessChecker implements AccessInterface {
18
19   /**
20    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
21    */
22   protected $entityTypeManager;
23
24   /**
25    * Stores calculated access check results.
26    *
27    * @var array
28    */
29   protected $accessCache = [];
30
31   /**
32    * The currently active route match object.
33    *
34    * @var \Drupal\Core\Routing\RouteMatchInterface
35    */
36   protected $routeMatch;
37
38   /**
39    * Creates a new EntityRevisionRouteAccessChecker instance.
40    *
41    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
42    *   The entity manager.
43    * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
44    *   The currently active route match object.
45    */
46   public function __construct(EntityTypeManagerInterface $entity_type_manager, RouteMatchInterface $route_match) {
47     $this->entityTypeManager = $entity_type_manager;
48     $this->routeMatch = $route_match;
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   public function access(Route $route, AccountInterface $account, RouteMatchInterface $route_match = NULL) {
55     if (empty($route_match)) {
56       $route_match = $this->routeMatch;
57     }
58
59     $operation = $route->getRequirement('_entity_access_revision');
60     list($entity_type_id, $operation) = explode('.', $operation, 2);
61
62     if ($operation === 'list') {
63       $_entity = $route_match->getParameter($entity_type_id);
64       return AccessResult::allowedIf($this->checkAccess($_entity, $account, $operation))->cachePerPermissions();
65     }
66     else {
67       $_entity_revision = $route_match->getParameter($entity_type_id . '_revision');
68       return AccessResult::allowedIf($_entity_revision && $this->checkAccess($_entity_revision, $account, $operation))->cachePerPermissions();
69     }
70   }
71
72   /**
73    * Performs access checks.
74    *
75    * @param \Drupal\Core\Entity\ContentEntityInterface $entity
76    *   The entity for which to check access.
77    * @param \Drupal\Core\Session\AccountInterface $account
78    *   The user for which to check access.
79    * @param string $operation
80    *   The entity operation. Usually one of 'view', 'view label', 'update' or
81    *   'delete'.
82    *
83    * @return bool
84    *   The access result.
85    */
86   protected function checkAccess(ContentEntityInterface $entity, AccountInterface $account, $operation = 'view') {
87     $entity_type = $entity->getEntityType();
88     $entity_type_id = $entity->getEntityTypeId();
89     $entity_access = $this->entityTypeManager->getAccessControlHandler($entity_type_id);
90
91     /** @var \Drupal\Core\Entity\EntityStorageInterface $entity_storage */
92     $entity_storage = $this->entityTypeManager->getStorage($entity_type_id);
93
94     $map = [
95       'view' => "view all $entity_type_id revisions",
96       'list' => "view all $entity_type_id revisions",
97       'update' => "revert all $entity_type_id revisions",
98       'delete' => "delete all $entity_type_id revisions",
99     ];
100     $bundle = $entity->bundle();
101     $type_map = [
102       'view' => "view $entity_type_id $bundle revisions",
103       'list' => "view $entity_type_id $bundle revisions",
104       'update' => "revert $entity_type_id $bundle revisions",
105       'delete' => "delete $entity_type_id $bundle revisions",
106     ];
107
108     if (!$entity || !isset($map[$operation]) || !isset($type_map[$operation])) {
109       // If there was no node to check against, or the $op was not one of the
110       // supported ones, we return access denied.
111       return FALSE;
112     }
113
114     // Statically cache access by revision ID, language code, user account ID,
115     // and operation.
116     $langcode = $entity->language()->getId();
117     $cid = $entity->getRevisionId() . ':' . $langcode . ':' . $account->id() . ':' . $operation;
118
119     if (!isset($this->accessCache[$cid])) {
120       $admin_permission = $entity_type->getAdminPermission();
121
122       // Perform basic permission checks first.
123       if (!$account->hasPermission($map[$operation]) && !$account->hasPermission($type_map[$operation]) && ($admin_permission && !$account->hasPermission($admin_permission))) {
124         $this->accessCache[$cid] = FALSE;
125         return FALSE;
126       }
127
128       if (($admin_permission = $entity_type->getAdminPermission()) && $account->hasPermission($admin_permission)) {
129         $this->accessCache[$cid] = TRUE;
130       }
131       else {
132         // Entity access handlers are generally not aware of the "list" operation.
133         $operation = $operation == 'list' ? 'view' : $operation;
134         // First check the access to the default revision and finally, if the
135         // node passed in is not the default revision then access to that, too.
136         $this->accessCache[$cid] = $entity_access->access($entity_storage->load($entity->id()), $operation, $account) && ($entity->isDefaultRevision() || $entity_access->access($entity, $operation, $account));
137       }
138     }
139
140     return $this->accessCache[$cid];
141   }
142
143   /**
144    * Counts the number of revisions in the default language.
145    *
146    * @param \Drupal\Core\Entity\ContentEntityInterface $entity
147    *   The entity.
148    * @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
149    *   The entity storage.
150    *
151    * @return int
152    *   The number of revisions in the default language.
153    */
154   protected function countDefaultLanguageRevisions(ContentEntityInterface $entity, EntityStorageInterface $entity_storage) {
155     $entity_type = $entity->getEntityType();
156     $count = $entity_storage->getQuery()
157       ->allRevisions()
158       ->condition($entity_type->getKey('id'), $entity->id())
159       ->condition($entity_type->getKey('default_langcode'), 1)
160       ->count()
161       ->execute();
162     return $count;
163   }
164
165   /**
166    * Resets the access cache.
167    *
168    * @return $this
169    */
170   public function resetAccessCache() {
171     $this->accessCache = [];
172     return $this;
173   }
174
175 }