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 = array();
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();
72 protected function checkAccess(ContentEntityInterface $entity, AccountInterface $account, $operation = 'view') {
73 $entity_type = $entity->getEntityType();
74 $entity_type_id = $entity->getEntityTypeId();
75 $entity_access = $this->entityTypeManager->getAccessControlHandler($entity_type_id);
77 /** @var \Drupal\Core\Entity\EntityStorageInterface $entity_storage */
78 $entity_storage = $this->entityTypeManager->getStorage($entity_type_id);
81 'view' => "view all $entity_type_id revisions",
82 'list' => "view all $entity_type_id revisions",
83 'update' => "revert all $entity_type_id revisions",
84 'delete' => "delete all $entity_type_id revisions",
86 $bundle = $entity->bundle();
88 'view' => "view $entity_type_id $bundle revisions",
89 'list' => "view $entity_type_id $bundle revisions",
90 'update' => "revert $entity_type_id $bundle revisions",
91 'delete' => "delete $entity_type_id $bundle revisions",
94 if (!$entity || !isset($map[$operation]) || !isset($type_map[$operation])) {
95 // If there was no node to check against, or the $op was not one of the
96 // supported ones, we return access denied.
100 // Statically cache access by revision ID, language code, user account ID,
102 $langcode = $entity->language()->getId();
103 $cid = $entity->getRevisionId() . ':' . $langcode . ':' . $account->id() . ':' . $operation;
105 if (!isset($this->accessCache[$cid])) {
106 $admin_permission = $entity_type->getAdminPermission();
108 // Perform basic permission checks first.
109 if (!$account->hasPermission($map[$operation]) && !$account->hasPermission($type_map[$operation]) && ($admin_permission && !$account->hasPermission($admin_permission))) {
110 $this->accessCache[$cid] = FALSE;
114 if (($admin_permission = $entity_type->getAdminPermission()) && $account->hasPermission($admin_permission)) {
115 $this->accessCache[$cid] = TRUE;
118 // Entity access handlers are generally not aware of the "list" operation.
119 $operation = $operation == 'list' ? 'view' : $operation;
120 // First check the access to the default revision and finally, if the
121 // node passed in is not the default revision then access to that, too.
122 $this->accessCache[$cid] = $entity_access->access($entity_storage->load($entity->id()), $operation, $account) && ($entity->isDefaultRevision() || $entity_access->access($entity, $operation, $account));
126 return $this->accessCache[$cid];
131 * Counts the number of revisions in the default language.
133 * @param \Drupal\Core\Entity\ContentEntityInterface $entity
135 * @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
136 * The entity storage.
139 * The number of revisions in the default language.
141 protected function countDefaultLanguageRevisions(ContentEntityInterface $entity, EntityStorageInterface $entity_storage) {
142 $entity_type = $entity->getEntityType();
143 $count = $entity_storage->getQuery()
145 ->condition($entity_type->getKey('id'), $entity->id())
146 ->condition($entity_type->getKey('default_langcode'), 1)
153 * Resets the access cache.
157 public function resetAccessCache() {
158 $this->accessCache = [];