Remove 'user' perm from X.net accounts.
[platal.git] / classes / xnetsession.php
index cb125d5..c1c284b 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2009 Polytechnique.org                              *
+ *  Copyright (C) 2003-2014 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -29,19 +29,19 @@ class XnetSession extends XorgSession
     public function startAvailableAuth()
     {
         if (!S::logged() && Get::has('auth')) {
-            if (!$this->start(AUTH_MDP)) {
+            if (!$this->start(AUTH_PASSWD)) {
                 return false;
             }
         }
 
         global $globals;
-        if (!S::logged()) {
+        if (!S::logged() && $globals->xnet->auth_baseurl) {
             // prevent connection to be linked to disconnection
             if (($i = strpos($_SERVER['REQUEST_URI'], 'exit')) !== false)
                 $returl = "http://{$_SERVER['SERVER_NAME']}".substr($_SERVER['REQUEST_URI'], 0, $i);
             else
                 $returl = "http://{$_SERVER['SERVER_NAME']}{$_SERVER['REQUEST_URI']}";
-            $url  = "https://www.polytechnique.org/auth-groupex";
+            $url  = $globals->xnet->auth_baseurl;
             $url .= "?session=" . session_id();
             $url .= "&challenge=" . S::v('challenge');
             $url .= "&pass=" . md5(S::v('challenge') . $globals->xnet->secret);
@@ -64,6 +64,8 @@ class XnetSession extends XorgSession
                 if ($globals->asso('pub') != 'private') {
                     $perms->addFlag('groupannu');
                 }
+            } else if ($globals->asso('pub') == 'public') {
+                $perms->addFlag('groupannu');
             }
             if ($globals->asso('cat') == 'Promotions') {
                 $perms->addFlag('groupannu');
@@ -75,8 +77,8 @@ class XnetSession extends XorgSession
 
     protected function doAuth($level)
     {
-        if (S::identified()) { // ok, c'est bon, on n'a rien à faire
-            return User::getSilentWithValues(null, array('user_id' => S::i('uid')));
+        if (S::identified()) { // Nothing to do there
+            return User::getSilentWithValues(null, array('uid' => S::i('uid')));
         }
         if (!Get::has('auth')) {
             return null;
@@ -86,26 +88,35 @@ class XnetSession extends XorgSession
             return null;
         }
         Get::kill('auth');
-        S::set('auth', AUTH_MDP);
-        return User::getSilentWithValues(null, array('user_id' => Get::i('uid')));
+        S::set('auth', AUTH_PASSWD);
+        return User::getSilentWithValues(null, array('uid' => Get::i('uid')));
     }
 
     protected function startSessionAs($user, $level)
     {
+        // The user must have 'groups' permission to access X.net
+        if (!$user->checkPerms('groups')) {
+            return false;
+        }
+
         if ($level == AUTH_SUID) {
-            S::set('auth', AUTH_MDP);
+            S::set('auth', AUTH_PASSWD);
         }
-        $res  = XDB::query("SELECT  a.uid, a.hruid, a.display_name, a.full_name,
-                                    a.sex = 'female' AS femme,
-                                    a.email_format, a.token,
-                                    at.perms, a.is_admin
-                              FROM  accounts AS a
-                        INNER JOIN  account_types AS at ON (at.type = a.type)
-                             WHERE  a.uid = {?} AND a.state = 'active'
-                             LIMIT  1", $user->id());
-        $sess = $res->fetchOneAssoc();
-        $_SESSION = array_merge($_SESSION, $sess);
-        $this->makePerms(S::s('perms'), S::b('is_admin'));
+
+        S::set('uid', $user->uid);
+        S::set('hruid', $user->hruid);
+
+        // XXX: Transition code, should not be in session anymore
+        S::set('display_name', $user->display_name);
+        S::set('full_name', $user->full_name);
+        S::set('femme', $user->isFemale());
+        S::set('email_format', $user->email_format);
+        S::set('token', $user->token);
+        S::set('perms', $user->perms);
+        S::set('is_admin', $user->is_admin);
+
+        // Add the 'user' perms to the user.
+        $this->makePerms($user->perms, $user->is_admin);
         S::kill('challenge');
         S::kill('loginX');
         S::kill('may_update');
@@ -126,7 +137,7 @@ class XnetSession extends XorgSession
         if (!$this->startSUID($user)) {
             return false;
         }
-        S::set('perms', User::makePerms('user'));
+        S::set('perms', User::makePerms("groups"));
         return true;
     }
 
@@ -166,7 +177,7 @@ function may_update($force = false, $lose = false)
         $may_update[$asso_id] = true;
     } elseif (!isset($may_update[$asso_id]) || $force) {
         $res = XDB::query("SELECT  perms
-                             FROM  groupex.membres
+                             FROM  group_members
                             WHERE  uid={?} AND asso_id={?}",
                           S::v('uid'), $asso_id);
         $may_update[$asso_id] = ($res->fetchOneCell() == 'admin');
@@ -198,7 +209,7 @@ function is_member($force = false, $lose = false)
         $is_member[$asso_id] = true;
     } elseif (!isset($is_member[$asso_id]) || $force) {
         $res = XDB::query("SELECT  COUNT(*)
-                             FROM  groupex.membres
+                             FROM  group_members
                             WHERE  uid={?} AND asso_id={?}",
                 S::v('uid'), $asso_id);
         $is_member[$asso_id] = ($res->fetchOneCell() == 1);
@@ -207,5 +218,5 @@ function is_member($force = false, $lose = false)
 }
 
 // }}}
-// 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:
 ?>