You are here

public function SubscriptionAddAccessCheck::access in Mailing List 8

Checks access to the subscription add page for the mailing list.

Parameters

\Drupal\Core\Session\AccountInterface $account: The currently logged in account.

\Drupal\mailing_list\MailingListInterface $mailing_list: (optional) The mailing list. If not specified, access is allowed if there exists at least one mailing list for which the user may create a node.

Return value

string A \Drupal\Core\Access\AccessInterface constant value.

File

src/Access/SubscriptionAddAccessCheck.php, line 45

Class

SubscriptionAddAccessCheck
Determines access to for subscription add pages.

Namespace

Drupal\mailing_list\Access

Code

public function access(AccountInterface $account, MailingListInterface $mailing_list = NULL) {
  $access_control_handler = $this->entityManager
    ->getAccessControlHandler('mailing_list_subscription');

  // If checking whether a subscription of a particular list may be created.
  if ($account
    ->hasPermission('administer mailing list subscriptions')) {
    return AccessResult::allowed()
      ->cachePerPermissions();
  }
  if ($mailing_list) {
    return $access_control_handler
      ->createAccess($mailing_list
      ->id(), $account, [], TRUE);
  }

  // If checking whether a subscription for any mailing list may be created.
  foreach ($this->entityManager
    ->getStorage('mailing_list')
    ->loadMultiple() as $mailing_list) {
    if (($access = $access_control_handler
      ->createAccess($mailing_list
      ->id(), $account, [], TRUE)) && $access
      ->isAllowed()) {
      return $access;
    }
  }

  // No opinion.
  return AccessResult::neutral();
}