You are here

protected function EntityRevisionRouteAccessChecker::checkAccess in Entity API 8.0

Same name and namespace in other branches
  1. 8 src/Access/EntityRevisionRouteAccessChecker.php \Drupal\entity\Access\EntityRevisionRouteAccessChecker::checkAccess()

File

src/Access/EntityRevisionRouteAccessChecker.php, line 63
Contains \Drupal\entity\Access\EntityRevisionRouteAccessChecker

Class

EntityRevisionRouteAccessChecker
Checks access to a entity revision.

Namespace

Drupal\entity\Access

Code

protected function checkAccess(ContentEntityInterface $entity, AccountInterface $account, $operation = 'view') {
  $entity_type = $entity
    ->getEntityType();
  $entity_type_id = $entity
    ->getEntityTypeId();
  $entity_access = $this->entityTypeManager
    ->getAccessControlHandler($entity_type_id);

  /** @var \Drupal\Core\Entity\EntityStorageInterface $entity_storage */
  $entity_storage = $this->entityTypeManager
    ->getStorage($entity_type_id);
  $map = [
    'view' => "view all {$entity_type_id} revisions",
    'list' => "view all {$entity_type_id} revisions",
    'update' => "revert all {$entity_type_id} revisions",
    'delete' => "delete all {$entity_type_id} revisions",
  ];
  $bundle = $entity
    ->bundle();
  $type_map = [
    'view' => "view {$entity_type_id} {$bundle} revisions",
    'list' => "view {$entity_type_id} {$bundle} revisions",
    'update' => "revert {$entity_type_id} {$bundle} revisions",
    'delete' => "delete {$entity_type_id} {$bundle} revisions",
  ];
  if (!$entity || !isset($map[$operation]) || !isset($type_map[$operation])) {

    // If there was no node to check against, or the $op was not one of the
    // supported ones, we return access denied.
    return FALSE;
  }

  // Statically cache access by revision ID, language code, user account ID,
  // and operation.
  $langcode = $entity
    ->language()
    ->getId();
  $cid = $entity
    ->getRevisionId() . ':' . $langcode . ':' . $account
    ->id() . ':' . $operation;
  if (!isset($this->accessCache[$cid])) {

    // Perform basic permission checks first.
    if (!$account
      ->hasPermission($map[$operation]) && !$account
      ->hasPermission($type_map[$operation]) && !$account
      ->hasPermission('administer nodes')) {
      $this->accessCache[$cid] = FALSE;
      return FALSE;
    }
    if (($admin_permission = $entity_type
      ->getAdminPermission()) && $account
      ->hasPermission($admin_permission)) {
      $this->accessCache[$cid] = TRUE;
    }
    else {

      // First check the access to the default revision and finally, if the
      // node passed in is not the default revision then access to that, too.
      $this->accessCache[$cid] = $entity_access
        ->access($entity_storage
        ->load($entity
        ->id()), $operation, $account) && ($entity
        ->isDefaultRevision() || $entity_access
        ->access($entity, $operation, $account));
    }
  }
  return $this->accessCache[$cid];
}