Add name formatting to the Profile class.
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Tue, 30 Dec 2008 23:30:22 +0000 (00:30 +0100)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Tue, 30 Dec 2008 23:30:22 +0000 (00:30 +0100)
Port module auth (auth-groupex should work... not tested).

Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
classes/profile.php
modules/auth.php
modules/auth/auth.inc.php
upgrade/account/01_profiles.sql
upgrade/account/99_insertion.sql

index 130cd35..fc819f6 100644 (file)
@@ -23,36 +23,31 @@ class Profile
 {
     private $pid;
     private $hrpid;
-    private $promo;
-
     private $data = array();
 
     private function __construct($login)
     {
         if ($login instanceof PlUser) {
-            $res = XDB::query('SELECT  p.pid, p.hrpid, pd.promo_display
+            $res = XDB::query('SELECT  p.pid, p.hrpid
                                  FROM  account_profiles AS ap
                            INNER JOIN  profiles AS p ON (p.pid = ap.pid)
-                           INNER JOIN  profile_display AS pd ON (pd.uid = p.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, pd.promo_display
+            $res = XDB::query('SELECT  p.pid, p.hrpid
                                  FROM  profiles AS p
-                           INNER JOIN  profile_display AS pd ON (pd.uid = p.pid)
                                 WHERE  p.pid = {?}',
                               $login);
         } else {
-            $res = XDB::query('SELECT  p.pid, p.hrpid, pd.promo_display
+            $res = XDB::query('SELECT  p.pid, p.hrpid
                                  FROM  profiles AS p
-                           INNER JOIN  profile_display AS pd ON (pd.uid = p.pid)
                                 WHERE  p.hrpid = {?}',
                               $login);
         }
         if ($res->numRows() != 1) {
             throw new UserNotFoundException();
         }
-        list($this->pid, $this->hrpid, $this->promo) = $res->fetchOneRow();
+        list($this->pid, $this->hrpid) = $res->fetchOneRow();
     }
 
     public function id()
@@ -70,6 +65,60 @@ class Profile
         return $this->promo;
     }
 
+    /** Print a name with the given formatting:
+     * %s = • for women
+     * %f = firstname
+     * %l = lastname
+     * %F = fullname
+     * %S = shortname
+     * %p = promo
+     */
+    public function name($format)
+    {
+        return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'),
+                           array($this->isFemale() ? '•' : '',
+                                 $this->first_name, $this->last_name,
+                                 $this->full_name, $this->short_name,
+                                 $this->promo), $format);
+    }
+
+    public function fullName($with_promo = false)
+    {
+        if ($with_promo) {
+            return $this->full_name . ' (' . $this->promo . ')';
+        }
+        return $this->full_name;
+    }
+
+    public function shortName($with_promo = false)
+    {
+        if ($with_promo) {
+            return $this->short_name . ' (' . $this->promo . ')';
+        }
+        return $this->short_name;
+    }
+
+    public function firstName()
+    {
+        return $this->first_name;
+    }
+
+    public function lastName()
+    {
+        return $this->last_name;
+    }
+
+    public function isFemale()
+    {
+        return $this->sex == PlUser::GENDER_FEMALE;
+    }
+
+    public function data()
+    {
+        $this->first_name;
+        return $this->data;
+    }
+
     public function __get($name)
     {
         if (property_exists($this, $name)) {
@@ -77,9 +126,17 @@ class Profile
         }
 
         if (empty($this->data)) {
-            $this->data = XDB::fetchOneAssoc('SELECT  *
-                                                FROM  profiles
-                                               WHERE  pid = {?}',
+            // XXX: Temporary, use data from auth_user_md5 (waiting for data from newdirectory
+            $this->data = XDB::fetchOneAssoc('SELECT  p.*, u.prenom AS first_name,
+                                                      IF(u.nom_usage != "", u.nom_usage, u.nom) AS last_name,
+                                                      u.promo AS promo,
+                                                      CONCAT(u.prenom, " ", u.nom) AS short_name,
+                                                      IF(u.nom_usage != "",
+                                                         CONCAT(u.nom_usage, " (", u.nom, "),", u.prenom),
+                                                         CONCAT(u.nom, ", ", u.prenom)) AS full_name
+                                                FROM  profiles AS p
+                                          INNER JOIN  auth_user_md5 AS u ON (u.user_id = p.pid)
+                                               WHERE  p.pid = {?}',
                                              $this->id());
         }
         if (isset($this->data[$name])) {
index 47801ae..616b4d9 100644 (file)
@@ -52,39 +52,25 @@ class AuthModule extends PLModule
 
         $cle = $globals->core->econfiance;
 
+        $res = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n<membres>\n\n";
+
         if (S::v('chall') && $_GET['PASS'] == md5(S::v('chall').$cle)) {
             $list = new MMList(User::getWithUID(10154), "x-econfiance.polytechnique.org");
             $members = $list->get_members('membres');
             if (is_array($members)) {
                 $membres = Array();
                 foreach($members[1] as $member) {
-                    if (preg_match('/^([^.]*.[^.]*.(\d\d\d\d))@polytechnique.org$/',
-                                   $member[1], $matches))
-                    {
-                        $membres[] = "a.alias='{$matches[1]}'";
+                    $user = User::getSilent($member[1]);
+                    if ($user && $user->hasProfile()) {
+                        $profile = $user->profile();
+                        $res .= "<membre>\n";
+                        $res .= "\t<nom>" . $profile->lastName() . "</nom>\n";
+                        $res .= "\t<prenom>" . $profile->firstName() . "</prenom>\n";
+                        $res .= "\t<email>" . $user->forlifeEmail() . "</email>\n";
+                        $res .= "</membre>\n\n";
                     }
                 }
             }
-
-            $where = join(' OR ',$membres);
-
-            $all = XDB::iterRow(
-                    "SELECT  u.prenom,u.nom,a.alias
-                       FROM  auth_user_md5 AS u
-                 INNER JOIN  aliases       AS a ON ( u.user_id = a.id AND a.type!='homonyme' )
-                      WHERE  $where
-                   ORDER BY  nom");
-
-            $res = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n<membres>\n\n";
-
-            while (list ($prenom1,$nom1,$email1) = $all->next()) {
-                    $res .= "<membre>\n";
-                    $res .= "\t<nom>$nom1</nom>\n";
-                    $res .= "\t<prenom>$prenom1</prenom>\n";
-                    $res .= "\t<email>$email1</email>\n";
-                    $res .= "</membre>\n\n";
-            }
-
             $res .= "</membres>\n\n";
 
             header('Content-Type: text/xml; charset="UTF-8"');
index 45b41a2..8d6b70e 100644 (file)
@@ -32,13 +32,20 @@ function gpex_make($chlg, $privkey, $datafields, $charset)
     $params   = "";
     $fieldarr = explode(',', $datafields);
 
-    $res = XDB::query("SELECT  matricule, matricule_ax, promo,
-                               promo_sortie, flags, deces, nom,
-                               prenom, nationalite, section,
-                               naissance
-                         FROM  auth_user_md5 WHERE user_id = {?}",
-                       S::v('uid'));
-    $personnal_data = $res->fetchOneAssoc();
+    $user =& S::user();
+    if ($user->hasProfile()) {
+        // XXX: Transition table for auth.
+        $personnal_data = $user->profile()->data();
+        $personnal_data['matricule'] = $personnal_data['xorg_id'];
+        $personnal_data['matricule_ax'] = $personnal_data['ax_id'];
+        $personnal_data['promo_sortie'] = $personnal_data['promo'] + 3; // FIXME: Hum, not that good
+        $personnal_data['nationalite'] = $personnal_data['nationality1'];
+        $personnal_data['naissance'] = $personnal_data['birthdate'];
+        $personnal_data['deces'] = $personnal_data['deathdate'];
+        $personnal_data['flags'] = $user->profile()->isFemale() ? 'femme' : '';
+    } else {
+        $personnal_data = array();
+    }
 
     foreach ($fieldarr as $val) {
         // Determine the requested value, and add it to the answer.
@@ -51,9 +58,10 @@ function gpex_make($chlg, $privkey, $datafields, $charset)
         } else if (isset($personnal_data[$val])) {
             $params .= gpex_prepare_param($val, $personnal_data[$val], $tohash, $charset);
         } else if ($val == 'username') {
-            $res = XDB::query("SELECT  alias FROM aliases
+            $res = XDB::query("SELECT  alias
+                                 FROM  aliases
                                 WHERE  id = {?} AND FIND_IN_SET('bestalias', flags)",
-                              S::v('uid'));
+                              S::i('uid'));
             $min_username = $res->fetchOneCell();
             $params      .= gpex_prepare_param($val, $min_username, $tohash, $charset);
         } else if ($val == 'grpauth') {
@@ -69,6 +77,8 @@ function gpex_make($chlg, $privkey, $datafields, $charset)
                 $perms = S::has_perms() ? 'admin' : 'membre';
             }
             $params .= gpex_prepare_param($val, $perms, $tohash, $charset);
+        } else {
+            $params .= gpex_prepare_param($val, '', $tohash, $charset);
         }
     }
     $tohash .= "1";
index e8eb099..4ecb6b7 100644 (file)
@@ -13,6 +13,7 @@ create table profiles (
   birthdate_ref date default null,
   deathdate date default null,
 
+  sex enum('female', 'male') not null default 'male',
   section tinyint(2) unsigned default null,
   cv text default null,
   freetext mediumtext default null,
index a5df54e..bb9961e 100644 (file)
@@ -39,6 +39,7 @@ insert into profiles
      select u.user_id AS pid, u.hruid AS hrpid, u.matricule AS xorg_id,
             u.matricule_ax AS ax_id, u.naissance AS birthdate, u.naissance_ini AS birthdate_ref,
             IF(u.deces = 0, NULL, u.deces) AS deathdate,
+            IF(FIND_IN_SET('femme', flags), 'female', 'male') AS sex,
             IF(u.section = 0, NULL, u.section) AS section,
             IF(LENGTH(u.cv) > 0, u.cv, NULL) AS cv,
             IF(LENGTH(q.profile_freetext) > 0, q.profile_freetext, NULL) AS freetext,