You are here

protected function Cookie::getUserFromSession in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/user/src/Authentication/Provider/Cookie.php \Drupal\user\Authentication\Provider\Cookie::getUserFromSession()

Returns the UserSession object for the given session.

Parameters

\Symfony\Component\HttpFoundation\Session\SessionInterface $session: The session.

Return value

\Drupal\Core\Session\AccountInterface|NULL The UserSession object for the current user, or NULL if this is an anonymous session.

1 call to Cookie::getUserFromSession()
Cookie::authenticate in core/modules/user/src/Authentication/Provider/Cookie.php
Authenticates the user.

File

core/modules/user/src/Authentication/Provider/Cookie.php, line 74
Contains \Drupal\user\Authentication\Provider\Cookie.

Class

Cookie
Cookie based authentication provider.

Namespace

Drupal\user\Authentication\Provider

Code

protected function getUserFromSession(SessionInterface $session) {
  if ($uid = $session
    ->get('uid')) {

    // @todo Load the User entity in SessionHandler so we don't need queries.
    // @see https://www.drupal.org/node/2345611
    $values = $this->connection
      ->query('SELECT * FROM {users_field_data} u WHERE u.uid = :uid AND u.default_langcode = 1', [
      ':uid' => $uid,
    ])
      ->fetchAssoc();

    // Check if the user data was found and the user is active.
    if (!empty($values) && $values['status'] == 1) {

      // Add the user's roles.
      $rids = $this->connection
        ->query('SELECT roles_target_id FROM {user__roles} WHERE entity_id = :uid', [
        ':uid' => $values['uid'],
      ])
        ->fetchCol();
      $values['roles'] = array_merge([
        AccountInterface::AUTHENTICATED_ROLE,
      ], $rids);
      return new UserSession($values);
    }
  }

  // This is an anonymous session.
  return NULL;
}