You are here

class LoginStatusCheck in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/user/src/Access/LoginStatusCheck.php \Drupal\user\Access\LoginStatusCheck

Determines access to routes based on login status of current user.

Hierarchy

Expanded class hierarchy of LoginStatusCheck

1 string reference to 'LoginStatusCheck'
user.services.yml in core/modules/user/user.services.yml
core/modules/user/user.services.yml
1 service uses LoginStatusCheck
access_check.user.login_status in core/modules/user/user.services.yml
Drupal\user\Access\LoginStatusCheck

File

core/modules/user/src/Access/LoginStatusCheck.php, line 13

Namespace

Drupal\user\Access
View source
class LoginStatusCheck implements AccessInterface {

  /**
   * Checks access.
   *
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The currently logged in account.
   * @param \Symfony\Component\Routing\Route $route
   *   The route to check against.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
   */
  public function access(AccountInterface $account, Route $route) {
    $required_status = filter_var($route
      ->getRequirement('_user_is_logged_in'), FILTER_VALIDATE_BOOLEAN);
    $actual_status = $account
      ->isAuthenticated();
    $access_result = AccessResult::allowedIf($required_status === $actual_status)
      ->addCacheContexts([
      'user.roles:authenticated',
    ]);
    if (!$access_result
      ->isAllowed()) {
      $access_result
        ->setReason($required_status === TRUE ? 'This route can only be accessed by authenticated users.' : 'This route can only be accessed by anonymous users.');
    }
    return $access_result;
  }

}

Members