Enables creation of multiple Xnet accounts for groups.
authorStéphane Jacob <sj@m4x.org>
Fri, 29 Apr 2011 12:31:51 +0000 (14:31 +0200)
committerStéphane Jacob <sj@m4x.org>
Sat, 30 Apr 2011 22:37:10 +0000 (00:37 +0200)
Signed-off-by: Stéphane Jacob <sj@m4x.org>
12 files changed:
ChangeLog
bin/cron/cron_xnet_accounts.php [new file with mode: 0755]
configs/platal.cron.in
htdocs/images/icons/group_gear.gif [new file with mode: 0644]
include/validations/account.inc.php
include/validations/bulkaccounts.inc.php [new file with mode: 0644]
modules/xnetgrp.php
templates/include/form.valid.bulk_accounts.tpl [new file with mode: 0644]
templates/xnet/account.mail.tpl
templates/xnetgrp/annuaire.tpl
templates/xnetgrp/non_active.tpl [new file with mode: 0644]
upgrade/1.1.1/13_bulk_xnet_account.sql [new file with mode: 0644]

index 9a99433..e6308bb 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,11 @@
 ================================================================================
 VERSION 1.1.1                                                         XX XX XXXX
 
+New:
+
+    * XnetGrp:
+        - Enables creation of multiple Xnet accounts for groups            -JAC
+
 Bug/Wish:
 
     * Auth:
