Implements administrator actions on Google Apps accounts (password synchronization...
[platal.git] / classes / user.php
CommitLineData
9f8ebb9f
VZ
1<?php
2/***************************************************************************
8d84c630 3 * Copyright (C) 2003-2009 Polytechnique.org *
9f8ebb9f
VZ
4 * http://opensource.polytechnique.org/ *
5 * *
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. *
10 * *
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. *
15 * *
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 *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
2d96cf7b 22class User extends PlUser
9f8ebb9f 23{
70232020 24 // Implementation of the login to uid method.
b1719b13
VZ
25 protected function getLogin($login)
26 {
27 global $globals;
28
29 // If $data is an integer, fetches directly the result.
30 if (is_numeric($login)) {
70232020 31 $res = XDB::query("SELECT user_id FROM auth_user_md5 WHERE user_id = {?}", $login);
b1719b13 32 if ($res->numRows()) {
70232020 33 return $res->fetchOneCell();
b1719b13
VZ
34 }
35
36 throw new UserNotFoundException();
37 }
38
39 // Checks whether $login is a valid hruid or not.
70232020 40 $res = XDB::query("SELECT user_id FROM auth_user_md5 WHERE hruid = {?}", $login);
b1719b13 41 if ($res->numRows()) {
70232020 42 return $res->fetchOneCell();
b1719b13
VZ
43 }
44
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;
50 }
51
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) {
70232020 55 $res = XDB::query("SELECT u.user_id
b1719b13
VZ
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()) {
70232020 60 return $res->fetchOneCell();
b1719b13
VZ
61 }
62
63 if (preg_match('/^(.*)\.([0-9]{4})$/u', $mbox, $matches)) {
70232020 64 $res = XDB::query("SELECT u.user_id
b1719b13
VZ
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) {
70232020 69 return $res->fetchOneCell();
b1719b13
VZ
70 }
71 }
72
73 throw new UserNotFoundException();
74 }
75
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
79 FROM virtual_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);
70232020 85 $res = XDB::query("SELECT u.user_id
b1719b13
VZ
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()) {
70232020 90 return $res->fetchOneCell();
b1719b13
VZ
91 }
92 }
93
94 throw new UserNotFoundException();
95 }
96
97 // Otherwise, we do suppose $login is an email redirection.
70232020 98 $res = XDB::query("SELECT u.user_id
b1719b13
VZ
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) {
70232020 103 return $res->fetchOneCell();
b1719b13
VZ
104 }
105
106 throw new UserNotFoundException($res->fetchColumn(1));
107 }
108
70232020
VZ
109 // Implementation of the data loader.
110 protected function loadMainFields()
111 {
c4012d9b
VZ
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
115 && $this->gender !== null && $this->email_format !== null) {
70232020
VZ
116 return;
117 }
118
119 global $globals;
120 $res = XDB::query("SELECT u.hruid, u.promo,
121 CONCAT(af.alias, '@{$globals->mail->domain}') AS forlife,
122 CONCAT(ab.alias, '@{$globals->mail->domain}') AS bestalias,
77057a5c 123 CONCAT(u.prenom, ' ', IF(u.nom_usage <> '', u.nom_usage, u.nom)) AS full_name,
50d5ec0b 124 IF(u.prenom != '', u.prenom, u.nom) AS display_name,
c4012d9b
VZ
125 FIND_IN_SET('femme', u.flags) AS gender,
126 q.core_mail_fmt AS email_format,
50d5ec0b 127 u.perms
70232020 128 FROM auth_user_md5 AS u
c4012d9b 129 LEFT JOIN auth_user_quick AS q ON (q.user_id = u.user_id)
70232020
VZ
130 LEFT JOIN aliases AS af ON (af.id = u.user_id AND af.type = 'a_vie')
131 LEFT JOIN aliases AS ab ON (ab.id = u.user_id AND FIND_IN_SET('bestalias', ab.flags))
132 WHERE u.user_id = {?}", $this->user_id);
133 $this->fillFromArray($res->fetchOneAssoc());
134 }
135
136 // Specialization of the fillFromArray method, to implement hacks to enable
137 // lazy loading of user's main properties from the session.
c4012d9b
VZ
138 // TODO(vzanotti): remove the conversion hacks once the old codebase will
139 // stop being used actively.
70232020
VZ
140 protected function fillFromArray(array $values)
141 {
142 // It might happen that the 'user_id' field is called uid in some places
143 // (eg. in sessions), so we hard link uid to user_id to prevent useless
144 // SQL requests.
145 if (!isset($values['user_id']) && isset($values['uid'])) {
146 $values['user_id'] = $values['uid'];
147 }
148
149 // Also, if display_name and full_name are not known, but the user's
150 // surname and last name are, we can construct the former two.
151 if (isset($values['prenom']) && isset($values['nom'])) {
152 if (!isset($values['display_name'])) {
153 $values['display_name'] = ($values['prenom'] ? $values['prenom'] : $values['nom']);
154 }
155 if (!isset($values['full_name'])) {
156 $values['full_name'] = $values['prenom'] . ' ' . $values['nom'];
157 }
158 }
159
c4012d9b
VZ
160 // We also need to convert the gender (usually named "femme"), and the
161 // email format parameter (valued "texte" instead of "text").
162 if (isset($values['femme'])) {
163 $values['gender'] = (bool) $values['femme'];
164 }
165 if (isset($values['mail_fmt'])) {
166 $values['email_format'] = $values['mail_fmt'];
167 }
168 if (isset($values['email_format'])) {
169 $values['email_format'] = ($values['email_format'] ? self::FORMAT_HTML : self::FORMAT_TEXT);
170 }
171
70232020
VZ
172 parent::fillFromArray($values);
173 }
174
50d5ec0b
FB
175 // Specialization of the buildPerms method
176 // This function build 'generic' permissions for the user. It does not take
177 // into account page specific permissions (e.g X.net group permissions)
178 protected function buildPerms()
179 {
180 if (!is_null($this->perm_flags)) {
181 return;
182 }
183 if ($this->perms === null) {
184 $this->loadMainFields();
185 }
186 $this->perm_flags = self::makePerms($this->perms);
187 }
188
d69477d3
FB
189 // Return the password of the user
190 public function password()
191 {
192 return XDB::fetchOneCell('SELECT u.password
193 FROM auth_user_md5 AS u
194 WHERE u.user_id = {?}', $this->id());
195 }
196
50d5ec0b
FB
197 // Return permission flags for a given permission level.
198 public static function makePerms($perms)
199 {
200 $flags = new PlFlagSet();
201 if (is_null($flags) || $perms == 'disabled' || $perms == 'ext') {
202 return $flags;
203 }
204 $flags->addFlag(PERMS_USER);
205 if ($perms == 'admin') {
206 $flags->addFlag(PERMS_ADMIN);
207 }
208 return $flags;
209 }
210
b1719b13
VZ
211 // Implementation of the default user callback.
212 public static function _default_user_callback($login, $results)
213 {
b1719b13
VZ
214 $result_count = count($results);
215 if ($result_count == 0 || !S::has_perms()) {
70232020 216 Platal::page()->trigError("Il n'y a pas d'utilisateur avec l'identifiant : $login");
b1719b13 217 } else {
70232020 218 Platal::page()->trigError("Il y a $result_count utilisateurs avec cet identifiant : " . join(', ', $results));
b1719b13
VZ
219 }
220 }
70232020
VZ
221
222 // Implementation of the static email locality checker.
223 public static function isForeignEmailAddress($email)
224 {
225 global $globals;
226 if (strpos($email, '@') === false) {
227 return false;
228 }
229
230 list($user, $dom) = explode('@', $email);
231 return $dom != $globals->mail->domain &&
232 $dom != $globals->mail->domain2 &&
233 $dom != $globals->mail->alias_dom &&
234 $dom != $globals->mail->alias_dom2;
235 }
9f8ebb9f
VZ
236}
237
238// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
239?>