EntityAccessControlHandler.php 14.3 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\Core\Entity;

5
use Drupal\Core\Access\AccessResult;
6 7
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
8
use Drupal\Core\Language\LanguageInterface;
9
use Drupal\Core\Session\AccountInterface;
10 11

/**
12
 * Defines a default implementation for entity access control handler.
13
 */
14
class EntityAccessControlHandler extends EntityHandlerBase implements EntityAccessControlHandlerInterface {
15

16
  /**
17
   * Stores calculated access check results.
18 19 20
   *
   * @var array
   */
21
  protected $accessCache = [];
22

23
  /**
24
   * The entity type ID of the access control handler instance.
25 26 27
   *
   * @var string
   */
28
  protected $entityTypeId;
29

30
  /**
31
   * Information about the entity type.
32
   *
33
   * @var \Drupal\Core\Entity\EntityTypeInterface
34
   */
35
  protected $entityType;
36

37 38 39 40 41 42 43 44 45 46
  /**
   * Allows to grant access to just the labels.
   *
   * By default, the "view label" operation falls back to "view". Set this to
   * TRUE to allow returning different access when just listing entity labels.
   *
   * @var bool
   */
  protected $viewLabelOperation = FALSE;

47
  /**
48
   * Constructs an access control handler instance.
49
   *
50 51
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
52
   */
53 54 55
  public function __construct(EntityTypeInterface $entity_type) {
    $this->entityTypeId = $entity_type->id();
    $this->entityType = $entity_type;
56 57
  }

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

65 66 67 68
    if ($operation === 'view label' && $this->viewLabelOperation == FALSE) {
      $operation = 'view';
    }

69 70 71 72 73 74 75 76 77 78 79 80 81
    // If an entity does not have a UUID, either from not being set or from not
    // having them, use the 'entity type:ID' pattern as the cache $cid.
    $cid = $entity->uuid() ?: $entity->getEntityTypeId() . ':' . $entity->id();

    // If the entity is revisionable, then append the revision ID to allow
    // individual revisions to have specific access control and be cached
    // separately.
    if ($entity instanceof RevisionableInterface) {
      /** @var $entity \Drupal\Core\Entity\RevisionableInterface */
      $cid .= ':' . $entity->getRevisionId();
    }

    if (($return = $this->getCache($cid, $operation, $langcode, $account)) !== NULL) {
82
      // Cache hit, no work necessary.
83
      return $return_as_object ? $return : $return->isAllowed();
84 85
    }

86 87
    // Invoke hook_entity_access() and hook_ENTITY_TYPE_access(). Hook results
    // take precedence over overridden implementations of
88
    // EntityAccessControlHandler::checkAccess(). Entities that have checks that
89 90
    // need to be done before the hook is invoked should do so by overriding
    // this method.
91

92 93 94
    // We grant access to the entity if both of these conditions are met:
    // - No modules say to deny access.
    // - At least one module says to grant access.
95
    $access = array_merge(
96 97
      $this->moduleHandler()->invokeAll('entity_access', [$entity, $operation, $account]),
      $this->moduleHandler()->invokeAll($entity->getEntityTypeId() . '_access', [$entity, $operation, $account])
98
    );
99

100
    $return = $this->processAccessHookResults($access);
101 102 103 104

    // Also execute the default access check except when the access result is
    // already forbidden, as in that case, it can not be anything else.
    if (!$return->isForbidden()) {
105
      $return = $return->orIf($this->checkAccess($entity, $operation, $account));
106
    }
107
    $result = $this->setCache($return, $cid, $operation, $langcode, $account);
108
    return $return_as_object ? $result : $result->isAllowed();
109 110 111 112 113 114 115
  }

  /**
   * We grant access to the entity if both of these conditions are met:
   * - No modules say to deny access.
   * - At least one module says to grant access.
   *
116
   * @param \Drupal\Core\Access\AccessResultInterface[] $access
117 118
   *   An array of access results of the fired access hook.
   *
119 120 121 122 123
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The combined result of the various access checks' results. All their
   *   cacheability metadata is merged as well.
   *
   * @see \Drupal\Core\Access\AccessResultInterface::orIf()
124 125
   */
  protected function processAccessHookResults(array $access) {
126 127
    // No results means no opinion.
    if (empty($access)) {
128
      return AccessResult::neutral();
129
    }
130 131 132 133 134

    /** @var \Drupal\Core\Access\AccessResultInterface $result */
    $result = array_shift($access);
    foreach ($access as $other) {
      $result = $result->orIf($other);
135
    }
136
    return $result;
137 138 139
  }

  /**
140 141 142 143
   * Performs access checks.
   *
   * This method is supposed to be overwritten by extending classes that
   * do their own custom access checking.
144 145
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
146
   *   The entity for which to check access.
147
   * @param string $operation
148 149
   *   The entity operation. Usually one of 'view', 'view label', 'update' or
   *   'delete'.
150
   * @param \Drupal\Core\Session\AccountInterface $account
151
   *   The user for which to check access.
152
   *
153 154
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
155
   */
