3 namespace Drupal\entity\Access;
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;
15 * Checks access to a entity revision.
17 class EntityRevisionRouteAccessChecker implements AccessInterface {
20 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
22 protected $entityTypeManager;
25 * Stores calculated access check results.
29 protected $accessCache = [];
32 * The currently active route match object.
34 * @var \Drupal\Core\Routing\RouteMatchInterface
36 protected $routeMatch;
39 * Creates a new EntityRevisionRouteAccessChecker instance.
41 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
43 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
44 * The currently active route match object.
46 public function __construct(EntityTypeManagerInterface $entity_type_manager, RouteMatchInterface $route_match) {
47 $this->entityTypeManager = $entity_type_manager;
48 $this->routeMatch = $route_match;
54 public function access(Route $route, AccountInterface $account, RouteMatchInterface $route_match = NULL) {
55 if (empty($route_match)) {
56 $route_match = $this->routeMatch;
59 $operation = $route->getRequirement('_entity_access_revision');
60 list($entity_type_id, $operation) = explode('.', $operation, 2);
62 if ($operation === 'list') {
63 $_entity = $route_match->getParameter($entity_type_id);
64 return AccessResult::allowedIf($this->checkAccess($_entity, $account, $operation))->cachePerPermissions();
67 $_entity_revision = $route_match->getParameter($entity_type_id . '_revision');
68 return AccessResult::allowedIf($_entity_revision && $this->checkAccess($_entity_revision, $account, $operation))->cachePerPermissions();
73 * Performs access checks.
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
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);
91 /** @var \Drupal\Core\Entity\EntityStorageInterface $entity_storage */
92 $entity_storage = $this->entityTypeManager->getStorage($entity_type_id);
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",
100 $bundle = $entity->bundle();
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",
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.
114 // Statically cache access by revision ID, language code, user account ID,
116 $langcode = $entity->language()->getId();
117 $cid = $entity->getRevisionId() . ':' . $langcode . ':' . $account->id() . ':' . $operation;
119 if (!isset($this->accessCache[$cid])) {
120 $admin_permission = $entity_type->getAdminPermission();
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;
128 if (($admin_permission = $entity_type->getAdminPermission()) && $account->hasPermission($admin_permission)) {
129 $this->accessCache[$cid] = TRUE;
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));
140 return $this->accessCache[$cid];
144 * Counts the number of revisions in the default language.
146 * @param \Drupal\Core\Entity\ContentEntityInterface $entity
148 * @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
149 * The entity storage.
152 * The number of revisions in the default language.
154 protected function countDefaultLanguageRevisions(ContentEntityInterface $entity, EntityStorageInterface $entity_storage) {
155 $entity_type = $entity->getEntityType();
156 $count = $entity_storage->getQuery()
158 ->condition($entity_type->getKey('id'), $entity->id())
159 ->condition($entity_type->getKey('default_langcode'), 1)
166 * Resets the access cache.
170 public function resetAccessCache() {
171 $this->accessCache = [];