Bolt\AccessControl\AccessChecker::isValidSession PHP Method

isValidSession() public method

1. If user has a valid session and it is fresh, check against cookie: - If NOT a match refuse - If a match accept 2. If user has a valid session and it is stale (>10 minutes), check the database records again: - If disabled refuse - If enabled - If NOT a match refuse - If a match accept - Update session data 3. If user has no session check authtoken table entry (closed broswer): - If passed validity date refuse - If within validity date, hash username and IP against salt and compare to database: - If NOT a match refuse - If a match accept
public isValidSession ( string $authCookie ) : boolean
$authCookie string
return boolean
    public function isValidSession($authCookie)
    {
        if ($authCookie === null) {
            throw new AccessControlException('Can not validate session with an empty token.');
        }
        if ($this->validSession !== null) {
            return $this->validSession;
        }
        $check = false;
        $sessionAuth = null;
        /** @var \Bolt\AccessControl\Token\Token $sessionAuth */
        if ($this->session->isStarted() && ($sessionAuth = $this->session->get('authentication'))) {
            $check = $this->checkSessionStored($sessionAuth);
        }
        if (!$check) {
            // Either the session keys don't match, or the session is too old
            $check = $this->checkSessionDatabase($authCookie);
        }
        if ($check) {
            return $this->validSession = true;
        }
        $this->validSession = false;
        $this->systemLogger->debug("Clearing sessions for expired or invalid token: {$authCookie}", ['event' => 'authentication']);
        return $this->revokeSession();
    }

Usage Example

Example #1
0
 /**
  * When redirecting to the backend dashboard (while logged in),
  * if the user does not have access change the redirect to the homepage.
  *
  * @param \Symfony\Component\HttpFoundation\RedirectResponse $response
  */
 protected function handleNoBackendAccess(RedirectResponse $response)
 {
     $authCookie = $this->session->get('authentication');
     if (!$this->authentication->isValidSession((string) $authCookie)) {
         return;
     }
     $dashboardPath = $this->urlGenerator->generate('dashboard');
     $dashboardAccess = $this->users->isAllowed('dashboard');
     if ($response->getTargetUrl() === $dashboardPath && !$dashboardAccess) {
         $this->session->getFlashBag()->clear();
         $response->setTargetUrl($this->urlGenerator->generate('homepage'));
     }
 }