You are here

public function MongodbSessionHandler::read in MongoDB 8

Overrides SessionHandler::read

File

mongodb_user/src/MongodbSessionHandler.php, line 39
Contains \Drupal\mongodb_user\MongodbSessionHandler.

Class

MongodbSessionHandler

Namespace

Drupal\mongodb_user

Code

public function read($sid) {
  global $_session_user;

  // Handle the case of first time visitors and clients that don't store
  // cookies (eg. web crawlers).
  $cookies = $this->requestStack
    ->getCurrentRequest()->cookies;
  if (empty($sid) || !$cookies
    ->has($this
    ->getName())) {
    $_session_user = new UserSession();
    return '';
  }

  // Try to load a session using the non-HTTPS session id.
  $values = $this
    ->findOne([
    'sid' => Crypt::hashBase64($sid),
  ]);

  // We found the client's session record and they are an authenticated,
  // active user.
  if ($values && $values['uid'] > 0 && $values['status'] == 1) {
    $values['roles'][] = DRUPAL_AUTHENTICATED_RID;
    $_session_user = new UserSession($values);
  }
  elseif ($values) {

    // The user is anonymous or blocked. Only preserve two fields from the
    // {sessions} table.
    $_session_user = new UserSession(array(
      'session' => $values['session'],
      'access' => $values['access'],
    ));
  }
  else {

    // The session has expired.
    $_session_user = new UserSession();
  }
  return $_session_user->session;
}