X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;ds=sidebyside;f=classes%2Fxorgsession.php;h=02e34e4830f50b64386eaee4dbc9fb14f70a782a;hb=843a2191e4c76f0beb00c754f06974de76791238;hp=9dfca1d15ba39e5205c8ac637f763440b146917b;hpb=866bd5351ad9e3dd9e5603d078fd19825487d976;p=platal.git diff --git a/classes/xorgsession.php b/classes/xorgsession.php index 9dfca1d..02e34e4 100644 --- a/classes/xorgsession.php +++ b/classes/xorgsession.php @@ -1,6 +1,6 @@ tryCookie(); - if ($cookie == 0) { - return $this->start(AUTH_COOKIE); - } else if ($cookie == 1 || $cookie == -2) { + switch ($this->tryCookie()) { + case self::COOKIE_SUCCESS: + if (!$this->start(AUTH_COOKIE)) { + return false; + } + break; + + case self::INVALID_USER: + case self::INVALID_COOKIE: return false; } } @@ -42,13 +52,13 @@ class XorgSession extends PlSession return true; } - /** Check the cookie and set the associated user_id in the auth_by_cookie session variable. + /** Check the cookie and set the associated uid in the auth_by_cookie session variable. */ private function tryCookie() { S::kill('auth_by_cookie'); if (Cookie::v('access') == '' || !Cookie::has('uid')) { - return -1; + return self::NO_COOKIE; } $res = XDB::query('SELECT uid, password @@ -57,27 +67,26 @@ class XorgSession extends PlSession Cookie::i('uid')); if ($res->numRows() != 0) { list($uid, $password) = $res->fetchOneRow(); - $expected_value = sha1($password); - if ($expected_value == Cookie::v('access')) { + if (sha1($password) == Cookie::v('access')) { S::set('auth_by_cookie', $uid); - return 0; + return self::COOKIE_SUCCESS; } else { - return 1; + return self::INVALID_COOKIE; } } - return -2; + return self::INVALID_USER; } private function checkPassword($uname, $login, $response, $login_type) { $res = XDB::query('SELECT a.uid, a.password FROM accounts AS a - INNER JOIN aliases AS l ON (l.id = a.uid AND l.type != \'homonyme\') + INNER JOIN aliases AS l ON (l.uid = a.uid AND l.type != \'homonyme\') WHERE l.' . $login_type . ' = {?} AND a.state = \'active\'', $login); if (list($uid, $password) = $res->fetchOneRow()) { $expected_response = sha1("$uname:$password:" . S::v('challenge')); - /* XXX: Deprecates len(password) > 10 conversion */ + /* Deprecates len(password) > 10 conversion. */ if ($response != $expected_response) { if (!S::logged()) { Platal::page()->trigError('Mot de passe ou nom d\'utilisateur invalide'); @@ -89,6 +98,7 @@ class XorgSession extends PlSession } return $uid; } + Platal::page()->trigError('Mot de passe ou nom d\'utilisateur invalide'); return null; } @@ -108,7 +118,7 @@ class XorgSession extends PlSession if (!S::logged()) { S::set('auth', AUTH_COOKIE); } - return User::getSilentWithValues(null, array('user_id' => S::i('auth_by_cookie'))); + return User::getSilentWithUID(S::i('auth_by_cookie')); } @@ -143,7 +153,7 @@ class XorgSession extends PlSession } } - $uid = $this->checkPassword($uname, $login, Post::v('response'), (!$redirect && is_numeric($uname)) ? 'id' : 'alias'); + $uid = $this->checkPassword($uname, $login, Post::v('response'), (!$redirect && is_numeric($uname)) ? 'uid' : 'alias'); if (!is_null($uid) && S::suid()) { if (S::suid('uid') == $uid) { $uid = S::i('uid'); @@ -165,12 +175,12 @@ class XorgSession extends PlSession S::kill('challenge'); S::logger($uid)->log('auth_ok'); } - return User::getSilentWithValues(null, array('user_id' => $uid)); + return User::getSilentWithUID($uid); } protected function startSessionAs($user, $level) { - if ((!is_null(S::v('user')) && S::i('user') != $user->id()) + if ((!is_null(S::user()) && S::user()->id() != $user->id()) || (S::has('uid') && S::i('uid') != $user->id())) { return false; } else if (S::has('uid')) { @@ -180,31 +190,8 @@ class XorgSession extends PlSession S::set('auth', AUTH_MDP); } - // Retrieves main user properties. - /** TODO: Move needed informations to account tables */ - /** TODO: Currently suppressed data are matricule, promo */ - /** TODO: Data to move are: banana_last, watch_last, last_version */ - /** TODO: Use the User object to fetch all this */ - $res = XDB::query("SELECT a.uid, a.hruid, a.display_name, a.full_name, a.password, - a.sex = 'female' AS femme, a.email_format as mail_fmt, - a.token, FIND_IN_SET('watch', a.flags) AS watch_account, - UNIX_TIMESTAMP(q.banana_last) AS banana_last, q.watch_last, - q.last_version, g.g_account_name IS NOT NULL AS googleapps, - UNIX_TIMESTAMP(s.start) AS lastlogin, s.host, - a.is_admin, at.perms - FROM accounts AS a - INNER JOIN account_types AS at ON(a.type = at.type) - INNER JOIN auth_user_quick AS q ON(a.uid = q.user_id) - LEFT JOIN gapps_accounts AS g ON(a.uid = g.l_userid AND g.g_status = 'active') - LEFT JOIN logger.last_sessions AS ls ON (ls.uid = a.uid) - LEFT JOIN logger.sessions AS s ON(s.id = ls.id) - WHERE a.uid = {?} AND a.state = 'active'", $user->id()); - $sess = $res->fetchOneAssoc(); - $perms = $sess['perms']; - unset($sess['perms']); - - // Loads the data into the real session. - $_SESSION = array_merge($_SESSION, $sess); + // Loads uid and hruid into the session for developement conveniance. + $_SESSION = array_merge($_SESSION, array('uid' => $user->id(), 'hruid' => $user->hruid)); // Starts the session's logger, and sets up the permanent cookie. if (S::suid()) { @@ -221,7 +208,7 @@ class XorgSession extends PlSession } // Finalizes the session setup. - $this->makePerms($perms, S::b('is_admin')); + $this->makePerms($user->perms, $user->is_admin); $this->securityChecks(); $this->setSkin(); $this->updateNbNotifs(); @@ -256,14 +243,13 @@ class XorgSession extends PlSession public function tokenAuth($login, $token) { - $res = XDB::query('SELECT a.hruid + $res = XDB::query('SELECT a.uid, a.hruid FROM aliases AS l - INNER JOIN accounts AS a ON (l.id = a.uid AND a.state = \'active\') + INNER JOIN accounts AS a ON (l.uid = a.uid AND a.state = \'active\') WHERE a.token = {?} AND l.alias = {?} AND l.type != \'homonyme\'', $token, $login); if ($res->numRows() == 1) { - $data = $res->fetchOneAssoc(); - return new User($data['hruid'], $data); + return new User(null, $res->fetchOneAssoc()); } return null; } @@ -276,7 +262,6 @@ class XorgSession extends PlSession public function setSkin() { if (S::logged() && (!S::has('skin') || S::suid())) { - $uid = S::v('uid'); $res = XDB::query('SELECT skin_tpl FROM accounts AS a INNER JOIN skins AS s on (a.skin = s.id) @@ -299,15 +284,16 @@ class XorgSession extends PlSession public function updateNbNotifs() { require_once 'notifs.inc.php'; - $n = select_notifs(false, S::i('uid'), S::v('watch_last'), false); - S::set('notifs', $n->numRows()); + $user = S::user(); + $n = Watch::getCount($user); + S::set('notifs', $n); } public function setAccessCookie($replace = false, $log = true) { if (S::suid() || ($replace && !Cookie::blank('access'))) { return; } - Cookie::set('access', sha1(S::v('password')), 300, true); + Cookie::set('access', sha1(S::user()->password()), 300, true); if ($log) { S::logger()->log('cookie_on'); }