156
  protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
157
    if ($operation == 'delete' && $entity->isNew()) {
158
      return AccessResult::forbidden()->addCacheableDependency($entity);
159
    }
160
    if ($admin_permission = $this->entityType->getAdminPermission()) {
161
      return AccessResult::allowedIfHasPermission($account, $admin_permission);
162 163
    }
    else {
164
      // No opinion.
165
      return AccessResult::neutral();
166
    }
167 168 169 170 171
  }

  /**
   * Tries to retrieve a previously cached access value from the static cache.
   *
172 173 174
   * @param string $cid
   *   Unique string identifier for the entity/operation, for example the
   *   entity UUID or a custom string.
175
   * @param string $operation
176
   *   The entity operation. Usually one of 'view', 'update', 'create' or
177 178
   *   'delete'.
   * @param string $langcode
179
   *   The language code for which to check access.
180
   * @param \Drupal\Core\Session\AccountInterface $account
181
   *   The user for which to check access.
182
   *
183 184 185
   * @return \Drupal\Core\Access\AccessResultInterface|null
   *   The cached AccessResult, or NULL if there is no record for the given
   *   user, operation, langcode and entity in the cache.
186
   */
187
  protected function getCache($cid, $operation, $langcode, AccountInterface $account) {
188
    // Return from cache if a value has been set for it previously.
189 190
    if (isset($this->accessCache[$account->id()][$cid][$langcode][$operation])) {
      return $this->accessCache[$account->id()][$cid][$langcode][$operation];
191 192 193 194 195 196
    }
  }

  /**
   * Statically caches whether the given user has access.
   *
197 198
   * @param \Drupal\Core\Access\AccessResultInterface $access
   *   The access result.
199 200 201
   * @param string $cid
   *   Unique string identifier for the entity/operation, for example the
   *   entity UUID or a custom string.
202
   * @param string $operation
203
   *   The entity operation. Usually one of 'view', 'update', 'create' or
204 205
   *   'delete'.
   * @param string $langcode
206
   *   The language code for which to check access.
207
   * @param \Drupal\Core\Session\AccountInterface $account
208
   *   The user for which to check access.
209
   *
210 211
   * @return \Drupal\Core\Access\AccessResultInterface
   *   Whether the user has access, plus cacheability metadata.
212
   */
213
  protected function setCache($access, $cid, $operation, $langcode, AccountInterface $account) {
214
    // Save the given value in the static cache and directly return it.
215
    return $this->accessCache[$account->id()][$cid][$langcode][$operation] = $access;
216 217 218
  }

  /**
219
   * {@inheritdoc}
220 221
   */
  public function resetCache() {
222
    $this->accessCache = [];
223 224
  }

225 226 227
  /**
   * {@inheritdoc}
   */
228
  public function createAccess($entity_bundle = NULL, AccountInterface $account = NULL, array $context = [], $return_as_object = FALSE) {
229
    $account = $this->prepareUser($account);
230
    $context += [
231
      'entity_type_id' => $this->entityTypeId,
232
      'langcode' => LanguageInterface::LANGCODE_DEFAULT,
233
    ];
234 235 236 237

    $cid = $entity_bundle ? 'create:' . $entity_bundle : 'create';
    if (($access = $this->getCache($cid, 'create', $context['langcode'], $account)) !== NULL) {
      // Cache hit, no work necessary.
238
      return $return_as_object ? $access : $access->isAllowed();
239 240
    }

241 242
    // Invoke hook_entity_create_access() and hook_ENTITY_TYPE_create_access().
    // Hook results take precedence over overridden implementations of
243 244 245
    // EntityAccessControlHandler::checkCreateAccess(). Entities that have
    // checks that need to be done before the hook is invoked should do so by
    // overriding this method.
246 247 248 249

    // We grant access to the entity if both of these conditions are met:
    // - No modules say to deny access.
    // - At least one module says to grant access.
250
    $access = array_merge(
251 252
      $this->moduleHandler()->invokeAll('entity_create_access', [$account, $context, $entity_bundle]),
      $this->moduleHandler()->invokeAll($this->entityTypeId . '_create_access', [$account, $context, $entity_bundle])
253
    );
254

255
    $return = $this->processAccessHookResults($access);
256 257 258 259

    // Also execute the default access check except when the access result is
    // already forbidden, as in that case, it can not be anything else.
    if (!$return->isForbidden()) {
260
      $return = $return->orIf($this->checkCreateAccess($account, $context, $entity_bundle));
261
    }
262 263
    $result = $this->setCache($return, $cid, 'create', $context['langcode'], $account);
    return $return_as_object ? $result : $result->isAllowed();
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
  }

  /**
   * Performs create access checks.
   *
   * This method is supposed to be overwritten by extending classes that
   * do their own custom access checking.
   *
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The user for which to check access.
   * @param array $context
   *   An array of key-value pairs to pass additional context when needed.
   * @param string|null $entity_bundle
   *   (optional) The bundle of the entity. Required if the entity supports
   *   bundles, defaults to NULL otherwise.
   *
280 281
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
282 283
   */
  protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
