You are here

public function MailingListAccessControlHandler::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/MailingListAccessControlHandler.php, line 20

Class

MailingListAccessControlHandler
Defines an access controller for the mailing list entity.

Namespace

Drupal\mailing_list

Code

public function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
  if ($account
    ->hasPermission('administer mailing lists')) {
    return AccessResult::allowed();
  }
  $list_id = $entity
    ->id();
  if ($operation == 'view' || $operation == 'view label') {

    // Allowed for users with this entity related permission.
    $allowed_permissions = [
      'administer mailing list subscriptions',
      "subscribe to {$list_id} mailing list",
      "view any {$list_id} mailing list subscriptions",
      "update any {$list_id} mailing list subscriptions",
    ];
    foreach ($allowed_permissions as $permission) {
      if ($account
        ->hasPermission($permission)) {
        return AccessResult::allowed();
      }
    }
  }
  return parent::checkAccess($entity, $operation, $account);
}