Stub of a 'Profile' class.
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Mon, 22 Dec 2008 15:37:58 +0000 (16:37 +0100)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Mon, 22 Dec 2008 15:37:58 +0000 (16:37 +0100)
Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
classes/profile.php [new file with mode: 0644]
classes/user.php
classes/xorgsession.php

diff --git a/classes/profile.php b/classes/profile.php
new file mode 100644 (file)
index 0000000..217a3f0
--- /dev/null
@@ -0,0 +1,79 @@
+<?php
+/***************************************************************************
+ *  Copyright (C) 2003-2008 Polytechnique.org                              *
+ *  http://opensource.polytechnique.org/                                   *
+ *                                                                         *
+ *  This program is free software; you can redistribute it and/or modify   *
+ *  it under the terms of the GNU General Public License as published by   *
+ *  the Free Software Foundation; either version 2 of the License, or      *
+ *  (at your option) any later version.                                    *
+ *                                                                         *
+ *  This program is distributed in the hope that it will be useful,        *
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
+ *  GNU General Public License for more details.                           *
+ *                                                                         *
+ *  You should have received a copy of the GNU General Public License      *
+ *  along with this program; if not, write to the Free Software            *
+ *  Foundation, Inc.,                                                      *
+ *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
+ ***************************************************************************/
+
+class Profile
+{
+    private $pid;
+    private $hrpid;
+
+    private function __construct($login)
+    {
+        if ($login instanceof PlUser) {
+            $res = XDB::query('SELECT  p.pid, p.hrpid
+                                 FROM  account_profiles AS ap
+                           INNER JOIN  profiles AS p ON (p.pid = ap.pid)
+                                WHERE  ap.uid = {?} AND FIND_IN_SET(\'owner\', ap.perms)',
+                             $login->id());
+        } else if (is_numeric($login)) {
+            $res = XDB::query('SELECT  p.pid, p.hrpid
+                                 FROM  profiles AS p
+                                WHERE  p.pid = {?}',
+                              $login);
+        } else {
+            $res = XDB::query('SELECT  p.pid, p.hrpid
+                                 FROM  profiles AS p
+                                WHERE  p.hrpid = {?}',
+                              $login);
+        }
+        if ($res->numRows() != 1) {
+            throw new UserNotFoundException();
+        }
+        list($this->pid, $this->hrpid) = $res->fetchOneRow();
+    }
+
+    public function id()
+    {
+        return $this->pid;
+    }
+
+    public function hrid()
+    {
+        return $this->hrpid;
+    }
+
+    public function owner()
+    {
+        return User::getSilent($this);
+    }
+
+    /** Return the profile associated with the given login.
+     */
+    public static function get($login) {
+        try {
+            return new Profile($login);
+        } catch (UserNotFoundException $e) {
+            return false;
+        }
+    }
+}
+
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+?>
index e7c7472..f4c2732 100644 (file)
@@ -26,9 +26,22 @@ class User extends PlUser
     {
         global $globals;
 
+        if ($login instanceof Profile) {
+            $res = XDB::query('SELECT  ap.uid
+                                 FROM  account_profiles AS ap
+                                WHERE  ap.pid = {?} AND FIND_IN_SET(\'owner\', perms)',
+                              $login->id());
+            if ($res->numRows()) {
+                return $res->fetchOneCell();
+            }
+            throw new UserNotFoundException();
+        }
+
         // If $data is an integer, fetches directly the result.
         if (is_numeric($login)) {
-            $res = XDB::query("SELECT user_id FROM auth_user_md5 WHERE user_id = {?}", $login);
+            $res = XDB::query('SELECT  a.uid
+                                 FROM  accounts AS a
+                                WHERE  a.uid = {?}', $login);
             if ($res->numRows()) {
                 return $res->fetchOneCell();
             }
@@ -37,7 +50,9 @@ class User extends PlUser
         }
 
         // Checks whether $login is a valid hruid or not.
-        $res = XDB::query("SELECT user_id FROM auth_user_md5 WHERE hruid = {?}", $login);
+        $res = XDB::query('SELECT  a.uid
+                             FROM  accounts AS a
+                            WHERE  a.hruid = {?}', $login);
         if ($res->numRows()) {
             return $res->fetchOneCell();
         }
@@ -52,23 +67,24 @@ class User extends PlUser
         // Checks if $login is a valid alias on the main domains.
         list($mbox, $fqdn) = explode('@', $login);
         if ($fqdn == $globals->mail->domain || $fqdn == $globals->mail->domain2) {
-            $res = XDB::query("SELECT  u.user_id
-                                 FROM  auth_user_md5 AS u
-                           INNER JOIN  aliases AS a ON (a.id = u.user_id AND a.type IN ('alias', 'a_vie'))
-                                WHERE  a.alias = {?}", $mbox);
+            $res = XDB::query('SELECT  a.uid
+                                 FROM  accounts AS a
+                           INNER JOIN  aliases AS al ON (al.id = a.uid AND al.type IN (\'alias\', \'a_vie\'))
+                                WHERE  al.alias = {?}', $mbox);
             if ($res->numRows()) {
                 return $res->fetchOneCell();
             }
 
+            /** TODO: implements this by inspecting the profile.
             if (preg_match('/^(.*)\.([0-9]{4})$/u', $mbox, $matches)) {
-                $res = XDB::query("SELECT  u.user_id
-                                     FROM  auth_user_md5 AS u
-                               INNER JOIN  aliases AS a ON (a.id = u.user_id AND a.type IN ('alias', 'a_vie'))
-                                    WHERE  a.alias = {?} AND u.promo = {?}", $matches[1], $matches[2]);
+                $res = XDB::query('SELECT  a.uid
+                                     FROM  accounts AS a
+                               INNER JOIN  aliases AS al ON (al.id = a.uid AND al.type IN ('alias', 'a_vie'))
+                                    WHERE  al.alias = {?} AND a.promo = {?}', $matches[1], $matches[2]);
                 if ($res->numRows() == 1) {
                     return $res->fetchOneCell();
                 }
-            }
+            }*/
 
             throw new UserNotFoundException();
         }
@@ -82,10 +98,10 @@ class User extends PlUser
             if ($redir = $res->fetchOneCell()) {
                 // We now have a valid alias, which has to be translated to an hruid.
                 list($alias, $alias_fqdn) = explode('@', $redir);
-                $res = XDB::query("SELECT  u.user_id
-                                     FROM  auth_user_md5 AS u
-                                LEFT JOIN  aliases AS a ON (a.id = u.user_id AND a.type IN ('alias', 'a_vie'))
-                                    WHERE  a.alias = {?}", $alias);
+                $res = XDB::query("SELECT  a.uid
+                                     FROM  accounts AS a
+                                LEFT JOIN  aliases AS al ON (al.id = a.uid AND al.type IN ('alias', 'a_vie'))
+                                    WHERE  al.alias = {?}", $alias);
                 if ($res->numRows()) {
                     return $res->fetchOneCell();
                 }
@@ -95,9 +111,9 @@ class User extends PlUser
         }
 
         // Otherwise, we do suppose $login is an email redirection.
-        $res = XDB::query("SELECT  u.user_id
-                             FROM  auth_user_md5 AS u
-                        LEFT JOIN  emails AS e ON (e.uid = u.user_id)
+        $res = XDB::query("SELECT  a.uid
+                             FROM  accounts AS a
+                        LEFT JOIN  emails AS e ON (e.uid = a.uid)
                             WHERE  e.email = {?}", $login);
         if ($res->numRows() == 1) {
             return $res->fetchOneCell();
@@ -117,20 +133,18 @@ class User extends PlUser
         }
 
         global $globals;
-        $res = XDB::query("SELECT  u.hruid, d.promo_display AS promo,
+        /** TODO: promo stuff again */
+        /** TODO: fix perms field to fit new perms system */
+        $res = XDB::query("SELECT  a.hruid, d.promo_display AS promo,
                                    CONCAT(af.alias, '@{$globals->mail->domain}') AS forlife,
                                    CONCAT(ab.alias, '@{$globals->mail->domain}') AS bestalias,
-                                   CONCAT(u.prenom, ' ', IF(u.nom_usage <> '', u.nom_usage, u.nom)) AS full_name,
-                                   IF(u.prenom != '', u.prenom, u.nom) AS display_name,
-                                   FIND_IN_SET('femme', u.flags) AS gender,
-                                   q.core_mail_fmt AS email_format,
-                                   u.perms
-                             FROM  auth_user_md5 AS u
-                       INNER JOIN  profile_display AS d ON (d.uid = u.user_id)
-                        LEFT JOIN  auth_user_quick AS q ON (q.user_id = u.user_id)
-                        LEFT JOIN  aliases AS af ON (af.id = u.user_id AND af.type = 'a_vie')
-                        LEFT JOIN  aliases AS ab ON (ab.id = u.user_id AND FIND_IN_SET('bestalias', ab.flags))
-                            WHERE  u.user_id = {?}", $this->user_id);
+                                   a.full_name, a.display_name, a.sex = 'female' AS gender,
+                                   a.email_format, a.state AS perms
+                             FROM  accounts AS a
+                       INNER JOIN  profile_display AS d ON (d.uid = a.uid)
+                        LEFT JOIN  aliases AS af ON (af.id = a.uid AND af.type = 'a_vie')
+                        LEFT JOIN  aliases AS ab ON (ab.id = a.uid AND FIND_IN_SET('bestalias', ab.flags))
+                            WHERE  a.uid = {?}", $this->user_id);
         $this->fillFromArray($res->fetchOneAssoc());
     }
 
@@ -166,9 +180,6 @@ class User extends PlUser
         if (isset($values['mail_fmt'])) {
             $values['email_format'] = $values['mail_fmt'];
         }
-        if (isset($values['email_format'])) {
-            $values['email_format'] = ($values['email_format'] ? self::FORMAT_HTML : self::FORMAT_TEXT);
-        }
 
         parent::fillFromArray($values);
     }
@@ -187,6 +198,13 @@ class User extends PlUser
         $this->perm_flags = self::makePerms($this->perms);
     }
 
+    /** Return the main profile attached with this account if any.
+     */
+    public function profile()
+    {
+        return Profile::get($this);
+    }
+
     // Return permission flags for a given permission level.
     public static function makePerms($perms)
     {
index 406ab2a..ed3382d 100644 (file)
@@ -187,7 +187,7 @@ class XorgSession extends PlSession
         /** TODO: Data to move are: banana_last, watch_last, last_version */
         /** TODO: Switch to new permission system */
         $res  = XDB::query("SELECT  a.uid, a.hruid, a.display_name, a.full_name, a.password,
-                                    a.sex = 'female' AS femme, a.mail_format as mail_fmt,
+                                    a.sex = 'female' AS femme, a.email_format as mail_fmt,
                                     a.token, FIND_IN_SET('watch', a.flags) AS watch_account,
                                     UNIX_TIMESTAMP(q.banana_last) AS banana_last, q.watch_last,
                                     q.last_version, g.g_account_name IS NOT NULL AS googleapps,