cache current user in a global variable
darcs-hash:20081209173402-84dde-eb1c47ddbf45a831379f6571729187267411ed07.gz
This commit is contained in:
parent
ed440c734e
commit
6179d2d236
39
lib/util.php
39
lib/util.php
@ -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() {
|
||||||
|
global $_cur;
|
||||||
|
|
||||||
|
if ($_cur === false) {
|
||||||
|
|
||||||
if (isset($_REQUEST[session_name()]) || (isset($_SESSION['userid']) && $_SESSION['userid'])) {
|
if (isset($_REQUEST[session_name()]) || (isset($_SESSION['userid']) && $_SESSION['userid'])) {
|
||||||
common_ensure_session();
|
common_ensure_session();
|
||||||
$id = isset($_SESSION['userid']) ? $_SESSION['userid'] : false;
|
$id = isset($_SESSION['userid']) ? $_SESSION['userid'] : false;
|
||||||
if ($id) {
|
if ($id) {
|
||||||
# note: this should cache
|
$_cur = User::staticGet($id);
|
||||||
$user = User::staticGet($id);
|
return $_cur;
|
||||||
return $user;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
# that didn't work; try to remember
|
|
||||||
$user = common_remembered_user();
|
# that didn't work; try to remember; will init $_cur to NULL on failure
|
||||||
if ($user) {
|
$_cur = common_remembered_user();
|
||||||
common_debug("Got User " . $user->nickname);
|
|
||||||
|
if ($_cur) {
|
||||||
|
common_debug("Got User " . $_cur->nickname);
|
||||||
common_debug("Faking session on remembered user");
|
common_debug("Faking session on remembered user");
|
||||||
$_SESSION['userid'] = $user->id;
|
# XXX: Is this necessary?
|
||||||
|
$_SESSION['userid'] = $_cur->id;
|
||||||
}
|
}
|
||||||
return $user;
|
}
|
||||||
|
|
||||||
|
return $_cur;
|
||||||
}
|
}
|
||||||
|
|
||||||
# Logins that are 'remembered' aren't 'real' -- they're subject to
|
# Logins that are 'remembered' aren't 'real' -- they're subject to
|
||||||
|
Loading…
Reference in New Issue
Block a user