From 33a4f3f9360899d16915493b909a6db0bcbb7164 Mon Sep 17 00:00:00 2001 From: =?utf8?q?St=C3=A9phane=20Jacob?= Date: Wed, 4 Aug 2010 22:08:55 +0200 Subject: [PATCH] Fixes a few issues with Xnet accounts, not registered users with Xnet accounts... (Closes #859, #1200). MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Jacob --- classes/user.php | 142 +++++++++++++++++++++++-------------- modules/register.php | 2 +- modules/xnetgrp.php | 112 +++++++++++++++++------------ templates/xnetgrp/membres-add.tpl | 4 +- templates/xnetgrp/membres-edit.tpl | 14 ++-- 5 files changed, 165 insertions(+), 109 deletions(-) diff --git a/classes/user.php b/classes/user.php index 9b1ee28..1c22fef 100644 --- a/classes/user.php +++ b/classes/user.php @@ -179,11 +179,11 @@ class User extends PlUser $uids = array_map(array('XDB', 'escape'), $uids); return XDB::iterator('SELECT a.uid, a.hruid, a.registration_date, ah.alias AS homonym, - IF (af.alias IS NULL, a.email, CONCAT(af.alias, \'@' . $globals->mail->domain . '\')) AS forlife, - CONCAT(af.alias, \'@' . $globals->mail->domain2 . '\') AS forlife_alternate, - IF (ab.alias IS NULL, a.email, CONCAT(ab.alias, \'@' . $globals->mail->domain . '\')) AS bestalias, - CONCAT(ab.alias, \'@' . $globals->mail->domain2 . '\') AS bestalias_alternate, - a.full_name, a.directory_name, a.display_name, a.sex = \'female\' AS gender, + IF (af.alias IS NULL, NULL, CONCAT(af.alias, \'@' . $globals->mail->domain . '\')) AS forlife, + IF (af.alias IS NULL, NULL, CONCAT(af.alias, \'@' . $globals->mail->domain2 . '\')) AS forlife_alternate, + IF (ab.alias IS NULL, NULL, CONCAT(ab.alias, \'@' . $globals->mail->domain . '\')) AS bestalias, + IF (ab.alias IS NULL, NULL, CONCAT(ab.alias, \'@' . $globals->mail->domain2 . '\')) AS bestalias_alternate, + a.email, a.full_name, a.directory_name, a.display_name, a.sex = \'female\' AS gender, IF(a.state = \'active\', at.perms, \'\') AS perms, a.email_format, a.is_admin, a.state, a.type, a.skin, FIND_IN_SET(\'watch\', a.flags) AS watch, a.comment, @@ -681,61 +681,93 @@ class User extends PlUser // Merge all infos in other user and then clean this one public function mergeIn(User &$newuser) { - if ($this->profile() || !$newuser->id()) { - // don't disable user with profile in this way + if ($this->profile()) { + // Don't disable user with profile in this way. + global $globals; + Platal::page()->trigError('Impossible de fusionner les comptes ' . $this->hruid . ' et ' . $newuser->hruid . + '. Contacte support@' . $globals->mail->domain . '.'); return false; } - // TODO check all tables to see if there is no other info to use - $newemail = $newuser->forlifeEmail(); - if (!$newemail && $this->forlifeEmail()) { - XDB::execute("UPDATE accounts - SET email = {?} - WHERE uid = {?} AND email IS NULL", - $this->forlifeEmail(), $newuser->id()); - $newemail = $this->forlifeEmail(); - } - - // change email used in aliases and mailing lists - if ($this->forlifeEmail() != $newemail && $this->forlifeEmail()) { - // virtual_redirect (email aliases) - XDB::execute("DELETE v1 - FROM virtual_redirect AS v1, virtual_redirect AS v2 - WHERE v1.vid = v2.vid AND v1.redirect = {?} AND v2.redirect = {?}", - $this->forlifeEmail(), $newemail); - XDB::execute("UPDATE virtual_redirect - SET redirect = {?} - WHERE redirect = {?}", - $newemail, $this->forlifeEmail()); - - // require_once 'mmlist.php'; - - // group mailing lists - $group_domains = XDB::fetchColumn("SELECT g.mail_domain - FROM groups AS g - INNER JOIN group_members AS gm ON(g.id = gm.asso_id) - WHERE g.mail_domain != '' AND gm.uid = {?}", - $this->id()); - foreach ($group_domains as $mail_domain) { - $mmlist = new MMList($this, $mail_domain); - $mmlist->replace_email_in_all($this->forlifeEmail(), $newmail); + if ($this->forlifeEmail()) { + // If the new user is not registered and does not have already an email address, + // we need to give him the old user's email address if he has any. + if (!$newuser->perms) { + XDB::execute('UPDATE accounts + SET email = {?} + WHERE uid = {?} AND email IS NULL', + $this->forlifeEmail(), $newuser->id()); } - // main domain lists - $mmlist = new MMList($this); - $mmlist->replace_email_in_all($this->forlifeEmail(), $newmail); - } - - // group_members (xnet group membership) - XDB::execute("DELETE g1 - FROM group_members AS g1, group_members AS g2 - WHERE g1.uid = {?} AND g2.uid = {?} AND g1.asso_id = g2.asso_id", - $this->id(), $newuser->id()); - XDB::execute("UPDATE group_members - SET uid = {?} - WHERE uid = {?}", - $this->id(), $newuser->id()); + $newemail = XDB::fetchOneCell('SELECT email + FROM accounts + WHERE uid = {?}', + $newuser->id()); + + // Change email used in aliases and mailing lists. + if ($this->forlifeEmail() != $newemail) { + // virtual_redirect (email aliases) + XDB::execute('DELETE v1 + FROM virtual_redirect AS v1, virtual_redirect AS v2 + WHERE v1.vid = v2.vid AND v1.redirect = {?} AND v2.redirect = {?}', + $this->forlifeEmail(), $newemail); + XDB::execute('UPDATE virtual_redirect + SET redirect = {?} + WHERE redirect = {?}', + $newemail, $this->forlifeEmail()); + + // group mailing lists + $group_domains = XDB::fetchColumn('SELECT g.mail_domain + FROM groups AS g + INNER JOIN group_members AS gm ON(g.id = gm.asso_id) + WHERE g.mail_domain != \'\' AND gm.uid = {?}', + $this->id()); + foreach ($group_domains as $mail_domain) { + $mmlist = new MMList($this, $mail_domain); + $mmlist->replace_email_in_all($this->forlifeEmail(), $newemail); + } + // main domain lists + $mmlist = new MMList($this); + $mmlist->replace_email_in_all($this->forlifeEmail(), $newemail); + } + } + + // Updates user in following tables. + foreach (array('group_announces', 'payment_transactions', 'log_sessions') as $table) { + XDB::execute('UPDATE ' . $table . ' + SET uid = {?} + WHERE uid = {?}', + $newuser->id(), $this->id()); + } + XDB::execute('UPDATE group_events + SET organisateur_uid = {?} + WHERE organisateur_uid = {?}', + $newuser->id(), $this->id()); + + // Merges user in following tables, ie updates when possible, then deletes remaining occurences of the old user. + foreach (array('group_announces_read', 'group_event_participants', 'group_member_sub_requests', 'group_members') as $table) { + XDB::execute('UPDATE IGNORE ' . $table . ' + SET uid = {?} + WHERE uid = {?}', + $newuser->id(), $this->id()); + XDB::execute('DELETE FROM ' . $table . ' + WHERE uid = {?}', + $this->id()); + } - XDB::execute("DELETE FROM accounts WHERE uid = {?}", $this->id()); + // Eventually updates last session id and deletes old user's accounts entry. + $lastSession = XDB::fetchOneCell('SELECT id + FROM log_sessions + WHERE uid = {?} + ORDER BY start DESC + LIMIT 1', + $newuser->id()); + XDB::execute('UPDATE log_last_sessions + SET id = {?} + WHERE uid = {?}', + $newuser->id()); + XDB::execute('DELETE FROM accounts + WHERE uid = {?}', + $this->id()); return true; } diff --git a/modules/register.php b/modules/register.php index 22a206e..826e5c4 100644 --- a/modules/register.php +++ b/modules/register.php @@ -329,7 +329,7 @@ class RegisterModule extends PLModule // XDB::execute("UPDATE accounts SET password = {?}, state = 'active', - registration_date = NOW() + registration_date = NOW(), email = NULL WHERE uid = {?}", $password, $uid); XDB::execute("UPDATE profiles SET birthdate = {?}, last_change = NOW() diff --git a/modules/xnetgrp.php b/modules/xnetgrp.php index 6036310..12585a6 100644 --- a/modules/xnetgrp.php +++ b/modules/xnetgrp.php @@ -636,55 +636,66 @@ class XnetGrpModule extends PLModule S::assert_xsrf_token(); - // Finds or creates account + // Finds or creates account: first cases are for users with an account. if (!User::isForeignEmailAddress($email)) { - // standard x account + // Standard account $user = User::get($email); } else if (!isvalid_email($email)) { // email might not be a regular email but an alias or a hruid $user = User::get($email); if (!$user) { // need a valid email address - $page->trigError("« $email Â» n'est pas une adresse email valide."); + $page->trigError('« ' . $email . ' Â» n\'est pas une adresse email valide.'); return; } } else if (Env::v('x') && Env::i('userid')) { - // user is an x but might not yet be registered $user = User::getWithUID(Env::i('userid')); if (!$user) { - $page->trigError("Utilisateur invalide"); + $page->trigError('Utilisateur invalide.'); return; } - // add email for marketing if unknown - if ($user->state == 'pending' && Env::v('market')) { - $market = Marketing::get($user->uid, $email); - if (!$market) { - $market = new Marketing($user->uid, $email, 'group', $globals->asso('nom'), - Env::v('market_from'), S::v('uid')); - $market->add(); + + // User has an account but is not yet registered. + if ($user->state == 'pending') { + // Add email in account table. + XDB::query('UPDATE accounts + SET email = {?} + WHERE uid = {?} AND email IS NULL', + Post::t('email'), $user->id()); + // Add email for marketing if required. + if (Env::v('market')) { + $market = Marketing::get($user->uid, $email); + if (!$market) { + $market = new Marketing($user->uid, $email, 'group', $globals->asso('nom'), + Env::v('market_from'), S::v('uid')); + $market->add(); + } } } } else { - // user is not an x - $hruid = strtolower(str_replace('@','.',$email).'.ext'); - // might already exists (in another group for example) + // User is of type xnet. + list($firstname, $lastname) = explode('@', $email); + $hruid = User::makeHrid($firstname, $lastname, 'ext'); + // User might already have an account (in another group for example). $user = User::get($hruid); + + // If the user has no account yet, creates new account: build names from email address. if (empty($user)) { - // creates new account: build names from email address $display_name = ucwords(strtolower(substr($hruid, strpos('.', $hruid)))); - $full_name = ucwords(strtolower(str_replace('.', ' ',substr($email, strpos('@', $email))))); - XDB::execute("INSERT INTO accounts (hruid, display_name, full_name, email, type) - VALUES({?}, {?}, {?}, {?}, 'xnet')", - $hruid, $display_name, $full_name, $email); + $full_name = ucwords(strtolower(str_replace('.', ' ', substr($email, strpos('@', $email))))); + XDB::execute('INSERT INTO accounts (hruid, display_name, full_name, email, type) + VALUES ({?}, {?}, {?}, {?}, \'xnet\')', + $hruid, $display_name, $full_name, $email); $user = User::get($hruid); } } + if ($user) { - XDB::execute("REPLACE INTO group_members (uid, asso_id) - VALUES ({?}, {?})", + XDB::execute('REPLACE INTO group_members (uid, asso_id) + VALUES ({?}, {?})', $user->id(), $globals->asso('id')); $this->removeSubscriptionRequest($user->id()); - pl_redirect("member/" . $user->login()); + pl_redirect('member/' . $user->login()); } } @@ -700,16 +711,16 @@ class XnetGrpModule extends PLModule } } if (empty($users)) { - list($nom, $prenom) = str_replace(array('-', ' ', "'"), '%', array(Env::t('nom'), Env::t('prenom'))); + list($lastname, $firstname) = str_replace(array('-', ' ', "'"), '%', array(Env::t('nom'), Env::t('prenom'))); $cond = new PFC_And(new PFC_Not(new UFC_Registered())); - if (!empty($nom)) { - $cond->addChild(new UFC_Name(Profile::LASTNAME, $nom, UFC_Name::CONTAINS)); + if (!empty($lastname)) { + $cond->addChild(new UFC_Name(Profile::LASTNAME, $lastname, UFC_Name::CONTAINS)); } - if (!empty($prenom)) { - $cond->addChild(new UFC_Name(Profile::FIRSTNAME, $prenom, UFC_Name::CONTAINS)); + if (!empty($firstname)) { + $cond->addChild(new UFC_Name(Profile::FIRSTNAME, $firstname, UFC_Name::CONTAINS)); } - if (Env::i('promo')) { - $cond->addChild(new UFC_Promo('=', UserFilter::GRADE_ING, Env::i('promo'))); + if (Env::t('promo')) { + $cond->addChild(new UFC_Promo('=', UserFilter::DISPLAY, Env::t('promo'))); } $uf = new UserFilter($cond); $users = $uf->getUsers(new PlLimit(30)); @@ -824,7 +835,7 @@ class XnetGrpModule extends PLModule private function changeLogin(PlPage &$page, PlUser &$user, MMList &$mmlist, $login) { - // Search the uid of the user... + // Search the user's uid. $xuser = User::getSilent($login); if (!$xuser) { $accounts = User::getPendingAccounts($login); @@ -836,16 +847,16 @@ class XnetGrpModule extends PLModule return false; } $xuser = User::getSilent($accounts[0]['uid']); - $sub = false; } if (!$xuser) { return false; } - $user->mergeIn($xuser); - - return $xuser->login(); + if ($user->mergeIn($xuser)) { + return $xuser->login(); + } + return $user->login(); } function handler_admin_member(&$page, $user) @@ -877,13 +888,20 @@ class XnetGrpModule extends PLModule $from_email = $user->forlifeEmail(); if (!$user->profile()) { XDB::query('UPDATE accounts - SET full_name = {?}, directory_name = {?}, display_name = {?}, sex = {?}, email = {?}, type = {?} + SET full_name = {?}, directory_name = {?}, display_name = {?}, + sex = {?}, email = {?}, type = {?} WHERE uid = {?}', - Post::t('full_name'), Post::t('directory_name'), Post::t('display_name'), (Post::v('sex') == 'male')?'male':'female', Post::v('email'), (Post::v('type') == 'xnet')?'xnet':'virtual', - $user->id()); - if (XDB::affectedRows()) { - $page->trigSuccess('Données de l\'utilisateur mise à jour.'); - } + Post::t('full_name'), Post::t('directory_name'), Post::t('display_name'), + (Post::t('sex') == 'male') ? 'male' : 'female', Post::t('email'), + (Post::t('type') == 'xnet') ? 'xnet' : 'virtual', $user->id()); + } else if (!$user->perms) { + XDB::query('UPDATE accounts + SET email = {?} + WHERE uid = {?}', + Post::t('email'), $user->id()); + } + if (XDB::affectedRows()) { + $page->trigSuccess('Données de l\'utilisateur mise à jour.'); } // Update group params for user @@ -895,11 +913,13 @@ class XnetGrpModule extends PLModule WHERE uid = {?} AND asso_id = {?}', ($perms == 'admin') ? 'admin' : 'membre', $comm, $user->id(), $globals->asso('id')); - if ($perms != $user->group_perms) { - $page->trigSuccess('Permissions modifiées !'); - } - if ($comm != $user->group_comm) { - $page->trigSuccess('Commentaire mis à jour.'); + if (XDB::affectedRows()) { + if ($perms != $user->group_perms) { + $page->trigSuccess('Permissions modifiées !'); + } + if ($comm != $user->group_comm) { + $page->trigSuccess('Commentaire mis à jour.'); + } } } diff --git a/templates/xnetgrp/membres-add.tpl b/templates/xnetgrp/membres-add.tpl index a6db51e..05b9b09 100644 --- a/templates/xnetgrp/membres-add.tpl +++ b/templates/xnetgrp/membres-add.tpl @@ -80,6 +80,7 @@ function searchX() + {* TODO: adapts text for masters and doctorates when required. *} @@ -93,7 +94,8 @@ function searchX() Promotion : - + {* TODO: add examples for masters and doctorates when required. *} + (X2004) diff --git a/templates/xnetgrp/membres-edit.tpl b/templates/xnetgrp/membres-edit.tpl index fe25f63..aafe431 100644 --- a/templates/xnetgrp/membres-edit.tpl +++ b/templates/xnetgrp/membres-edit.tpl @@ -71,7 +71,7 @@ Type d'utilisateur : - @@ -82,7 +82,7 @@ Nom affiché : - profile()} disabled="disabled"{/if} /> + @@ -90,7 +90,7 @@ Nom complet : - profile()} disabled="disabled"{/if} /> + @@ -98,7 +98,7 @@ Nom annuaire : - profile()} disabled="disabled"{/if} /> + type eq "virtual"}style="display: none"{/if}> @@ -106,18 +106,20 @@ Sexe : - + {/if} + {if !$user->profile() || !$user->perms} Email : - profile()} disabled="disabled"{/if} /> + {/if} -- 2.1.4