<?php
/***************************************************************************
- * Copyright (C) 2003-2008 Polytechnique.org *
+ * Copyright (C) 2003-2010 Polytechnique.org *
* http://opensource.polytechnique.org/ *
* *
* This program is free software; you can redistribute it and/or modify *
class XorgSession extends PlSession
{
+ const INVALID_USER = -2;
+ const NO_COOKIE = -1;
+ const COOKIE_SUCCESS = 0;
+ const INVALID_COOKIE = 1;
+
public function __construct()
{
parent::__construct();
public function startAvailableAuth()
{
if (!S::logged()) {
- $cookie = $this->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;
}
}
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('ORGaccess') == '' || !Cookie::has('ORGuid')) {
- return -1;
+ if (Cookie::v('access') == '' || !Cookie::has('uid')) {
+ return self::NO_COOKIE;
}
$res = XDB::query('SELECT uid, password
FROM accounts
WHERE uid = {?} AND state = \'active\'',
- Cookie::i('ORGuid'));
+ Cookie::i('uid'));
if ($res->numRows() != 0) {
list($uid, $password) = $res->fetchOneRow();
- $expected_value = sha1($password);
- if ($expected_value == Cookie::v('ORGaccess')) {
+ 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 */
if ($response != $expected_response) {
+ if (!S::logged()) {
+ Platal::page()->trigError('Mot de passe ou nom d\'utilisateur invalide');
+ } else {
+ Platal::page()->trigError('Mot de passe invalide');
+ }
S::logger($uid)->log('auth_fail', 'bad password');
return null;
}
return $uid;
}
+ Platal::page()->trigError('Mot de passe ou nom d\'utilisateur invalide');
return null;
}
if (!S::logged()) {
S::set('auth', AUTH_COOKIE);
}
- return S::i('auth_by_cookie');
+ return User::getSilentWithUID(S::i('auth_by_cookie'));
}
/** We come from an authentication form.
*/
- if (S::has('suid')) {
- $suid = S::v('suid');
- $login = $uname = $suid['uid'];
+ if (S::suid()) {
+ $login = $uname = S::suid('uid');
$redirect = false;
} else {
$uname = Env::v('username');
}
}
- $uid = $this->checkPassword($uname, $login, Post::v('response'), (!$redirect && is_numeric($uname)) ? 'id' : 'alias');
- if (!is_null($uid) && S::has('suid')) {
- $suid = S::v('suid');
- if ($suid['uid'] == $uid) {
+ $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');
} else {
$uid = null;
}
if (!is_null($uid)) {
S::set('auth', AUTH_MDP);
- if (!S::has('suid')) {
+ if (!S::suid()) {
if (Post::has('domain')) {
if (($domain = Post::v('domain', 'login')) == 'alias') {
- setcookie('ORGdomain', "alias", (time() + 25920000), '/', '', 0);
+ Cookie::set('domain', 'alias', 300);
} else {
- setcookie('ORGdomain', '', (time() - 3600), '/', '', 0);
+ Cookie::kill('domain');
}
- // pour que la modification soit effective dans le reste de la page
- $_COOKIE['ORGdomain'] = $domain;
}
}
S::kill('challenge');
S::logger($uid)->log('auth_ok');
}
- return $uid;
+ return User::getSilentWithUID($uid);
}
- protected function startSessionAs($uid, $level)
+ protected function startSessionAs($user, $level)
{
- if ((!is_null(S::v('user')) && S::i('user') != $uid) || (S::has('uid') && S::i('uid') != $uid)) {
+ 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')) {
return true;
}
if ($level == AUTH_SUID) {
S::set('auth', AUTH_MDP);
- unset($_SESSION['log']);
}
// 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: Switch to new permission system */
- $res = XDB::query("SELECT a.uid, a.hruid, a.display_name, a.full_name, a.password,
- a.sex = 'female' AS femme, a.mail_format as mail_fmt,
+ /** TODO: Use the User object to fetch all this */
+ $res = XDB::query("SELECT a.uid, a.hruid, a.display_name, a.full_name,
+ a.sex = 'female' AS femme, a.email_format,
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(fp.last_seen) AS banana_last,
+ a.last_version, g.g_account_name IS NOT NULL AS googleapps,
UNIX_TIMESTAMP(s.start) AS lastlogin, s.host,
- IF(a.is_admin, 'admin', 'user') AS perms
- FROM accounts AS a
- 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'", $uid);
+ a.is_admin, at.perms
+ FROM accounts AS a
+ INNER JOIN account_types AS at ON (a.type = at.type)
+ LEFT JOIN watch AS w ON (w.uid = a.uid)
+ LEFT JOIN forum_profiles AS fp ON (fp.uid = a.uid)
+ LEFT JOIN gapps_accounts AS g ON (a.uid = g.l_userid AND g.g_status = 'active')
+ LEFT JOIN log_last_sessions AS ls ON (ls.uid = a.uid)
+ LEFT JOIN log_sessions AS s ON(s.id = ls.id)
+ WHERE a.uid = {?} AND a.state = 'active'", $user->id());
+ if ($res->numRows() != 1) {
+ return false;
+ }
+
$sess = $res->fetchOneAssoc();
$perms = $sess['perms'];
unset($sess['perms']);
$_SESSION = array_merge($_SESSION, $sess);
// Starts the session's logger, and sets up the permanent cookie.
- if (S::has('suid')) {
- $suid = S::v('suid');
- $logger = S::logger($uid);
- $logger->log("suid_start", S::v('hruid') . " by " . $suid['hruid']);
+ if (S::suid()) {
+ S::logger()->log("suid_start", S::v('hruid') . ' by ' . S::suid('hruid'));
} else {
- $logger = S::logger($uid);
- $logger->saveLastSession();
- setcookie('ORGuid', $uid, (time() + 25920000), '/', '', 0);
-
- if (S::i('auth_by_cookie') == $uid || Post::v('remember', 'false') == 'true') {
- $cookie = sha1($sess['password']);
- setcookie('ORGaccess', $cookie, (time() + 25920000), '/', '', 0);
- if (S::i('auth_by_cookie') != $uid) {
- $logger->log("cookie_on");
- }
+ S::logger()->saveLastSession();
+ Cookie::set('uid', $user->id(), 300);
+
+ if (S::i('auth_by_cookie') == $user->id() || Post::v('remember', 'false') == 'true') {
+ $this->setAccessCookie(false, S::i('auth_by_cookie') != $user->id());
} else {
- setcookie('ORGaccess', '', time() - 3600, '/', '', 0);
- $logger->log("cookie_off");
+ $this->killAccessCookie();
}
}
// Finalizes the session setup.
- S::set('perms', User::makePerms($perms));
+ $this->makePerms($perms, S::b('is_admin'));
$this->securityChecks();
$this->setSkin();
$this->updateNbNotifs();
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;
}
- public function makePerms($perm)
+ protected function makePerms($perm, $is_admin)
{
- $flags = new PlFlagSet();
- if ($perm == 'disabled' || $perm == 'ext') {
- S::set('perms', $flags);
- return;
- }
- $flags->addFlag(PERMS_USER);
- if ($perm == 'admin') {
- $flags->addFlag(PERMS_ADMIN);
- }
- S::set('perms', $flags);
+ S::set('perms', User::makePerms($perm, $is_admin));
}
public function setSkin()
{
- if (S::logged() && (!S::has('skin') || S::has('suid'))) {
- $uid = S::v('uid');
+ if (S::logged() && (!S::has('skin') || S::suid())) {
$res = XDB::query('SELECT skin_tpl
FROM accounts AS a
INNER JOIN skins AS s on (a.skin = s.id)
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::user()->password()), 300, true);
+ if ($log) {
+ S::logger()->log('cookie_on');
+ }
+ }
+
+ public function killAccessCookie($log = true) {
+ Cookie::kill('access');
+ if ($log) {
+ S::logger()->log('cookie_off');
+ }
+ }
+
+ public function killLoginFormCookies() {
+ Cookie::kill('uid');
+ Cookie::kill('domain');
}
}