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

/**
 * @file
5
 * Contains \Drupal\node\NodeAccessControlHandler.
6 7 8 9
 */

namespace Drupal\node;

10
use Drupal\Core\Entity\EntityControllerInterface;
11
use Drupal\Core\Entity\EntityTypeInterface;
12 13
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
14
use Drupal\Core\Language\LanguageInterface;
15
use Drupal\Core\Entity\EntityAccessControlHandler;
16
use Drupal\Core\Entity\EntityInterface;
17
use Drupal\Core\Session\AccountInterface;
18
use Symfony\Component\DependencyInjection\ContainerInterface;
19 20

/**
21 22 23
 * Defines the access control handler for the node entity type.
 *
 * @see \Drupal\node\Entity\Node
24
 */
25
class NodeAccessControlHandler extends EntityAccessControlHandler implements NodeAccessControlHandlerInterface, EntityControllerInterface {
26 27 28 29

  /**
   * The node grant storage.
   *
30
   * @var \Drupal\node\NodeGrantStorageInterface
31 32 33 34
   */
  protected $grantStorage;

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

  /**
   * {@inheritdoc}
   */
50
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
51
    return new static(
52
      $entity_type,
53
      $container->get('node.grant_storage')
54 55 56
    );
  }

57 58

  /**
59
   * {@inheritdoc}
60
   */
61
  public function access(EntityInterface $entity, $operation, $langcode = LanguageInterface::LANGCODE_DEFAULT, AccountInterface $account = NULL) {
62 63 64
    $account = $this->prepareUser($account);

    if ($account->hasPermission('bypass node access')) {
65 66
      return TRUE;
    }
67
    if (!$account->hasPermission('access content')) {
68 69
      return FALSE;
    }
70 71
    return parent::access($entity, $operation, $langcode, $account);
  }
72

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

79
    if ($account->hasPermission('bypass node access')) {
80 81
      return TRUE;
    }
82
    if (!$account->hasPermission('access content')) {
83 84 85 86 87 88
      return FALSE;
    }

    return parent::createAccess($entity_bundle, $account, $context);
  }

89 90 91
  /**
   * {@inheritdoc}
   */
92
  protected function checkAccess(EntityInterface $node, $operation, $langcode, AccountInterface $account) {
93
    /** @var \Drupal\node\NodeInterface $node */
94 95
    /** @var \Drupal\node\NodeInterface $translation */
    $translation = $node->getTranslation($langcode);
96 97
    // Fetch information from the node object if possible.
    $status = $translation->isPublished();
98
    $uid = $translation->getOwnerId();
99 100

    // Check if authors can view their own unpublished nodes.
101
    if ($operation === 'view' && !$status && $account->hasPermission('view own unpublished content')) {
102 103 104 105 106 107 108 109

      if ($account->id() != 0 && $account->id() == $uid) {
        return TRUE;
      }
    }

    // If no module specified either allow or deny, we fall back to the
    // node_access table.
110
    if (($grants = $this->grantStorage->access($node, $operation, $langcode, $account)) !== NULL) {
111 112 113 114 115 116 117
      return $grants;
    }

    // If no modules implement hook_node_grants(), the default behavior is to
    // allow all users to view published nodes, so reflect that here.
    if ($operation === 'view') {
      return $status;
118 119 120
    }
  }

121 122 123 124
  /**
   * {@inheritdoc}
   */
  protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
125
    return $account->hasPermission('create ' . $entity_bundle . ' content');
126 127
  }

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
  /**
   * {@inheritdoc}
   */
  protected function checkFieldAccess($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
    $administrative_fields = array('uid', 'status', 'created', 'promote', 'sticky', 'revision_log');
    $read_only_fields = array('changed', 'revision_timestamp', 'revision_uid');
    if ($operation == 'edit' && in_array($field_definition->getName(), $administrative_fields)) {
      return $account->hasPermission('administer nodes');
    }
    if ($operation == 'edit' && in_array($field_definition->getName(), $read_only_fields)) {
      return FALSE;
    }
    return parent::checkFieldAccess($operation, $field_definition, $account, $items);
  }

143
  /**
144
   * {@inheritdoc}
145
   */
146 147 148 149 150 151 152
  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);
153
    }
154 155
    return $grants;
  }
156

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

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

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

}