From b1719b137280020a1fffee3dc76c68cfaade61ea Mon Sep 17 00:00:00 2001 From: Vincent Zanotti Date: Sun, 15 Jun 2008 17:25:43 +0200 Subject: [PATCH] Moves SQL-dependant method of PlUser to the Polytechnique.org's implementation of PlUser. Signed-off-by: Vincent Zanotti --- classes/pluser.php | 144 ++++------------------------------------------------- classes/user.php | 123 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+), 133 deletions(-) diff --git a/classes/pluser.php b/classes/pluser.php index d80cb42..f18bac1 100644 --- a/classes/pluser.php +++ b/classes/pluser.php @@ -40,12 +40,12 @@ abstract class PlUser // been fetched yet, and false means the information is not available. // Main (unique) identifiers. - private $user_id = null; - private $hruid = null; + protected $user_id = null; + protected $hruid = null; // User's emails (bestalias should be preferred when sending emails). - private $forlife = null; - private $bestalias = null; + protected $forlife = null; + protected $bestalias = null; // Constructs the object from an identifier (hruid/uid/email alias/email @@ -58,131 +58,19 @@ abstract class PlUser // Properties accessors. - public function id() - { - return $this->user_id; - } - - public function login() - { - return $this->hruid; - } - - public function bestEmail() - { - if (!isset($this->bestalias)) { - global $globals; - $res = XDB::query("SELECT CONCAT(alias, '@{$globals->mail->domain}') - FROM aliases - WHERE FIND_IN_SET('bestalias', flags) - AND id = {?}", $this->user_id); - $this->bestalias = $res->numRows() ? $res->fetchOneCell() : false; - } - return $this->bestalias; - } - - public function forlifeEmail() - { - if (!isset($this->forlife)) { - global $globals; - $res = XDB::query("SELECT CONCAT(alias, '@{$globals->mail->domain}') - FROM aliases - WHERE type = 'a_vie' AND id = {?}", $this->user_id); - $this->forlife = $res->numRows() ? $res->fetchOneCell() : false; - } - return $this->forlife; - } + public function id() { return $this->user_id; } + public function login() { return $this->hruid; } + abstract public function bestEmail(); + abstract public function forlifeEmail(); // Determines if the @p id is a valid identifier; if so, returns the user_id // and the hruid. Otherwise raises UserNotFoundException. - private function getLogin($login) - { - global $globals; - - // If $data is an integer, fetches directly the result. - if (is_numeric($login)) { - $res = XDB::query("SELECT user_id, hruid FROM auth_user_md5 WHERE user_id = {?}", $login); - if ($res->numRows()) { - return $res->fetchOneRow(); - } - - throw new UserNotFoundException(); - } - - // Checks whether $login is a valid hruid or not. - $res = XDB::query("SELECT user_id, hruid FROM auth_user_md5 WHERE hruid = {?}", $login); - if ($res->numRows()) { - return $res->fetchOneRow(); - } - - // From now, $login can only by an email alias, or an email redirection. - // If it doesn't look like a valid address, appends the plat/al's main domain. - $login = trim(strtolower($login)); - if (strstr($login, '@') === false) { - $login = $login . '@' . $globals->mail->domain; - } - - // 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, u.hruid - 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); - if ($res->numRows()) { - return $res->fetchOneRow(); - } - - if (preg_match('/^(.*)\.([0-9]{4})$/u', $mbox, $matches)) { - $res = XDB::query("SELECT u.user_id, u.hruid - 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]); - if ($res->numRows() == 1) { - return $res->fetchOneRow(); - } - } - - throw new UserNotFoundException(); - } - - // Looks for $login as an email alias from the dedicated alias domain. - if ($fqdn == $globals->mail->alias_dom || $fqdn == $globals->mail->alias_dom2) { - $res = XDB::query("SELECT redirect - FROM virtual_redirect - INNER JOIN virtual USING(vid) - WHERE alias = {?}", $mbox . '@' . $globals->mail->alias_dom); - 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, u.hruid - 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); - if ($res->numRows()) { - return $res->fetchOneRow(); - } - } - - throw new UserNotFoundException(); - } - - // Otherwise, we do suppose $login is an email redirection. - $res = XDB::query("SELECT u.user_id, u.hruid - FROM auth_user_md5 AS u - LEFT JOIN emails AS e ON (e.uid = u.user_id) - WHERE e.email = {?}", $login); - if ($res->numRows() == 1) { - return $res->fetchOneRow(); - } - - throw new UserNotFoundException($res->fetchColumn(1)); - } + abstract protected function getLogin($login); // Fills the object from associative arrays containing our data. // The use case is for arrays got directly from anoter SQL request. - private function fillFromArray(array $values) + protected function fillFromArray(array $values) { foreach ($values as $key => $value) { if (property_exists($this, $key) && !isset($this->$key)) { @@ -252,17 +140,7 @@ abstract class PlUser // Default callback for user lookup -- displays an error message w.r.t. the // number of matching users found. - public static function _default_user_callback($login, $results) - { - global $page; - - $result_count = count($results); - if ($result_count == 0 || !S::has_perms()) { - $page->trigError("Il n'y a pas d'utilisateur avec l'identifiant : $login"); - } else { - $page->trigError("Il y a $result_count utilisateurs avec cet identifiant : " . join(', ', $results)); - } - } + abstract public static function _default_user_callback($login, $results); } // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: diff --git a/classes/user.php b/classes/user.php index 99fcc13..2d41c43 100644 --- a/classes/user.php +++ b/classes/user.php @@ -21,6 +21,129 @@ class User extends PlUser { + // Implementation of properties accessors. + public function bestEmail() + { + if (!isset($this->bestalias)) { + global $globals; + $res = XDB::query("SELECT CONCAT(alias, '@{$globals->mail->domain}') + FROM aliases + WHERE FIND_IN_SET('bestalias', flags) + AND id = {?}", $this->user_id); + $this->bestalias = $res->numRows() ? $res->fetchOneCell() : false; + } + return $this->bestalias; + } + + public function forlifeEmail() + { + if (!isset($this->forlife)) { + global $globals; + $res = XDB::query("SELECT CONCAT(alias, '@{$globals->mail->domain}') + FROM aliases + WHERE type = 'a_vie' AND id = {?}", $this->user_id); + $this->forlife = $res->numRows() ? $res->fetchOneCell() : false; + } + return $this->forlife; + } + + // Implementation of the login to array(user_id, hruid) function. + protected function getLogin($login) + { + global $globals; + + // If $data is an integer, fetches directly the result. + if (is_numeric($login)) { + $res = XDB::query("SELECT user_id, hruid FROM auth_user_md5 WHERE user_id = {?}", $login); + if ($res->numRows()) { + return $res->fetchOneRow(); + } + + throw new UserNotFoundException(); + } + + // Checks whether $login is a valid hruid or not. + $res = XDB::query("SELECT user_id, hruid FROM auth_user_md5 WHERE hruid = {?}", $login); + if ($res->numRows()) { + return $res->fetchOneRow(); + } + + // From now, $login can only by an email alias, or an email redirection. + // If it doesn't look like a valid address, appends the plat/al's main domain. + $login = trim(strtolower($login)); + if (strstr($login, '@') === false) { + $login = $login . '@' . $globals->mail->domain; + } + + // 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, u.hruid + 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); + if ($res->numRows()) { + return $res->fetchOneRow(); + } + + if (preg_match('/^(.*)\.([0-9]{4})$/u', $mbox, $matches)) { + $res = XDB::query("SELECT u.user_id, u.hruid + 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]); + if ($res->numRows() == 1) { + return $res->fetchOneRow(); + } + } + + throw new UserNotFoundException(); + } + + // Looks for $login as an email alias from the dedicated alias domain. + if ($fqdn == $globals->mail->alias_dom || $fqdn == $globals->mail->alias_dom2) { + $res = XDB::query("SELECT redirect + FROM virtual_redirect + INNER JOIN virtual USING(vid) + WHERE alias = {?}", $mbox . '@' . $globals->mail->alias_dom); + 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, u.hruid + 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); + if ($res->numRows()) { + return $res->fetchOneRow(); + } + } + + throw new UserNotFoundException(); + } + + // Otherwise, we do suppose $login is an email redirection. + $res = XDB::query("SELECT u.user_id, u.hruid + FROM auth_user_md5 AS u + LEFT JOIN emails AS e ON (e.uid = u.user_id) + WHERE e.email = {?}", $login); + if ($res->numRows() == 1) { + return $res->fetchOneRow(); + } + + throw new UserNotFoundException($res->fetchColumn(1)); + } + + // Implementation of the default user callback. + public static function _default_user_callback($login, $results) + { + global $page; + + $result_count = count($results); + if ($result_count == 0 || !S::has_perms()) { + $page->trigError("Il n'y a pas d'utilisateur avec l'identifiant : $login"); + } else { + $page->trigError("Il y a $result_count utilisateurs avec cet identifiant : " . join(', ', $results)); + } + } } // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: -- 2.1.4