819a7a6aa82295879f5bce0b17395375412d066d
2 /***************************************************************************
3 * Copyright (C) 2003-2009 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 class User
extends PlUser
24 // Implementation of the login to uid method.
25 protected function getLogin($login)
30 throw new UserNotFoundException();
33 // If $data is an integer, fetches directly the result.
34 if (is_numeric($login)) {
35 $res = XDB
::query("SELECT user_id FROM auth_user_md5 WHERE user_id = {?}", $login);
36 if ($res->numRows()) {
37 return $res->fetchOneCell();
40 throw new UserNotFoundException();
43 // Checks whether $login is a valid hruid or not.
44 $res = XDB
::query("SELECT user_id FROM auth_user_md5 WHERE hruid = {?}", $login);
45 if ($res->numRows()) {
46 return $res->fetchOneCell();
49 // From now, $login can only by an email alias, or an email redirection.
50 // If it doesn't look like a valid address, appends the plat/al's main domain.
51 $login = trim(strtolower($login));
52 if (strstr($login, '@') === false
) {
53 $login = $login . '@' . $globals->mail
->domain
;
56 // Checks if $login is a valid alias on the main domains.
57 list($mbox, $fqdn) = explode('@', $login);
58 if ($fqdn == $globals->mail
->domain ||
$fqdn == $globals->mail
->domain2
) {
59 $res = XDB
::query("SELECT u.user_id
60 FROM auth_user_md5 AS u
61 INNER JOIN aliases AS a ON (a.id = u.user_id AND a.type IN ('alias', 'a_vie'))
62 WHERE a.alias = {?}", $mbox);
63 if ($res->numRows()) {
64 return $res->fetchOneCell();
67 if (preg_match('/^(.*)\.([0-9]{4})$/u', $mbox, $matches)) {
68 $res = XDB
::query("SELECT u.user_id
69 FROM auth_user_md5 AS u
70 INNER JOIN aliases AS a ON (a.id = u.user_id AND a.type IN ('alias', 'a_vie'))
71 WHERE a.alias = {?} AND u.promo = {?}", $matches[1], $matches[2]);
72 if ($res->numRows() == 1) {
73 return $res->fetchOneCell();
77 throw new UserNotFoundException();
80 // Looks for $login as an email alias from the dedicated alias domain.
81 if ($fqdn == $globals->mail
->alias_dom ||
$fqdn == $globals->mail
->alias_dom2
) {
82 $res = XDB
::query("SELECT redirect
84 INNER JOIN virtual USING(vid)
85 WHERE alias = {?}", $mbox . '@' . $globals->mail
->alias_dom
);
86 if ($redir = $res->fetchOneCell()) {
87 // We now have a valid alias, which has to be translated to an hruid.
88 list($alias, $alias_fqdn) = explode('@', $redir);
89 $res = XDB
::query("SELECT u.user_id
90 FROM auth_user_md5 AS u
91 LEFT JOIN aliases AS a ON (a.id = u.user_id AND a.type IN ('alias', 'a_vie'))
92 WHERE a.alias = {?}", $alias);
93 if ($res->numRows()) {
94 return $res->fetchOneCell();
98 throw new UserNotFoundException();
101 // Otherwise, we do suppose $login is an email redirection.
102 $res = XDB
::query("SELECT u.user_id
103 FROM auth_user_md5 AS u
104 LEFT JOIN emails AS e ON (e.uid = u.user_id)
105 WHERE e.email = {?}", $login);
106 if ($res->numRows() == 1) {
107 return $res->fetchOneCell();
110 throw new UserNotFoundException($res->fetchColumn(1));
113 // Implementation of the data loader.
114 protected function loadMainFields()
116 if ($this->hruid
!== null
&& $this->forlife
!== null
117 && $this->bestalias
!== null
&& $this->display_name
!== null
118 && $this->full_name
!== null
&& $this->promo
!== null
&& $this->perms
!== null
119 && $this->gender
!== null
&& $this->email_format
!== null
) {
124 $res = XDB
::query("SELECT u.hruid, d.promo,
125 CONCAT(af.alias, '@{$globals->mail->domain}') AS forlife,
126 CONCAT(ab.alias, '@{$globals->mail->domain}') AS bestalias,
127 CONCAT(u.prenom, ' ', IF(u.nom_usage <> '', u.nom_usage, u.nom)) AS full_name,
128 IF(u.prenom != '', u.prenom, u.nom) AS display_name,
129 FIND_IN_SET('femme', u.flags) AS gender,
130 q.core_mail_fmt AS email_format,
132 FROM auth_user_md5 AS u
133 INNER JOIN profile_display AS d ON (d.pid = u.user_id)
134 LEFT JOIN auth_user_quick AS q ON (q.user_id = u.user_id)
135 LEFT JOIN aliases AS af ON (af.id = u.user_id AND af.type = 'a_vie')
136 LEFT JOIN aliases AS ab ON (ab.id = u.user_id AND FIND_IN_SET('bestalias', ab.flags))
137 WHERE u.user_id = {?}", $this->user_id
);
138 $this->fillFromArray($res->fetchOneAssoc());
141 // Specialization of the fillFromArray method, to implement hacks to enable
142 // lazy loading of user's main properties from the session.
143 // TODO(vzanotti): remove the conversion hacks once the old codebase will
144 // stop being used actively.
145 protected function fillFromArray(array $values)
147 // It might happen that the 'user_id' field is called uid in some places
148 // (eg. in sessions), so we hard link uid to user_id to prevent useless
150 if (!isset($values['user_id']) && isset($values['uid'])) {
151 $values['user_id'] = $values['uid'];
154 // Also, if display_name and full_name are not known, but the user's
155 // surname and last name are, we can construct the former two.
156 if (isset($values['prenom']) && isset($values['nom'])) {
157 if (!isset($values['display_name'])) {
158 $values['display_name'] = ($values['prenom'] ?
$values['prenom'] : $values['nom']);
160 if (!isset($values['full_name'])) {
161 $values['full_name'] = $values['prenom'] . ' ' . $values['nom'];
165 // We also need to convert the gender (usually named "femme"), and the
166 // email format parameter (valued "texte" instead of "text").
167 if (isset($values['femme'])) {
168 $values['gender'] = (bool
) $values['femme'];
170 if (isset($values['mail_fmt'])) {
171 $values['email_format'] = $values['mail_fmt'];
173 if (isset($values['email_format'])) {
174 $values['email_format'] = ($values['email_format'] ? self
::FORMAT_HTML
: self
::FORMAT_TEXT
);
177 parent
::fillFromArray($values);
180 // Specialization of the buildPerms method
181 // This function build 'generic' permissions for the user. It does not take
182 // into account page specific permissions (e.g X.net group permissions)
183 protected function buildPerms()
185 if (!is_null($this->perm_flags
)) {
188 if ($this->perms
=== null
) {
189 $this->loadMainFields();
191 $this->perm_flags
= self
::makePerms($this->perms
);
194 // Return the password of the user
195 public function password()
197 return XDB
::fetchOneCell('SELECT u.password
198 FROM auth_user_md5 AS u
199 WHERE u.user_id = {?}', $this->id());
202 // Return permission flags for a given permission level.
203 public static function makePerms($perms)
205 $flags = new PlFlagSet();
206 if (is_null($flags) ||
$perms == 'disabled' ||
$perms == 'ext') {
209 $flags->addFlag(PERMS_USER
);
210 if ($perms == 'admin') {
211 $flags->addFlag(PERMS_ADMIN
);
216 // Implementation of the default user callback.
217 public static function _default_user_callback($login, $results)
219 $result_count = count($results);
220 if ($result_count == 0 ||
!S
::has_perms()) {
221 Platal
::page()->trigError("Il n'y a pas d'utilisateur avec l'identifiant : $login");
223 Platal
::page()->trigError("Il y a $result_count utilisateurs avec cet identifiant : " . join(', ', $results));
227 // Implementation of the static email locality checker.
228 public static function isForeignEmailAddress($email)
231 if (strpos($email, '@') === false
) {
235 list($user, $dom) = explode('@', $email);
236 return $dom != $globals->mail
->domain
&&
237 $dom != $globals->mail
->domain2
&&
238 $dom != $globals->mail
->alias_dom
&&
239 $dom != $globals->mail
->alias_dom2
;
243 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: