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 (GoogleApps username and user_id).
27 $parameters = json_decode($job['j_parameters'], true
);
28 $username = isset($parameters['username']) ?
$parameters['username'] : null
;
29 if (!($user = User
::getSilent($username))) {
33 // Adds a redirection to the Google Apps delivery address, if requested by
34 // the user at creation time.
35 $account = new GoogleAppsAccount($user);
36 if ($account->activate_mail_redirection
) {
37 require_once('emails.inc.php');
38 $storage = new EmailStorage($user->id(), 'googleapps');
42 // Sends the 'account created' email to the user, with basic documentation.
44 "SELECT FIND_IN_SET('femme', u.flags), prenom
45 FROM auth_user_md5 AS u
46 WHERE u.user_id = {?}", $user->id());
47 list($sexe, $prenom) = $res->fetchOneRow();
49 $mailer = new PlMailer('googleapps/create.mail.tpl');
50 $mailer->assign('account', $account);
51 $mailer->assign('email', $user->bestEmail());
52 $mailer->assign('googleapps_domain', $globals->mailstorage
->googleapps_domain
);
53 $mailer->assign('prenom', $prenom);
54 $mailer->assign('sexe', $sexe);
58 // Post-processes the successful Google Apps account update queue job.
59 function post_queue_u_update($job) {
62 // If the u_update job was an unsuspend request, re-adds the redirection
63 // to the Google Apps delivery address, provided the account is active (it might
64 // have been deleted between the unsuspension and the post-queue processing).
65 $parameters = json_decode($job['j_parameters'], true
);
66 $username = isset($parameters['username']) ?
$parameters['username'] : null
;
67 if (!($user = User
::getSilent($username))) {
71 if (isset($parameters['suspended']) && $parameters['suspended'] == false
) {
72 require_once('emails.inc.php');
73 $account = new GoogleAppsAccount($user);
74 if ($account->active()) {
75 // Re-adds the email redirection (if the user did request it).
76 if ($account->activate_mail_redirection
) {
77 $storage = new EmailStorage($user->id
, 'googleapps');
81 // Sends an email to the account owner.
83 "SELECT FIND_IN_SET('femme', u.flags), prenom
84 FROM auth_user_md5 AS u
85 WHERE u.user_id = {?}", $user->id());
86 list($sexe, $prenom) = $res->fetchOneRow();
88 $mailer = new PlMailer('googleapps/unsuspend.mail.tpl');
89 $mailer->assign('account', $account);
90 $mailer->assign('email', $user->bestEmail());
91 $mailer->assign('prenom', $prenom);
92 $mailer->assign('sexe', $sexe);
98 // Reprensentation of an SQL-stored Google Apps account.
99 // This class is the interface with the gappsd SQL tables: gappsd is the python
100 // daemon which deals with Google Apps provisioning APIs.
101 // TODO(vincent.zanotti): add the url of gappsd, when available.
102 class GoogleAppsAccount
104 // User identification: user id, and forlife.
106 public $g_account_name;
108 // Local account parameters.
109 public $sync_password;
110 public $activate_mail_redirection;
112 // Account status, obtained from Google Apps provisioning & reporting APIs.
113 public $g_account_id;
115 public $g_suspension;
116 public $r_disk_usage;
118 public $r_last_login;
119 public $r_last_webmail;
120 public $reporting_date;
122 // Pending requests in the gappsd job queue (cf. top note).
123 public $pending_create;
124 public $pending_delete;
125 public $pending_update;
126 public $pending_update_admin;
127 public $pending_update_other;
128 public $pending_update_password;
129 public $pending_update_suspension;
131 // Pending requests in plat/al validation queue.
132 public $pending_validation_unsuspend;
134 // Constructs the account object, by retrieving all informations from the
135 // GApps account table, from GApps job queue, and from plat/al validation queue.
136 public function __construct(User
&$user)
138 $this->user
= &$user;
139 if (!$this->user ||
!$this->user
->login()) {
143 // TODO: switch to multi-domain Google Apps, and use $this->user->forlifeEmail()
144 // as Google Apps idenfiant (requires changes in gappsd).
145 $this->g_account_name
= $this->user
->login();
146 $this->g_status
= NULL
;
149 "SELECT l_sync_password, l_activate_mail_redirection,
150 g_account_name, g_account_id, g_status, g_suspension, r_disk_usage,
151 UNIX_TIMESTAMP(r_creation) as r_creation,
152 UNIX_TIMESTAMP(r_last_login) as r_last_login,
153 UNIX_TIMESTAMP(r_last_webmail) as r_last_webmail
155 WHERE g_account_name = {?}", $this->g_account_name
);
156 if ($account = $res->fetchOneAssoc()) {
157 $this->sync_password
= $account['l_sync_password'];
158 $this->activate_mail_redirection
= $account['l_activate_mail_redirection'];
159 $this->g_account_id
= $account['g_account_id'];
160 $this->g_status
= $account['g_status'];
161 $this->g_suspension
= $account['g_suspension'];
162 $this->r_disk_usage
= $account['r_disk_usage'];
163 $this->r_creation
= $account['r_creation'];
164 $this->r_last_login
= $account['r_last_webmail'];
165 $this->r_last_webmail
= $account['r_last_webmail'];
167 $this->load_pending_counts();
168 $this->load_pending_validations();
169 if ($this->pending_update
) {
170 $this->load_pending_updates();
173 $res = XDB
::query("SELECT MAX(date) FROM gapps_reporting");
174 $this->reporting_date
= $res->fetchOneCell();
178 // Determines if changes to the Google Account are currently waiting in the
179 // GApps job queue, and initializes the local values accordingly.
180 private function load_pending_counts()
183 "SELECT SUM(j_type = 'u_create') AS pending_create,
184 SUM(j_type = 'u_update') AS pending_update,
185 SUM(j_type = 'u_delete') AS pending_delete
187 WHERE q_recipient_id = {?} AND
188 p_status IN ('idle', 'active', 'softfail')
189 GROUP BY j_type", $this->user
->id());
190 $pending = $res->fetchOneAssoc();
191 $this->pending_create
= $pending['pending_create'];
192 $this->pending_update
= $pending['pending_update'];
193 $this->pending_delete
= $pending['pending_delete'];
195 $this->pending_update_admin
= false
;
196 $this->pending_update_other
= false
;
197 $this->pending_update_password
= false
;
198 $this->pending_update_suspension
= false
;
201 // Checks for unsuspend requests waiting for validation in plat/al
203 private function load_pending_validations()
205 require_once('validations.inc.php');
206 $this->pending_validation_unsuspend
=
207 Validate
::get_typed_requests_count($this->user
->id(), 'gapps-unsuspend');
210 // Retrieves all the pending update job in the gappsd queue for the current
211 // user, and analyzes the scope of the update (ie. the fields in the user
212 // account which are going to be updated).
213 private function load_pending_updates()
215 $res = XDB
::iterator(
218 WHERE q_recipient_id = {?} AND
219 p_status IN ('idle', 'active', 'softfail') AND
220 j_type = 'u_update'", $this->user
->id());
221 while ($update = $res->next()) {
222 $update_data = json_decode($update["j_parameters"], true
);
224 if (isset($update_data["suspended"])) {
225 $this->pending_update_suspension
= true
;
226 } elseif (isset($update_data["password"])) {
227 $this->pending_update_password
= true
;
228 } elseif (isset($update_data["admin"])) {
229 $this->pending_update_admin
= true
;
231 $this->pending_update_other
= true
;
236 // Creates a queue job of the @p type, for the user represented by this
237 // GoogleAppsAccount object, using @p parameters. @p parameters is supposed
238 // to be a one-dimension array of key-value mappings.
239 // The created job as a 'immediate' priority, and is scheduled for immediate
241 private function create_queue_job($type, $parameters) {
242 $parameters["username"] = $this->g_account_name
;
244 "INSERT INTO gapps_queue
245 SET q_owner_id = {?}, q_recipient_id = {?},
246 p_entry_date = NOW(), p_notbefore_date = NOW(),
247 p_priority = 'immediate',
248 j_type = {?}, j_parameters = {?}",
252 json_encode($parameters));
256 // Returns true if the account is currently active.
257 public function active()
259 return $this->g_status
== 'active';
262 // Returns true if the account exists in Google Apps.
263 public function provisioned()
265 return $this->g_status
== 'active' or $this->g_status
== 'disabled';
268 // Returns true if the account exists, but cannot be used (user-requested
269 // suspension, or Google-requested suspension).
270 public function suspended()
272 return $this->g_status
== 'disabled';
276 // Changes the GoogleApps password.
277 public function set_password($password) {
278 if (!$this->provisioned()) {
282 if (!$this->pending_update_password
) {
283 $this->create_queue_job('u_update', array('password' => $password));
284 $this->pending_update_password
= true
;
289 // Changes the password synchronization status ("sync = true" means that the
290 // Polytechnique.org password will be replicated to the Google Apps account).
291 public function set_password_sync($sync) {
292 if (!$this->provisioned()) {
296 $this->sync_password
= $sync;
298 "UPDATE gapps_accounts
299 SET l_sync_password = {?}
300 WHERE g_account_name = {?}",
302 $this->g_account_name
);
305 // Suspends the Google Apps account.
306 public function suspend() {
307 if (!$this->provisioned()) {
311 if (!$this->pending_update_suspension
) {
312 $this->create_queue_job('u_update', array('suspended' => true
));
313 $this->pending_update_suspension
= true
;
315 "UPDATE gapps_accounts
316 SET g_status = 'disabled'
317 WHERE g_account_name = {?} AND g_status = 'active'",
318 $this->g_account_name
);
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->user
->id());
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 = {?}", $this->user
->id());
358 $password = ($res->numRows() > 0 ?
$res->fetchOneCell() : false
);
364 $this->create_queue_job('u_update', array('suspended' => false
, 'password' => $password));
366 $this->create_queue_job('u_update', array('suspended' => false
));
368 $this->pending_update_suspension
= true
;
374 // Creates a new Google Apps account with the @p local parameters.
375 public function create($password_sync, $password, $redirect_mails) {
376 if ($this->g_status
!= NULL
) {
380 if (!$this->pending_create
) {
381 // Retrieves information on the new account.
383 "SELECT nom, nom_usage, prenom
385 WHERE user_id = {?}", $this->user
->id());
386 list($nom, $nom_usage, $prenom) = $res->fetchOneRow();
388 // Adds an 'unprovisioned' entry in the gapps_accounts table.
390 "INSERT INTO gapps_accounts
392 l_sync_password = {?},
393 l_activate_mail_redirection = {?},
394 g_account_name = {?},
397 g_status = 'unprovisioned'",
401 $this->g_account_name
,
403 ($nom_usage ?
$nom_usage : $nom));
405 // Adds the creation job in the GApps queue.
406 $this->create_queue_job(
409 'username' => $this->g_account_name
,
410 'first_name' => $prenom,
411 'last_name' => ($nom_usage ?
$nom_usage : $nom),
412 'password' => $password,
415 // Updates the GoogleAppsAccount status.
416 $this->__construct($this->user
);
421 // Returns the status of the Google Apps account for @p user, or false
422 // when no account exists.
423 static public function account_status($uid) {
427 WHERE l_userid = {?}", $uid);
428 return ($res->numRows() > 0 ?
$res->fetchOneCell() : false
);
431 // Returns true if the @p user is an administrator of the Google Apps domain.
432 static public function is_administrator($uid) {
436 WHERE l_userid = {?} AND g_status = 'active'", $uid);
437 return ($res->numRows() > 0 ?
(bool
)$res->fetchOneCell() : false
);
441 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: