Fix login on X.net with only 'groups' perm.
[platal.git] / modules / xnet.php
index 136382c..a1bba73 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2010 Polytechnique.org                              *
+ *  Copyright (C) 2003-2014 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -24,21 +24,32 @@ class XnetModule extends PLModule
     function handlers()
     {
         return array(
-            'index'       => $this->make_hook('index',     AUTH_PUBLIC),
-            'exit'        => $this->make_hook('exit',      AUTH_PUBLIC),
-
-            'admin'       => $this->make_hook('admin',     AUTH_MDP, 'admin'),
-            'groups'      => $this->make_hook('groups',    AUTH_PUBLIC),
-            'groupes.php' => $this->make_hook('groups2',   AUTH_PUBLIC),
-            'plan'        => $this->make_hook('plan',      AUTH_PUBLIC),
-            'photo'       => $this->make_hook('photo',     AUTH_MDP),
-            'autologin'   => $this->make_hook('autologin', AUTH_MDP),
-
-            'Xnet'        => $this->make_wiki_hook(),
+            'index'        => $this->make_hook('index',        AUTH_PUBLIC),
+            'exit'         => $this->make_hook('exit',         AUTH_PUBLIC),
+
+            'login'        => $this->make_hook('login',        AUTH_COOKIE, 'groups'),
+            'admin'        => $this->make_hook('admin',        AUTH_PASSWD, 'admin'),
+            'groups'       => $this->make_hook('groups',       AUTH_PUBLIC),
+            'groupes.php'  => $this->make_hook('groups2',      AUTH_PUBLIC),
+            'plan'         => $this->make_hook('plan',         AUTH_PUBLIC),
+            // Should be removed in a future release as links will have expired anyway.
+            'register/ext' => $this->make_hook('register_ext', AUTH_PUBLIC),
+            'photo'        => $this->make_hook('photo',        AUTH_PASSWD, 'groups'),
+            'autologin'    => $this->make_hook('autologin',    AUTH_PASSWD, 'groups'),
+            'edit'         => $this->make_hook('edit',         AUTH_PASSWD, 'groups'),
+            'Xnet'         => $this->make_wiki_hook(),
         );
     }
 
-    function handler_photo(&$page, $x = null)
+    function handler_login()
+    {
+        // We require different credentials for '/login/' ("groups" instead of "user").
+        // We have to redirect the call to the actual CoreModule->handler_login.
+        $args = func_get_args();
+        return call_user_func_array(array("CoreModule", "handler_login"), $args);
+    }
+
+    function handler_photo($page, $x = null)
     {
         if (!$x || !($profile = Profile::get($x))) {
             return PL_NOT_FOUND;
@@ -51,20 +62,20 @@ class XnetModule extends PLModule
         $photo->send();
     }
 
-    function handler_index(&$page)
+    function handler_index($page)
     {
         $page->nomenu = true;
         $page->changeTpl('xnet/index.tpl');
     }
 
-    function handler_exit(&$page)
+    function handler_exit($page)
     {
         Platal::session()->stopSUID();
         Platal::session()->destroy();
         $page->changeTpl('xnet/deconnexion.tpl');
     }
 
-    function handler_admin(&$page)
+    function handler_admin($page)
     {
         $page->changeTpl('xnet/admin.tpl');
 
@@ -81,10 +92,13 @@ class XnetModule extends PLModule
                 $page->trigSuccess('membres supprimés');
 
                 if ($domain) {
-                    XDB::query('DELETE FROM  virtual_domains WHERE domain={?}', $domain);
-                    XDB::query('DELETE FROM  virtual, virtual_redirect
-                                                USING  virtual INNER JOIN virtual_redirect USING (vid)
-                                                WHERE  alias LIKE {?}', '%@'.$domain);
+                    XDB::execute('DELETE  v
+                                    FROM  email_virtual         AS v
+                              INNER JOIN  email_virtual_domains AS d ON (v.domain = d.id)
+                                   WHERE  d.name = {?}',
+                                 $domain);
+                    XDB::execute('DELETE FROM  email_virtual_domains
+                                        WHERE  name = {?}', $domain);
                     $page->trigSuccess('suppression des alias mails');
 
                     $mmlist = new MMList(S::v('uid'), S::v('password'), $domain);
@@ -129,7 +143,7 @@ class XnetModule extends PLModule
         $page->assign('assos', $res->fetchAllAssoc());
     }
 
-    function handler_plan(&$page)
+    function handler_plan($page)
     {
         $page->changeTpl('xnet/plan.tpl');
 
@@ -170,15 +184,15 @@ class XnetModule extends PLModule
         $page->assign('inst', $res);
     }
 
-    function handler_groups2(&$page)
+    function handler_groups2($page)
     {
-        $this->handler_groups(&$page, Get::v('cat'), Get::v('dom'));
+        $this->handler_groups($page, Get::v('cat'), Get::v('dom'));
     }
 
-    function handler_groups(&$page, $cat = null, $dom = null)
+    function handler_groups($page, $cat = null, $dom = null)
     {
         if (!$cat) {
-            $this->handler_index(&$page);
+            $this->handler_index($page);
         }
 
         $cat = mb_strtolower($cat);
@@ -187,7 +201,7 @@ class XnetModule extends PLModule
         $page->assign('cat', $cat);
         $page->assign('dom', $dom);
 
-        $res  = XDB::query("SELECT  id,nom 
+        $res  = XDB::query("SELECT  id,nom
                               FROM  group_dom
                              WHERE  FIND_IN_SET({?}, cat)
                           ORDER BY  nom", $cat);
@@ -211,7 +225,7 @@ class XnetModule extends PLModule
         $page->setType($cat);
     }
 
-    function handler_autologin(&$page)
+    function handler_autologin($page)
     {
         $allkeys = func_get_args();
         unset($allkeys[0]);
@@ -220,7 +234,73 @@ class XnetModule extends PLModule
         echo '$.ajax({ url: "'.$url.'?forceXml=1", dataType: "xml", success: function(xml) { $("body",xml).insertBefore("body"); $("body:eq(1)").remove(); }});';
         exit;
     }
+
+    function handler_edit($page)
+    {
+        global $globals;
+
+        $user = S::user();
+        if (empty($user)) {
+            return PL_NOT_FOUND;
+        }
+        if ($user->type != 'xnet') {
+            pl_redirect('index');
+        }
+
+        $page->changeTpl('xnet/edit.tpl');
+        if (Post::has('change')) {
+            S::assert_xsrf_token();
+
+            // Convert user status to X
+            if (!Post::blank('login_X')) {
+                $forlife = $this->changeLogin($page, $user, Post::t('login_X'));
+                if ($forlife) {
+                    pl_redirect('index');
+                }
+            }
+
+            require_once 'emails.inc.php';
+            require_once 'name.func.inc.php';
+
+            // Update user info
+            $lastname = capitalize_name(Post::t('lastname'));
+            $firstname = capitalize_name(Post::t('firstname'));
+            $full_name = build_full_name($firstname, $lastname);
+            $directory_name = build_directory_name($firstname, $lastname);
+            $sort_name = build_sort_name($firstname, $lastname);
+            XDB::query('UPDATE  accounts
+                           SET  full_name = {?}, directory_name = {?}, sort_name = {?}, display_name = {?},
+                                firstname = {?}, lastname = {?}, sex = {?}
+                         WHERE  uid = {?}',
+                       $full_name, $directory_name, $sort_name, Post::t('display_name'),
+                       Post::t('firstname'), Post::t('lastname'),
+                       (Post::t('sex') == 'male') ? 'male' : 'female', $user->id());
+
+            // Updates email.
+            $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(), $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');
+        $page->assign('user', $user);
+    }
+
+    function handler_register_ext($page, $hash = null)
+    {
+        http_redirect(Platal::globals()->xnet->xorg_baseurl . 'register/ext/' . $hash);
+    }
 }
 
-// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
 ?>