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 // Post-processes the successful Google Apps account creation queue job.
23 function post_queue_u_create($job) {
26 // Retrieves the user parameters (userid and forlife).
27 $parameters = json_decode($job['j_parameters'], true
);
28 $forlife = isset($parameters['username']) ?
$parameters['username'] : null
;
29 $userid = $job['q_recipient_id'];
30 if (!$forlife ||
!$userid) {
34 // Adds a redirection to the Google Apps delivery address, if requested by
35 // the user at creation time.
36 $account = new GoogleAppsAccount($userid, $forlife);
37 if ($account->activate_mail_redirection
) {
38 require_once('emails.inc.php');
39 $storage = new MailStorageGoogleApps($userid);
43 // Sends the 'account created' email to the user, with basic documentation.
45 "SELECT FIND_IN_SET('femme', u.flags), prenom
46 FROM auth_user_md5 AS u
47 INNER JOIN aliases AS a ON (a.id = u.user_id)
50 list($sexe, $prenom) = $res->fetchOneRow();
52 $mailer = new PlMailer('googleapps/create.mail.tpl');
53 $mailer->assign('account', $account);
54 $mailer->assign('email', $forlife . '@' . $globals->mail
->domain
);
55 $mailer->assign('googleapps_domain', $globals->mailstorage
->googleapps_domain
);
56 $mailer->assign('prenom', $prenom);
57 $mailer->assign('sexe', $sexe);
61 // Post-processes the successful Google Apps account update queue job.
62 function post_queue_u_update($job) {
65 // If the u_update job was an unsuspend request, re-adds the redirection
66 // to the Google Apps delivery address, provided the account is active (it might
67 // have been deleted between the unsuspension and the post-queue processing).
68 $parameters = json_decode($job['j_parameters'], true
);
69 $forlife = isset($parameters['username']) ?
$parameters['username'] : null
;
70 $userid = $job['q_recipient_id'];
71 if (!$forlife ||
!$userid) {
75 if (isset($parameters['suspended']) && $parameters['suspended'] == false
) {
76 require_once('emails.inc.php');
77 $account = new GoogleAppsAccount($userid, $forlife);
78 if ($account->active()) {
79 // Re-adds the email redirection (if the user did request it).
80 if ($account->activate_mail_redirection
) {
81 $storage = new MailStorageGoogleApps($userid);
85 // Sends an email to the account owner.
87 "SELECT FIND_IN_SET('femme', u.flags), prenom
88 FROM auth_user_md5 AS u
89 INNER JOIN aliases AS a ON (a.id = u.user_id)
92 list($sexe, $prenom) = $res->fetchOneRow();
94 $mailer = new PlMailer('googleapps/unsuspend.mail.tpl');
95 $mailer->assign('account', $account);
96 $mailer->assign('email', $forlife . '@' . $globals->mail
->domain
);
97 $mailer->assign('prenom', $prenom);
98 $mailer->assign('sexe', $sexe);
104 // Reprensentation of an SQL-stored Google Apps account.
105 // This class is the interface with the gappsd SQL tables: gappsd is the python
106 // daemon which deals with Google Apps provisioning APIs.
107 // TODO(vincent.zanotti): add the url of gappsd, when available.
108 class GoogleAppsAccount
110 // User identification: user id, and forlife.
112 public $g_account_name;
114 // Local account parameters.
115 public $sync_password;
116 public $activate_mail_redirection;
118 // Account status, obtained from Google Apps provisioning & reporting APIs.
120 public $g_suspension;
121 public $r_disk_usage;
123 public $r_last_login;
124 public $r_last_webmail;
125 public $reporting_date;
127 // Pending requests in the gappsd job queue (cf. top note).
128 public $pending_create;
129 public $pending_delete;
130 public $pending_update;
131 public $pending_update_admin;
132 public $pending_update_other;
133 public $pending_update_password;
134 public $pending_update_suspension;
136 // Pending requests in plat/al validation queue.
137 public $pending_validation_unsuspend;
139 // Constructs the account object, by retrieving all informations from the
140 // GApps account table, from GApps job queue, and from plat/al validation queue.
141 public function __construct($uid, $account_name = NULL
)
143 if ($account_name == NULL
) {
144 require_once 'user.func.inc.php';
145 $account_name = get_user_forlife($uid, '_silent_user_callback');
149 $this->g_account_name
= $account_name;
150 $this->g_status
= NULL
;
153 "SELECT l_sync_password, l_activate_mail_redirection,
154 g_account_name, g_status, g_suspension, r_disk_usage,
155 UNIX_TIMESTAMP(r_creation) as r_creation,
156 UNIX_TIMESTAMP(r_last_login) as r_last_login,
157 UNIX_TIMESTAMP(r_last_webmail) as r_last_webmail
159 WHERE g_account_name = {?}",
161 if ($account = $res->fetchOneAssoc()) {
162 $this->sync_password
= $account['l_sync_password'];
163 $this->activate_mail_redirection
= $account['l_activate_mail_redirection'];
164 $this->g_status
= $account['g_status'];
165 $this->g_suspension
= $account['g_suspension'];
166 $this->r_disk_usage
= $account['r_disk_usage'];
167 $this->r_creation
= $account['r_creation'];
168 $this->r_last_login
= $account['r_last_webmail'];
169 $this->r_last_webmail
= $account['r_last_webmail'];
171 $this->load_pending_counts();
172 $this->load_pending_validations();
173 if ($this->pending_update
) {
174 $this->load_pending_updates();
177 $res = XDB
::query("SELECT MAX(date) FROM gapps_reporting");
178 $this->reporting_date
= $res->fetchOneCell();
182 // Determines if changes to the Google Account are currently waiting in the
183 // GApps job queue, and initializes the local values accordingly.
184 private function load_pending_counts()
187 "SELECT SUM(j_type = 'u_create') AS pending_create,
188 SUM(j_type = 'u_update') AS pending_update,
189 SUM(j_type = 'u_delete') AS pending_delete
191 WHERE q_recipient_id = {?} AND
192 p_status IN ('idle', 'active', 'softfail')
195 $pending = $res->fetchOneAssoc();
196 $this->pending_create
= $pending['pending_create'];
197 $this->pending_update
= $pending['pending_update'];
198 $this->pending_delete
= $pending['pending_delete'];
200 $this->pending_update_admin
= false
;
201 $this->pending_update_other
= false
;
202 $this->pending_update_password
= false
;
203 $this->pending_update_suspension
= false
;
206 // Checks for unsuspend requests waiting for validation in plat/al
208 private function load_pending_validations()
210 require_once('validations.inc.php');
211 $this->pending_validation_unsuspend
=
212 Validate
::get_typed_requests_count($this->uid
, 'gapps-unsuspend');
215 // Retrieves all the pending update job in the gappsd queue for the current
216 // user, and analyzes the scope of the update (ie. the fields in the user
217 // account which are going to be updated).
218 private function load_pending_updates()
220 $res = XDB
::iterator(
223 WHERE q_recipient_id = {?} AND
224 p_status IN ('idle', 'active', 'softfail') AND
225 j_type = 'u_update'",
227 while ($update = $res->next()) {
228 $update_data = json_decode($update["j_parameters"], true
);
230 if (isset($update_data["suspended"])) {
231 $this->pending_update_suspension
= true
;
232 } elseif (isset($update_data["password"])) {
233 $this->pending_update_password
= true
;
234 } elseif (isset($update_data["admin"])) {
235 $this->pending_update_admin
= true
;
237 $this->pending_update_other
= true
;
242 // Creates a queue job of the @p type, for the user represented by this
243 // GoogleAppsAccount object, using @p parameters. @p parameters is supposed
244 // to be a one-dimension array of key-value mappings.
245 // The created job as a 'normal' priority, and is scheduled for immediate
247 private function create_queue_job($type, $parameters) {
248 $parameters["username"] = $this->g_account_name
;
250 "INSERT INTO gapps_queue
251 SET q_owner_id = {?}, q_recipient_id = {?},
252 p_entry_date = NOW(), p_notbefore_date = NOW(),
253 p_priority = 'normal',
254 j_type = {?}, j_parameters = {?}",
258 json_encode($parameters));
262 // Returns true if the account is currently active.
263 public function active()
265 return $this->g_status
== 'active';
268 // Returns true if the account exists in Google Apps.
269 public function provisioned()
271 return $this->g_status
== 'active' or $this->g_status
== 'disabled';
274 // Returns true if the account exists, but cannot be used (user-requested
275 // suspension, or Google-requested suspension).
276 public function suspended()
278 return $this->g_status
== 'disabled';
282 // Changes the GoogleApps password.
283 public function set_password($password) {
284 if (!$this->provisioned()) {
288 if (!$this->pending_update_password
) {
289 $this->create_queue_job('u_update', array('password' => $password));
294 // Changes the password synchronization status ("sync = true" means that the
295 // Polytechnique.org password will be replicated to the Google Apps account).
296 public function set_password_sync($sync) {
297 if (!$this->provisioned()) {
301 $this->sync_password
= $sync;
303 "UPDATE gapps_accounts
304 SET l_sync_password = {?}
305 WHERE g_account_name = {?}",
307 $this->g_account_name
);
310 // Suspends the Google Apps account.
311 public function suspend() {
312 if (!$this->provisioned()) {
316 if (!$this->pending_update_suspension
) {
317 $this->create_queue_job('u_update', array('suspended' => true
));
318 $this->pending_update_suspension
= true
;
322 // Adds an unsuspension request to the validation queue (used on user-request).
323 public function unsuspend($activate_mail_redirection = NULL
) {
324 if (!$this->provisioned()) {
327 if ($activate_mail_redirection !== NULL
) {
328 $this->activate_mail_redirection
= $activate_mail_redirection;
330 "UPDATE gapps_accounts
331 SET l_activate_mail_redirection = {?}
332 WHERE g_account_name = {?}",
333 $activate_mail_redirection,
334 $this->g_account_name
);
337 if (!$this->pending_update_suspension
&& !$this->pending_validation_unsuspend
) {
338 require_once('validations.inc.php');
339 $unsuspend = new GoogleAppsUnsuspendReq($this->uid
);
340 $unsuspend->submit();
341 $this->pending_validation_unsuspend
= true
;
345 // Unsuspends the Google Apps account (used on admin-request, or on validation of
347 public function do_unsuspend() {
348 if (!$this->provisioned()) {
352 if (!$this->pending_update_suspension
) {
353 if ($this->sync_password
) {
357 WHERE user_id = {?}",
359 $password = ($res->numRows() > 0 ?
$res->fetchOneCell() : false
);
365 $this->create_queue_job('u_update', array('suspended' => false
, 'password' => $password));
367 $this->create_queue_job('u_update', array('suspended' => false
));
369 $this->pending_update_suspension
= true
;
375 // Creates a new Google Apps account with the @p local parameters.
376 public function create($password_sync, $password, $redirect_mails) {
377 if ($this->g_status
!= NULL
) {
381 if (!$this->pending_create
) {
382 // Retrieves information on the new account.
384 "SELECT nom, nom_usage, prenom
386 WHERE user_id = {?}",
388 list($nom, $nom_usage, $prenom) = $res->fetchOneRow();
390 // Adds an 'unprovisioned' entry in the gapps_accounts table.
392 "INSERT INTO gapps_accounts
394 l_sync_password = {?},
395 l_activate_mail_redirection = {?},
396 g_account_name = {?},
399 g_status = 'unprovisioned'",
403 $this->g_account_name
,
405 ($nom_usage ?
$nom_usage : $nom));
407 // Adds the creation job in the GApps queue.
408 $this->create_queue_job(
411 'username' => $this->g_account_name
,
412 'first_name' => $prenom,
413 'last_name' => ($nom_usage ?
$nom_usage : $nom),
414 'password' => $password,
417 // Updates the GoogleAppsAccount status.
418 $this->__construct($this->uid
, $this->g_account_name
);
423 // Returns the status of the Google Apps account for @p user, or false
424 // when no account exists.
425 static public function account_status($uid) {
429 WHERE l_userid = {?}", $uid);
430 return ($res->numRows() > 0 ?
$res->fetchOneCell() : false
);
433 // Returns true if the @p user is an administrator of the Google Apps domain.
434 static public function is_administrator($uid) {
438 WHERE l_userid = {?} AND g_status = 'active'", $uid);
439 return ($res->numRows() > 0 ?
(bool
)$res->fetchOneRow() : false
);
443 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: