Newer
Older

Dries Buytaert
committed
<?php
namespace Drupal\node;

catch
committed
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;

Alex Pott
committed
use Drupal\Core\Entity\EntityHandlerInterface;

Alex Pott
committed
use Drupal\Core\Entity\EntityTypeInterface;

Alex Pott
committed
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Entity\EntityAccessControlHandler;

Dries Buytaert
committed
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;

catch
committed
use Symfony\Component\DependencyInjection\ContainerInterface;

Dries Buytaert
committed
/**
* Defines the access control handler for the node entity type.
*
* @see \Drupal\node\Entity\Node
* @ingroup node_access

Dries Buytaert
committed
*/

Alex Pott
committed
class NodeAccessControlHandler extends EntityAccessControlHandler implements NodeAccessControlHandlerInterface, EntityHandlerInterface {

catch
committed
/**
* The node grant storage.
*

catch
committed
* @var \Drupal\node\NodeGrantDatabaseStorageInterface

catch
committed
*/
protected $grantStorage;
/**
* Constructs a NodeAccessControlHandler object.

catch
committed
*

Alex Pott
committed
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.

catch
committed
* @param \Drupal\node\NodeGrantDatabaseStorageInterface $grant_storage
* The node grant storage.
*/

Alex Pott
committed
public function __construct(EntityTypeInterface $entity_type, NodeGrantDatabaseStorageInterface $grant_storage) {
parent::__construct($entity_type);
$this->grantStorage = $grant_storage;

catch
committed
}
/**
* {@inheritdoc}
*/

Alex Pott
committed
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {

catch
committed
return new static(

Alex Pott
committed
$entity_type,

Alex Pott
committed
$container->get('node.grant_storage')

catch
committed
);
}

Dries Buytaert
committed
/**

Alex Pott
committed
* {@inheritdoc}

Dries Buytaert
committed
*/

Alex Bronstein
committed
public function access(EntityInterface $entity, $operation, AccountInterface $account = NULL, $return_as_object = FALSE) {
$account = $this->prepareUser($account);
if ($account->hasPermission('bypass node access')) {
$result = AccessResult::allowed()->cachePerPermissions();

catch
committed
return $return_as_object ? $result : $result->isAllowed();

Dries Buytaert
committed
}
if (!$account->hasPermission('access content')) {

Alex Pott
committed
$result = AccessResult::forbidden("The 'access content' permission is required.")->cachePerPermissions();

catch
committed
return $return_as_object ? $result : $result->isAllowed();

Dries Buytaert
committed
}

Alex Bronstein
committed
$result = parent::access($entity, $operation, $account, TRUE)->cachePerPermissions();

Alex Pott
committed

catch
committed
return $return_as_object ? $result : $result->isAllowed();

Alex Pott
committed
}

Dries Buytaert
committed
/**
* {@inheritdoc}
*/
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();

catch
committed
return $return_as_object ? $result : $result->isAllowed();
if (!$account->hasPermission('access content')) {
$result = AccessResult::forbidden("The 'access content' permission is required.")->cachePerPermissions();

catch
committed
return $return_as_object ? $result : $result->isAllowed();
}
$result = parent::createAccess($entity_bundle, $account, $context, TRUE)->cachePerPermissions();

catch
committed
return $return_as_object ? $result : $result->isAllowed();
}

Alex Pott
committed
/**
* {@inheritdoc}
*/

Alex Bronstein
committed
protected function checkAccess(EntityInterface $node, $operation, AccountInterface $account) {
/** @var \Drupal\node\NodeInterface $node */
// Fetch information from the node object if possible.

Alex Bronstein
committed
$status = $node->isPublished();
$uid = $node->getOwnerId();

Dries Buytaert
committed
// Check if authors can view their own unpublished nodes.

catch
committed
if ($operation === 'view' && !$status && $account->hasPermission('view own unpublished content') && $account->isAuthenticated() && $account->id() == $uid) {
return AccessResult::allowed()->cachePerPermissions()->cachePerUser()->addCacheableDependency($node);

Dries Buytaert
committed
}
// Evaluate node grants.
$access_result = $this->grantStorage->access($node, $operation, $account);
if ($operation === 'view' && $access_result instanceof RefinableCacheableDependencyInterface) {
// Node variations can affect the access to the node. For instance, the
// access result cache varies on the node's published status. Only the
// 'view' node grant can currently be cached. The 'update' and 'delete'
// grants are already marked as uncacheable in the node grant storage.
// @see \Drupal\node\NodeGrantDatabaseStorage::access()
$access_result->addCacheableDependency($node);
}
return $access_result;

Dries Buytaert
committed
}
/**
* {@inheritdoc}
*/
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
return AccessResult::allowedIf($account->hasPermission('create ' . $entity_bundle . ' content'))->cachePerPermissions();
}

Alex Pott
committed
/**
* {@inheritdoc}
*/
protected function checkFieldAccess($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
// Only users with the administer nodes permission can edit administrative
// fields.
$administrative_fields = ['uid', 'status', 'created', 'promote', 'sticky'];

Alex Pott
committed
if ($operation == 'edit' && in_array($field_definition->getName(), $administrative_fields, TRUE)) {

catch
committed
return AccessResult::allowedIfHasPermission($account, 'administer nodes');

Alex Pott
committed
}
// No user can change read only fields.
$read_only_fields = ['revision_timestamp', 'revision_uid'];

Alex Pott
committed
if ($operation == 'edit' && in_array($field_definition->getName(), $read_only_fields, TRUE)) {

catch
committed
return AccessResult::forbidden();

Alex Pott
committed
}
// Users have access to the revision_log field either if they have
// administrative permissions or if the new revision option is enabled.
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();
}

Alex Pott
committed
return parent::checkFieldAccess($operation, $field_definition, $account, $items);
}

Dries Buytaert
committed
/**

catch
committed
* {@inheritdoc}

Dries Buytaert
committed
*/

catch
committed
public function acquireGrants(NodeInterface $node) {
$grants = $this->moduleHandler->invokeAll('node_access_records', [$node]);

catch
committed
// Let modules alter the grants.
$this->moduleHandler->alter('node_access_records', $grants, $node);
// If no grants are set and the node is published, then use the default grant.
if (empty($grants) && $node->isPublished()) {
$grants[] = ['realm' => 'all', 'gid' => 0, 'grant_view' => 1, 'grant_update' => 0, 'grant_delete' => 0];

Dries Buytaert
committed
}

catch
committed
return $grants;
}

Dries Buytaert
committed

catch
committed
/**
* {@inheritdoc}
*/
public function writeDefaultGrant() {
$this->grantStorage->writeDefault();
}

Dries Buytaert
committed

catch
committed
/**
* {@inheritdoc}
*/
public function deleteGrants() {
$this->grantStorage->delete();
}
/**
* {@inheritdoc}
*/
public function countGrants() {
return $this->grantStorage->count();
}
/**
* {@inheritdoc}
*/
public function checkAllGrants(AccountInterface $account) {
return $this->grantStorage->checkAll($account);