54a3737fdaff9c152ef8acfa075b4ea0c58ff33a
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 private $_profile_fetched = false
;
25 private $_profile = null
;
27 // Implementation of the login to uid method.
28 protected function getLogin($login)
32 if ($login instanceof Profile
) {
33 $this->_profile
= $login;
34 $this->_profile_fetched
= true
;
35 $res = XDB
::query('SELECT ap.uid
36 FROM account_profiles AS ap
37 WHERE ap.pid = {?} AND FIND_IN_SET(\'owner\', perms)',
39 if ($res->numRows()) {
40 return $res->fetchOneCell();
42 throw new UserNotFoundException();
45 // If $data is an integer, fetches directly the result.
46 if (is_numeric($login)) {
47 $res = XDB
::query('SELECT a.uid
49 WHERE a.uid = {?}', $login);
50 if ($res->numRows()) {
51 return $res->fetchOneCell();
54 throw new UserNotFoundException();
57 // Checks whether $login is a valid hruid or not.
58 $res = XDB
::query('SELECT a.uid
60 WHERE a.hruid = {?}', $login);
61 if ($res->numRows()) {
62 return $res->fetchOneCell();
65 // From now, $login can only by an email alias, or an email redirection.
66 // If it doesn't look like a valid address, appends the plat/al's main domain.
67 $login = trim(strtolower($login));
68 if (strstr($login, '@') === false
) {
69 $login = $login . '@' . $globals->mail
->domain
;
72 // Checks if $login is a valid alias on the main domains.
73 list($mbox, $fqdn) = explode('@', $login);
74 if ($fqdn == $globals->mail
->domain ||
$fqdn == $globals->mail
->domain2
) {
75 $res = XDB
::query('SELECT a.uid
77 INNER JOIN aliases AS al ON (al.id = a.uid AND al.type IN (\'alias\', \'a_vie\'))
78 WHERE al.alias = {?}', $mbox);
79 if ($res->numRows()) {
80 return $res->fetchOneCell();
83 /** TODO: implements this by inspecting the profile.
84 if (preg_match('/^(.*)\.([0-9]{4})$/u', $mbox, $matches)) {
85 $res = XDB::query('SELECT a.uid
87 INNER JOIN aliases AS al ON (al.id = a.uid AND al.type IN ('alias', 'a_vie'))
88 WHERE al.alias = {?} AND a.promo = {?}', $matches[1], $matches[2]);
89 if ($res->numRows() == 1) {
90 return $res->fetchOneCell();
94 throw new UserNotFoundException();
97 // Looks for $login as an email alias from the dedicated alias domain.
98 if ($fqdn == $globals->mail
->alias_dom ||
$fqdn == $globals->mail
->alias_dom2
) {
99 $res = XDB
::query("SELECT redirect
100 FROM virtual_redirect
101 INNER JOIN virtual USING(vid)
102 WHERE alias = {?}", $mbox . '@' . $globals->mail
->alias_dom
);
103 if ($redir = $res->fetchOneCell()) {
104 // We now have a valid alias, which has to be translated to an hruid.
105 list($alias, $alias_fqdn) = explode('@', $redir);
106 $res = XDB
::query("SELECT a.uid
108 LEFT JOIN aliases AS al ON (al.id = a.uid AND al.type IN ('alias', 'a_vie'))
109 WHERE al.alias = {?}", $alias);
110 if ($res->numRows()) {
111 return $res->fetchOneCell();
115 throw new UserNotFoundException();
118 // Looks for an account with the given email.
119 $res = XDB
::query('SELECT a.uid
121 WHERE a.email = {?}', $login);
122 if ($res->numRows() == 1) {
123 return $res->fetchOneCell();
126 // Otherwise, we do suppose $login is an email redirection.
127 $res = XDB
::query("SELECT a.uid
129 LEFT JOIN emails AS e ON (e.uid = a.uid)
130 WHERE e.email = {?}", $login);
131 if ($res->numRows() == 1) {
132 return $res->fetchOneCell();
135 throw new UserNotFoundException($res->fetchColumn(1));
138 protected static function loadMainFieldsFromUIDs(array $uids, $sorted = null
)
140 $uids = array_map(array('XDB', 'escape'), $uids);
143 if (!is_null($sorted)) {
147 foreach (explode(',', $sorted) as $part) {
148 $desc = ($part[0] == '-');
150 $part = substr($desc, 1);
156 $part = 'IF (pd.promo IS NULL, \'ext\', pd.promo)';
159 $part = 'a.full_name';
162 $part = 'a.display_name';
167 if (!is_null($part)) {
174 if (count($order) > 0) {
176 $joins .= "LEFT JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET('owner', ap.perms))\n";
179 $joins .= "LEFT JOIN profile_display AS pd ON (pd.pid = ap.pid)\n";
181 $orderby = 'ORDER BY ' . implode(', ', $order);
185 return XDB
::iterator('SELECT a.uid, a.hruid, a.registration_date,
186 CONCAT(af.alias, \'@' . $globals->mail
->domain
. '\') AS forlife,
187 CONCAT(ab.alias, \'@' . $globals->mail
->domain
. '\') AS bestalias,
188 a.full_name, a.display_name, a.sex = \'female\' AS gender,
189 IF(a.state = \'active\', at.perms, \'\') AS perms,
190 a.email_format, a.is_admin, a.state, a.type, a.skin,
191 FIND_IN_SET(\'watch\', a.flags) AS watch, a.comment,
192 a.weak_password IS NOT NULL AS weak_access,
193 a.token IS NOT NULL AS token_access
195 INNER JOIN account_types AS at ON (at.type = a.type)
196 LEFT JOIN aliases AS af ON (af.id = a.uid AND af.type = \'a_vie\')
197 LEFT JOIN aliases AS ab ON (ab.id = a.uid AND FIND_IN_SET(\'bestalias\', ab.flags))
199 WHERE a.uid IN (' . implode(', ', $uids) . ')
203 // Implementation of the data loader.
204 protected function loadMainFields()
206 if ($this->hruid
!== null
&& $this->forlife
!== null
207 && $this->bestalias
!== null
&& $this->display_name
!== null
208 && $this->full_name
!== null
&& $this->perms
!== null
209 && $this->gender
!== null
&& $this->email_format
!== null
) {
212 $this->fillFromArray(self
::loadMainFieldsFromUIDs(array($this->user_id
))->next());
215 // Specialization of the fillFromArray method, to implement hacks to enable
216 // lazy loading of user's main properties from the session.
217 // TODO(vzanotti): remove the conversion hacks once the old codebase will
218 // stop being used actively.
219 protected function fillFromArray(array $values)
221 // It might happen that the 'user_id' field is called uid in some places
222 // (eg. in sessions), so we hard link uid to user_id to prevent useless
224 if (!isset($values['user_id']) && isset($values['uid'])) {
225 $values['user_id'] = $values['uid'];
228 // Also, if display_name and full_name are not known, but the user's
229 // surname and last name are, we can construct the former two.
230 if (isset($values['prenom']) && isset($values['nom'])) {
231 if (!isset($values['display_name'])) {
232 $values['display_name'] = ($values['prenom'] ?
$values['prenom'] : $values['nom']);
234 if (!isset($values['full_name'])) {
235 $values['full_name'] = $values['prenom'] . ' ' . $values['nom'];
239 // We also need to convert the gender (usually named "femme"), and the
240 // email format parameter (valued "texte" instead of "text").
241 if (isset($values['femme'])) {
242 $values['gender'] = (bool
) $values['femme'];
244 if (isset($values['mail_fmt'])) {
245 $values['email_format'] = $values['mail_fmt'];
248 parent
::fillFromArray($values);
251 // Specialization of the buildPerms method
252 // This function build 'generic' permissions for the user. It does not take
253 // into account page specific permissions (e.g X.net group permissions)
254 protected function buildPerms()
256 if (!is_null($this->perm_flags
)) {
259 if ($this->perms
=== null
) {
260 $this->loadMainFields();
262 $this->perm_flags
= self
::makePerms($this->perms
, $this->is_admin
);
265 // We do not want to store the password in the object.
266 // So, fetch it 'on demand'
267 public function password()
269 return XDB
::fetchOneCell('SELECT a.password
271 WHERE a.uid = {?}', $this->id());
274 /** Overload PlUser::promo(): there no promo defined for a user in the current
275 * schema. The promo is a field from the profile.
277 public function promo()
279 if (!$this->hasProfile()) {
282 return $this->profile()->promo();
285 /** Return the main profile attached with this account if any.
287 public function profile()
289 if (!$this->_profile_fetched
) {
290 $this->_profile_fetched
= true
;
291 $this->_profile
= Profile
::get($this);
293 return $this->_profile
;
296 /** Return true if the user has an associated profile.
298 public function hasProfile()
300 return !is_null($this->profile());
303 /** Check if the user can edit to given profile.
305 public function canEdit(Profile
$profile)
307 // XXX: Check permissions (e.g. secretary permission)
308 // and flags from the profile
309 return XDB
::fetchOneCell('SELECT pid
310 FROM account_profiles
311 WHERE uid = {?} AND pid = {?}',
312 $this->id(), $profile->id());
315 /** Get the email alias of the user.
317 public function emailAlias()
320 $data = $this->emailAliases($globals->mail
->alias_dom
);
321 if (count($data) > 0) {
322 return array_pop($data);
327 /** Get all the aliases the user belongs to.
329 public function emailAliases($domain = null
)
332 if (!is_null($domain)) {
333 $where = XDB
::format(' AND alias LIKE CONCAT("%@", {?})', $domain);
335 return XDB
::fetchColumn('SELECT v.alias
337 INNER JOIN virtual_redirect AS vr ON (v.vid = vr.vid)
338 WHERE (vr.redirect = {?} OR vr.redirect = {?})
340 $this->forlifeEmail(), $this->m4xForlifeEmail());
343 /** Get the alternative forlife email
344 * TODO: remove this uber-ugly hack. The issue is that you need to remove
345 * all @m4x.org addresses in virtual_redirect first.
346 * XXX: This is juste to make code more readable, to be remove as soon as possible
348 public function m4xForlifeEmail()
351 trigger_error('USING M4X FORLIFE', E_USER_NOTICE
);
352 return $this->login() . '@' . $globals->mail
->domain2
;
355 // Return permission flags for a given permission level.
356 public static function makePerms($perms, $is_admin)
358 $flags = new PlFlagSet($perms);
359 $flags->addFlag(PERMS_USER
);
361 $flags->addFlag(PERMS_ADMIN
);
366 // Implementation of the default user callback.
367 public static function _default_user_callback($login, $results)
369 $result_count = count($results);
370 if ($result_count == 0 ||
!S
::has_perms()) {
371 Platal
::page()->trigError("Il n'y a pas d'utilisateur avec l'identifiant : $login");
373 Platal
::page()->trigError("Il y a $result_count utilisateurs avec cet identifiant : " . join(', ', $results));
377 // Implementation of the static email locality checker.
378 public static function isForeignEmailAddress($email)
381 if (strpos($email, '@') === false
) {
385 list($user, $dom) = explode('@', $email);
386 return $dom != $globals->mail
->domain
&&
387 $dom != $globals->mail
->domain2
&&
388 $dom != $globals->mail
->alias_dom
&&
389 $dom != $globals->mail
->alias_dom2
;
392 // Fetch a set of users from a list of UIDs
393 public static function getBuildUsersWithUIDs(array $uids, $sortby = null
)
395 $fields = self
::loadMainFieldsFromUIDs($uids, $sortby);
397 while (($list = $fields->next())) {
398 $users[] = User
::getSilentWithValues(null
, $list);
404 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: