NodeAccessControlHandler.php 5.03 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
use Drupal\Core\Language\LanguageInterface;
13
use Drupal\Core\Entity\EntityAccessControlHandler;
14
use Drupal\Core\Entity\EntityInterface;
15
use Drupal\Core\Session\AccountInterface;
16
use Symfony\Component\DependencyInjection\ContainerInterface;
17 18

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

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

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

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

55 56

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

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

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

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

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

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

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

      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.
108
    if (($grants = $this->grantStorage->access($node, $operation, $langcode, $account)) !== NULL) {
109 110 111 112 113 114 115
      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;
116 117 118
    }
  }

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

129
  /**
130
   * {@inheritdoc}
131
   */
132 133 134 135 136 137 138
  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);
139
    }
140 141
    return $grants;
  }
142

143 144 145 146 147 148 149
  /**
   * {@inheritdoc}
   */
  public function writeGrants(NodeInterface $node, $delete = TRUE) {
    $grants = $this->acquireGrants($node);
    $this->grantStorage->write($node, $grants, NULL, $delete);
  }
150

151 152 153 154 155 156
  /**
   * {@inheritdoc}
   */
  public function writeDefaultGrant() {
    $this->grantStorage->writeDefault();
  }
157

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
  /**
   * {@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);
177 178 179
  }

}