Fixes a few issues with Xnet accounts, not registered users with Xnet accounts.....
authorStéphane Jacob <sj@m4x.org>
Wed, 4 Aug 2010 20:08:55 +0000 (22:08 +0200)
committerStéphane Jacob <sj@m4x.org>
Sat, 7 Aug 2010 17:15:19 +0000 (19:15 +0200)
Signed-off-by: Stéphane Jacob <sj@m4x.org>
classes/user.php
modules/register.php
modules/xnetgrp.php
templates/xnetgrp/membres-add.tpl
templates/xnetgrp/membres-edit.tpl

index 9b1ee28..1c22fef 100644 (file)
@@ -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;
     }
index 22a206e..826e5c4 100644 (file)
@@ -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()
index 6036310..12585a6 100644 (file)
@@ -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("«&nbsp;<strong>$email</strong>&nbsp;» n'est pas une adresse email valide.");
+                $page->trigError('«&nbsp;<strong>' . $email . '</strong>&nbsp;» 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&nbsp;!');
-                }
-                if ($comm != $user->group_comm) {
-                    $page->trigSuccess('Commentaire mis à jour.');
+                if (XDB::affectedRows()) {
+                    if ($perms != $user->group_perms) {
+                        $page->trigSuccess('Permissions modifiées&nbsp;!');
+                    }
+                    if ($comm != $user->group_comm) {
+                        $page->trigSuccess('Commentaire mis à jour.');
+                    }
                 }
             }
 
index a6db51e..05b9b09 100644 (file)
@@ -80,6 +80,7 @@ function searchX()
     <tr>
       <td colspan="2">
         <input type="checkbox" id="x" name="x" onchange="xStateChange(this);" />
+        {* TODO: adapts text for masters and doctorates when required. *}
         <label for="x">Coche cette case s'il s'agit d'un X non inscrit à Polytechnique.org.</label>
       </td>
     </tr>
@@ -93,7 +94,8 @@ function searchX()
     </tr>
     <tr id="xpromo" style="display: none">
       <td class="titre">Promotion&nbsp;:</td>
-      <td><input type="text" id="promo" name="promo" size="4" value="" onkeyup="searchX();" /></td>
+      {* TODO: add examples for masters and doctorates when required. *}
+      <td><input type="text" id="promo" name="promo" size="4" value="" onkeyup="searchX();" /> <small>(X2004)</small></td>
     </tr>
     <tr id="xsearch" style="display: none" class="pair">
       <td colspan="2" id="search_result">
index fe25f63..aafe431 100644 (file)
@@ -71,7 +71,7 @@
         Type d'utilisateur&nbsp;:
       </td>
       <td>
-        <select name="type" onchange="showInformations(this); return true"{if $user->profile()} disabled="disabled"{/if}>
+        <select name="type" onchange="showInformations(this); return true">
           <option value="xnet" {if $user->type neq 'virtual'}selected="selected"{/if}>Personne physique</option>
           <option value="virtual" {if $user->type eq "virtual"}selected="selected"{/if}>Personne morale</option>
         </select>
@@ -82,7 +82,7 @@
         Nom affiché&nbsp;:
       </td>
       <td>
-        <input type="text" value="{$user->displayName()}" name="display_name" size="40"{if $user->profile()} disabled="disabled"{/if} />
+        <input type="text" value="{$user->displayName()}" name="display_name" size="40" />
       </td>
     </tr>
     <tr class="impair">
@@ -90,7 +90,7 @@
         Nom complet&nbsp;:
       </td>
       <td>
-        <input type="text" value="{$user->fullName()}" name="full_name" size="40"{if $user->profile()} disabled="disabled"{/if} />
+        <input type="text" value="{$user->fullName()}" name="full_name" size="40" />
       </td>
     </tr>
     <tr class="impair">
@@ -98,7 +98,7 @@
         Nom annuaire&nbsp;:
       </td>
       <td>
-        <input type="text" value="{$user->directoryName()}" name="directory_name" size="40"{if $user->profile()} disabled="disabled"{/if} />
+        <input type="text" value="{$user->directoryName()}" name="directory_name" size="40" />
       </td>
     </tr>
     <tr id="sexe" class="impair" {if $user->type eq "virtual"}style="display: none"{/if}>
         Sexe&nbsp;:
       </td>
       <td>
-        <select name="sex"{if $user->profile()} disabled="disabled"{/if}>
+        <select name="sex">
           <option value="male"{if !$user->isFemale()} selected="selected"{/if}>Homme</option>
           <option value="female"{if $user->isFemale()} selected="selected"{/if}>Femme</option>
         </select>
       </td>
     </tr>
+    {/if}
+    {if !$user->profile() || !$user->perms}
     <tr class="impair">
       <td class="titre">
         Email&nbsp;:
       </td>
       <td>
-        <input type="text" value="{$user->forlifeEmail()}" name="email" size="40"{if $user->profile()} disabled="disabled"{/if} />
+        <input type="text" value="{$user->forlifeEmail()}" name="email" size="40" />
       </td>
     </tr>
     {/if}