Adapts homonyms issues to new mail chain.
authorStéphane Jacob <sj@m4x.org>
Fri, 25 Feb 2011 12:59:12 +0000 (13:59 +0100)
committerStéphane Jacob <sj@m4x.org>
Sun, 27 Feb 2011 22:19:02 +0000 (23:19 +0100)
Signed-off-by: Stéphane Jacob <sj@m4x.org>
include/validations/homonymes.inc.php
modules/admin.php
modules/admin/homonyms.inc.php
modules/register/register.inc.php
templates/admin/homonymes.tpl
upgrade/1.1.0/01_new_mail_schema.sql
upgrade/1.1.0/02_new_mail_insertion.sql

index f39209e..7f181ef 100644 (file)
@@ -121,12 +121,24 @@ est ambigu pour des raisons d'homonymie et signalera ton email exact.";
     public function commit()
     {
         Platal::load('admin', 'homonyms.inc.php');
-        switch_bestalias($this->user, $this->loginbis);
         if (!$this->warning) {
-            XDB::execute("UPDATE aliases SET type = 'homonyme', expire = NOW() WHERE alias = {?}", $this->loginbis);
-            XDB::execute('INSERT IGNORE INTO  homonyms (homonyme_id, uid)
+            global $globals;
+            require_once 'emails.inc.php';
+
+            XDB::execute('DELETE  e
+                            FROM  email_source_account  AS e
+                      INNER JOIN  email_virtual_domains AS d ON (e.domain = d.id)
+                           WHERE  e.email = {?} AND d.name = {?}',
+                         $this->loginbis, $globals->mail->domain);
+            XDB::execute('INSERT INTO  email_source_other (hrmid, email, domain, type, expire)
+                               SELECT  CONCAT(\'h.\', {?}, \'.\', {?}), {?}, id, \'homonym\', NOW()
+                                 FROM  email_virtual_domains
+                                WHERE  name = {?}',
+                         $this->loginbis, $globals->mail->domain, $this->loginbis, $globals->mail->domain);
+            XDB::execute('INSERT IGNORE INTO  homonyms_list (hrmid, uid)
                                       VALUES  ({?}, {?})',
-                         $this->user->id(), $this->user->id());
+                         'h.' . $this->loginbis . '.' . $globals->mail->domain, $this->user->id());
+            fix_bestalias($this->user);
         }
 
         return true;
index ea0928b..c533a6c 100644 (file)
@@ -938,13 +938,14 @@ class AdminModule extends PLModule
 
     function handler_homonyms($page, $op = 'list', $target = null)
     {
+        global $globals;
         $page->changeTpl('admin/homonymes.tpl');
         $page->setTitle('Administration - Homonymes');
         $this->load("homonyms.inc.php");
 
         if ($target) {
-            $user = User::getSilent($target);
-            if (!$user || !($loginbis = select_if_homonyme($user))) {
+            $user = User::getSilentWithUID($target);
+            if (!$user || !($loginbis = select_if_homonym($user))) {
                 $target = 0;
             } else {
                 $page->assign('user', $user);
@@ -955,15 +956,16 @@ class AdminModule extends PLModule
         $page->assign('op', $op);
         $page->assign('target', $target);
 
-        // on a un $target valide, on prepare les mails
+        // When we have a valid target, prepare emails.
         if ($target) {
-            // on examine l'op a effectuer
+            require_once 'emails.inc.php';
+            // Examine what operation needs to be performed.
             switch ($op) {
                 case 'mail':
                     S::assert_xsrf_token();
 
                     send_warning_homonyme($user, $loginbis);
-                    switch_bestalias($user, $loginbis);
+                    fix_bestalias($user);
                     $op = 'list';
                     $page->trigSuccess('Email envoyé à ' . $user->forlifeEmail() . '.');
                     break;
@@ -971,12 +973,20 @@ class AdminModule extends PLModule
                 case 'correct':
                     S::assert_xsrf_token();
 
-                    switch_bestalias($user, $loginbis);
-                    XDB::execute("UPDATE  aliases
-                                     SET  type = 'homonyme', expire=NOW()
-                                   WHERE  alias = {?}", $loginbis);
-                    XDB::execute('INSERT IGNORE INTO  homonyms (homonyme_id, uid)
-                                              VALUES  ({?}, {?})', $target, $target);
+                    XDB::execute('DELETE  e
+                                    FROM  email_source_account  AS e
+                              INNER JOIN  email_virtual_domains AS d ON (e.domain = d.id)
+                                   WHERE  e.email = {?} AND d.name = {?}',
+                                 $loginbis, $globals->mail->domain);
+                    XDB::execute('INSERT INTO  email_source_other (hrmid, email, domain, type, expire)
+                                       SELECT  CONCAT(\'h.\', {?}, \'.\', {?}), {?}, id, \'homonym\', NOW()
+                                         FROM  email_virtual_domains
+                                        WHERE  name = {?}',
+                                 $loginbis, $globals->mail->domain, $loginbis, $globals->mail->domain);
+                    XDB::execute('INSERT IGNORE INTO  homonyms_list (hrmid, uid)
+                                              VALUES  ({?}, {?})',
+                                 'h.' . $loginbis . '.' . $globals->mail->domain, $target);
+                    fix_bestalias($user);
                     send_robot_homonyme($user, $loginbis);
                     $op = 'list';
                     $page->trigSuccess('Email envoyé à ' . $user->forlifeEmail() . ', alias supprimé.');
@@ -985,21 +995,36 @@ class AdminModule extends PLModule
         }
 
         if ($op == 'list') {
-            $res = XDB::iterator(
-                    "SELECT  a.alias AS homonyme, s.alias AS forlife,
-                             IF(h.homonyme_id = s.uid, a.expire, NULL) AS expire,
-                             IF(h.homonyme_id = s.uid, a.type, NULL) AS type, ac.uid
-                       FROM  aliases       AS a
-                  LEFT JOIN  homonyms      AS h  ON (h.homonyme_id = a.uid)
-                 INNER JOIN  aliases       AS s  ON (s.uid = h.uid AND s.type = 'a_vie')
-                 INNER JOIN  accounts      AS ac ON (ac.uid = a.uid)
-                      WHERE  a.type = 'homonyme' OR a.expire != ''
-                   ORDER BY  a.alias, forlife");
-            $hnymes = Array();
-            while ($tab = $res->next()) {
-                $hnymes[$tab['homonyme']][] = $tab;
-            }
-            $page->assign_by_ref('hnymes', $hnymes);
+            // Retrieves homonyms that are already been fixed.
+            $res = XDB::iterator('SELECT  o.email AS homonym, f.email AS forlife, o.expire, f.uid
+                                    FROM  email_source_other    AS o
+                              INNER JOIN  homonyms_list         AS h ON (o.hrmid = h.hrmid)
+                              INNER JOIN  email_source_account  AS f ON (h.uid = f.uid AND f.type = \'forlife\')
+                              INNER JOIN  email_virtual_domains AS d ON (f.domain = d.id)
+                                   WHERE  o.expire IS NOT NULL AND d.name = {?}
+                                ORDER BY  homonym, forlife',
+                                 $globals->mail->domain);
+            $homonyms = array();
+            while ($item = $res->next()) {
+                $homonyms[$item['homonym']][] = $item;
+            }
+            $page->assign_by_ref('homonyms', $homonyms);
+
+            // Retrieves homonyms that needs to be fixed.
+            $res = XDB::iterator('SELECT  e.email AS homonym, f.email AS forlife, e.expire, e.uid, (e.expire < NOW()) AS urgent
+                                    FROM  email_source_account  AS e
+                              INNER JOIN  homonyms_list         AS l ON (e.uid = l.uid)
+                              INNER JOIN  homonyms_list         AS h ON (l.hrmid = h.hrmid)
+                              INNER JOIN  email_source_account  AS f ON (h.uid = f.uid AND f.type = \'forlife\')
+                              INNER JOIN  email_virtual_domains AS d ON (f.domain = d.id)
+                                   WHERE  e.expire IS NOT NULL AND d.name = {?}
+                                ORDER BY  homonym, forlife',
+                                 $globals->mail->domain);
+            $homonyms_to_fix = array();
+            while ($item = $res->next()) {
+                $homonyms_to_fix[$item['homonym']][] = $item;
+            }
+            $page->assign_by_ref('homonyms_to_fix', $homonyms_to_fix);
         }
     }
 
index 0d22b94..d96e735 100644 (file)
  *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
  ***************************************************************************/
 
-function select_if_homonyme(PlUser $user) {
-    return XDB::fetchOneCell("SELECT  a.alias
-                                FROM  aliases AS a
-                               WHERE  a.uid = {?} AND a.expire != ''",
+function select_if_homonym(PlUser $user) {
+    return XDB::fetchOneCell('SELECT  email
+                                FROM  email_source_account
+                               WHERE  uid = {?} AND expire IS NOT NULL',
                              $user->id());
 }
 
@@ -50,28 +50,5 @@ function send_robot_homonyme(PlUser $user, $loginbis) {
     $mymail->sendTo($user);
 }
 
-function switch_bestalias(PlUser $user, $loginbis) {
-    // check if loginbis was the bestalias
-    $bestailas = XDB::fetchOneCell("SELECT  alias
-                                      FROM  aliases
-                                     WHERE  uid = {?} AND FIND_IN_SET('bestalias', flags)",
-                                   $user->id());
-    if ($bestalias && $bestalias != $loginbis) {
-        return false;
-    }
-
-    // select the shortest alias still alive
-    $newbest = XDB::fetchOneCell("SELECT  alias
-                                    FROM  aliases
-                                   WHERE  uid = {?} AND alias != {?} AND expire IS NULL
-                                ORDER BY  LENGTH(alias)
-                                   LIMIT  1", $user->id(), $loginbis);
-    // change the bestalias flag
-    XDB::execute("UPDATE  aliases
-                     SET  flags = (flags & (255 - 1)) | IF(alias = {?}, 1, 0)
-                   WHERE  uid = {?}", $newbest, $user->id());
-    return $newbest;
-}
-
 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
 ?>
index c32d35a..bd6f860 100644 (file)
@@ -152,22 +152,28 @@ function createAliases($subState)
              . "<a href=\"mailto:support@{$globals->mail->domain}\">support@{$globals->mail->domain}</a>.";
     }
 
-    $res = XDB::query('SELECT  uid, type, expire
-                         FROM  aliases
-                        WHERE  alias = {?}', $emailXorg);
-    if ($res->numRows()) {
+    $count = XDB::numRows('SELECT  *
+                             FROM  email_source_account  AS s
+                       INNER JOIN  email_virtual_domains AS d ON (s.domain = d.id)
+                            WHERE  s.email = {?} AND d.name = {?}',
+                          $emailXorg, $globals->mail->domain);
+    if ($count) {
         list($h_id, $h_type, $expire) = $res->fetchOneRow();
         if ($h_type != 'homonyme' and empty($expire)) {
-            XDB::execute('UPDATE  aliases
-                             SET  expire = ADDDATE(NOW(), INTERVAL 1 MONTH)
-                           WHERE  alias = {?}', $emailXorg);
-            XDB::execute('INSERT IGNORE INTO  homonyms (homonyme_id, uid)
+            XDB::execute('UPDATE  email_source_account  AS e
+                      INNER JOIN  email_virtual_domains AS d ON (e.domain = d.id)
+                             SET  e.expire = ADDDATE(NOW(), INTERVAL 1 MONTH)
+                           WHERE  e.email = {?} AND d.name = {?}',
+                         $emailXorg, $globals->mail->domain);
+            $hrmid = 'h.' . $emailXorg . '.' . $globals->mail->domain;
+            XDB::execute('INSERT IGNORE INTO  homonyms_list (hrmid, uid)
                                       VALUES  ({?}, {?}), ({?}, {?})',
-                         $subState->i('uid'), $h_id, $h_id, $subState->i('uid'));
-            $res = XDB::query('SELECT  alias
-                                 FROM  aliases
-                                WHERE  uid = {?} AND expire IS NULL', $h_id);
-            $als = $res->fetchColumn();
+                         $hrmid, $h_id, $hrmid, $subState->i('uid'));
+            $als = XDB::fetchColumn('SELECT  *
+                                       FROM  email_source_account  AS s
+                                 INNER JOIN  email_virtual_domains AS d ON (s.domain = d.id)
+                                      WHERE  s.uid = {?} AND d.name = {?} AND s.expire IS NULL',
+                                    $h_id, $globals->mail->domain);
 
             $mailer = new PlMailer('register/lostalias.mail.tpl');
             $mailer->addTo($emailXorg . '@' . $globals->mail->domain);
index 1567407..9f9fed5 100644 (file)
 
 {if $op eq 'list' || $op eq 'mail' || $op eq 'correct'}
 
+{if $homonyms_to_fix|@count}
 <p>
-  Les utilisateurs signalés en rouge sont ceux qui conservent actuellement
-  l'alias prenom.nom et empêchent donc la mise en place du robot détrompeur.
+  Liste des homonymies à corriger, celles en rouge devraient déjà être traitées.
 </p>
 
 <table class="bicol">
   <tr>
     <th>alias concerné</th>
     <th>date de péremption de l'alias</th>
-    <th>op</th>
+    <th>actions</th>
   </tr>
-  {foreach from=$hnymes key=login item=row}
+  {foreach from=$homonyms_to_fix key=login item=row}
   <tr class="pair">
-    <td colspan="3">
+    <td>
+      {if $row.0.urgent}
+      <span class="erreur"><strong>{$login}</strong></span>
+      {else}
       <strong>{$login}</strong>
+      {/if}
+    </td>
+    <td>{$row.0.expire|date_format}</td>
+    <td>
+      <a href="admin/homonyms/mail-conf/{$row.0.uid}">envoyer un email</a>
+      <a href="admin/homonyms/correct-conf/{$row.0.uid}">corriger</a>
     </td>
   </tr>
   {foreach from=$row item=user}
   <tr class="impair">
-    <td>&nbsp;&nbsp;
-      {if $user.type eq 'alias'}
-      <span class="erreur"><strong>{$user.forlife}</strong></span>
-      {else}
-      {$user.forlife}
-      {/if}
+    <td>&nbsp;&nbsp;{$user.forlife}</td>
+    <td></td>
+    <td>
+      <a href="profile/{$user.forlife}" class='popup2'>fiche</a>
+      <a href="admin/user/{$user.forlife}">edit</a>
     </td>
-    <td>{$user.expire|date_format}</td>
+  </tr>
+  {/foreach}
+  {/foreach}
+</table>
+{/if}
+
+<p>
+  Liste des homonymies déjà corrigées.
+</p>
+
+<table class="bicol">
+  <tr>
+    <th>alias concerné</th>
+    <th>alias prémimé depuis</th>
+    <th>actions</th>
+  </tr>
+  {foreach from=$homonyms key=login item=row}
+  <tr class="pair">
+    <td><strong>{$login}</strong></td>
+    <td>{if $row.0.expire eq '0000-00-00'}---{else}{$row.0.expire|date_format}{/if}</td>
+    <td></td>
+  </tr>
+  {foreach from=$row item=user}
+  <tr class="impair">
+    <td>&nbsp;&nbsp;{$user.forlife}</td>
+    <td></td>
     <td>
       <a href="profile/{$user.forlife}" class='popup2'>fiche</a>
       <a href="admin/user/{$user.forlife}">edit</a>
-      {if $user.type eq 'alias'}
-      <a href="admin/homonyms/mail-conf/{$user.uid}">envoyer un email</a>
-      <a href="admin/homonyms/correct-conf/{$user.uid}">corriger</a>
-      {/if}
     </td>
   </tr>
   {/foreach}
index 1d599b1..46cbd97 100644 (file)
@@ -35,7 +35,7 @@ CREATE TABLE email_source_other (
   domain SMALLINT(3) UNSIGNED NOT NULL DEFAULT 1,
   hrmid VARCHAR(255) NOT NULL,
   type ENUM('homonym', 'ax', 'honeypot'),
-  expire DATE NOT NULL DEFAULT '0000-00-00',
+  expire DATE DEFAULT NULL,
   PRIMARY KEY (email, domain),
   KEY (domain),
   FOREIGN KEY (domain) REFERENCES email_virtual_domains (id) ON UPDATE CASCADE ON DELETE CASCADE
index fa60db8..2229789 100644 (file)
@@ -56,8 +56,8 @@ INSERT INTO  email_source_account (uid, domain, email, type)
       WHERE  v.type = 'user' AND v.alias LIKE '%@melix.net' AND a.uid IS NOT NULL;
 
 -- 3/ Feeds email_source_other
-INSERT INTO  email_source_other (hrmid, email, domain, type)
-     SELECT  CONCAT(CONCAT('h.', alias), '.polytechnique.org'), alias, @p_domain_id, 'homonym'
+INSERT INTO  email_source_other (hrmid, email, domain, type, expire)
+     SELECT  CONCAT(CONCAT('h.', alias), '.polytechnique.org'), alias, @p_domain_id, 'homonym', IF(expire IS NULL, '0000-00-00', expire)
        FROM  aliases
       WHERE  type = 'homonyme'
    GROUP BY  alias;