From 5219595e1d160ccb40cc7e04b91975f5e5ad3160 Mon Sep 17 00:00:00 2001
From: Jean-Louis Ercolani <jeanlouis.ercolani@maarch.org>
Date: Fri, 15 Apr 2011 15:48:44 +0000
Subject: [PATCH] Fix : #174 (lgi, cfi, jle)

---
 core/trunk/core/class/ServiceControler.php | 51 +++++++++++-----------
 core/trunk/core/class/class_core_tools.php |  2 +
 core/trunk/core/class/class_db.php         |  1 +
 core/trunk/core/class/class_security.php   | 17 +++++++-
 4 files changed, 44 insertions(+), 27 deletions(-)

diff --git a/core/trunk/core/class/ServiceControler.php b/core/trunk/core/class/ServiceControler.php
index cc852f2abdc..8415c865da5 100644
--- a/core/trunk/core/class/ServiceControler.php
+++ b/core/trunk/core/class/ServiceControler.php
@@ -41,6 +41,8 @@ define("_CODE_INCREMENT",1);
 try {
 	require_once("core/class/Service.php");
 	require_once("core".DIRECTORY_SEPARATOR."class".DIRECTORY_SEPARATOR."usergroups_controler.php");
+	require_once("core".DIRECTORY_SEPARATOR."class".DIRECTORY_SEPARATOR."users_controler.php");
+	require_once 'core/core_tables.php';
 } catch (Exception $e){
 	echo $e->getMessage().' // ';
 }
@@ -115,40 +117,37 @@ class ServiceControler
 	public function loadUserServices($user_id)
 	{
 		$services = array();
-		$ugc = new usergroups_controler();
+		
+		// #TODO : Au lieu de partir des services, partir plutot des groupes de l'utilisateur et récuperer tous les services 
+		// associés aux groupes
 		if($user_id == "superadmin")
 		{
 			$services = self::getAllServices();
 		}
 		else
 		{
-			for($i=0; $i< count($_SESSION['enabled_services']);$i++)
-			{
-				if($_SESSION['enabled_services'][$i]['system'] == true )
-				{
+			$tmpServices = array();
+			for ($i = 0; $i < count($_SESSION['enabled_services']); $i ++) {
+				if ($_SESSION['enabled_services'][$i]['system'] == true ) {
 					$services[$_SESSION['enabled_services'][$i]['id']] = true;
+				} else {
+					$tmpServices[] = $_SESSION['enabled_services'][$i]['id'];
 				}
-				else
-				{
-					self::connect();
-					self::$db->query("select group_id from ".self::$usergroups_services_table." where service_id = '".$_SESSION['enabled_services'][$i]['id']."'");
-					$find = false;
-					while($res = self::$db->fetch_object())
-					{
-						if($ugc->inGroup($user_id, $res->group_id) == true)
-						{
-							$find = true;
-							break;
-						}
-					}
-					if($find == true)
-					{
-						$services[$_SESSION['enabled_services'][$i]['id']] = true;
-					}
-					else
-					{
-						$services[$_SESSION['enabled_services'][$i]['id']] = false;
-					}
+			}
+			$ugc = new usergroups_controler();
+			self::connect();
+			self::$db->query(
+				'select distinct us.service_id from ' . USERGROUPS_SERVICES_TABLE
+				. ' us, ' . USERGROUP_CONTENT_TABLE 
+				. " uc where us.group_id = uc.group_id and uc.user_id = '". $user_id . "'"
+			);
+			
+			while($res = self::$db->fetch_object()) {
+				$serviceId = $res->service_id;
+				if (in_array($serviceId, $tmpServices)) {
+					$services[$serviceId] = true;
+				} else {
+					$services[$serviceId] = false;
 				}
 			}
 		}
diff --git a/core/trunk/core/class/class_core_tools.php b/core/trunk/core/class/class_core_tools.php
index bdef8a41dcb..a466e193034 100644
--- a/core/trunk/core/class/class_core_tools.php
+++ b/core/trunk/core/class/class_core_tools.php
@@ -198,6 +198,7 @@ class core_tools extends functions
             $path_module_tools = 'modules' . DIRECTORY_SEPARATOR
                 . $modules[$i]['moduleid'] . DIRECTORY_SEPARATOR . 'class'
                 . DIRECTORY_SEPARATOR . 'class_modules_tools.php';
+				//echo "<br/>".$modules[$i]['moduleid']."<br/>";
             if (file_exists($path_module_tools)) {
                 require_once $path_module_tools;
                 $modules_tools = new $modules[$i]['moduleid'];
@@ -208,6 +209,7 @@ class core_tools extends functions
                     $modules_tools->load_module_var_session($userData);
                 }
             }
+			//$this->show_array($_SESSION['user']['baskets']);
         }
     }
 
diff --git a/core/trunk/core/class/class_db.php b/core/trunk/core/class/class_db.php
index f5c074aaf2c..2fb5409b805 100644
--- a/core/trunk/core/class/class_db.php
+++ b/core/trunk/core/class/class_db.php
@@ -343,6 +343,7 @@ class dbquery extends functions
         } else {
             $this->query = false;
         }
+		//$this->show();
         if ((($this->_databasetype == 'ORACLE' && $this->statement == false)
             || ($this->_databasetype <> 'ORACLE' && $this->query == false))
             && ! $catchError
diff --git a/core/trunk/core/class/class_security.php b/core/trunk/core/class/class_security.php
index b3d27a2bac8..e20d4635bba 100644
--- a/core/trunk/core/class/class_security.php
+++ b/core/trunk/core/class/class_security.php
@@ -93,6 +93,7 @@ class security extends dbquery
         $array = array();
         $error = '';
         $uc = new users_controler();
+		// #TODO : Not usefull anymore, loginmode field is always in users table
         //Compatibility test, if loginmode column doesn't exists, Maarch can't crash
         if ($this->test_column($_SESSION['tablename']['users'], 'loginmode')) {
             if ($method == 'activex') {
@@ -140,6 +141,7 @@ class security extends dbquery
                         'cookie_date', date('Y-m-d') . ' ' . date('H:m:i')
                     );
                 }
+				// #TODO : usefull ?
                 $uc->save($user, 'up');
                 setcookie(
                     'maarch', 'UserId=' . $array['UserId'] . '&key='
@@ -159,10 +161,23 @@ class security extends dbquery
                 $core_tools = new core_tools();
                 $business_app_tools->load_app_var_session($array);
                 $core_tools->load_var_session($_SESSION['modules'], $array);
+				
+				/************Temporary fix*************/ 
+				// #TODO : revoir les functions load_var_session dans class_modules_tools pour ne plus charger en session les infos
+				if (isset($_SESSION['user']['baskets'])) {
+					$array['baskets'] = $_SESSION['user']['baskets'];
+				}
+				if (isset($_SESSION['user']['entities'])) {
+					$array['entities'] = $_SESSION['user']['entities'];
+				}
+				if (isset($_SESSION['user']['primaryentity'])) {
+					$array['primaryentity'] = $_SESSION['user']['primaryentity'];
+				}
+				/*************************************/
                 $array['services'] = $serv_controler->loadUserServices(
                     $array['UserId']
                 );
-
+				
                 if ($_SESSION['history']['userlogin'] == 'true') {
                     //add new instance in history table for the user's connexion
                     $hist = new history();
-- 
GitLab