View source
<?php
namespace Drupal\node;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\Core\Entity\EntityHandlerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class NodeAccessControlHandler extends EntityAccessControlHandler implements NodeAccessControlHandlerInterface, EntityHandlerInterface {
protected $grantStorage;
protected $entityTypeManager;
protected const REVISION_OPERATION_MAP = [
'view all revisions' => [
'view',
'view',
],
'view revision' => [
'view',
'view',
],
'revert revision' => [
'revert',
'update',
],
'delete revision' => [
'delete',
'delete',
],
];
public function __construct(EntityTypeInterface $entity_type, NodeGrantDatabaseStorageInterface $grant_storage, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($entity_type);
$this->grantStorage = $grant_storage;
$this->entityTypeManager = $entity_type_manager;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('node.grant_storage'), $container
->get('entity_type.manager'));
}
public function access(EntityInterface $entity, $operation, AccountInterface $account = NULL, $return_as_object = FALSE) {
$account = $this
->prepareUser($account);
if ($account
->hasPermission('bypass node access') && !isset(static::REVISION_OPERATION_MAP[$operation])) {
$result = AccessResult::allowed()
->cachePerPermissions();
return $return_as_object ? $result : $result
->isAllowed();
}
if (!$account
->hasPermission('access content')) {
$result = AccessResult::forbidden("The 'access content' permission is required.")
->cachePerPermissions();
return $return_as_object ? $result : $result
->isAllowed();
}
$result = parent::access($entity, $operation, $account, TRUE)
->cachePerPermissions();
return $return_as_object ? $result : $result
->isAllowed();
}
public function createAccess($entity_bundle = NULL, AccountInterface $account = NULL, array $context = [], $return_as_object = FALSE) {
$account = $this
->prepareUser($account);
if ($account
->hasPermission('bypass node access')) {
$result = AccessResult::allowed()
->cachePerPermissions();
return $return_as_object ? $result : $result
->isAllowed();
}
if (!$account
->hasPermission('access content')) {
$result = AccessResult::forbidden("The 'access content' permission is required.")
->cachePerPermissions();
return $return_as_object ? $result : $result
->isAllowed();
}
$result = parent::createAccess($entity_bundle, $account, $context, TRUE)
->cachePerPermissions();
return $return_as_object ? $result : $result
->isAllowed();
}
protected function checkAccess(EntityInterface $node, $operation, AccountInterface $account) {
$status = $node
->isPublished();
$uid = $node
->getOwnerId();
if ($operation === 'view' && !$status && $account
->hasPermission('view own unpublished content') && $account
->isAuthenticated() && $account
->id() == $uid) {
return AccessResult::allowed()
->cachePerPermissions()
->cachePerUser()
->addCacheableDependency($node);
}
[
$revision_permission_operation,
$entity_operation,
] = static::REVISION_OPERATION_MAP[$operation] ?? [
NULL,
NULL,
];
if ($revision_permission_operation) {
$bundle = $node
->bundle();
if (!$account
->hasPermission("{$revision_permission_operation} all revisions") && !$account
->hasPermission("{$revision_permission_operation} {$bundle} revisions") && !$account
->hasPermission('administer nodes')) {
return AccessResult::neutral()
->cachePerPermissions();
}
if ($operation === 'view all revisions') {
return AccessResult::allowed()
->cachePerPermissions();
}
if ($node
->isDefaultRevision() && ($operation === 'revert revision' || $operation === 'delete revision')) {
return AccessResult::forbidden()
->addCacheableDependency($node);
}
elseif ($account
->hasPermission('administer nodes')) {
return AccessResult::allowed()
->cachePerPermissions();
}
$node_storage = $this->entityTypeManager
->getStorage($node
->getEntityTypeId());
$access = $this
->access($node_storage
->load($node
->id()), $entity_operation, $account, TRUE);
if (!$node
->isDefaultRevision()) {
$access = $access
->andIf($this
->access($node, $entity_operation, $account, TRUE));
}
return $access
->cachePerPermissions()
->addCacheableDependency($node);
}
$access_result = $this->grantStorage
->access($node, $operation, $account);
if ($operation === 'view' && $access_result instanceof RefinableCacheableDependencyInterface) {
$access_result
->addCacheableDependency($node);
}
return $access_result;
}
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
return AccessResult::allowedIf($account
->hasPermission('create ' . $entity_bundle . ' content'))
->cachePerPermissions();
}
protected function checkFieldAccess($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
$administrative_fields = [
'uid',
'status',
'created',
'promote',
'sticky',
];
if ($operation == 'edit' && in_array($field_definition
->getName(), $administrative_fields, TRUE)) {
return AccessResult::allowedIfHasPermission($account, 'administer nodes');
}
$read_only_fields = [
'revision_timestamp',
'revision_uid',
];
if ($operation == 'edit' && in_array($field_definition
->getName(), $read_only_fields, TRUE)) {
return AccessResult::forbidden();
}
if ($operation == 'edit' && $field_definition
->getName() == 'revision_log') {
if ($account
->hasPermission('administer nodes')) {
return AccessResult::allowed()
->cachePerPermissions();
}
return AccessResult::allowedIf($items
->getEntity()->type->entity
->shouldCreateNewRevision())
->cachePerPermissions();
}
return parent::checkFieldAccess($operation, $field_definition, $account, $items);
}
public function acquireGrants(NodeInterface $node) {
$grants = $this->moduleHandler
->invokeAll('node_access_records', [
$node,
]);
$this->moduleHandler
->alter('node_access_records', $grants, $node);
if (empty($grants) && $node
->isPublished()) {
$grants[] = [
'realm' => 'all',
'gid' => 0,
'grant_view' => 1,
'grant_update' => 0,
'grant_delete' => 0,
];
}
return $grants;
}
public function writeDefaultGrant() {
$this->grantStorage
->writeDefault();
}
public function deleteGrants() {
$this->grantStorage
->delete();
}
public function countGrants() {
return $this->grantStorage
->count();
}
public function checkAllGrants(AccountInterface $account) {
return $this->grantStorage
->checkAll($account);
}
}