From 8a41e7f9824e57509e00e73c2e32d2801e949e40 Mon Sep 17 00:00:00 2001 From: =?utf8?q?St=C3=A9phane=20Jacob?= Date: Tue, 18 Oct 2011 16:03:24 +0200 Subject: [PATCH] Ensures accounts.email are all distinct. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Jacob --- include/emails.inc.php | 15 ++++++++++++++- modules/admin.php | 9 +++++---- modules/xnet.php | 27 ++++++++++++++++----------- modules/xnetgrp.php | 16 ++++++++++------ 4 files changed, 45 insertions(+), 22 deletions(-) diff --git a/include/emails.inc.php b/include/emails.inc.php index bd24d3e..a22adb5 100644 --- a/include/emails.inc.php +++ b/include/emails.inc.php @@ -30,7 +30,20 @@ function require_email_update(User $user, $new_email) { Platal::assert(!is_null($user), 'User cannot be null.'); - return !$user->checkPerms(User::PERM_MAIL) && strtolower($new_email) != strtolower($user->forlifeEmail()); + $is_new = !$user->checkPerms(User::PERM_MAIL) && $new_email != strtolower($user->email); + if ($new_email && $is_new) { + $already = XDB::fetchOneCell('SELECT hruid + FROM accounts + WHERE email = {?} AND uid != {?}', + $new_email, $user->id()); + if ($already) { + Platal::page()->trigError("L'email ne peut pas être utilisé pour ce compte car il correspond déjà au compte : " + . $already . ". Si l'utilisateur courant et cette personne ne sont en fait qu'une " + . "seul et même personne, ou en cas de problème, contacter : contact@polytechnique.org"); + return false; + } + } + return $is_new; } function format_email_alias($email) diff --git a/modules/admin.php b/modules/admin.php index ed0f84a..9cf5e24 100644 --- a/modules/admin.php +++ b/modules/admin.php @@ -518,10 +518,11 @@ class AdminModule extends PLModule if (Post::t('comment') != $user->comment) { $to_update['comment'] = Post::blank('comment') ? null : Post::t('comment'); } - if (require_email_update($user, Post::t('email'))) { - $to_update['email'] = Post::t('email'); - $listClient->change_user_email($user->forlifeEmail(), Post::t('email')); - update_alias_user($user->forlifeEmail(), Post::t('email')); + $new_email = strtolower(Post::t('email')); + if (require_email_update($user, $new_email)) { + $to_update['email'] = $new_email; + $listClient->change_user_email($user->forlifeEmail(), $new_email); + update_alias_user($user->forlifeEmail(), $new_email); } } if (!empty($to_update)) { diff --git a/modules/xnet.php b/modules/xnet.php index ae6edc4..9f7e6b3 100644 --- a/modules/xnet.php +++ b/modules/xnet.php @@ -255,22 +255,27 @@ class XnetModule extends PLModule $directory_name = mb_strtoupper(Post::t('lastname')) . ' ' . Post::t('firstname'); XDB::query('UPDATE accounts SET full_name = {?}, directory_name = {?}, display_name = {?}, - firstname = {?}, lastname = {?}, sex = {?}, email = {?} + firstname = {?}, lastname = {?}, sex = {?} WHERE uid = {?}', $full_name, $directory_name, Post::t('display_name'), Post::t('firstname'), Post::t('lastname'), - (Post::t('sex') == 'male') ? 'male' : 'female', Post::t('email'), $user->id()); - if (XDB::affectedRows()) { - require_once 'emails.inc.php'; - if (require_email_update($user, Post::t('email'))) { + (Post::t('sex') == 'male') ? 'male' : 'female', $user->id()); + + // Updates email. + require_once 'emails.inc.php'; + $new_email = strtolower(Post::t('email')); + if (require_email_update($user, $new_email)) { + XDB::query('UPDATE accounts + SET email = {?} + WHERE uid = {?}', + $new_email, $user->id()); $listClient = new MMList(S::user()); - $listClient->change_user_email($user->forlifeEmail(), Post::t('email')); - update_alias_user($user->forlifeEmail(), Post::t('email')); - } - $user = User::getWithUID($user->id()); - S::set('user', $user); - $page->trigSuccess('Données mises à jour.'); + $listClient->change_user_email($user->forlifeEmail(), $new_email); + update_alias_user($user->forlifeEmail(), $new_email); } + $user = User::getWithUID($user->id()); + S::set('user', $user); + $page->trigSuccess('Données mises à jour.'); } $page->addJsLink('password.js'); diff --git a/modules/xnetgrp.php b/modules/xnetgrp.php index ccaf4cd..8960524 100644 --- a/modules/xnetgrp.php +++ b/modules/xnetgrp.php @@ -1218,19 +1218,23 @@ class XnetGrpModule extends PLModule } XDB::query('UPDATE accounts SET full_name = {?}, directory_name = {?}, display_name = {?}, - firstname = {?}, lastname = {?}, sex = {?}, email = {?}, type = {?} + firstname = {?}, lastname = {?}, sex = {?}, type = {?} WHERE uid = {?}', $full_name, $directory_name, Post::t('display_name'), $firstname, $lastname, - (Post::t('sex') == 'male') ? 'male' : 'female', Post::t('email'), + (Post::t('sex') == 'male') ? 'male' : 'female', (Post::t('type') == 'xnet') ? 'xnet' : 'virtual', $user->id()); - } else if (!$user->perms && Post::has('email') && require_email_update($user, Post::t('email'))) { + } + + // Updates email. + $new_email = strtolower(Post::t('email')); + if (!$user->perms && require_email_update($user, $new_email)) { XDB::query('UPDATE accounts SET email = {?} WHERE uid = {?}', - Post::t('email'), $user->id()); + $new_email, $user->id()); $listClient = new MMList(S::user()); - $listClient->change_user_email($user->forlifeEmail(), Post::t('email')); - update_alias_user($user->forlifeEmail(), Post::t('email')); + $listClient->change_user_email($user->forlifeEmail(), $new_email); + update_alias_user($user->forlifeEmail(), $new_email); } if (XDB::affectedRows()) { $page->trigSuccess('Données de l\'utilisateur mises à jour.'); -- 2.1.4