From d56cb887a972f39263f93a671479672733db63bb Mon Sep 17 00:00:00 2001 From: Vincent Zanotti Date: Mon, 16 Jun 2008 00:39:00 +0200 Subject: [PATCH] Converts GoogleApps to the new hruid/forlife scheme. Signed-off-by: Vincent Zanotti --- include/googleapps.inc.php | 75 ++++++++++++++-------------------- include/user.func.inc.php | 8 ++-- include/validations/googleapps.inc.php | 2 +- include/validations/nomusage.inc.php | 2 +- modules/admin.php | 4 +- modules/googleapps.php | 17 ++++---- modules/platal.php | 4 +- modules/register.php | 4 +- 8 files changed, 50 insertions(+), 66 deletions(-) diff --git a/include/googleapps.inc.php b/include/googleapps.inc.php index 28d9459..45337d4 100644 --- a/include/googleapps.inc.php +++ b/include/googleapps.inc.php @@ -23,20 +23,19 @@ function post_queue_u_create($job) { global $globals; - // Retrieves the user parameters (userid and forlife). + // Retrieves the user parameters (GoogleApps username and user_id). $parameters = json_decode($job['j_parameters'], true); - $forlife = isset($parameters['username']) ? $parameters['username'] : null; - $userid = $job['q_recipient_id']; - if (!$forlife || !$userid) { + $username = isset($parameters['username']) ? $parameters['username'] : null; + if (!($user = User::getSilent($username))) { return; } // Adds a redirection to the Google Apps delivery address, if requested by // the user at creation time. - $account = new GoogleAppsAccount($userid, $forlife); + $account = new GoogleAppsAccount($user); if ($account->activate_mail_redirection) { require_once('emails.inc.php'); - $storage = new EmailStorage($userid, 'googleapps'); + $storage = new EmailStorage($user->id(), 'googleapps'); $storage->activate(); } @@ -44,14 +43,12 @@ function post_queue_u_create($job) { $res = XDB::query( "SELECT FIND_IN_SET('femme', u.flags), prenom FROM auth_user_md5 AS u - INNER JOIN aliases AS a ON (a.id = u.user_id) - WHERE a.alias = {?}", - $forlife); + WHERE u.user_id = {?}", $user->id()); list($sexe, $prenom) = $res->fetchOneRow(); $mailer = new PlMailer('googleapps/create.mail.tpl'); $mailer->assign('account', $account); - $mailer->assign('email', $forlife . '@' . $globals->mail->domain); + $mailer->assign('email', $user->bestEmail()); $mailer->assign('googleapps_domain', $globals->mailstorage->googleapps_domain); $mailer->assign('prenom', $prenom); $mailer->assign('sexe', $sexe); @@ -66,19 +63,18 @@ function post_queue_u_update($job) { // to the Google Apps delivery address, provided the account is active (it might // have been deleted between the unsuspension and the post-queue processing). $parameters = json_decode($job['j_parameters'], true); - $forlife = isset($parameters['username']) ? $parameters['username'] : null; - $userid = $job['q_recipient_id']; - if (!$forlife || !$userid) { + $username = isset($parameters['username']) ? $parameters['username'] : null; + if (!($user = User::getSilent($username))) { return; } if (isset($parameters['suspended']) && $parameters['suspended'] == false) { require_once('emails.inc.php'); - $account = new GoogleAppsAccount($userid, $forlife); + $account = new GoogleAppsAccount($user); if ($account->active()) { // Re-adds the email redirection (if the user did request it). if ($account->activate_mail_redirection) { - $storage = new EmailStorage($userid, 'googleapps'); + $storage = new EmailStorage($user->id, 'googleapps'); $storage->activate(); } @@ -86,14 +82,12 @@ function post_queue_u_update($job) { $res = XDB::query( "SELECT FIND_IN_SET('femme', u.flags), prenom FROM auth_user_md5 AS u - INNER JOIN aliases AS a ON (a.id = u.user_id) - WHERE a.alias = {?}", - $forlife); + WHERE u.user_id = {?}", $user->id()); list($sexe, $prenom) = $res->fetchOneRow(); $mailer = new PlMailer('googleapps/unsuspend.mail.tpl'); $mailer->assign('account', $account); - $mailer->assign('email', $forlife . '@' . $globals->mail->domain); + $mailer->assign('email', $user->bestEmail()); $mailer->assign('prenom', $prenom); $mailer->assign('sexe', $sexe); $mailer->send(); @@ -108,7 +102,7 @@ function post_queue_u_update($job) { class GoogleAppsAccount { // User identification: user id, and forlife. - private $uid; + private $user; public $g_account_name; // Local account parameters. @@ -139,19 +133,17 @@ class GoogleAppsAccount // Constructs the account object, by retrieving all informations from the // GApps account table, from GApps job queue, and from plat/al validation queue. - public function __construct($uid, $account_name = NULL) + public function __construct(User &$user) { - if ($account_name == NULL) { - require_once 'user.func.inc.php'; - $account_name = get_user_forlife($uid, '_silent_user_callback'); + $this->user = &$user; + if (!$this->user || !$this->user->login()) { + return; } - $this->uid = $uid; - $this->g_account_name = $account_name; + // TODO: switch to multi-domain Google Apps, and use $this->user->forlifeEmail() + // as Google Apps idenfiant (requires changes in gappsd). + $this->g_account_name = $this->user->login(); $this->g_status = NULL; - if (!$this->g_account_name) { - return; - } $res = XDB::query( "SELECT l_sync_password, l_activate_mail_redirection, @@ -160,8 +152,7 @@ class GoogleAppsAccount UNIX_TIMESTAMP(r_last_login) as r_last_login, UNIX_TIMESTAMP(r_last_webmail) as r_last_webmail FROM gapps_accounts - WHERE g_account_name = {?}", - $account_name); + WHERE g_account_name = {?}", $this->g_account_name); if ($account = $res->fetchOneAssoc()) { $this->sync_password = $account['l_sync_password']; $this->activate_mail_redirection = $account['l_activate_mail_redirection']; @@ -195,8 +186,7 @@ class GoogleAppsAccount FROM gapps_queue WHERE q_recipient_id = {?} AND p_status IN ('idle', 'active', 'softfail') - GROUP BY j_type", - $this->uid); + GROUP BY j_type", $this->user->id()); $pending = $res->fetchOneAssoc(); $this->pending_create = $pending['pending_create']; $this->pending_update = $pending['pending_update']; @@ -214,7 +204,7 @@ class GoogleAppsAccount { require_once('validations.inc.php'); $this->pending_validation_unsuspend = - Validate::get_typed_requests_count($this->uid, 'gapps-unsuspend'); + Validate::get_typed_requests_count($this->user->id(), 'gapps-unsuspend'); } // Retrieves all the pending update job in the gappsd queue for the current @@ -227,8 +217,7 @@ class GoogleAppsAccount FROM gapps_queue WHERE q_recipient_id = {?} AND p_status IN ('idle', 'active', 'softfail') AND - j_type = 'u_update'", - $this->uid); + j_type = 'u_update'", $this->user->id()); while ($update = $res->next()) { $update_data = json_decode($update["j_parameters"], true); @@ -258,7 +247,7 @@ class GoogleAppsAccount p_priority = 'immediate', j_type = {?}, j_parameters = {?}", S::v('uid'), - $this->uid, + $this->user->id(), $type, json_encode($parameters)); } @@ -347,7 +336,7 @@ class GoogleAppsAccount if (!$this->pending_update_suspension && !$this->pending_validation_unsuspend) { require_once('validations.inc.php'); - $unsuspend = new GoogleAppsUnsuspendReq($this->uid); + $unsuspend = new GoogleAppsUnsuspendReq($this->user->id()); $unsuspend->submit(); $this->pending_validation_unsuspend = true; } @@ -365,8 +354,7 @@ class GoogleAppsAccount $res = XDB::query( "SELECT password FROM auth_user_md5 - WHERE user_id = {?}", - $this->uid); + WHERE user_id = {?}", $this->user->id()); $password = ($res->numRows() > 0 ? $res->fetchOneCell() : false); } else { $password = false; @@ -394,8 +382,7 @@ class GoogleAppsAccount $res = XDB::query( "SELECT nom, nom_usage, prenom FROM auth_user_md5 - WHERE user_id = {?}", - $this->uid); + WHERE user_id = {?}", $this->user->id()); list($nom, $nom_usage, $prenom) = $res->fetchOneRow(); // Adds an 'unprovisioned' entry in the gapps_accounts table. @@ -408,7 +395,7 @@ class GoogleAppsAccount g_first_name = {?}, g_last_name = {?}, g_status = 'unprovisioned'", - $this->uid, + $this->user->id(), $password_sync, $redirect_mails, $this->g_account_name, @@ -426,7 +413,7 @@ class GoogleAppsAccount )); // Updates the GoogleAppsAccount status. - $this->__construct($this->uid, $this->g_account_name); + $this->__construct($this->user); } } diff --git a/include/user.func.inc.php b/include/user.func.inc.php index 09ffcc1..438630c 100644 --- a/include/user.func.inc.php +++ b/include/user.func.inc.php @@ -32,9 +32,9 @@ function user_clear_all_subs($user_id, $really_del=true) // + delete maillists global $globals; - $uid = intval($user_id); - $res = XDB::query("SELECT alias FROM aliases WHERE type='a_vie' AND id={?}", $uid); - $alias = $res->fetchOneCell(); + $uid = intval($user_id); + $user = User::getSilent($uid); + list($alias) = explode('@', $user->forlifeEmail()); $tables_to_clear = array('uid' => array('competences_ins', 'entreprises', 'langues_ins', 'mentor_pays', 'mentor_secteurs', 'mentor', 'perte_pass', 'watch_sub'), @@ -75,7 +75,7 @@ function user_clear_all_subs($user_id, $really_del=true) if ($globals->mailstorage->googleapps_domain) { require_once 'googleapps.inc.php'; if (GoogleAppsAccount::account_status($uid)) { - $account = new GoogleAppsAccount($uid, $alias); + $account = new GoogleAppsAccount($user); $account->suspend(); } } diff --git a/include/validations/googleapps.inc.php b/include/validations/googleapps.inc.php index 7413d73..52a008f 100644 --- a/include/validations/googleapps.inc.php +++ b/include/validations/googleapps.inc.php @@ -61,7 +61,7 @@ class GoogleAppsUnsuspendReq extends Validate public function commit() { require_once dirname(__FILE__) . '/../googleapps.inc.php'; - $account = new GoogleAppsAccount($this->uid, $this->forlife); + $account = new GoogleAppsAccount(User::get($this->forlife)); return $account->do_unsuspend(); } diff --git a/include/validations/nomusage.inc.php b/include/validations/nomusage.inc.php index 952c0c1..ce05b47 100644 --- a/include/validations/nomusage.inc.php +++ b/include/validations/nomusage.inc.php @@ -94,7 +94,7 @@ class UsageReq extends Validate } if ($globals->mailstorage->googleapps_domain) { require_once 'googleapps.inc.php'; - $account = new GoogleAppsAccount($this->uid, $this->forlife); + $account = new GoogleAppsAccount(User::get($this->uid)); if ($account->active()) { $res .= "\n\n Si tu utilises Google Apps, tu peux changer ton nom d'usage sur https://mail.google.com/a/polytechnique.org/#settings/accounts"; } diff --git a/modules/admin.php b/modules/admin.php index 50a0c94..18d301a 100644 --- a/modules/admin.php +++ b/modules/admin.php @@ -597,7 +597,7 @@ class AdminModule extends PLModule // and the password was changed, updates the Google Apps password as well. if ($globals->mailstorage->googleapps_domain && Env::v('newpass_clair') != "********") { require_once 'googleapps.inc.php'; - $account = new GoogleAppsAccount($mr['user_id'], $mr['forlife']); + $account = new GoogleAppsAccount(User::get($mr['forlife'])); if ($account->active() && $account->sync_password) { $account->set_password($pass_encrypted); } @@ -608,7 +608,7 @@ class AdminModule extends PLModule $new_fields['perms'] == 'disabled' && $new_fields['perms'] != $old_fields['perms']) { require_once 'googleapps.inc.php'; - $account = new GoogleAppsAccount($mr['user_id'], $mr['forlife']); + $account = new GoogleAppsAccount(User::get($mr['forlife'])); $account->suspend(); } break; diff --git a/modules/googleapps.php b/modules/googleapps.php index fcfb153..61fd0d6 100644 --- a/modules/googleapps.php +++ b/modules/googleapps.php @@ -44,7 +44,7 @@ class GoogleAppsModule extends PLModule $page->addJsLink('motdepasse.js'); $page->assign('xorg_title', 'Polytechnique.org - Compte Google Apps'); - $account = new GoogleAppsAccount(S::v('uid'), S::v('forlife')); + $account = new GoogleAppsAccount(S::user()); // Fills up the 'is Google Apps redirection active' variable. $page->assign('redirect_active', false); @@ -189,34 +189,31 @@ class GoogleAppsModule extends PLModule if (!$user && Post::has('login')) { $user = Post::v('login'); } - if ($user && !is_numeric($user)) { - $res = XDB::query("SELECT id FROM aliases WHERE alias = {?} AND type != 'homonyme'", $user); - $user = $res->fetchOneCell(); - } + $user = User::get($user); if ($user) { $account = new GoogleAppsAccount($user); - $storage = new EmailStorage($user, 'googleapps'); + $storage = new EmailStorage($user->id(), 'googleapps'); // Force synchronization of plat/al and Google Apps passwords. if ($action == 'forcesync' && $account->sync_password) { - $res = XDB::query("SELECT password FROM auth_user_md5 WHERE user_id = {?}", $user); + $res = XDB::query("SELECT password FROM auth_user_md5 WHERE user_id = {?}", $user->id()); $account->set_password($res->fetchOneCell()); $page->trigSuccess('Le mot de passe a été synchronisé.'); } // Displays basic account information. $page->assign('account', $account); - $page->assign('admin_account', GoogleAppsAccount::is_administrator($user)); + $page->assign('admin_account', GoogleAppsAccount::is_administrator($user->id())); $page->assign('googleapps_storage', $storage->active); - $page->assign('user', $user); + $page->assign('user', $user->id()); // Retrieves user's pending requests. $res = XDB::iterator( "SELECT q_id, q_recipient_id, p_status, j_type, UNIX_TIMESTAMP(p_entry_date) AS p_entry_date FROM gapps_queue WHERE q_recipient_id = {?} - ORDER BY p_entry_date DESC", $user); + ORDER BY p_entry_date DESC", $user->id()); $page->assign('requests', $res); } } diff --git a/modules/platal.php b/modules/platal.php index 2e6570c..077a504 100644 --- a/modules/platal.php +++ b/modules/platal.php @@ -202,7 +202,7 @@ class PlatalModule extends PLModule // updates the Google Apps password as well. if ($globals->mailstorage->googleapps_domain) { require_once 'googleapps.inc.php'; - $account = new GoogleAppsAccount(S::v('uid'), S::v('forlife')); + $account = new GoogleAppsAccount(S::user()); if ($account->active() && $account->sync_password) { $account->set_password($password); } @@ -367,7 +367,7 @@ Adresse de secours : " . Post::v('email') : "")); // updates the Google Apps password as well. if ($globals->mailstorage->googleapps_domain) { require_once 'googleapps.inc.php'; - $account = new GoogleAppsAccount($uid); + $account = new GoogleAppsAccount(User::getSilent($uid)); if ($account->active() && $account->sync_password) { $account->set_password($password); } diff --git a/modules/register.php b/modules/register.php index 43dc9e9..ae1144a 100644 --- a/modules/register.php +++ b/modules/register.php @@ -405,11 +405,11 @@ class RegisterModule extends PLModule S::v('uid')); // If GoogleApps is enabled, and the user did choose to use synchronized passwords, - // and if the (stupid) user has decided to user /register/success another time, + // and if the (stupid) user has decided to use /register/success another time, // updates the Google Apps password as well. if ($globals->mailstorage->googleapps_domain) { require_once 'googleapps.inc.php'; - $account = new GoogleAppsAccount(S::v('uid'), S::v('forlife')); + $account = new GoogleAppsAccount(S::user()); if ($account->active() && $account->sync_password) { $account->set_password($password); } -- 2.1.4