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 $data is an integer, fetches directly the result.
30 if (is_numeric($login)) {
31 $res = XDB
::query("SELECT user_id FROM auth_user_md5 WHERE user_id = {?}", $login);
32 if ($res->numRows()) {
33 return $res->fetchOneCell();
36 throw new UserNotFoundException();
39 // Checks whether $login is a valid hruid or not.
40 $res = XDB
::query("SELECT user_id FROM auth_user_md5 WHERE hruid = {?}", $login);
41 if ($res->numRows()) {
42 return $res->fetchOneCell();
45 // From now, $login can only by an email alias, or an email redirection.
46 // If it doesn't look like a valid address, appends the plat/al's main domain.
47 $login = trim(strtolower($login));
48 if (strstr($login, '@') === false
) {
49 $login = $login . '@' . $globals->mail
->domain
;
52 // Checks if $login is a valid alias on the main domains.
53 list($mbox, $fqdn) = explode('@', $login);
54 if ($fqdn == $globals->mail
->domain ||
$fqdn == $globals->mail
->domain2
) {
55 $res = XDB
::query("SELECT u.user_id
56 FROM auth_user_md5 AS u
57 INNER JOIN aliases AS a ON (a.id = u.user_id AND a.type IN ('alias', 'a_vie'))
58 WHERE a.alias = {?}", $mbox);
59 if ($res->numRows()) {
60 return $res->fetchOneCell();
63 if (preg_match('/^(.*)\.([0-9]{4})$/u', $mbox, $matches)) {
64 $res = XDB
::query("SELECT u.user_id
65 FROM auth_user_md5 AS u
66 INNER JOIN aliases AS a ON (a.id = u.user_id AND a.type IN ('alias', 'a_vie'))
67 WHERE a.alias = {?} AND u.promo = {?}", $matches[1], $matches[2]);
68 if ($res->numRows() == 1) {
69 return $res->fetchOneCell();
73 throw new UserNotFoundException();
76 // Looks for $login as an email alias from the dedicated alias domain.
77 if ($fqdn == $globals->mail
->alias_dom ||
$fqdn == $globals->mail
->alias_dom2
) {
78 $res = XDB
::query("SELECT redirect
80 INNER JOIN virtual USING(vid)
81 WHERE alias = {?}", $mbox . '@' . $globals->mail
->alias_dom
);
82 if ($redir = $res->fetchOneCell()) {
83 // We now have a valid alias, which has to be translated to an hruid.
84 list($alias, $alias_fqdn) = explode('@', $redir);
85 $res = XDB
::query("SELECT u.user_id
86 FROM auth_user_md5 AS u
87 LEFT JOIN aliases AS a ON (a.id = u.user_id AND a.type IN ('alias', 'a_vie'))
88 WHERE a.alias = {?}", $alias);
89 if ($res->numRows()) {
90 return $res->fetchOneCell();
94 throw new UserNotFoundException();
97 // Otherwise, we do suppose $login is an email redirection.
98 $res = XDB
::query("SELECT u.user_id
99 FROM auth_user_md5 AS u
100 LEFT JOIN emails AS e ON (e.uid = u.user_id)
101 WHERE e.email = {?}", $login);
102 if ($res->numRows() == 1) {
103 return $res->fetchOneCell();
106 throw new UserNotFoundException($res->fetchColumn(1));
109 // Implementation of the data loader.
110 protected function loadMainFields()
112 if ($this->hruid
!== null
&& $this->forlife
!== null
&&
113 $this->bestalias
!== null
&& $this->display_name
!== null
&&
114 $this->full_name
!== null
&& $this->promo
!== null
&& $this->perms
!== null
) {
119 $res = XDB
::query("SELECT u.hruid, u.promo,
120 CONCAT(af.alias, '@{$globals->mail->domain}') AS forlife,
121 CONCAT(ab.alias, '@{$globals->mail->domain}') AS bestalias,
122 CONCAT(u.prenom, ' ', u.nom) AS full_name,
123 IF(u.prenom != '', u.prenom, u.nom) AS display_name,
125 FROM auth_user_md5 AS u
126 LEFT JOIN aliases AS af ON (af.id = u.user_id AND af.type = 'a_vie')
127 LEFT JOIN aliases AS ab ON (ab.id = u.user_id AND FIND_IN_SET('bestalias', ab.flags))
128 WHERE u.user_id = {?}", $this->user_id
);
129 $this->fillFromArray($res->fetchOneAssoc());
132 // Specialization of the fillFromArray method, to implement hacks to enable
133 // lazy loading of user's main properties from the session.
134 protected function fillFromArray(array $values)
136 // It might happen that the 'user_id' field is called uid in some places
137 // (eg. in sessions), so we hard link uid to user_id to prevent useless
139 if (!isset($values['user_id']) && isset($values['uid'])) {
140 $values['user_id'] = $values['uid'];
143 // Also, if display_name and full_name are not known, but the user's
144 // surname and last name are, we can construct the former two.
145 if (isset($values['prenom']) && isset($values['nom'])) {
146 if (!isset($values['display_name'])) {
147 $values['display_name'] = ($values['prenom'] ?
$values['prenom'] : $values['nom']);
149 if (!isset($values['full_name'])) {
150 $values['full_name'] = $values['prenom'] . ' ' . $values['nom'];
154 parent
::fillFromArray($values);
157 // Specialization of the buildPerms method
158 // This function build 'generic' permissions for the user. It does not take
159 // into account page specific permissions (e.g X.net group permissions)
160 protected function buildPerms()
162 if (!is_null($this->perm_flags
)) {
165 if ($this->perms
=== null
) {
166 $this->loadMainFields();
168 $this->perm_flags
= self
::makePerms($this->perms
);
171 // Return permission flags for a given permission level.
172 public static function makePerms($perms)
174 $flags = new PlFlagSet();
175 if (is_null($flags) ||
$perms == 'disabled' ||
$perms == 'ext') {
178 $flags->addFlag(PERMS_USER
);
179 if ($perms == 'admin') {
180 $flags->addFlag(PERMS_ADMIN
);
185 // Implementation of the default user callback.
186 public static function _default_user_callback($login, $results)
188 $result_count = count($results);
189 if ($result_count == 0 ||
!S
::has_perms()) {
190 Platal
::page()->trigError("Il n'y a pas d'utilisateur avec l'identifiant : $login");
192 Platal
::page()->trigError("Il y a $result_count utilisateurs avec cet identifiant : " . join(', ', $results));
196 // Implementation of the static email locality checker.
197 public static function isForeignEmailAddress($email)
200 if (strpos($email, '@') === false
) {
204 list($user, $dom) = explode('@', $email);
205 return $dom != $globals->mail
->domain
&&
206 $dom != $globals->mail
->domain2
&&
207 $dom != $globals->mail
->alias_dom
&&
208 $dom != $globals->mail
->alias_dom2
;
212 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: