From 9f3f87bc8e2fc21c6b160f0db67bfd1b52d40007 Mon Sep 17 00:00:00 2001 From: =?utf8?q?St=C3=A9phane=20Jacob?= Date: Mon, 7 Mar 2011 16:33:08 +0100 Subject: [PATCH] Implements best_domain (Closes #1404). MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Jacob --- ChangeLog | 1 + classes/user.php | 5 +---- include/emails.inc.php | 16 ++++++++++++++++ include/vcard.inc.php | 3 ++- modules/carnet/outlook.inc.php | 2 -- modules/email.php | 15 +++++++++++---- templates/emails/index.tpl | 3 +-- upgrade/1.1.0/13_best_domain.sql | 4 ++++ upgrade/1.1.0/best_domain.php | 25 +++++++++++++++++++++++++ 9 files changed, 61 insertions(+), 13 deletions(-) create mode 100644 upgrade/1.1.0/13_best_domain.sql create mode 100755 upgrade/1.1.0/best_domain.php diff --git a/ChangeLog b/ChangeLog index 5ba7dd4..26e5f8e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -11,6 +11,7 @@ Bug/Wish: * Emails: - Allows antispam level choice for each redirection -JAC - #1035: Bases emails on users types (eg educationnal types) -JAC + - #1404: Implements best_domain -JAC * Payments: - #1398: Creates csv for payments -JAC diff --git a/classes/user.php b/classes/user.php index a169ebb..b11bacf 100644 --- a/classes/user.php +++ b/classes/user.php @@ -163,7 +163,6 @@ class User extends PlUser IF(ef.email IS NULL, NULL, CONCAT(ef.email, \'@\', mf.name)) AS forlife, IF(ef.email IS NULL, NULL, CONCAT(ef.email, \'@\', df.name)) AS forlife_alternate, IF(eb.email IS NULL, NULL, CONCAT(eb.email, \'@\', mb.name)) AS bestalias, - IF(eb.email IS NULL, NULL, CONCAT(eb.email, \'@\', db.name)) AS bestalias_alternate, (er.redirect IS NULL AND a.state = \'active\') AS lost, a.email, a.full_name, a.directory_name, a.display_name, a.sex = \'female\' AS gender, IF(a.state = \'active\', CONCAT(at.perms, \',\', IF(a.user_perms IS NULL, \'\', a.user_perms)), \'\') AS perms, @@ -180,9 +179,7 @@ class User extends PlUser LEFT JOIN email_virtual_domains AS df ON (df.aliasing = mf.id AND df.name LIKE CONCAT(\'%\', {?}) AND df.name NOT LIKE \'alumni.%\') LEFT JOIN email_source_account AS eb ON (eb.uid = a.uid AND eb.flags = \'bestalias\') - LEFT JOIN email_virtual_domains AS mb ON (eb.domain = mb.id) - LEFT JOIN email_virtual_domains AS db ON (db.aliasing = mb.id AND - db.name LIKE CONCAT(\'%\', {?}) AND db.name NOT LIKE \'alumni.%\') + LEFT JOIN email_virtual_domains AS mb ON (a.best_domain = mb.id) LEFT JOIN email_redirect_account AS er ON (er.uid = a.uid AND er.flags = \'active\' AND er.broken_level < 3 AND er.type != \'imap\' AND er.type != \'homonym\') LEFT JOIN homonyms_list AS h ON (h.uid = a.uid) diff --git a/include/emails.inc.php b/include/emails.inc.php index aac9a25..0ff0d33 100644 --- a/include/emails.inc.php +++ b/include/emails.inc.php @@ -176,6 +176,22 @@ function mark_broken_email($email, $admin = false) // eventually selects a new bestalias when required. function fix_bestalias(User $user) { + // First check if best_domain is properly set. + $count = XDB::fetchOneCell('SELECT COUNT(*) + FROM accounts AS a + INNER JOIN email_virtual_domains AS d ON (d.id = a.best_domain) + INNER JOIN email_virtual_domains AS m ON (d.aliasing = m.id) + WHERE a.uid = {?} AND m.name = {?}', + $user->id(), $user->mainEmailDomain()); + if ($count == 0) { + XDB::execute('UPDATE accounts AS a + INNER JOIN email_virtual_domains AS d ON (d.name = {?}) + SET a.best_domain = d.id + WHERE a.uid = {?}', + $user->mainEmailDomain(), $user->id()); + } + + // Then check the alias. $count = XDB::fetchOneCell('SELECT COUNT(*) FROM email_source_account WHERE uid = {?} AND FIND_IN_SET(\'bestalias\', flags) AND expire IS NULL', diff --git a/include/vcard.inc.php b/include/vcard.inc.php index ddaa420..99086cc 100644 --- a/include/vcard.inc.php +++ b/include/vcard.inc.php @@ -75,9 +75,10 @@ class VCard extends PlVCard // Emails if (!is_null($user)) { $entry->addMail(null, $user->bestalias, true); - $entry->addMail(null, $user->bestalias_alternate); if ($user->forlife != $user->bestalias) { $entry->addMail(null, $user->forlife); + } + if ($user->forlife_alternate != $user->bestalias) { $entry->addMail(null, $user->forlife_alternate); } } diff --git a/modules/carnet/outlook.inc.php b/modules/carnet/outlook.inc.php index e7eb413..1f33f0a 100644 --- a/modules/carnet/outlook.inc.php +++ b/modules/carnet/outlook.inc.php @@ -91,8 +91,6 @@ class Outlook { if ($user) { $contact['Adresse de messagerie'] = $user->bestalias; $contact['Nom complet de l\'adresse de messagerie'] = $p->fullName().' <'.$user->bestalias.'>'; - $contact['Adresse de messagerie 2'] = $user->bestalias_alternate; - $contact['Nom complet de l\'adresse de messagerie 2'] = $p->fullName().' <'.$user->bestalias_alternate.'>'; if ($user->bestalias != $user->forlife) { $contact['Adresse de messagerie 3'] = $user->forlife; $contact['Nom complet de l\'adresse de messagerie 3'] = $p->fullName().' <'.$user->forlife.'>'; diff --git a/modules/email.php b/modules/email.php index d412b2a..ef77898 100644 --- a/modules/email.php +++ b/modules/email.php @@ -71,6 +71,11 @@ class EmailModule extends PLModule INNER JOIN email_virtual_domains AS d ON (s.domain = d.id) SET s.flags = CONCAT_WS(',', IF(s.flags = '', NULL, s.flags), 'bestalias') WHERE s.uid = {?} AND s.email = {?} AND d.name = {?}", $user->id(), $email, $domain); + XDB::execute('UPDATE accounts AS a + INNER JOIN email_virtual_domains AS d ON (d.name = {?}) + SET a.best_domain = d.id + WHERE a.uid = {?}', + $domain, $user->id()); // As having a non-null bestalias value is critical in // plat/al's code, we do an a posteriori check on the @@ -80,11 +85,13 @@ class EmailModule extends PLModule // Fetch and display aliases. $aliases = XDB::iterator("SELECT CONCAT(s.email, '@', d.name) AS email, (s.type = 'forlife') AS forlife, - (s.email REGEXP '\\\\.[0-9]{2}$') AS hundred_year, - FIND_IN_SET('bestalias', s.flags) AS bestalias, s.expire, - (s.type = 'alias_aux') AS alias + (s.email REGEXP '\\\\.[0-9]{2}$') AS hundred_year, s.expire, + (FIND_IN_SET('bestalias', s.flags) AND a.best_domain = d.id) AS bestalias, + ((s.type = 'alias_aux') AND d.aliasing = d.id) AS alias FROM email_source_account AS s - INNER JOIN email_virtual_domains AS d ON (s.domain = d.id) + INNER JOIN accounts AS a ON (s.uid = a.uid) + INNER JOIN email_virtual_domains AS m ON (s.domain = m.id) + INNER JOIN email_virtual_domains AS d ON (d.aliasing = m.id) WHERE s.uid = {?} ORDER BY !alias, s.email", $user->id()); diff --git a/templates/emails/index.tpl b/templates/emails/index.tpl index 9c545bf..c8bafdd 100644 --- a/templates/emails/index.tpl +++ b/templates/emails/index.tpl @@ -46,8 +46,7 @@ {iterate from=$aliases item=a}  {if $a.forlife}(**){/if}{if $a.hundred_year}(*){/if} {if $a.expire}(expire le {$a.expire|date_format}){/if} {if $a.alias}(changer ou supprimer mon alias melix){/if} diff --git a/upgrade/1.1.0/13_best_domain.sql b/upgrade/1.1.0/13_best_domain.sql new file mode 100644 index 0000000..6988e99 --- /dev/null +++ b/upgrade/1.1.0/13_best_domain.sql @@ -0,0 +1,4 @@ +ALTER TABLE accounts ADD COLUMN best_domain SMALLINT(3) UNSIGNED DEFAULT NULL; +ALTER TABLE accounts ADD FOREIGN KEY (best_domain) REFERENCES email_virtual_domains (id) ON DELETE CASCADE ON UPDATE CASCADE; + +-- vim:set syntax=mysql: diff --git a/upgrade/1.1.0/best_domain.php b/upgrade/1.1.0/best_domain.php new file mode 100755 index 0000000..aaf8291 --- /dev/null +++ b/upgrade/1.1.0/best_domain.php @@ -0,0 +1,25 @@ +#!/usr/bin/php5 +debug = 0; // Do not store backtraces. + +$domains = array( + 'x' => 'polytechnique.org', + 'master' => 'master.polytechnique.org', + 'phd' => 'doc.polytechnique.org' +); +$domains_ids = XDB::fetchAllAssoc('name', 'SELECT name, id + FROM email_virtual_domains + WHERE name IN {?}', + $domains); +foreach ($domains as $type => $domain) { + XDB::execute('UPDATE accounts + SET best_domain = {?} + WHERE type = {?}', + $domains_ids[$domain], $type); +} + +/* vim:set et sw=4 sts=4 ts=4: */ +?> -- 2.1.4