You are here

protected function ReferenceAccessControlHandler::checkAccess in Bibliography & Citation 2.0.x

Same name and namespace in other branches
  1. 8 modules/bibcite_entity/src/ReferenceAccessControlHandler.php \Drupal\bibcite_entity\ReferenceAccessControlHandler::checkAccess()

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

modules/bibcite_entity/src/ReferenceAccessControlHandler.php, line 22

Class

ReferenceAccessControlHandler
Access controller for the Reference entity.

Namespace

Drupal\bibcite_entity

Code

protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
  $type = $entity
    ->bundle();

  /** @var \Drupal\bibcite_entity\Entity\Reference $entity */
  switch ($operation) {
    case 'view':
      return AccessResult::allowedIf($entity
        ->isPublished() && $account
        ->hasPermission('view bibcite_reference'))
        ->orIf(AccessResult::allowedIf(!$entity
        ->isPublished() && $account
        ->hasPermission('view own unpublished bibcite_reference') && $account
        ->isAuthenticated() && $entity
        ->getOwnerId() == $account
        ->id()))
        ->cachePerPermissions()
        ->cachePerUser()
        ->addCacheableDependency($entity);
    case 'update':
      return AccessResult::allowedIfHasPermission($account, 'edit any bibcite_reference')
        ->orIf(AccessResult::allowedIfHasPermission($account, "edit any {$type} bibcite_reference"))
        ->orIf(AccessResult::allowedIf($entity
        ->getOwnerId() == $account
        ->id() && ($account
        ->hasPermission('edit own bibcite_reference') || $account
        ->hasPermission("edit own {$type} bibcite_reference")))
        ->cachePerPermissions()
        ->cachePerUser());
    case 'delete':
      return AccessResult::allowedIfHasPermission($account, 'delete any bibcite_reference')
        ->orIf(AccessResult::allowedIfHasPermission($account, "delete any {$type} bibcite_reference"))
        ->orIf(AccessResult::allowedIf($entity
        ->getOwnerId() == $account
        ->id() && ($account
        ->hasPermission('delete own bibcite_reference') || $account
        ->hasPermission("delete own {$type} bibcite_reference")))
        ->cachePerPermissions()
        ->cachePerUser());
  }

  // Unknown operation, no opinion.
  return AccessResult::neutral();
}