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 // Checks the admin status of the @p account_name.
23 function is_google_apps_administrator($account_name) {
24 static $last_account_name = null
;
25 static $last_result = null
;
27 if ($last_account_name == $account_name) {
34 WHERE g_account_name = {?} AND g_status = 'active'",
36 $last_account_name = $account_name;
37 $last_result = ($res->numRows() > 0 ?
(bool
)$res->fetchOneRow() : false
);
41 // Post-queue job cleanup functions; they are used to update the plat/al database
42 // when a specific Google Apps queue job enters 'success' state.
43 function post_queue_u_create($job) {
46 // Retrieves the user parameters (userid and forlife).
47 $parameters = json_decode($job['j_parameters'], true
);
48 $forlife = isset($parameters['username']) ?
$parameters['username'] : null
;
49 $userid = $job['q_recipient_id'];
50 if (!$forlife ||
!$userid) {
54 // Adds a redirection to the Google Apps delivery address.
55 $account = new GoogleAppsAccount($userid, $forlife);
56 if ($account->activate_mail_redirection
) {
57 require_once('emails.inc.php');
58 $storage = new MailStorageGoogleApps($userid);
62 // Sends an email to the account owner.
64 "SELECT FIND_IN_SET('femme', u.flags), prenom
65 FROM auth_user_md5 AS u
66 INNER JOIN aliases AS a ON (a.id = u.user_id)
69 list($sexe, $prenom) = $res->fetchOneRow();
71 $mailer = new PlMailer('googleapps/create.mail.tpl');
72 $mailer->assign('account', $account);
73 $mailer->assign('email', $forlife . '@' . $globals->mail
->domain
);
74 $mailer->assign('googleapps_domain', $globals->mailstorage
->googleapps_domain
);
75 $mailer->assign('prenom', $prenom);
76 $mailer->assign('sexe', $sexe);
80 function post_queue_u_update($job) {
83 // If the u_update job was an unsuspend request, re-adds the redirection
84 // to the Google Apps delivery address, provided the account is active (it might
85 // have been deleted between the unsuspension and the post-queue processing).
86 $parameters = json_decode($job['j_parameters'], true
);
87 $forlife = isset($parameters['username']) ?
$parameters['username'] : null
;
88 $userid = $job['q_recipient_id'];
89 if (!$forlife ||
!$userid) {
93 if (isset($parameters['suspended']) && $parameters['suspended'] == false
) {
94 require_once('emails.inc.php');
95 $account = new GoogleAppsAccount($userid, $forlife);
96 if ($account->g_status
== 'active') {
97 // Re-adds the email redirection (if the user did request it).
98 if ($account->activate_mail_redirection
) {
99 $storage = new MailStorageGoogleApps($userid);
103 // Sends an email to the account owner.
105 "SELECT FIND_IN_SET('femme', u.flags), prenom
106 FROM auth_user_md5 AS u
107 INNER JOIN aliases AS a ON (a.id = u.user_id)
108 WHERE a.alias = {?}",
110 list($sexe, $prenom) = $res->fetchOneRow();
112 $mailer = new PlMailer('googleapps/unsuspend.mail.tpl');
113 $mailer->assign('account', $account);
114 $mailer->assign('email', $forlife . '@' . $globals->mail
->domain
);
115 $mailer->assign('prenom', $prenom);
116 $mailer->assign('sexe', $sexe);
122 // Reprensentation of an SQL-stored Google Apps account.
123 class GoogleAppsAccount
126 public $g_account_name;
128 public $sync_password;
129 public $activate_mail_redirection;
131 public $g_suspension;
132 public $r_disk_usage;
134 public $r_last_login;
135 public $r_last_webmail;
136 public $reporting_date;
138 public $pending_create;
139 public $pending_delete;
140 public $pending_update;
141 public $pending_update_admin;
142 public $pending_update_other;
143 public $pending_update_password;
144 public $pending_update_suspension;
146 public $pending_validation_unsuspend;
148 public function __construct($uid, $account_name)
151 $this->g_account_name
= $account_name;
152 $this->g_status
= NULL
;
155 "SELECT l_sync_password, l_activate_mail_redirection,
156 g_account_name, g_status, g_suspension, r_disk_usage,
157 UNIX_TIMESTAMP(r_creation) as r_creation,
158 UNIX_TIMESTAMP(r_last_login) as r_last_login,
159 UNIX_TIMESTAMP(r_last_webmail) as r_last_webmail
161 WHERE g_account_name = {?}",
163 if ($account = $res->fetchOneAssoc()) {
164 $this->sync_password
= $account['l_sync_password'];
165 $this->activate_mail_redirection
= $account['l_activate_mail_redirection'];
166 $this->g_status
= $account['g_status'];
167 $this->g_suspension
= $account['g_suspension'];
168 $this->r_disk_usage
= $account['r_disk_usage'];
169 $this->r_creation
= $account['r_creation'];
170 $this->r_last_login
= $account['r_last_webmail'];
171 $this->r_last_webmail
= $account['r_last_webmail'];
173 $this->load_pending_counts();
174 $this->load_pending_validations();
175 if ($this->pending_update
) {
176 $this->load_pending_updates();
179 $res = XDB
::query("SELECT MAX(date) FROM gapps_reporting");
180 $this->reporting_date
= $res->fetchOneCell();
184 // Account object initialization methods.
185 private function load_pending_counts()
187 // Determines if changes to the Google Account are currently waiting
188 // in the Google Apps queue.
190 "SELECT SUM(j_type = 'u_create') AS pending_create,
191 SUM(j_type = 'u_update') AS pending_update,
192 SUM(j_type = 'u_delete') AS pending_delete
194 WHERE q_recipient_id = {?} AND
195 p_status IN ('idle', 'active', 'softfail')
198 $pending = $res->fetchOneAssoc();
199 $this->pending_create
= $pending['pending_create'];
200 $this->pending_update
= $pending['pending_update'];
201 $this->pending_delete
= $pending['pending_delete'];
203 $this->pending_update_admin
= false
;
204 $this->pending_update_other
= false
;
205 $this->pending_update_password
= false
;
206 $this->pending_update_suspension
= false
;
209 private function load_pending_validations()
211 require_once('validations.inc.php');
212 $this->pending_validation_unsuspend
=
213 Validate
::get_typed_requests_count($this->uid
, 'gapps-unsuspend');
216 private function load_pending_updates()
218 // If updates are pending, determines their nature (more specifically:
219 // determines which part of the account is concerned).
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.
244 private function create_queue_job($type, $parameters) {
245 $parameters["username"] = $this->g_account_name
;
247 "INSERT INTO gapps_queue
248 SET q_owner_id = {?}, q_recipient_id = {?},
249 p_entry_date = NOW(), p_notbefore_date = NOW(),
250 p_priority = 'normal',
251 j_type = {?}, j_parameters = {?}",
255 json_encode($parameters));
258 // Changes the GoogleApps password.
259 public function set_password($password) {
260 if ($this->g_status
== NULL ||
$this->g_status
== 'unprovisioned') {
264 if (!$this->pending_update_password
) {
265 $this->create_queue_job('u_update', array('password' => $password));
269 // Changes the password synchronization status ("sync = true" means that the
270 // Polytechnique.org password will be replicated to the Google Apps account).
271 public function set_password_sync($sync) {
272 if ($this->g_status
== NULL ||
$this->g_status
== 'unprovisioned') {
276 $this->sync_password
= $sync;
278 "UPDATE gapps_accounts
279 SET l_sync_password = {?}
280 WHERE g_account_name = {?}",
282 $this->g_account_name
);
285 // Suspends the Google Apps account.
286 public function suspend() {
287 if ($this->g_status
== NULL ||
$this->g_status
== 'unprovisioned') {
291 if (!$this->pending_update_suspension
) {
292 $this->create_queue_job('u_update', array('suspended' => true
));
293 $this->pending_update_suspension
= true
;
297 // Adds an unsuspension request to the validation queue (used on user-request).
298 public function unsuspend($activate_mail_redirection = NULL
) {
299 if ($this->g_status
== NULL ||
$this->g_status
== 'unprovisioned') {
302 if ($activate_mail_redirection !== NULL
) {
303 $this->activate_mail_redirection
= $activate_mail_redirection;
305 "UPDATE gapps_accounts
306 SET l_activate_mail_redirection = {?}
307 WHERE g_account_name = {?}",
308 $activate_mail_redirection);
311 if (!$this->pending_update_suspension
&& !$this->pending_validation_unsuspend
) {
312 require_once('validations.inc.php');
313 $unsuspend = new GoogleAppsUnsuspendReq($this->uid
);
314 $unsuspend->submit();
315 $this->pending_validation_unsuspend
= true
;
319 // Unsuspends the Google Apps account (used on admin-request, or on validation of
321 public function do_unsuspend() {
322 if ($this->g_status
== NULL ||
$this->g_status
== 'unprovisioned') {
326 if (!$this->pending_update_suspension
) {
327 if ($this->sync_password
) {
331 WHERE user_id = {?}",
333 $password = ($res->numRows() > 0 ?
$res->fetchOneCell() : false
);
339 $this->create_queue_job('u_update', array('suspended' => false
, 'password' => $password));
341 $this->create_queue_job('u_update', array('suspended' => false
));
343 $this->pending_update_suspension
= true
;
349 // Adds a creation request in the job queue.
350 public function create($password_sync, $password, $redirect_mails) {
351 if ($this->g_status
!= NULL
) {
355 if (!$this->pending_create
) {
356 // Retrieves information on the new account.
358 "SELECT nom, nom_usage, prenom
360 WHERE user_id = {?}",
362 list($nom, $nom_usage, $prenom) = $res->fetchOneRow();
364 // Adds an entry in the gapps_accounts table.
366 "INSERT INTO gapps_accounts
368 l_sync_password = {?},
369 l_activate_mail_redirection = {?},
370 g_account_name = {?},
373 g_status = 'unprovisioned'",
377 $this->g_account_name
,
379 ($nom_usage ?
$nom_usage : $nom));
381 // Adds the creation job in the GApps queue.
382 $this->create_queue_job(
385 'username' => $this->g_account_name
,
386 'first_name' => $prenom,
387 'last_name' => ($nom_usage ?
$nom_usage : $nom),
388 'password' => $password,
391 // Updates the GoogleAppsAccount status.
392 $this->__construct($this->uid
, $this->g_account_name
);
397 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: