You are here

function sess_user_load in Memcache API and Integration 6

Same name and namespace in other branches
  1. 5 memcache-session.inc \sess_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 sess_user_load()
sess_read in ./memcache-session.inc
sess_write in ./memcache-session.inc
Write a session to session storage.

File

./memcache-session.inc, line 279
User session handling functions.

Code

function sess_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 (empty($user->uid) && isset($session->uid) && $session->uid != 0) {
      $user = db_fetch_object(db_query('SELECT u.* FROM {users} u WHERE u.uid = %d', $session->uid));
      if (!$user->status) {
        $user = drupal_anonymous_user($session->session);
      }
      else {
        $user = drupal_unpack($user);

        // 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 = %d", $user->uid);
        while ($role = db_fetch_object($result)) {
          $user->roles[$role->rid] = $role->name;
        }
      }

      // 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;

      // Write back to memcache before setting $user->session since this
      // is available separately.
      dmemcache_set($user->uid, $user, ini_get('session.gc_maxlifetime'), 'users');
      $user->session = empty($session->session) ? '' : $session->session;
    }
    else {
      if ($user->uid && $user->status) {

        // 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($session->session);
      }
    }
  }
  else {
    $session = isset($session->session) ? $session->session : '';
    $user = drupal_anonymous_user($session);
  }
  return $user;
}