You are here

protected function SubscriptionAccessControlHandler::checkAccess in Mailing List 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/SubscriptionAccessControlHandler.php, line 22

Class

SubscriptionAccessControlHandler
Access controller for the subscription entity.

Namespace

Drupal\mailing_list

Code

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

  /** @var \Drupal\mailing_list\SubscriptionInterface $entity */

  // Subscriptions administrators have global access.
  if ($account
    ->hasPermission('administer mailing list subscriptions')) {
    return AccessResult::allowed()
      ->cachePerPermissions();
  }

  // Treat view label operation as view.
  if ($operation == 'view label') {
    $operation = 'view';
  }
  $list_id = $entity
    ->getListId();

  // Inactive subscription access.
  if (!$entity
    ->isActive() && !$account
    ->hasPermission("access inactive {$list_id} mailing list subscriptions")) {
    return AccessResult::forbidden();
  }

  // Subscription owner check.
  $is_owner = $account
    ->id() == $entity
    ->getOwnerId();
  if ($account
    ->isAnonymous() || !$is_owner) {

    // Check for session grants.
    $is_owner = \Drupal::service('mailing_list.manager')
      ->hasSessionAccess($entity);
  }

  // Access allowed if user has unrestricted access or is the owner and can
  // subscribe to such mailing list.
  if ($account
    ->hasPermission("{$operation} any {$list_id} mailing list subscriptions") || $is_owner && $account
    ->hasPermission("subscribe to {$list_id} mailing list")) {
    return AccessResult::allowed();
  }

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