You are here

public function EntityAccessControlHandler::access in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Entity/EntityAccessControlHandler.php \Drupal\Core\Entity\EntityAccessControlHandler::access()

Checks access to an operation on a given entity or entity translation.

Use \Drupal\Core\Entity\EntityAccessControlHandlerInterface::createAccess() to check access to create an entity.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity for which to check access.

string $operation: The operation access should be checked for. Usually one of "view", "update" or "delete".

\Drupal\Core\Session\AccountInterface $account: (optional) The user session for which to check access, or NULL to check access for the current user. Defaults to NULL.

bool $return_as_object: (optional) Defaults to FALSE.

Return value

bool|\Drupal\Core\Access\AccessResultInterface The access result. Returns a boolean if $return_as_object is FALSE (this is the default) and otherwise an AccessResultInterface object. When a boolean is returned, the result of AccessInterface::isAllowed() is returned, i.e. TRUE means access is explicitly allowed, FALSE means access is either explicitly forbidden or "no opinion".

Overrides EntityAccessControlHandlerInterface::access

1 method overrides EntityAccessControlHandler::access()
NodeAccessControlHandler::access in core/modules/node/src/NodeAccessControlHandler.php
Checks access to an operation on a given entity or entity translation.

File

core/lib/Drupal/Core/Entity/EntityAccessControlHandler.php, line 56
Contains \Drupal\Core\Entity\EntityAccessControlHandler.

Class

EntityAccessControlHandler
Defines a default implementation for entity access control handler.

Namespace

Drupal\Core\Entity

Code

public function access(EntityInterface $entity, $operation, AccountInterface $account = NULL, $return_as_object = FALSE) {
  $account = $this
    ->prepareUser($account);
  $langcode = $entity
    ->language()
    ->getId();
  if (($return = $this
    ->getCache($entity
    ->uuid(), $operation, $langcode, $account)) !== NULL) {

    // Cache hit, no work necessary.
    return $return_as_object ? $return : $return
      ->isAllowed();
  }

  // Invoke hook_entity_access() and hook_ENTITY_TYPE_access(). Hook results
  // take precedence over overridden implementations of
  // EntityAccessControlHandler::checkAccess(). Entities that have checks that
  // need to be done before the hook is invoked should do so by overriding
  // this method.
  // 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.
  $access = array_merge($this
    ->moduleHandler()
    ->invokeAll('entity_access', [
    $entity,
    $operation,
    $account,
  ]), $this
    ->moduleHandler()
    ->invokeAll($entity
    ->getEntityTypeId() . '_access', [
    $entity,
    $operation,
    $account,
  ]));
  $return = $this
    ->processAccessHookResults($access);

  // 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()) {
    $return = $return
      ->orIf($this
      ->checkAccess($entity, $operation, $account));
  }
  $result = $this
    ->setCache($return, $entity
    ->uuid(), $operation, $langcode, $account);
  return $return_as_object ? $result : $result
    ->isAllowed();
}