fix UserSession: login

This commit is contained in:
Yuan Chiu 2014-11-13 22:20:50 +08:00
parent 0d42a96787
commit 1e2cfd59b6

View File

@ -59,41 +59,35 @@ class UserSession {
*/ */
public function login($userId, $password, $agent) { public function login($userId, $password, $agent) {
try { $user = new User($userId);
$user = new User($userId);
// 登入密碼錯誤的話 // 登入密碼錯誤的話
if( !$user->isPasswordCorrect($password) ) { if( !$user->isPasswordCorrect($password) ) {
throw new Exception\UserPasswordErrException($userId); throw new Exception\UserPasswordErrException($userId);
}
// 此帳號已被停用
else if( !$user->isEnable() ) {
throw new Exception\UserNoActivatedException($userId);
}
// 沒問題,登入此帳號
else {
// 使用資料庫
$db = new Database\DBUserSession();
// 產生登入token
$passUtil = new Util\Password();
$token = null;
// 防止產生出重複的token
do {
$token = $passUtil->generator(32);
}
while ($db->queryByToken($token));
// 登入資訊寫入資料庫
$db->login($token, $userId, $agent);
return $token;
}
} }
// 沒有找到使用者 // 此帳號已被停用
catch (Exception\UserNoFoundException $e) { else if( !$user->isEnable() ) {
echo 'No Found user: '. $e->getUserId(); throw new Exception\UserNoActivatedException($userId);
}
// 沒問題,登入此帳號
else {
// 使用資料庫
$db = new Database\DBUserSession();
// 產生登入token
$passUtil = new Util\Password();
$token = null;
// 防止產生出重複的token
do {
$token = $passUtil->generator(32);
}
while ($db->queryByToken($token));
// 登入資訊寫入資料庫
$db->login($token, $userId, $agent);
return $token;
} }
} }