You are here

public function Subscribe::access in Mailing List 8

Determine if the current user has access or not.

Parameters

\Drupal\Core\Session\AccountInterface $account: The user who wants to access this view.

Return value

bool Returns whether the user has access to the view.

Overrides AccessPluginBase::access

File

src/Plugin/views/access/Subscribe.php, line 75

Class

Subscribe
Views access plugin that provides access for mailing list subscribers.

Namespace

Drupal\mailing_list\Plugin\views\access

Code

public function access(AccountInterface $account) {

  // Walk over user roles.
  foreach ($this->entityManager
    ->getStorage('user_role')
    ->loadMultiple($account
    ->getRoles()) as $role) {

    /** @var \Drupal\user\RoleInterface $role */
    if ($role
      ->isAdmin()) {
      return TRUE;
    }

    // Look for any subscribe permission on current user role.
    foreach ($role
      ->getPermissions() as $permission) {
      $matches = [];

      // Mailing list bundle must exists.
      if (preg_match('/^subscribe to (.+) mailing list$/', $permission, $matches) && $this->entityManager
        ->getStorage('mailing_list')
        ->load($matches[1])) {
        return TRUE;
      }
    }
  }
  return FALSE;
}