You are here

public function AdministrativeRoleCheck::access in Lightning Core 8.3

Same name and namespace in other branches
  1. 8.5 src/Access/AdministrativeRoleCheck.php \Drupal\lightning_core\Access\AdministrativeRoleCheck::access()
  2. 8 src/Access/AdministrativeRoleCheck.php \Drupal\lightning_core\Access\AdministrativeRoleCheck::access()
  3. 8.2 src/Access/AdministrativeRoleCheck.php \Drupal\lightning_core\Access\AdministrativeRoleCheck::access()
  4. 8.4 src/Access/AdministrativeRoleCheck.php \Drupal\lightning_core\Access\AdministrativeRoleCheck::access()

Checks if the user has at least one administrative role.

Parameters

\Symfony\Component\Routing\Route $route: The route being checked.

\Drupal\Core\Routing\RouteMatchInterface $route_match: The route matcher.

\Drupal\Core\Session\AccountInterface $account: The user account to check.

Return value

\Drupal\Core\Access\AccessResult The access result.

File

src/Access/AdministrativeRoleCheck.php, line 44

Class

AdministrativeRoleCheck

Namespace

Drupal\lightning_core\Access

Code

public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) {
  if (intval($account
    ->id()) === 1) {
    return AccessResult::allowed();
  }

  /** @var \Drupal\user\RoleInterface[] $roles */
  $roles = $this->entityTypeManager
    ->getStorage('user_role')
    ->loadMultiple($account
    ->getRoles(TRUE));
  foreach ($roles as $role) {
    if ($role
      ->isAdmin()) {
      return AccessResult::allowed();
    }
  }
  return AccessResult::forbidden('The user must have at least one administrative role.');
}