You are here

public function LoginStatusCheck::access in Drupal 9

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

Checks access.

Parameters

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

\Symfony\Component\Routing\Route $route: The route to check against.

Return value

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

File

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

Class

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

Namespace

Drupal\user\Access

Code

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;
}