You are here

class AdministrativeRoleCheck in Lightning Core 8.3

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

Hierarchy

Expanded class hierarchy of AdministrativeRoleCheck

1 file declares its use of AdministrativeRoleCheck
AdministrativeRoleCheckTest.php in tests/src/Kernel/Access/AdministrativeRoleCheckTest.php
1 string reference to 'AdministrativeRoleCheck'
lightning_core.services.yml in ./lightning_core.services.yml
lightning_core.services.yml
1 service uses AdministrativeRoleCheck
access_check.administrator_role in ./lightning_core.services.yml
\Drupal\lightning_core\Access\AdministrativeRoleCheck

File

src/Access/AdministrativeRoleCheck.php, line 12

Namespace

Drupal\lightning_core\Access
View source
class AdministrativeRoleCheck implements AccessInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * AdministratorRoleAccessCheck constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * Checks if the user has at least one administrative role.
   *
   * @param \Symfony\Component\Routing\Route $route
   *   The route being checked.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route matcher.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The user account to check.
   *
   * @return \Drupal\Core\Access\AccessResult
   *   The access result.
   */
  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.');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AdministrativeRoleCheck::$entityTypeManager protected property The entity type manager.
AdministrativeRoleCheck::access public function Checks if the user has at least one administrative role.
AdministrativeRoleCheck::__construct public function AdministratorRoleAccessCheck constructor.