You are here

function _memcache_session_user_load in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 modules/memcache/unstable/memcache-session.inc \_memcache_session_user_load()

Create the user object.

Parameters

$session: The session object (see sess_write() for the structure).

Return value

$user The user object.

2 calls to _memcache_session_user_load()
_drupal_session_read in modules/memcache/unstable/memcache-session.inc
_drupal_session_write in modules/memcache/unstable/memcache-session.inc
Write a session to session storage.

File

modules/memcache/unstable/memcache-session.inc, line 336

Code

function _memcache_session_user_load($session) {

  // We found the client's session record and they are an authenticated user.
  if ($session && $session->uid != 0) {
    $user = dmemcache_get($session->uid, 'users');

    // If the 'users' memcache bin is unavailable, $user will be NULL.
    // If the cached user was not found in the 'users' memcache bin, $user will
    // be FALSE.
    // In either of these cases, the user must be retrieved from the database.
    if (!$user && isset($session->uid) && $session->uid != 0) {
      $user = db_query('SELECT u.* FROM {users} u WHERE u.uid = :uid', array(
        'uid' => $session->uid,
      ))
        ->fetchObject();
      if (!$user->status) {
        $user = drupal_anonymous_user();
        $user->session = empty($session->session) ? '' : $session->session;
      }
      else {
        $user = drupal_unpack($user);

        // Normally we would join the session and user tables. But we already
        // have the session information. So add that in.
        $user->sid = $session->sid;
        $user->cache = $session->cache;
        $user->hostname = $session->hostname;
        $user->timestamp = $session->timestamp;
        $user->session = empty($session->session) ? '' : $session->session;
        $user->data = unserialize($user->data);

        // Add roles element to $user
        $user->roles = array();
        $user->roles[DRUPAL_AUTHENTICATED_RID] = 'authenticated user';
        $result = db_query("SELECT r.rid, r.name FROM {role} r INNER JOIN {users_roles} ur ON ur.rid = r.rid WHERE ur.uid = :uid", array(
          ':uid' => $user->uid,
        ));
        while ($role = $result
          ->fetchObject()) {
          $user->roles[$role->rid] = $role->name;
        }
      }
    }
    else {
      if ($user->uid) {

        // Got a user object from 'users' memcache bin. Mark it in case modules
        // want to know that this user was created from memcache.
        $user->from_cache = TRUE;
        $user->session = empty($session->session) ? '' : $session->session;
      }
      else {

        // We will only get here when the session has a nonzero uid, a user object
        // was successfully retrieved from the 'users' bin, and that user
        // object's uid is 0. Not sure why this would ever happen. Leaving former
        // comment in:
        // This is a rare case that we have a session cached, but no session user object cached.
        // This usually only happens if you kill memcached and restart it.
        $user = drupal_anonymous_user();
        $user->session = empty($session->session) ? '' : $session->session;
      }
    }
  }
  else {
    $user = drupal_anonymous_user();
    $user->session = empty($session->session) ? '' : $session->session;
  }
  return $user;
}