3 namespace Drupal\node\Access;
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\Core\Routing\Access\AccessInterface;
8 use Drupal\Core\Session\AccountInterface;
9 use Drupal\node\NodeInterface;
10 use Symfony\Component\Routing\Route;
13 * Provides an access checker for node revisions.
15 * @ingroup node_access
17 class NodeRevisionAccessCheck implements AccessInterface {
22 * @var \Drupal\node\NodeStorageInterface
24 protected $nodeStorage;
27 * The node access control handler.
29 * @var \Drupal\Core\Entity\EntityAccessControlHandlerInterface
31 protected $nodeAccess;
34 * A static cache of access checks.
38 protected $access = [];
41 * Constructs a new NodeRevisionAccessCheck.
43 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
46 public function __construct(EntityManagerInterface $entity_manager) {
47 $this->nodeStorage = $entity_manager->getStorage('node');
48 $this->nodeAccess = $entity_manager->getAccessControlHandler('node');
52 * Checks routing access for the node revision.
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 $node_revision
59 * (optional) The node revision ID. If not specified, but $node is, access
60 * is checked for that object's revision.
61 * @param \Drupal\node\NodeInterface $node
62 * (optional) A node object. Used for checking access to a node's default
63 * revision when $node_revision is unspecified. Ignored when $node_revision
64 * is specified. If neither $node_revision nor $node are specified, then
67 * @return \Drupal\Core\Access\AccessResultInterface
70 public function access(Route $route, AccountInterface $account, $node_revision = NULL, NodeInterface $node = NULL) {
72 $node = $this->nodeStorage->loadRevision($node_revision);
74 $operation = $route->getRequirement('_access_node_revision');
75 return AccessResult::allowedIf($node && $this->checkAccess($node, $account, $operation))->cachePerPermissions()->addCacheableDependency($node);
79 * Checks node revision access.
81 * @param \Drupal\node\NodeInterface $node
83 * @param \Drupal\Core\Session\AccountInterface $account
84 * A user object representing the user for whom the operation is to be
87 * (optional) The specific operation being checked. Defaults to 'view.'
90 * TRUE if the operation may be performed, FALSE otherwise.
92 public function checkAccess(NodeInterface $node, AccountInterface $account, $op = 'view') {
94 'view' => 'view all revisions',
95 'update' => 'revert all revisions',
96 'delete' => 'delete all revisions',
98 $bundle = $node->bundle();
100 'view' => "view $bundle revisions",
101 'update' => "revert $bundle revisions",
102 'delete' => "delete $bundle revisions",
105 if (!$node || !isset($map[$op]) || !isset($type_map[$op])) {
106 // If there was no node to check against, or the $op was not one of the
107 // supported ones, we return access denied.
111 // Statically cache access by revision ID, language code, user account ID,
113 $langcode = $node->language()->getId();
114 $cid = $node->getRevisionId() . ':' . $langcode . ':' . $account->id() . ':' . $op;
116 if (!isset($this->access[$cid])) {
117 // Perform basic permission checks first.
118 if (!$account->hasPermission($map[$op]) && !$account->hasPermission($type_map[$op]) && !$account->hasPermission('administer nodes')) {
119 $this->access[$cid] = FALSE;
123 // There should be at least two revisions. If the vid of the given node
124 // and the vid of the default revision differ, then we already have two
125 // different revisions so there is no need for a separate database check.
126 // Also, if you try to revert to or delete the default revision, that's
128 if ($node->isDefaultRevision() && ($this->nodeStorage->countDefaultLanguageRevisions($node) == 1 || $op == 'update' || $op == 'delete')) {
129 $this->access[$cid] = FALSE;
131 elseif ($account->hasPermission('administer nodes')) {
132 $this->access[$cid] = TRUE;
135 // First check the access to the default revision and finally, if the
136 // node passed in is not the default revision then access to that, too.
137 $this->access[$cid] = $this->nodeAccess->access($this->nodeStorage->load($node->id()), $op, $account) && ($node->isDefaultRevision() || $this->nodeAccess->access($node, $op, $account));
141 return $this->access[$cid];