You are here

protected function AccessControlHandler::checkAccess in Consumers 8

Performs access checks.

This method is supposed to be overwritten by extending classes that do their own custom access checking.

Parameters

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

string $operation: The entity operation. Usually one of 'view', 'view label', 'update' or 'delete'.

\Drupal\Core\Session\AccountInterface $account: The user for which to check access.

Return value

\Drupal\Core\Access\AccessResultInterface The access result.

Overrides EntityAccessControlHandler::checkAccess

File

src/AccessControlHandler.php, line 22

Class

AccessControlHandler
Access controller for the Access Token entity.

Namespace

Drupal\consumers

Code

protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {

  /** @var \Drupal\consumers\Entity\Consumer $entity */
  $admin_permission = $this->entityType
    ->getAdminPermission();
  if ($account
    ->hasPermission($admin_permission)) {
    return AccessResult::allowed()
      ->cachePerPermissions();
  }

  // Permissions only apply to own entities.
  $is_owner = $account
    ->id() && $account
    ->id() === $entity
    ->getOwnerId();
  $is_owner_access = AccessResult::allowedIf($is_owner)
    ->addCacheableDependency($entity);
  $operations = [
    'view',
    'update',
    'delete',
  ];
  if (!in_array($operation, $operations)) {
    $reason = sprintf('Supported operations on the entity are %s', implode(', ', $operations));
    return AccessResult::neutral($reason);
  }
  return $is_owner_access
    ->andIf(AccessResult::allowedIfHasPermission($account, sprintf('%s own %s entities', $operation, static::$name))
    ->cachePerPermissions());
}