Chris@0: nodeStorage = $entity_type_manager->getStorage('node'); Chris@18: $this->nodeAccess = $entity_type_manager->getAccessControlHandler('node'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks routing access for the node revision. Chris@0: * Chris@0: * @param \Symfony\Component\Routing\Route $route Chris@0: * The route to check against. Chris@0: * @param \Drupal\Core\Session\AccountInterface $account Chris@0: * The currently logged in account. Chris@0: * @param int $node_revision Chris@0: * (optional) The node revision ID. If not specified, but $node is, access Chris@0: * is checked for that object's revision. Chris@0: * @param \Drupal\node\NodeInterface $node Chris@0: * (optional) A node object. Used for checking access to a node's default Chris@0: * revision when $node_revision is unspecified. Ignored when $node_revision Chris@0: * is specified. If neither $node_revision nor $node are specified, then Chris@0: * access is denied. Chris@0: * Chris@0: * @return \Drupal\Core\Access\AccessResultInterface Chris@0: * The access result. Chris@0: */ Chris@0: public function access(Route $route, AccountInterface $account, $node_revision = NULL, NodeInterface $node = NULL) { Chris@0: if ($node_revision) { Chris@0: $node = $this->nodeStorage->loadRevision($node_revision); Chris@0: } Chris@0: $operation = $route->getRequirement('_access_node_revision'); Chris@0: return AccessResult::allowedIf($node && $this->checkAccess($node, $account, $operation))->cachePerPermissions()->addCacheableDependency($node); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks node revision access. Chris@0: * Chris@0: * @param \Drupal\node\NodeInterface $node Chris@0: * The node to check. Chris@0: * @param \Drupal\Core\Session\AccountInterface $account Chris@0: * A user object representing the user for whom the operation is to be Chris@0: * performed. Chris@0: * @param string $op Chris@0: * (optional) The specific operation being checked. Defaults to 'view.' Chris@0: * Chris@0: * @return bool Chris@0: * TRUE if the operation may be performed, FALSE otherwise. Chris@0: */ Chris@0: public function checkAccess(NodeInterface $node, AccountInterface $account, $op = 'view') { Chris@0: $map = [ Chris@0: 'view' => 'view all revisions', Chris@0: 'update' => 'revert all revisions', Chris@0: 'delete' => 'delete all revisions', Chris@0: ]; Chris@0: $bundle = $node->bundle(); Chris@0: $type_map = [ Chris@0: 'view' => "view $bundle revisions", Chris@0: 'update' => "revert $bundle revisions", Chris@0: 'delete' => "delete $bundle revisions", Chris@0: ]; Chris@0: Chris@0: if (!$node || !isset($map[$op]) || !isset($type_map[$op])) { Chris@0: // If there was no node to check against, or the $op was not one of the Chris@0: // supported ones, we return access denied. Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: // Statically cache access by revision ID, language code, user account ID, Chris@0: // and operation. Chris@0: $langcode = $node->language()->getId(); Chris@0: $cid = $node->getRevisionId() . ':' . $langcode . ':' . $account->id() . ':' . $op; Chris@0: Chris@0: if (!isset($this->access[$cid])) { Chris@0: // Perform basic permission checks first. Chris@0: if (!$account->hasPermission($map[$op]) && !$account->hasPermission($type_map[$op]) && !$account->hasPermission('administer nodes')) { Chris@0: $this->access[$cid] = FALSE; Chris@0: return FALSE; Chris@0: } Chris@18: // If the revisions checkbox is selected for the content type, display the Chris@18: // revisions tab. Chris@18: $bundle_entity_type = $node->getEntityType()->getBundleEntityType(); Chris@18: $bundle_entity = \Drupal::entityTypeManager()->getStorage($bundle_entity_type)->load($bundle); Chris@18: if ($bundle_entity->shouldCreateNewRevision() && $op === 'view') { Chris@0: $this->access[$cid] = TRUE; Chris@0: } Chris@0: else { Chris@18: // There should be at least two revisions. If the vid of the given node Chris@18: // and the vid of the default revision differ, then we already have two Chris@18: // different revisions so there is no need for a separate database Chris@18: // check. Also, if you try to revert to or delete the default revision, Chris@18: // that's not good. Chris@18: if ($node->isDefaultRevision() && ($this->nodeStorage->countDefaultLanguageRevisions($node) == 1 || $op === 'update' || $op === 'delete')) { Chris@18: $this->access[$cid] = FALSE; Chris@18: } Chris@18: elseif ($account->hasPermission('administer nodes')) { Chris@18: $this->access[$cid] = TRUE; Chris@18: } Chris@18: else { Chris@18: // First check the access to the default revision and finally, if the Chris@18: // node passed in is not the default revision then check access to Chris@18: // that, too. Chris@18: $this->access[$cid] = $this->nodeAccess->access($this->nodeStorage->load($node->id()), $op, $account) && ($node->isDefaultRevision() || $this->nodeAccess->access($node, $op, $account)); Chris@18: } Chris@0: } Chris@0: } Chris@0: Chris@0: return $this->access[$cid]; Chris@0: } Chris@0: Chris@0: }