From 94b72319372118098d98cbee7b93a32e2643481c Mon Sep 17 00:00:00 2001 From: Florent Bruneau Date: Wed, 31 Dec 2008 00:30:22 +0100 Subject: [PATCH] Add name formatting to the Profile class. Port module auth (auth-groupex should work... not tested). Signed-off-by: Florent Bruneau --- classes/profile.php | 81 ++++++++++++++++++++++++++++++++++------ modules/auth.php | 34 +++++------------ modules/auth/auth.inc.php | 28 +++++++++----- upgrade/account/01_profiles.sql | 1 + upgrade/account/99_insertion.sql | 1 + 5 files changed, 100 insertions(+), 45 deletions(-) diff --git a/classes/profile.php b/classes/profile.php index 130cd35..fc819f6 100644 --- a/classes/profile.php +++ b/classes/profile.php @@ -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])) { diff --git a/modules/auth.php b/modules/auth.php index 47801ae..616b4d9 100644 --- a/modules/auth.php +++ b/modules/auth.php @@ -52,39 +52,25 @@ class AuthModule extends PLModule $cle = $globals->core->econfiance; + $res = "\n\n\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 .= "\n"; + $res .= "\t" . $profile->lastName() . "\n"; + $res .= "\t" . $profile->firstName() . "\n"; + $res .= "\t" . $user->forlifeEmail() . "\n"; + $res .= "\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 = "\n\n\n\n"; - - while (list ($prenom1,$nom1,$email1) = $all->next()) { - $res .= "\n"; - $res .= "\t$nom1\n"; - $res .= "\t$prenom1\n"; - $res .= "\t$email1\n"; - $res .= "\n\n"; - } - $res .= "\n\n"; header('Content-Type: text/xml; charset="UTF-8"'); diff --git a/modules/auth/auth.inc.php b/modules/auth/auth.inc.php index 45b41a2..8d6b70e 100644 --- a/modules/auth/auth.inc.php +++ b/modules/auth/auth.inc.php @@ -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"; diff --git a/upgrade/account/01_profiles.sql b/upgrade/account/01_profiles.sql index e8eb099..4ecb6b7 100644 --- a/upgrade/account/01_profiles.sql +++ b/upgrade/account/01_profiles.sql @@ -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, diff --git a/upgrade/account/99_insertion.sql b/upgrade/account/99_insertion.sql index a5df54e..bb9961e 100644 --- a/upgrade/account/99_insertion.sql +++ b/upgrade/account/99_insertion.sql @@ -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, -- 2.1.4