284
    if ($admin_permission = $this->entityType->getAdminPermission()) {
285
      return AccessResult::allowedIfHasPermission($account, $admin_permission);
286 287
    }
    else {
288
      // No opinion.
289
      return AccessResult::neutral();
290
    }
291 292 293 294 295 296 297 298 299 300 301 302 303
  }

  /**
   * Loads the current account object, if it does not exist yet.
   *
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The account interface instance.
   *
   * @return \Drupal\Core\Session\AccountInterface
   *   Returns the current account object.
   */
  protected function prepareUser(AccountInterface $account = NULL) {
    if (!$account) {
304
      $account = \Drupal::currentUser();
305 306 307 308
    }
    return $account;
  }

309 310 311
  /**
   * {@inheritdoc}
   */
312
  public function fieldAccess($operation, FieldDefinitionInterface $field_definition, AccountInterface $account = NULL, FieldItemListInterface $items = NULL, $return_as_object = FALSE) {
313 314 315
    $account = $this->prepareUser($account);

    // Get the default access restriction that lives within this field.
316
    $default = $items ? $items->defaultAccess($operation, $account) : AccessResult::allowed();
317

318
    // Explicitly disallow changing the entity ID and entity UUID.
319 320
    $entity = $items ? $items->getEntity() : NULL;
    if ($operation === 'edit' && $entity) {
321
      if ($field_definition->getName() === $this->entityType->getKey('id')) {
322 323
        // String IDs can be set when creating the entity.
        if (!($entity->isNew() && $field_definition->getType() === 'string')) {
324
          return $return_as_object ? AccessResult::forbidden('The entity ID cannot be changed.')->addCacheableDependency($entity) : FALSE;
325
        }
326 327 328
      }
      elseif ($field_definition->getName() === $this->entityType->getKey('uuid')) {
        // UUIDs can be set when creating an entity.
329
        if (!$entity->isNew()) {
330
          return $return_as_object ? AccessResult::forbidden('The entity UUID cannot be changed.')->addCacheableDependency($entity) : FALSE;
331 332 333 334
        }
      }
    }

335 336
    // Get the default access restriction as specified by the access control
    // handler.
337 338 339
    $entity_default = $this->checkFieldAccess($operation, $field_definition, $account, $items);

    // Combine default access, denying access wins.
340
    $default = $default->andIf($entity_default);
341

342 343
    // Invoke hook and collect grants/denies for field access from other
    // modules. Our default access flag is masked under the ':default' key.
344
    $grants = [':default' => $default];
345
    $hook_implementations = $this->moduleHandler()->getImplementations('entity_field_access');
346
    foreach ($hook_implementations as $module) {
347
      $grants = array_merge($grants, [$module => $this->moduleHandler()->invoke($module, 'entity_field_access', [$operation, $field_definition, $account, $items])]);
348 349 350
    }

    // Also allow modules to alter the returned grants/denies.
351
    $context = [
352 353 354 355
      'operation' => $operation,
      'field_definition' => $field_definition,
      'items' => $items,
      'account' => $account,
356
    ];
357
    $this->moduleHandler()->alter('entity_field_access', $grants, $context);
358

359 360
    $result = $this->processAccessHookResults($grants);
    return $return_as_object ? $result : $result->isAllowed();
361 362
  }

363
  /**
364
   * Default field access as determined by this access control handler.
365 366 367 368 369 370 371 372 373 374 375 376 377
   *
   * @param string $operation
   *   The operation access should be checked for.
   *   Usually one of "view" or "edit".
   * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
   *   The field definition.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The user session for which to check access.
   * @param \Drupal\Core\Field\FieldItemListInterface $items
   *   (optional) The field values for which to check access, or NULL if access
   *   is checked for the field definition, without any specific value
   *   available. Defaults to NULL.
   *
378 379
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
380 381
   */
  protected function checkFieldAccess($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
382
    return AccessResult::allowed();
383 384
  }

385
}