--- /dev/null
+<?php
+/***************************************************************************
+ * Copyright (C) 2003-2008 Polytechnique.org *
+ * http://opensource.polytechnique.org/ *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; if not, write to the Free Software *
+ * Foundation, Inc., *
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
+ ***************************************************************************/
+
+class Profile
+{
+ private $pid;
+ private $hrpid;
+
+ private function __construct($login)
+ {
+ if ($login instanceof PlUser) {
+ $res = XDB::query('SELECT p.pid, p.hrpid
+ FROM account_profiles AS ap
+ INNER JOIN profiles AS p ON (p.pid = ap.pid)
+ WHERE ap.uid = {?} AND FIND_IN_SET(\'owner\', ap.perms)',
+ $login->id());
+ } else if (is_numeric($login)) {
+ $res = XDB::query('SELECT p.pid, p.hrpid
+ FROM profiles AS p
+ WHERE p.pid = {?}',
+ $login);
+ } else {
+ $res = XDB::query('SELECT p.pid, p.hrpid
+ FROM profiles AS p
+ WHERE p.hrpid = {?}',
+ $login);
+ }
+ if ($res->numRows() != 1) {
+ throw new UserNotFoundException();
+ }
+ list($this->pid, $this->hrpid) = $res->fetchOneRow();
+ }
+
+ public function id()
+ {
+ return $this->pid;
+ }
+
+ public function hrid()
+ {
+ return $this->hrpid;
+ }
+
+ public function owner()
+ {
+ return User::getSilent($this);
+ }
+
+ /** Return the profile associated with the given login.
+ */
+ public static function get($login) {
+ try {
+ return new Profile($login);
+ } catch (UserNotFoundException $e) {
+ return false;
+ }
+ }
+}
+
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+?>
{
global $globals;
+ if ($login instanceof Profile) {
+ $res = XDB::query('SELECT ap.uid
+ FROM account_profiles AS ap
+ WHERE ap.pid = {?} AND FIND_IN_SET(\'owner\', perms)',
+ $login->id());
+ if ($res->numRows()) {
+ return $res->fetchOneCell();
+ }
+ throw new UserNotFoundException();
+ }
+
// If $data is an integer, fetches directly the result.
if (is_numeric($login)) {
- $res = XDB::query("SELECT user_id FROM auth_user_md5 WHERE user_id = {?}", $login);
+ $res = XDB::query('SELECT a.uid
+ FROM accounts AS a
+ WHERE a.uid = {?}', $login);
if ($res->numRows()) {
return $res->fetchOneCell();
}
}
// Checks whether $login is a valid hruid or not.
- $res = XDB::query("SELECT user_id FROM auth_user_md5 WHERE hruid = {?}", $login);
+ $res = XDB::query('SELECT a.uid
+ FROM accounts AS a
+ WHERE a.hruid = {?}', $login);
if ($res->numRows()) {
return $res->fetchOneCell();
}
// Checks if $login is a valid alias on the main domains.
list($mbox, $fqdn) = explode('@', $login);
if ($fqdn == $globals->mail->domain || $fqdn == $globals->mail->domain2) {
- $res = XDB::query("SELECT u.user_id
- FROM auth_user_md5 AS u
- INNER JOIN aliases AS a ON (a.id = u.user_id AND a.type IN ('alias', 'a_vie'))
- WHERE a.alias = {?}", $mbox);
+ $res = XDB::query('SELECT a.uid
+ FROM accounts AS a
+ INNER JOIN aliases AS al ON (al.id = a.uid AND al.type IN (\'alias\', \'a_vie\'))
+ WHERE al.alias = {?}', $mbox);
if ($res->numRows()) {
return $res->fetchOneCell();
}
+ /** TODO: implements this by inspecting the profile.
if (preg_match('/^(.*)\.([0-9]{4})$/u', $mbox, $matches)) {
- $res = XDB::query("SELECT u.user_id
- FROM auth_user_md5 AS u
- INNER JOIN aliases AS a ON (a.id = u.user_id AND a.type IN ('alias', 'a_vie'))
- WHERE a.alias = {?} AND u.promo = {?}", $matches[1], $matches[2]);
+ $res = XDB::query('SELECT a.uid
+ FROM accounts AS a
+ INNER JOIN aliases AS al ON (al.id = a.uid AND al.type IN ('alias', 'a_vie'))
+ WHERE al.alias = {?} AND a.promo = {?}', $matches[1], $matches[2]);
if ($res->numRows() == 1) {
return $res->fetchOneCell();
}
- }
+ }*/
throw new UserNotFoundException();
}
if ($redir = $res->fetchOneCell()) {
// We now have a valid alias, which has to be translated to an hruid.
list($alias, $alias_fqdn) = explode('@', $redir);
- $res = XDB::query("SELECT u.user_id
- FROM auth_user_md5 AS u
- LEFT JOIN aliases AS a ON (a.id = u.user_id AND a.type IN ('alias', 'a_vie'))
- WHERE a.alias = {?}", $alias);
+ $res = XDB::query("SELECT a.uid
+ FROM accounts AS a
+ LEFT JOIN aliases AS al ON (al.id = a.uid AND al.type IN ('alias', 'a_vie'))
+ WHERE al.alias = {?}", $alias);
if ($res->numRows()) {
return $res->fetchOneCell();
}
}
// Otherwise, we do suppose $login is an email redirection.
- $res = XDB::query("SELECT u.user_id
- FROM auth_user_md5 AS u
- LEFT JOIN emails AS e ON (e.uid = u.user_id)
+ $res = XDB::query("SELECT a.uid
+ FROM accounts AS a
+ LEFT JOIN emails AS e ON (e.uid = a.uid)
WHERE e.email = {?}", $login);
if ($res->numRows() == 1) {
return $res->fetchOneCell();
}
global $globals;
- $res = XDB::query("SELECT u.hruid, d.promo_display AS promo,
+ /** TODO: promo stuff again */
+ /** TODO: fix perms field to fit new perms system */
+ $res = XDB::query("SELECT a.hruid, d.promo_display AS promo,
CONCAT(af.alias, '@{$globals->mail->domain}') AS forlife,
CONCAT(ab.alias, '@{$globals->mail->domain}') AS bestalias,
- CONCAT(u.prenom, ' ', IF(u.nom_usage <> '', u.nom_usage, u.nom)) AS full_name,
- IF(u.prenom != '', u.prenom, u.nom) AS display_name,
- FIND_IN_SET('femme', u.flags) AS gender,
- q.core_mail_fmt AS email_format,
- u.perms
- FROM auth_user_md5 AS u
- INNER JOIN profile_display AS d ON (d.uid = u.user_id)
- LEFT JOIN auth_user_quick AS q ON (q.user_id = u.user_id)
- LEFT JOIN aliases AS af ON (af.id = u.user_id AND af.type = 'a_vie')
- LEFT JOIN aliases AS ab ON (ab.id = u.user_id AND FIND_IN_SET('bestalias', ab.flags))
- WHERE u.user_id = {?}", $this->user_id);
+ a.full_name, a.display_name, a.sex = 'female' AS gender,
+ a.email_format, a.state AS perms
+ FROM accounts AS a
+ INNER JOIN profile_display AS d ON (d.uid = a.uid)
+ LEFT JOIN aliases AS af ON (af.id = a.uid AND af.type = 'a_vie')
+ LEFT JOIN aliases AS ab ON (ab.id = a.uid AND FIND_IN_SET('bestalias', ab.flags))
+ WHERE a.uid = {?}", $this->user_id);
$this->fillFromArray($res->fetchOneAssoc());
}
if (isset($values['mail_fmt'])) {
$values['email_format'] = $values['mail_fmt'];
}
- if (isset($values['email_format'])) {
- $values['email_format'] = ($values['email_format'] ? self::FORMAT_HTML : self::FORMAT_TEXT);
- }
parent::fillFromArray($values);
}
$this->perm_flags = self::makePerms($this->perms);
}
+ /** Return the main profile attached with this account if any.
+ */
+ public function profile()
+ {
+ return Profile::get($this);
+ }
+
// Return permission flags for a given permission level.
public static function makePerms($perms)
{