diff --git a/bin/cron/cron_xnet_accounts.php b/bin/cron/cron_xnet_accounts.php
new file mode 100755 (executable)
index 0000000..4ccc4fc
--- /dev/null
@@ -0,0 +1,59 @@
+#!/usr/bin/php5 -q
+<?php
+/***************************************************************************
+ *  Copyright (C) 2003-2011 Polytechnique.org                              *
+ *  http://opensource.polytechnique.org/                                   *
+ *                                                                         *
+ *  This program is free software; you can redistribute it and/or modify   *
+ *  it under the terms of the GNU General Public License as published by   *
+ *  the Free Software Foundation; either version 2 of the License, or      *
+ *  (at your option) any later version.                                    *
+ *                                                                         *
+ *  This program is distributed in the hope that it will be useful,        *
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
+ *  GNU General Public License for more details.                           *
+ *                                                                         *
+ *  You should have received a copy of the GNU General Public License      *
+ *  along with this program; if not, write to the Free Software            *
+ *  Foundation, Inc.,                                                      *
+ *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
+ ***************************************************************************/
+
+require_once 'connect.db.inc.php';
+require_once 'plmailer.php';
+$limit = 60;
+
+$users = XDB::fetchAllAssoc('SELECT  a.uid, a.hruid, r.hash, r.group_name, r.sender_name, r.email
+                               FROM  register_pending_xnet AS r
+                         INNER JOIN  accounts              AS a ON (r.uid = a.uid)
+                              WHERE  a.state = \'disabled\'
+                           ORDER BY  r.date, a.uid');
+
+$mailer = new PlMailer('xnet/account.mail.tpl');
+
+$i = 0;
+foreach ($users as $user) {
+    $mailer->setTo($user['email']);
+    $mailer->assign('hash', $user['hash']);
+    $mailer->assign('hruid', $user['hruid']);
+    $mailer->assign('group', $user['group_name']);
+    $mailer->assign('sender_name', $user['sender_name']);
+    $mailer->send();
+
+    XDB::execute('UPDATE  accounts
+                     SET  state = \'pending\'
+                   WHERE  uid = {?}',
+                 $user['uid']);
+
+    if ($i == $limit) {
+        $i = 0;
+        sleep(60);
+    } else {
+        ++$i;
+    }
+}
+
+
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+?>
index f1e5936..4dd0058 100644 (file)
@@ -41,4 +41,7 @@ WD=/home/web/prod/platal/bin/cron
 # Clean-up of our logging tables
 45 0 1 * *     web     cd $WD; ./compliance.php | mail -e -s "Logging compliance" br@staff.m4x.org
 
+# xnet accounts creation
+0 * * * *      web     cd $WD; ./cron_xnet_accounts.php
+
 # vim:set noet syntax=crontab ts=8 sw=8 sts=8 enc=utf-8:
diff --git a/htdocs/images/icons/group_gear.gif b/htdocs/images/icons/group_gear.gif
new file mode 100644 (file)
index 0000000..ccf6337
Binary files /dev/null and b/htdocs/images/icons/group_gear.gif differ
index 6c380db..68b10a5 100644 (file)
@@ -89,21 +89,9 @@ class AccountReq extends Validate
     public function commit()
     {
         $hash = rand_url_id(12);
-        XDB::execute('UPDATE  accounts
-                         SET  state = \'pending\'
-                       WHERE  uid = {?}',
-                     $this->uid);
-        XDB::execute('INSERT INTO  register_pending_xnet (uid, hruid, email, date, hash)
-                           VALUES  ({?}, {?}, {?}, NOW(), {?})',
-                     $this->uid, $this->hruid, $this->email, $hash);
-
-        $mailer = new PlMailer('xnet/account.mail.tpl');
-        $mailer->setTo($this->email);
-        $mailer->assign('hash', $hash);
-        $mailer->assign('hruid', $this->hruid);
-        $mailer->assign('group', $this->group);
-        $mailer->assign('user', $this->user);
-        $mailer->send();
+        XDB::execute('INSERT INTO  register_pending_xnet (uid, hruid, email, date, hash, sender_name, group_name)
+                           VALUES  ({?}, {?}, {?}, NOW(), {?}, {?}, {?})',
+                     $this->uid, $this->hruid, $this->email, $hash, $this->user->fullName(), $this->group);
 
         return true;
     }
diff --git a/include/validations/bulkaccounts.inc.php b/include/validations/bulkaccounts.inc.php
new file mode 100644 (file)
index 0000000..b3fbcf2
--- /dev/null
@@ -0,0 +1,125 @@
+<?php
+/***************************************************************************
+ *  Copyright (C) 2003-2011 Polytechnique.org                              *
+ *  http://opensource.polytechnique.org/                                   *
+ *                                                                         *
+ *  This program is free software; you can redistribute it and/or modify   *
+ *  it under the terms of the GNU General Public License as published by   *
+ *  the Free Software Foundation; either version 2 of the License, or      *
+ *  (at your option) any later version.                                    *
+ *                                                                         *
+ *  This program is distributed in the hope that it will be useful,        *
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
+ *  GNU General Public License for more details.                           *
+ *                                                                         *
+ *  You should have received a copy of the GNU General Public License      *
+ *  along with this program; if not, write to the Free Software            *
+ *  Foundation, Inc.,                                                      *
+ *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
+ ***************************************************************************/
+
+
+class BulkAccountsReq extends Validate
+{
+    // {{{ properties
+
+    private $limit = 50;
+    public $users;
+    public $group;
+
+    public $rules = "Accepter si les adresses email paraissent correctes, et pas
+        absurdes et si le demandeur est de confiance.";
+    // }}}
+    // {{{ constructor
+
+    public function __construct(User $user, array $uids, $group)
+    {
+        parent::__construct($user, false, 'bulkaccounts');
+        $this->group = $group;
+        $this->users = XDB::fetchAllAssoc('SELECT  uid, hruid, email
+                                             FROM  accounts
+                                            WHERE  uid IN {?}',
+                                          $uids);
+    }
+
+    // }}}
+    // {{{ function formu()
+
+    public function formu()
+    {
+        return 'include/form.valid.bulk_accounts.tpl';
+    }
+
+    // }}}
+    // {{{ function _mail_subj
+
+    protected function _mail_subj()
+    {
+        return "[Polytechnique.org] Création de comptes Polytechnique.net";
+    }
+
+    // }}}
+    // {{{ function _mail_body
+
+    protected function _mail_body($isok)
+    {
+        if ($isok) {
+            return "  Un email vient d'être envoyé aux personnes concernées pour qu'elles puissent activer leur compte sur Polytechnique.net.";
+        } else {
+            return "  Nous n'avons pas jugé bon d'activer les comptes Polytechnique.net demandés.";
+        }
+    }
+
+    // }}}
+    // {{{ function commit()
+
+    public function commit()
+    {
+        $values = array();
+        $i = 0;
+        foreach ($this->users as $user) {
+            $values[] = XDB::format('({?}, {?}, {?}, NOW(), {?}, {?}, {?})',
+                                    $user['uid'], $user['hruid'], $user['email'], rand_url_id(12), $this->user->fullName(), $this->group);
+
+            if ($i == $this->limit) {
+                XDB::rawExecute('INSERT INTO  register_pending_xnet (uid, hruid, email, date, hash, sender_name, group_name)
+                                      VALUES  ' . implode(', ', $values));
+                $i = 0;
+                $values = array();
+            } else {
+                ++$i;
+            }
+        }
+        XDB::rawExecute('INSERT INTO  register_pending_xnet (uid, hruid, email, date, hash, sender_name, group_name)
+                              VALUES  ' . implode(', ', $values));
+
+        return true;
+    }
+
+    // }}}
+    // {{{ function isPending()
+
+    static public function isPending($uid)
+    {
+        $res = XDB::iterRow('SELECT  data
+                               FROM  requests
+                              WHERE  type = \'bulk_accounts\'
+                           ORDER BY  stamp');
+
+        while (list($data) = $res->next()) {
+            $request = Validate::unserialize($data);
+            foreach ($request->users as $user) {
+                if ($user['uid'] == $uid) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
+    // }}}
+}
+
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+?>
index 08342a0..dd225ac 100644 (file)
@@ -36,6 +36,7 @@ class XnetGrpModule extends PLModule
             '%grp/annuaire/vcard'  => $this->make_hook('vcard',                 AUTH_MDP, 'groupmember:groupannu'),
             '%grp/annuaire/csv'    => $this->make_hook('csv',                   AUTH_MDP, 'groupmember:groupannu'),
             '%grp/directory/sync'  => $this->make_hook('directory_sync',        AUTH_MDP, 'groupadmin'),
+            '%grp/directory/unact' => $this->make_hook('non_active',            AUTH_MDP, 'groupadmin'),
             '%grp/trombi'          => $this->make_hook('trombi',                AUTH_MDP, 'groupannu'),
             '%grp/geoloc'          => $this->make_hook('geoloc',                AUTH_MDP, 'groupannu'),
             '%grp/subscribe'       => $this->make_hook('subscribe',             AUTH_MDP),
@@ -509,6 +510,45 @@ class XnetGrpModule extends PLModule
         $page->assign('nonusers', $nonusers);
     }
 
+    function handler_non_active($page)
+    {
+        global $globals;
+        $page->changeTpl('xnetgrp/non_active.tpl');
+
+        $uids = XDB::fetchColumn('SELECT  g.uid
+                                    FROM  group_members         AS g
+                              INNER JOIN  accounts              AS a ON (a.uid = g.uid)
+                               LEFT JOIN  register_pending_xnet AS p ON (p.uid = g.uid)
+                                   WHERE  a.uid = g.uid AND g.asso_id = {?} AND a.type = \'xnet\' AND a.state = \'disabled\' AND p.uid IS NULL',
+                                 $globals->asso('id'));
+        foreach ($uids as $key => $uid) {
+            if (AccountReq::isPending($uid) || BulkAccountsReq::isPending($uid)) {
+                unset($uids[$key]);
+            }
+        }
+
+        if (Post::has('enable_accounts')) {
+            S::assert_xsrf_token();
+
+            $uids_to_enable = array_intersect(array_keys(Post::v('enable_accounts')), $uids);
+
+            $user = S::user();
+            $group = Platal::globals()->asso('nom');
+            $request = new BulkAccountsReq($user, $uids_to_enable, $group);
+            $request->submit();
+            $page->trigSuccess('Un email va bientôt être envoyé aux personnes sélectionnées pour l\'activation de leur compte.');
+
+            foreach ($uids as $key => $uid) {
+                if (in_array($uid, $uids_to_enable)) {
+                    unset($uids[$key]);
+                }
+            }
+        }
+
+        $users = User::getBulkUsersWithUIDs($uids);
+        $page->assign('users', $users);
+    }
+
     private function removeSubscriptionRequest($uid)
     {
         global $globals;
@@ -869,7 +909,7 @@ class XnetGrpModule extends PLModule
                                         FROM  register_pending_xnet
                                        WHERE  uid = {?}',
                                      $user->id());
-        $requested = AccountReq::isPending($user->id());
+        $requested = AccountReq::isPending($user->id()) || BulkAccountsReq::isPending($user->id());
 
         if ($active || $pending || $requested) {
             return false;
diff --git a/templates/include/form.valid.bulk_accounts.tpl b/templates/include/form.valid.bulk_accounts.tpl
new file mode 100644 (file)
index 0000000..59c27a4
--- /dev/null
@@ -0,0 +1,37 @@
+{**************************************************************************}
+{*                                                                        *}
+{*  Copyright (C) 2003-2011 Polytechnique.org                             *}
+{*  http://opensource.polytechnique.org/                                  *}
+{*                                                                        *}
+{*  This program is free software; you can redistribute it and/or modify  *}
+{*  it under the terms of the GNU General Public License as published by  *}
+{*  the Free Software Foundation; either version 2 of the License, or     *}
+{*  (at your option) any later version.                                   *}
+{*                                                                        *}
+{*  This program is distributed in the hope that it will be useful,       *}
+{*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *}
+{*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *}
+{*  GNU General Public License for more details.                          *}
+{*                                                                        *}
+{*  You should have received a copy of the GNU General Public License     *}
+{*  along with this program; if not, write to the Free Software           *}
+{*  Foundation, Inc.,                                                     *}
+{*  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA               *}
+{*                                                                        *}
+{**************************************************************************}
+
+<tr class="pair">
+  <td class="titre">Groupe demandeur&nbsp;:</td>
+  <td>{$valid->group}</td>
+</tr>
+<tr class="pair">
+  <td class="titre">Adresses emails&nbsp;:</td>
+  <td>
+  |
+  {foreach from=$valid->users item=user}
+  &nbsp;{$user.email}&nbsp;|
+  {/foreach}
+  </td>
+</tr>
+
+{* vim:set et sw=2 sts=2 sws=2 enc=utf-8: *}
index cdea70d..0746413 100644 (file)
@@ -29,7 +29,7 @@
 {elseif $mail_part eq 'text'}
 Bonjour,
 
-{$user->fullName()} nous a demandé de vous créer un compte pour que vous puissiez disposer pleinement de toutes les fonctionnalités liées au groupe {$group}.
+{$sender_name} nous a demandé de vous créer un compte pour que vous puissiez disposer pleinement de toutes les fonctionnalités liées au groupe {$group}.
 
 Après activation, vos paramètres de connexion seront :
 
index d18bffd..f86c54c 100644 (file)
@@ -40,6 +40,12 @@ Le groupe {$asso->nom} compte {$nb_tot} membres&nbsp;:
       Synchroniser avec les listes
     </a>
   </li>
+  <li>
+    <a href="{$platal->ns}directory/unact">
+      {icon name=group_gear title="Lister les membres du groupe sans compte actif"}
+      Lister les membres du groupe sans compte actif
+    </a>
+  </li>
   {if $asso->has_ml}
   <li>
     <a href="{$platal->ns}admin/annuaire">
diff --git a/templates/xnetgrp/non_active.tpl b/templates/xnetgrp/non_active.tpl
new file mode 100644 (file)
index 0000000..cd71c74
--- /dev/null
@@ -0,0 +1,79 @@
+{**************************************************************************}
+{*                                                                        *}
+{*  Copyright (C) 2003-2011 Polytechnique.org                             *}
+{*  http://opensource.polytechnique.org/                                  *}
+{*                                                                        *}
+{*  This program is free software; you can redistribute it and/or modify  *}
+{*  it under the terms of the GNU General Public License as published by  *}
+{*  the Free Software Foundation; either version 2 of the License, or     *}
+{*  (at your option) any later version.                                   *}
+{*                                                                        *}
+{*  This program is distributed in the hope that it will be useful,       *}
+{*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *}
+{*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *}
+{*  GNU General Public License for more details.                          *}
+{*                                                                        *}
+{*  You should have received a copy of the GNU General Public License     *}
+{*  along with this program; if not, write to the Free Software           *}
+{*  Foundation, Inc.,                                                     *}
+{*  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA               *}
+{*                                                                        *}
+{**************************************************************************}
+
+{if $users|@count}
+<form action="{$platal->ns}directory/unact" method="post">
+  {xsrf_token_field}
+  <table cellspacing="2" cellpadding="0" class="tiny">
+    <tr>
+      <th>Nom</th>
+      <th>Email</th>
+      <th>
+        <a href="javascript:toggleAll()">{icon name="arrow_refresh" title="Tout (dé)cocher"}</a>
+      </th>
+    </tr>
+    {foreach from=$users item=user}
+    <tr>
+      <td class="checkboxToggle">{profile user=$user promo=true}</td>
+      <td class="checkboxToggle">{$user->email}</td>
+      <td class="checkboxToggle"><input type="checkbox" class="moderate_email" name="enable_accounts[{$user->id()}]" /></td>
+    </tr>
+    {/foreach}
+    <tr>
+      <td colspan="3" class="center">
+        <input type="submit" value="Inscrire au groupe" />
+      </td>
+    </tr>
+  </table>
+
+  <script type="text/javascript">//<![CDATA[
+  {literal}
+  var toggleState = false;
+  function toggleAll() {
+    toggleState = !toggleState;
+    var boxes = $(':checkbox.moderate_email');
+    if (toggleState) {
+      boxes.attr('checked', 'checked');
+    } else {
+      boxes.removeAttr('checked');
+    }
+  }
+
+  $('.checkboxToggle').click(function (event) {
+    // Don't uncheck the checkbox when clicking it
+    if (event.target.tagName === 'INPUT') {
+      return;
+    }
+
+    var checkbox = $(this).parent().find(':checkbox');
+    checkbox = checkbox.attr('checked', !checkbox.attr('checked'));
+    event.stopPropagation();
+  });
+  {/literal}
+  //]]></script>
+
+</form>
+{else}
+<p>Tous les inscrits au groupe ont un compte leur permettant d'accéder aux pages du groupe.</p>
+{/if}
+
+{* vim:set et sw=2 sts=2 sws=2 enc=utf-8: *}
diff --git a/upgrade/1.1.1/13_bulk_xnet_account.sql b/upgrade/1.1.1/13_bulk_xnet_account.sql
new file mode 100644 (file)
index 0000000..2615e13
--- /dev/null
@@ -0,0 +1,4 @@
+ALTER TABLE register_pending_xnet ADD COLUMN sender_name VARCHAR(255) NOT NULL DEFAULT '';
+ALTER TABLE register_pending_xnet ADD COLUMN group_name VARCHAR(255) NOT NULL DEFAULT '';
+
+-- vim:set syntax=mysql: