cache current user in a global variable

darcs-hash:20081209173402-84dde-eb1c47ddbf45a831379f6571729187267411ed07.gz
This commit is contained in:
Evan Prodromou 2008-12-09 12:34:02 -05:00
parent ed440c734e
commit 6179d2d236

View File

@ -557,8 +557,16 @@ function common_ensure_session() {
# 2) a nickname # 2) a nickname
# 3) NULL to clear # 3) NULL to clear
# Initialize to false; set to NULL if none found
$_cur = false;
function common_set_user($user) { function common_set_user($user) {
global $_cur;
if (is_null($user) && common_have_session()) { if (is_null($user) && common_have_session()) {
$_cur = NULL;
unset($_SESSION['userid']); unset($_SESSION['userid']);
return true; return true;
} else if (is_string($user)) { } else if (is_string($user)) {
@ -571,7 +579,8 @@ function common_set_user($user) {
if ($user) { if ($user) {
common_ensure_session(); common_ensure_session();
$_SESSION['userid'] = $user->id; $_SESSION['userid'] = $user->id;
return $user; $_cur = $user;
return $_cur;
} }
return false; return false;
} }
@ -671,7 +680,7 @@ function common_remembered_user() {
common_log(LOG_INFO, 'logging in ' . $user->nickname . ' using rememberme code ' . $rm->code); common_log(LOG_INFO, 'logging in ' . $user->nickname . ' using rememberme code ' . $rm->code);
common_set_user($user->nickname); common_set_user($user);
common_real_login(false); common_real_login(false);
# We issue a new cookie, so they can log in # We issue a new cookie, so they can log in
@ -690,23 +699,31 @@ function common_forgetme() {
# who is the current user? # who is the current user?
function common_current_user() { function common_current_user() {
if (isset($_REQUEST[session_name()]) || (isset($_SESSION['userid']) && $_SESSION['userid'])) { global $_cur;
common_ensure_session();
$id = isset($_SESSION['userid']) ? $_SESSION['userid'] : false; if ($_cur === false) {
if ($id) {
# note: this should cache if (isset($_REQUEST[session_name()]) || (isset($_SESSION['userid']) && $_SESSION['userid'])) {
$user = User::staticGet($id); common_ensure_session();
return $user; $id = isset($_SESSION['userid']) ? $_SESSION['userid'] : false;
} if ($id) {
} $_cur = User::staticGet($id);
# that didn't work; try to remember return $_cur;
$user = common_remembered_user(); }
if ($user) { }
common_debug("Got User " . $user->nickname);
common_debug("Faking session on remembered user"); # that didn't work; try to remember; will init $_cur to NULL on failure
$_SESSION['userid'] = $user->id; $_cur = common_remembered_user();
}
return $user; if ($_cur) {
common_debug("Got User " . $_cur->nickname);
common_debug("Faking session on remembered user");
# XXX: Is this necessary?
$_SESSION['userid'] = $_cur->id;
}
}
return $_cur;
} }
# Logins that are 'remembered' aren't 'real' -- they're subject to # Logins that are 'remembered' aren't 'real' -- they're subject to