NodeAccessControlHandler.php 6.58 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\node;

5
use Drupal\Core\Access\AccessResult;
6
use Drupal\Core\Entity\EntityHandlerInterface;
7
use Drupal\Core\Entity\EntityTypeInterface;
8 9
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
10
use Drupal\Core\Entity\EntityAccessControlHandler;
11
use Drupal\Core\Entity\EntityInterface;
12
use Drupal\Core\Session\AccountInterface;
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14 15

/**
16 17 18
 * Defines the access control handler for the node entity type.
 *
 * @see \Drupal\node\Entity\Node
19
 * @ingroup node_access
20
 */
21
class NodeAccessControlHandler extends EntityAccessControlHandler implements NodeAccessControlHandlerInterface, EntityHandlerInterface {
22 23 24 25

  /**
   * The node grant storage.
   *
26
   * @var \Drupal\node\NodeGrantDatabaseStorageInterface
27 28 29 30
   */
  protected $grantStorage;

  /**
31
   * Constructs a NodeAccessControlHandler object.
32
   *
33 34
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
35 36 37
   * @param \Drupal\node\NodeGrantDatabaseStorageInterface $grant_storage
   *   The node grant storage.
   */
38 39
  public function __construct(EntityTypeInterface $entity_type, NodeGrantDatabaseStorageInterface $grant_storage) {
    parent::__construct($entity_type);
40
    $this->grantStorage = $grant_storage;
41 42 43 44 45
  }

  /**
   * {@inheritdoc}
   */
46
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
47
    return new static(
48
      $entity_type,
49
      $container->get('node.grant_storage')
50 51 52
    );
  }

53 54

  /**
55
   * {@inheritdoc}
56
   */
57
  public function access(EntityInterface $entity, $operation, AccountInterface $account = NULL, $return_as_object = FALSE) {
58 59 60
    $account = $this->prepareUser($account);

    if ($account->hasPermission('bypass node access')) {
61
      $result = AccessResult::allowed()->cachePerPermissions();
62
      return $return_as_object ? $result : $result->isAllowed();
63
    }
64
    if (!$account->hasPermission('access content')) {
65
      $result = AccessResult::forbidden()->cachePerPermissions();
66
      return $return_as_object ? $result : $result->isAllowed();
67
    }
68
    $result = parent::access($entity, $operation, $account, TRUE)->cachePerPermissions();
69
    return $return_as_object ? $result : $result->isAllowed();
70
  }
71

72 73 74
  /**
   * {@inheritdoc}
   */
75
  public function createAccess($entity_bundle = NULL, AccountInterface $account = NULL, array $context = array(), $return_as_object = FALSE) {
76 77
    $account = $this->prepareUser($account);

78
    if ($account->hasPermission('bypass node access')) {
79
      $result = AccessResult::allowed()->cachePerPermissions();
80
      return $return_as_object ? $result : $result->isAllowed();
81
    }
82
    if (!$account->hasPermission('access content')) {
83
      $result = AccessResult::forbidden()->cachePerPermissions();
84
      return $return_as_object ? $result : $result->isAllowed();
85 86
    }

87
    $result = parent::createAccess($entity_bundle, $account, $context, TRUE)->cachePerPermissions();
88
    return $return_as_object ? $result : $result->isAllowed();
89 90
  }

91 92 93
  /**
   * {@inheritdoc}
   */
94
  protected function checkAccess(EntityInterface $node, $operation, AccountInterface $account) {
95
    /** @var \Drupal\node\NodeInterface $node */
96

97
    // Fetch information from the node object if possible.
98 99
    $status = $node->isPublished();
    $uid = $node->getOwnerId();
100 101

    // Check if authors can view their own unpublished nodes.
102
    if ($operation === 'view' && !$status && $account->hasPermission('view own unpublished content') && $account->isAuthenticated() && $account->id() == $uid) {
103
      return AccessResult::allowed()->cachePerPermissions()->cachePerUser()->addCacheableDependency($node);
104 105
    }

106
    // Evaluate node grants.
107
    return $this->grantStorage->access($node, $operation, $account);
108 109
  }

110 111 112 113
  /**
   * {@inheritdoc}
   */
  protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
114
    return AccessResult::allowedIf($account->hasPermission('create ' . $entity_bundle . ' content'))->cachePerPermissions();
115 116
  }

117 118 119 120
  /**
   * {@inheritdoc}
   */
  protected function checkFieldAccess($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
121 122 123
    // Only users with the administer nodes permission can edit administrative
    // fields.
    $administrative_fields = array('uid', 'status', 'created', 'promote', 'sticky');
124
    if ($operation == 'edit' && in_array($field_definition->getName(), $administrative_fields, TRUE)) {
125
      return AccessResult::allowedIfHasPermission($account, 'administer nodes');
126
    }
127 128

    // No user can change read only fields.
129
    $read_only_fields = array('revision_timestamp', 'revision_uid');
130
    if ($operation == 'edit' && in_array($field_definition->getName(), $read_only_fields, TRUE)) {
131
      return AccessResult::forbidden();
132
    }
133 134 135 136 137

    // 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')) {
138
        return AccessResult::allowed()->cachePerPermissions();
139
      }
140
      return AccessResult::allowedIf($items->getEntity()->type->entity->isNewRevision())->cachePerPermissions();
141
    }
142 143 144
    return parent::checkFieldAccess($operation, $field_definition, $account, $items);
  }

145
  /**
146
   * {@inheritdoc}
147
   */
148 149 150 151 152 153 154
  public function acquireGrants(NodeInterface $node) {
    $grants = $this->moduleHandler->invokeAll('node_access_records', array($node));
    // 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[] = array('realm' => 'all', 'gid' => 0, 'grant_view' => 1, 'grant_update' => 0, 'grant_delete' => 0);
155
    }
156 157
    return $grants;
  }
158

159 160 161 162 163 164 165
  /**
   * {@inheritdoc}
   */
  public function writeGrants(NodeInterface $node, $delete = TRUE) {
    $grants = $this->acquireGrants($node);
    $this->grantStorage->write($node, $grants, NULL, $delete);
  }
166

167 168 169 170 171 172
  /**
   * {@inheritdoc}
   */
  public function writeDefaultGrant() {
    $this->grantStorage->writeDefault();
  }
173

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
  /**
   * {@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);
193 194 195
  }

}