function LoginStatusCheck::access

Same name in other branches
  1. 9 core/modules/user/src/Access/LoginStatusCheck.php \Drupal\user\Access\LoginStatusCheck::access()
  2. 8.9.x core/modules/user/src/Access/LoginStatusCheck.php \Drupal\user\Access\LoginStatusCheck::access()
  3. 11.x 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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.