You are here

protected function FlaggingCollectionAccessControlHandler::checkAccess in Flag Lists 8

Same name and namespace in other branches
  1. 4.0.x src/Access/FlaggingCollectionAccessControlHandler.php \Drupal\flag_lists\Access\FlaggingCollectionAccessControlHandler::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

src/Access/FlaggingCollectionAccessControlHandler.php, line 20

Class

FlaggingCollectionAccessControlHandler
Access controller for the Flagging collection entity.

Namespace

Drupal\flag_lists\Access

Code

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

  /** @var \Drupal\flag_lists\Entity\FlaggingCollectionInterface $entity */
  switch ($operation) {
    case 'view':
      if ($account
        ->id() == $entity
        ->getOwner()
        ->id()) {

        // These are my own flagging collections.
        return AccessResult::allowedIfHasPermission($account, 'view own flag lists');
      }
      elseif ($entity
        ->getBaseFlag()
        ->isGlobal()) {
        return AccessResult::allowedIfHasPermission($account, 'access global flag lists');
      }
      else {
        return AccessResult::allowedIfHasPermission($account, 'administer flag lists');
      }
    case 'update':
    case 'delete':
      if ($account
        ->id() == $entity
        ->getOwner()
        ->id()) {

        // These are my own flagging collections.
        return AccessResult::allowedIfHasPermission($account, 'edit own flag lists');
      }
      else {
        return AccessResult::allowedIfHasPermission($account, 'administer flag lists');
      }
  }

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