Moving to GitHub.
[platal.git] / include / googleapps.inc.php
index 7318ba3..a62f636 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2008 Polytechnique.org                              *
+ *  Copyright (C) 2003-2014 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -23,7 +23,7 @@
 function post_queue_u_create($job) {
     global $globals;
 
-    // Retrieves the user parameters (GoogleApps username and user_id).
+    // Retrieves the user parameters (GoogleApps username and uid).
     $parameters = json_decode($job['j_parameters'], true);
     $username = isset($parameters['username']) ? $parameters['username'] : null;
     if (!($user = User::getSilent($username))) {
@@ -34,9 +34,8 @@ function post_queue_u_create($job) {
     // the user at creation time.
     $account = new GoogleAppsAccount($user);
     if ($account->activate_mail_redirection) {
-        require_once('emails.inc.php');
-        $storage = new EmailStorage($user, 'googleapps');
-        $storage->activate();
+        require_once 'emails.inc.php';
+        Email::activate_storage($user, 'googleapps');
     }
 
     // Sends the 'account created' email to the user, with basic documentation.
@@ -63,13 +62,12 @@ function post_queue_u_update($job) {
     }
 
     if (isset($parameters['suspended']) && $parameters['suspended'] == false) {
-        require_once('emails.inc.php');
+        require_once 'emails.inc.php';
         $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($user, 'googleapps');
-                $storage->activate();
+                Email::activate_storage($user, 'googleapps');
             }
 
             // Sends an email to the account owner.
@@ -107,6 +105,9 @@ class GoogleAppsAccount
     public $r_last_webmail;
     public $reporting_date;
 
+    // Nicknames (aliases) registered for that user, lazily loaded.
+    public $nicknames;
+
     // Pending requests in the gappsd job queue (cf. top note).
     public $pending_create;
     public $pending_delete;
@@ -121,7 +122,7 @@ 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(User &$user)
+    public function __construct(User $user)
     {
         $this->user = &$user;
         if (!$this->user || !$this->user->login()) {
@@ -190,7 +191,6 @@ class GoogleAppsAccount
     // validation queue.
     private function load_pending_validations()
     {
-        require_once('validations.inc.php');
         $this->pending_validation_unsuspend =
             Validate::get_typed_requests_count($this->user->id(), 'gapps-unsuspend');
     }
@@ -260,6 +260,21 @@ class GoogleAppsAccount
         return $this->g_status == 'disabled';
     }
 
+    // Loads and returns the list of nicknames for the user.
+    public function nicknames()
+    {
+        if ($this->nicknames == null) {
+            $res = XDB::query(
+                "SELECT  g_nickname
+                   FROM  gapps_nicknames
+                  WHERE  g_account_name = {?}
+               ORDER BY  g_nickname",
+                $this->g_account_name);
+            $this->nicknames = $res->fetchColumn();
+        }
+        return $this->nicknames;
+    }
+
 
     // Changes the GoogleApps password.
     public function set_password($password) {
@@ -323,7 +338,6 @@ class GoogleAppsAccount
         }
 
         if (!$this->pending_update_suspension && !$this->pending_validation_unsuspend) {
-            require_once('validations.inc.php');
             $unsuspend = new GoogleAppsUnsuspendReq($this->user);
             $unsuspend->submit();
             $this->pending_validation_unsuspend = true;
@@ -339,17 +353,7 @@ class GoogleAppsAccount
 
         if (!$this->pending_update_suspension) {
             if ($this->sync_password) {
-                $res = XDB::query(
-                    "SELECT  password
-                       FROM  accounts
-                      WHERE  uid = {?}", $this->user->id());
-                $password = ($res->numRows() > 0 ? $res->fetchOneCell() : false);
-            } else {
-                $password = false;
-            }
-
-            if ($password) {
-                $this->create_queue_job('u_update', array('suspended' => false, 'password' => $password));
+                $this->create_queue_job('u_update', array('suspended' => false, 'password' => $this->user->password()));
             } else {
                 $this->create_queue_job('u_update', array('suspended' => false));
             }
@@ -360,19 +364,21 @@ class GoogleAppsAccount
     }
 
     // Creates a new Google Apps account with the @p local parameters.
-    public function create($password_sync, $password, $redirect_mails) {
+    public function create($password_sync, $password, $redirect_mails)
+    {
         if ($this->g_status != NULL) {
             return;
         }
 
         if (!$this->pending_create) {
             // Retrieves information on the new account.
-            // TODO: retreive first_name and last_name from the profile.
-            $res = XDB::query(
-                "SELECT  nom, nom_usage, prenom
-                   FROM  auth_user_md5
-                  WHERE  user_id = {?}", $this->user->id());
-            list($nom, $nom_usage, $prenom) = $res->fetchOneRow();
+            if (!$this->user->hasProfile()) {
+                $prenom = $this->user->displayName();
+                $nom    = $this->user->fullName();
+            } else {
+                $prenom = $this->user->profile()->firstName();
+                $nom    = $this->user->profile()->lastName();
+            }
 
             // Adds an 'unprovisioned' entry in the gapps_accounts table.
             XDB::execute(
@@ -388,8 +394,7 @@ class GoogleAppsAccount
                 $password_sync,
                 $redirect_mails,
                 $this->g_account_name,
-                $prenom,
-                ($nom_usage ? $nom_usage : $nom));
+                $prenom, $nom);
 
             // Adds the creation job in the GApps queue.
             $this->create_queue_job(
@@ -397,7 +402,7 @@ class GoogleAppsAccount
                 array(
                     'username' => $this->g_account_name,
                     'first_name' => $prenom,
-                    'last_name' => ($nom_usage ? $nom_usage : $nom),
+                    'last_name' => $nom,
                     'password' => $password,
                 ));
 
@@ -427,5 +432,5 @@ class GoogleAppsAccount
     }
 }
 
-// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
 ?>