2 /***************************************************************************
3 * Copyright (C) 2003-2008 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)
29 if ($login instanceof Profile
) {
30 $res = XDB
::query('SELECT ap.uid
31 FROM account_profiles AS ap
32 WHERE ap.pid = {?} AND FIND_IN_SET(\'owner\', perms)',
34 if ($res->numRows()) {
35 return $res->fetchOneCell();
37 throw new UserNotFoundException();
40 // If $data is an integer, fetches directly the result.
41 if (is_numeric($login)) {
42 $res = XDB
::query('SELECT a.uid
44 WHERE a.uid = {?}', $login);
45 if ($res->numRows()) {
46 return $res->fetchOneCell();
49 throw new UserNotFoundException();
52 // Checks whether $login is a valid hruid or not.
53 $res = XDB
::query('SELECT a.uid
55 WHERE a.hruid = {?}', $login);
56 if ($res->numRows()) {
57 return $res->fetchOneCell();
60 // From now, $login can only by an email alias, or an email redirection.
61 // If it doesn't look like a valid address, appends the plat/al's main domain.
62 $login = trim(strtolower($login));
63 if (strstr($login, '@') === false
) {
64 $login = $login . '@' . $globals->mail
->domain
;
67 // Checks if $login is a valid alias on the main domains.
68 list($mbox, $fqdn) = explode('@', $login);
69 if ($fqdn == $globals->mail
->domain ||
$fqdn == $globals->mail
->domain2
) {
70 $res = XDB
::query('SELECT a.uid
72 INNER JOIN aliases AS al ON (al.id = a.uid AND al.type IN (\'alias\', \'a_vie\'))
73 WHERE al.alias = {?}', $mbox);
74 if ($res->numRows()) {
75 return $res->fetchOneCell();
78 /** TODO: implements this by inspecting the profile.
79 if (preg_match('/^(.*)\.([0-9]{4})$/u', $mbox, $matches)) {
80 $res = XDB::query('SELECT a.uid
82 INNER JOIN aliases AS al ON (al.id = a.uid AND al.type IN ('alias', 'a_vie'))
83 WHERE al.alias = {?} AND a.promo = {?}', $matches[1], $matches[2]);
84 if ($res->numRows() == 1) {
85 return $res->fetchOneCell();
89 throw new UserNotFoundException();
92 // Looks for $login as an email alias from the dedicated alias domain.
93 if ($fqdn == $globals->mail
->alias_dom ||
$fqdn == $globals->mail
->alias_dom2
) {
94 $res = XDB
::query("SELECT redirect
96 INNER JOIN virtual USING(vid)
97 WHERE alias = {?}", $mbox . '@' . $globals->mail
->alias_dom
);
98 if ($redir = $res->fetchOneCell()) {
99 // We now have a valid alias, which has to be translated to an hruid.
100 list($alias, $alias_fqdn) = explode('@', $redir);
101 $res = XDB
::query("SELECT a.uid
103 LEFT JOIN aliases AS al ON (al.id = a.uid AND al.type IN ('alias', 'a_vie'))
104 WHERE al.alias = {?}", $alias);
105 if ($res->numRows()) {
106 return $res->fetchOneCell();
110 throw new UserNotFoundException();
113 // Otherwise, we do suppose $login is an email redirection.
114 $res = XDB
::query("SELECT a.uid
116 LEFT JOIN emails AS e ON (e.uid = a.uid)
117 WHERE e.email = {?}", $login);
118 if ($res->numRows() == 1) {
119 return $res->fetchOneCell();
122 throw new UserNotFoundException($res->fetchColumn(1));
125 // Implementation of the data loader.
126 protected function loadMainFields()
128 if ($this->hruid
!== null
&& $this->forlife
!== null
129 && $this->bestalias
!== null
&& $this->display_name
!== null
130 && $this->full_name
!== null
&& $this->promo
!== null
&& $this->perms
!== null
131 && $this->gender
!== null
&& $this->email_format
!== null
) {
136 /** TODO: promo stuff again */
137 /** TODO: fix perms field to fit new perms system */
138 $res = XDB
::query("SELECT a.hruid, d.promo_display AS promo,
139 CONCAT(af.alias, '@{$globals->mail->domain}') AS forlife,
140 CONCAT(ab.alias, '@{$globals->mail->domain}') AS bestalias,
141 a.full_name, a.display_name, a.sex = 'female' AS gender,
142 a.email_format, a.password,
143 IF (a.state = 'active', at.perms, '') AS perms,
146 INNER JOIN account_types AS at ON (at.type = a.type)
147 INNER JOIN profile_display AS d ON (d.uid = a.uid)
148 LEFT JOIN aliases AS af ON (af.id = a.uid AND af.type = 'a_vie')
149 LEFT JOIN aliases AS ab ON (ab.id = a.uid AND FIND_IN_SET('bestalias', ab.flags))
150 WHERE a.uid = {?}", $this->user_id
);
151 $this->fillFromArray($res->fetchOneAssoc());
154 // Specialization of the fillFromArray method, to implement hacks to enable
155 // lazy loading of user's main properties from the session.
156 // TODO(vzanotti): remove the conversion hacks once the old codebase will
157 // stop being used actively.
158 protected function fillFromArray(array $values)
160 // It might happen that the 'user_id' field is called uid in some places
161 // (eg. in sessions), so we hard link uid to user_id to prevent useless
163 if (!isset($values['user_id']) && isset($values['uid'])) {
164 $values['user_id'] = $values['uid'];
167 // Also, if display_name and full_name are not known, but the user's
168 // surname and last name are, we can construct the former two.
169 if (isset($values['prenom']) && isset($values['nom'])) {
170 if (!isset($values['display_name'])) {
171 $values['display_name'] = ($values['prenom'] ?
$values['prenom'] : $values['nom']);
173 if (!isset($values['full_name'])) {
174 $values['full_name'] = $values['prenom'] . ' ' . $values['nom'];
178 // We also need to convert the gender (usually named "femme"), and the
179 // email format parameter (valued "texte" instead of "text").
180 if (isset($values['femme'])) {
181 $values['gender'] = (bool
) $values['femme'];
183 if (isset($values['mail_fmt'])) {
184 $values['email_format'] = $values['mail_fmt'];
187 parent
::fillFromArray($values);
190 // Specialization of the buildPerms method
191 // This function build 'generic' permissions for the user. It does not take
192 // into account page specific permissions (e.g X.net group permissions)
193 protected function buildPerms()
195 if (!is_null($this->perm_flags
)) {
198 if ($this->perms
=== null
) {
199 $this->loadMainFields();
201 $this->perm_flags
= self
::makePerms($this->perms
, $this->is_admin
);
204 /** Return the main profile attached with this account if any.
206 public function profile()
208 return Profile
::get($this);
211 // Return permission flags for a given permission level.
212 public static function makePerms($perms, $is_admin)
214 $flags = new PlFlagSet($perms);
215 $flags->addFlag(PERMS_USER
);
217 $flags->addFlag(PERMS_ADMIN
);
222 // Implementation of the default user callback.
223 public static function _default_user_callback($login, $results)
225 $result_count = count($results);
226 if ($result_count == 0 ||
!S
::has_perms()) {
227 Platal
::page()->trigError("Il n'y a pas d'utilisateur avec l'identifiant : $login");
229 Platal
::page()->trigError("Il y a $result_count utilisateurs avec cet identifiant : " . join(', ', $results));
233 // Implementation of the static email locality checker.
234 public static function isForeignEmailAddress($email)
237 if (strpos($email, '@') === false
) {
241 list($user, $dom) = explode('@', $email);
242 return $dom != $globals->mail
->domain
&&
243 $dom != $globals->mail
->domain2
&&
244 $dom != $globals->mail
->alias_dom
&&
245 $dom != $globals->mail
->alias_dom2
;
249 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: