don't refetch user objects so much

darcs-hash:20080709055343-84dde-ac550608a4736ce5daed70af19866c75a1cfb416.gz
This commit is contained in:
Evan Prodromou 2008-07-09 01:53:43 -04:00
parent 625ac7e1d9
commit a67108190a
3 changed files with 69 additions and 46 deletions

View File

@ -37,16 +37,24 @@ class LoginAction extends Action {
# XXX: login throttle # XXX: login throttle
$nickname = $this->arg('nickname'); $nickname = $this->arg('nickname');
$password = $this->arg('password'); $password = $this->arg('password');
if (common_check_user($nickname, $password)) { $user = common_check_user($nickname, $password);
if (!$user) {
$this->show_form(_t('Incorrect username or password.'));
return;
}
# success! # success!
if (!common_set_user($nickname)) { if (!common_set_user($user)) {
common_server_error(_t('Error setting user.')); common_server_error(_t('Error setting user.'));
return; return;
} }
common_real_login(true); common_real_login(true);
if ($this->boolean('rememberme')) { if ($this->boolean('rememberme')) {
common_debug('Adding rememberme cookie for ' . $nickname); common_debug('Adding rememberme cookie for ' . $nickname);
common_rememberme(); common_rememberme($user);
} }
# success! # success!
$url = common_get_returnto(); $url = common_get_returnto();
@ -59,9 +67,6 @@ class LoginAction extends Action {
$nickname)); $nickname));
} }
common_redirect($url); common_redirect($url);
} else {
$this->show_form(_t('Incorrect username or password.'));
}
} }
function show_form($error=NULL) { function show_form($error=NULL) {
@ -113,3 +118,4 @@ class LoginAction extends Action {
} }
} }
} }
#

View File

@ -63,20 +63,24 @@ class RegisterAction extends Action {
$this->show_form(_t('Email address already exists.')); $this->show_form(_t('Email address already exists.'));
} else if ($password != $confirm) { } else if ($password != $confirm) {
$this->show_form(_t('Passwords don\'t match.')); $this->show_form(_t('Passwords don\'t match.'));
} else if ($this->register_user($nickname, $password, $email)) { } else {
$user = $this->register_user($nickname, $password, $email);
if (!$user) {
$this->show_form(_t('Invalid username or password.'));
return;
}
# success! # success!
if (!common_set_user($nickname)) { if (!common_set_user($user)) {
common_server_error(_t('Error setting user.')); common_server_error(_t('Error setting user.'));
return; return;
} }
# this is a real login
common_real_login(true); common_real_login(true);
if ($this->boolean('rememberme')) { if ($this->boolean('rememberme')) {
common_debug('Adding rememberme cookie for ' . $nickname); common_debug('Adding rememberme cookie for ' . $nickname);
common_rememberme(); common_rememberme($user);
} }
common_redirect(common_local_url('profilesettings')); common_redirect(common_local_url('profilesettings'));
} else {
$this->show_form(_t('Invalid username or password.'));
} }
} }
@ -148,7 +152,7 @@ class RegisterAction extends Action {
$email); $email);
} }
return $result; return $user;
} }
function show_top($error=NULL) { function show_top($error=NULL) {

View File

@ -421,8 +421,12 @@ function common_check_user($nickname, $password) {
if (is_null($user)) { if (is_null($user)) {
return false; return false;
} else { } else {
return (0 == strcmp(common_munge_password($password, $user->id), if (0 == strcmp(common_munge_password($password, $user->id),
$user->password)); $user->password)) {
return $user;
} else {
return false;
}
} }
} }
@ -441,19 +445,26 @@ function common_ensure_session() {
} }
} }
function common_set_user($nickname) { # Three kinds of arguments:
# 1) a user object
# 2) a nickname
# 3) NULL to clear
function common_set_user($user) {
if (is_null($nickname) && common_have_session()) { if (is_null($nickname) && common_have_session()) {
unset($_SESSION['userid']); unset($_SESSION['userid']);
return true; return true;
} else { } else if (is_string($user)) {
$nickname = $user;
$user = User::staticGet('nickname', $nickname); $user = User::staticGet('nickname', $nickname);
} else if (!($user instanceof User)) {
return false;
}
if ($user) { if ($user) {
common_ensure_session(); common_ensure_session();
$_SESSION['userid'] = $user->id; $_SESSION['userid'] = $user->id;
return true; return $user;
} else {
return false;
}
} }
return false; return false;
} }
@ -477,12 +488,14 @@ function common_set_cookie($key, $value, $expiration=0) {
define('REMEMBERME', 'rememberme'); define('REMEMBERME', 'rememberme');
define('REMEMBERME_EXPIRY', 30 * 24 * 60 * 60); define('REMEMBERME_EXPIRY', 30 * 24 * 60 * 60);
function common_rememberme() { function common_rememberme($user=NULL) {
if (!$user) {
$user = common_current_user(); $user = common_current_user();
if (!$user) { if (!$user) {
common_debug('No current user to remember', __FILE__); common_debug('No current user to remember', __FILE__);
return false; return false;
} }
}
$rm = new Remember_me(); $rm = new Remember_me();
$rm->code = common_good_rand(16); $rm->code = common_good_rand(16);
$rm->user_id = $user->id; $rm->user_id = $user->id;
@ -521,7 +534,7 @@ function common_remembered_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
# automatically again after this session # automatically again after this session
common_rememberme(); common_rememberme($user);
} }
} }
} }