Port marketing.
[platal.git] / include / googleapps.inc.php
CommitLineData
bb0727ea
VZ
1<?php
2/***************************************************************************
8d84c630 3 * Copyright (C) 2003-2009 Polytechnique.org *
bb0727ea
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
f5c4bf30 22// Post-processes the successful Google Apps account creation queue job.
bb0727ea
VZ
23function post_queue_u_create($job) {
24 global $globals;
25
d56cb887 26 // Retrieves the user parameters (GoogleApps username and user_id).
bb0727ea 27 $parameters = json_decode($job['j_parameters'], true);
d56cb887
VZ
28 $username = isset($parameters['username']) ? $parameters['username'] : null;
29 if (!($user = User::getSilent($username))) {
bb0727ea
VZ
30 return;
31 }
32
f5c4bf30
VZ
33 // Adds a redirection to the Google Apps delivery address, if requested by
34 // the user at creation time.
d56cb887 35 $account = new GoogleAppsAccount($user);
bb0727ea
VZ
36 if ($account->activate_mail_redirection) {
37 require_once('emails.inc.php');
12a587df 38 $storage = new EmailStorage($user, 'googleapps');
94c6c788 39 $storage->activate();
bb0727ea
VZ
40 }
41
f5c4bf30 42 // Sends the 'account created' email to the user, with basic documentation.
bb0727ea
VZ
43 $mailer = new PlMailer('googleapps/create.mail.tpl');
44 $mailer->assign('account', $account);
d56cb887 45 $mailer->assign('email', $user->bestEmail());
bb0727ea 46 $mailer->assign('googleapps_domain', $globals->mailstorage->googleapps_domain);
35accc05
FB
47 $mailer->assign('prenom', $user->displayName());
48 $mailer->assign('sexe', $user->isFemale());
bb0727ea
VZ
49 $mailer->send();
50}
51
f5c4bf30 52// Post-processes the successful Google Apps account update queue job.
bb0727ea
VZ
53function post_queue_u_update($job) {
54 global $globals;
55
56 // If the u_update job was an unsuspend request, re-adds the redirection
57 // to the Google Apps delivery address, provided the account is active (it might
58 // have been deleted between the unsuspension and the post-queue processing).
59 $parameters = json_decode($job['j_parameters'], true);
d56cb887
VZ
60 $username = isset($parameters['username']) ? $parameters['username'] : null;
61 if (!($user = User::getSilent($username))) {
bb0727ea
VZ
62 return;
63 }
64
65 if (isset($parameters['suspended']) && $parameters['suspended'] == false) {
66 require_once('emails.inc.php');
d56cb887 67 $account = new GoogleAppsAccount($user);
f5c4bf30 68 if ($account->active()) {
bb0727ea
VZ
69 // Re-adds the email redirection (if the user did request it).
70 if ($account->activate_mail_redirection) {
12a587df 71 $storage = new EmailStorage($user, 'googleapps');
94c6c788 72 $storage->activate();
bb0727ea
VZ
73 }
74
75 // Sends an email to the account owner.
bb0727ea
VZ
76 $mailer = new PlMailer('googleapps/unsuspend.mail.tpl');
77 $mailer->assign('account', $account);
d56cb887 78 $mailer->assign('email', $user->bestEmail());
35accc05
FB
79 $mailer->assign('prenom', $user->displayName());
80 $mailer->assign('sexe', $user->isFemale());
bb0727ea
VZ
81 $mailer->send();
82 }
83 }
84}
85
86// Reprensentation of an SQL-stored Google Apps account.
f5c4bf30
VZ
87// This class is the interface with the gappsd SQL tables: gappsd is the python
88// daemon which deals with Google Apps provisioning APIs.
89// TODO(vincent.zanotti): add the url of gappsd, when available.
bb0727ea
VZ
90class GoogleAppsAccount
91{
90c614cd 92 // User identification: user id, and hruid.
d56cb887 93 private $user;
bb0727ea
VZ
94 public $g_account_name;
95
f5c4bf30 96 // Local account parameters.
bb0727ea
VZ
97 public $sync_password;
98 public $activate_mail_redirection;
f5c4bf30
VZ
99
100 // Account status, obtained from Google Apps provisioning & reporting APIs.
0089e594 101 public $g_account_id;
bb0727ea
VZ
102 public $g_status;
103 public $g_suspension;
104 public $r_disk_usage;
105 public $r_creation;
106 public $r_last_login;
107 public $r_last_webmail;
108 public $reporting_date;
109
f5c4bf30 110 // Pending requests in the gappsd job queue (cf. top note).
bb0727ea
VZ
111 public $pending_create;
112 public $pending_delete;
113 public $pending_update;
114 public $pending_update_admin;
115 public $pending_update_other;
116 public $pending_update_password;
117 public $pending_update_suspension;
118
f5c4bf30 119 // Pending requests in plat/al validation queue.
bb0727ea
VZ
120 public $pending_validation_unsuspend;
121
f5c4bf30
VZ
122 // Constructs the account object, by retrieving all informations from the
123 // GApps account table, from GApps job queue, and from plat/al validation queue.
d56cb887 124 public function __construct(User &$user)
bb0727ea 125 {
d56cb887
VZ
126 $this->user = &$user;
127 if (!$this->user || !$this->user->login()) {
128 return;
f5c4bf30
VZ
129 }
130
d56cb887
VZ
131 // TODO: switch to multi-domain Google Apps, and use $this->user->forlifeEmail()
132 // as Google Apps idenfiant (requires changes in gappsd).
133 $this->g_account_name = $this->user->login();
bb0727ea
VZ
134 $this->g_status = NULL;
135
136 $res = XDB::query(
137 "SELECT l_sync_password, l_activate_mail_redirection,
0089e594 138 g_account_name, g_account_id, g_status, g_suspension, r_disk_usage,
bb0727ea
VZ
139 UNIX_TIMESTAMP(r_creation) as r_creation,
140 UNIX_TIMESTAMP(r_last_login) as r_last_login,
141 UNIX_TIMESTAMP(r_last_webmail) as r_last_webmail
142 FROM gapps_accounts
d56cb887 143 WHERE g_account_name = {?}", $this->g_account_name);
bb0727ea
VZ
144 if ($account = $res->fetchOneAssoc()) {
145 $this->sync_password = $account['l_sync_password'];
146 $this->activate_mail_redirection = $account['l_activate_mail_redirection'];
0089e594 147 $this->g_account_id = $account['g_account_id'];
bb0727ea
VZ
148 $this->g_status = $account['g_status'];
149 $this->g_suspension = $account['g_suspension'];
150 $this->r_disk_usage = $account['r_disk_usage'];
151 $this->r_creation = $account['r_creation'];
152 $this->r_last_login = $account['r_last_webmail'];
153 $this->r_last_webmail = $account['r_last_webmail'];
154
155 $this->load_pending_counts();
156 $this->load_pending_validations();
157 if ($this->pending_update) {
158 $this->load_pending_updates();
159 }
160
161 $res = XDB::query("SELECT MAX(date) FROM gapps_reporting");
162 $this->reporting_date = $res->fetchOneCell();
163 }
164 }
165
f5c4bf30
VZ
166 // Determines if changes to the Google Account are currently waiting in the
167 // GApps job queue, and initializes the local values accordingly.
bb0727ea
VZ
168 private function load_pending_counts()
169 {
bb0727ea
VZ
170 $res = XDB::query(
171 "SELECT SUM(j_type = 'u_create') AS pending_create,
172 SUM(j_type = 'u_update') AS pending_update,
173 SUM(j_type = 'u_delete') AS pending_delete
174 FROM gapps_queue
175 WHERE q_recipient_id = {?} AND
176 p_status IN ('idle', 'active', 'softfail')
d56cb887 177 GROUP BY j_type", $this->user->id());
bb0727ea
VZ
178 $pending = $res->fetchOneAssoc();
179 $this->pending_create = $pending['pending_create'];
180 $this->pending_update = $pending['pending_update'];
181 $this->pending_delete = $pending['pending_delete'];
182
183 $this->pending_update_admin = false;
184 $this->pending_update_other = false;
185 $this->pending_update_password = false;
186 $this->pending_update_suspension = false;
187 }
188
f5c4bf30
VZ
189 // Checks for unsuspend requests waiting for validation in plat/al
190 // validation queue.
bb0727ea
VZ
191 private function load_pending_validations()
192 {
193 require_once('validations.inc.php');
194 $this->pending_validation_unsuspend =
d56cb887 195 Validate::get_typed_requests_count($this->user->id(), 'gapps-unsuspend');
bb0727ea
VZ
196 }
197
f5c4bf30
VZ
198 // Retrieves all the pending update job in the gappsd queue for the current
199 // user, and analyzes the scope of the update (ie. the fields in the user
200 // account which are going to be updated).
bb0727ea
VZ
201 private function load_pending_updates()
202 {
bb0727ea
VZ
203 $res = XDB::iterator(
204 "SELECT j_parameters
205 FROM gapps_queue
206 WHERE q_recipient_id = {?} AND
207 p_status IN ('idle', 'active', 'softfail') AND
d56cb887 208 j_type = 'u_update'", $this->user->id());
bb0727ea
VZ
209 while ($update = $res->next()) {
210 $update_data = json_decode($update["j_parameters"], true);
211
212 if (isset($update_data["suspended"])) {
213 $this->pending_update_suspension = true;
214 } elseif (isset($update_data["password"])) {
215 $this->pending_update_password = true;
216 } elseif (isset($update_data["admin"])) {
217 $this->pending_update_admin = true;
218 } else {
219 $this->pending_update_other = true;
220 }
221 }
222 }
223
224 // Creates a queue job of the @p type, for the user represented by this
f5c4bf30
VZ
225 // GoogleAppsAccount object, using @p parameters. @p parameters is supposed
226 // to be a one-dimension array of key-value mappings.
d93451de 227 // The created job as a 'immediate' priority, and is scheduled for immediate
f5c4bf30 228 // execution.
bb0727ea
VZ
229 private function create_queue_job($type, $parameters) {
230 $parameters["username"] = $this->g_account_name;
231 XDB::execute(
232 "INSERT INTO gapps_queue
233 SET q_owner_id = {?}, q_recipient_id = {?},
234 p_entry_date = NOW(), p_notbefore_date = NOW(),
d93451de 235 p_priority = 'immediate',
bb0727ea
VZ
236 j_type = {?}, j_parameters = {?}",
237 S::v('uid'),
d56cb887 238 $this->user->id(),
bb0727ea
VZ
239 $type,
240 json_encode($parameters));
241 }
242
f5c4bf30
VZ
243
244 // Returns true if the account is currently active.
245 public function active()
246 {
247 return $this->g_status == 'active';
248 }
249
250 // Returns true if the account exists in Google Apps.
251 public function provisioned()
252 {
253 return $this->g_status == 'active' or $this->g_status == 'disabled';
254 }
255
256 // Returns true if the account exists, but cannot be used (user-requested
257 // suspension, or Google-requested suspension).
258 public function suspended()
259 {
260 return $this->g_status == 'disabled';
261 }
262
263
bb0727ea
VZ
264 // Changes the GoogleApps password.
265 public function set_password($password) {
f5c4bf30 266 if (!$this->provisioned()) {
bb0727ea
VZ
267 return;
268 }
269
270 if (!$this->pending_update_password) {
271 $this->create_queue_job('u_update', array('password' => $password));
d73f885f 272 $this->pending_update_password = true;
bb0727ea
VZ
273 }
274 }
275
f5c4bf30 276
bb0727ea
VZ
277 // Changes the password synchronization status ("sync = true" means that the
278 // Polytechnique.org password will be replicated to the Google Apps account).
279 public function set_password_sync($sync) {
f5c4bf30 280 if (!$this->provisioned()) {
bb0727ea
VZ
281 return;
282 }
283
284 $this->sync_password = $sync;
285 XDB::execute(
286 "UPDATE gapps_accounts
287 SET l_sync_password = {?}
288 WHERE g_account_name = {?}",
289 $sync,
290 $this->g_account_name);
291 }
292
293 // Suspends the Google Apps account.
294 public function suspend() {
f5c4bf30 295 if (!$this->provisioned()) {
bb0727ea
VZ
296 return;
297 }
298
299 if (!$this->pending_update_suspension) {
300 $this->create_queue_job('u_update', array('suspended' => true));
301 $this->pending_update_suspension = true;
5656271f
VZ
302 XDB::execute(
303 "UPDATE gapps_accounts
304 SET g_status = 'disabled'
305 WHERE g_account_name = {?} AND g_status = 'active'",
306 $this->g_account_name);
bb0727ea
VZ
307 }
308 }
309
310 // Adds an unsuspension request to the validation queue (used on user-request).
311 public function unsuspend($activate_mail_redirection = NULL) {
f5c4bf30 312 if (!$this->provisioned()) {
bb0727ea
VZ
313 return;
314 }
315 if ($activate_mail_redirection !== NULL) {
316 $this->activate_mail_redirection = $activate_mail_redirection;
317 XDB::execute(
318 "UPDATE gapps_accounts
319 SET l_activate_mail_redirection = {?}
320 WHERE g_account_name = {?}",
f5c4bf30
VZ
321 $activate_mail_redirection,
322 $this->g_account_name);
bb0727ea
VZ
323 }
324
325 if (!$this->pending_update_suspension && !$this->pending_validation_unsuspend) {
326 require_once('validations.inc.php');
5daf68f6 327 $unsuspend = new GoogleAppsUnsuspendReq($this->user);
bb0727ea
VZ
328 $unsuspend->submit();
329 $this->pending_validation_unsuspend = true;
330 }
331 }
332
333 // Unsuspends the Google Apps account (used on admin-request, or on validation of
334 // an user-request).
335 public function do_unsuspend() {
f5c4bf30 336 if (!$this->provisioned()) {
bb0727ea
VZ
337 return;
338 }
339
340 if (!$this->pending_update_suspension) {
341 if ($this->sync_password) {
342 $res = XDB::query(
343 "SELECT password
35accc05
FB
344 FROM accounts
345 WHERE uid = {?}", $this->user->id());
bb0727ea
VZ
346 $password = ($res->numRows() > 0 ? $res->fetchOneCell() : false);
347 } else {
348 $password = false;
349 }
350
351 if ($password) {
352 $this->create_queue_job('u_update', array('suspended' => false, 'password' => $password));
353 } else {
354 $this->create_queue_job('u_update', array('suspended' => false));
355 }
356 $this->pending_update_suspension = true;
357 return true;
358 }
359 return false;
360 }
361
f5c4bf30 362 // Creates a new Google Apps account with the @p local parameters.
38c6fe96
FB
363 public function create($password_sync, $password, $redirect_mails)
364 {
bb0727ea
VZ
365 if ($this->g_status != NULL) {
366 return;
367 }
368
369 if (!$this->pending_create) {
370 // Retrieves information on the new account.
35accc05 371 // TODO: retreive first_name and last_name from the profile.
bb0727ea
VZ
372 $res = XDB::query(
373 "SELECT nom, nom_usage, prenom
374 FROM auth_user_md5
d56cb887 375 WHERE user_id = {?}", $this->user->id());
bb0727ea
VZ
376 list($nom, $nom_usage, $prenom) = $res->fetchOneRow();
377
f5c4bf30 378 // Adds an 'unprovisioned' entry in the gapps_accounts table.
bb0727ea
VZ
379 XDB::execute(
380 "INSERT INTO gapps_accounts
381 SET l_userid = {?},
382 l_sync_password = {?},
383 l_activate_mail_redirection = {?},
384 g_account_name = {?},
385 g_first_name = {?},
386 g_last_name = {?},
387 g_status = 'unprovisioned'",
d56cb887 388 $this->user->id(),
bb0727ea
VZ
389 $password_sync,
390 $redirect_mails,
391 $this->g_account_name,
392 $prenom,
393 ($nom_usage ? $nom_usage : $nom));
394
395 // Adds the creation job in the GApps queue.
396 $this->create_queue_job(
397 'u_create',
398 array(
399 'username' => $this->g_account_name,
400 'first_name' => $prenom,
401 'last_name' => ($nom_usage ? $nom_usage : $nom),
402 'password' => $password,
403 ));
404
405 // Updates the GoogleAppsAccount status.
d56cb887 406 $this->__construct($this->user);
bb0727ea
VZ
407 }
408 }
f5c4bf30
VZ
409
410
411 // Returns the status of the Google Apps account for @p user, or false
412 // when no account exists.
413 static public function account_status($uid) {
414 $res = XDB::query(
415 "SELECT g_status
416 FROM gapps_accounts
417 WHERE l_userid = {?}", $uid);
418 return ($res->numRows() > 0 ? $res->fetchOneCell() : false);
419 }
420
421 // Returns true if the @p user is an administrator of the Google Apps domain.
422 static public function is_administrator($uid) {
423 $res = XDB::query(
424 "SELECT g_admin
425 FROM gapps_accounts
426 WHERE l_userid = {?} AND g_status = 'active'", $uid);
4b67332c 427 return ($res->numRows() > 0 ? (bool)$res->fetchOneCell() : false);
f5c4bf30 428 }
bb0727ea
VZ
429}
430
431// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
432?>