Index: ajax/dropdownUsers.php
===================================================================
--- ajax/dropdownUsers.php	(révision 7477)
+++ ajax/dropdownUsers.php	(copie de travail)
@@ -65,6 +65,9 @@
 		$used=unserialize(stripslashes($_POST['used']));
 	}
 }
+if (isset($_POST["entity_restrict"])&&!is_numeric($_POST["entity_restrict"])&&!is_array($_POST["entity_restrict"])){
+	$_POST["entity_restrict"]=unserialize(stripslashes($_POST["entity_restrict"]));
+}
 
 $result=dropdownUsersSelect(false, $_POST['right'], $_POST["entity_restrict"], $_POST['value'], $used, $_POST['searchText']);
 
Index: front/group.form.php
===================================================================
--- front/group.form.php	(révision 7477)
+++ front/group.form.php	(copie de travail)
@@ -43,7 +43,7 @@
 $group=new Group;
 if (isset($_POST["add"]))
 {
-	checkRight("group","w");
+	$group->check(-1,'w',$_POST['FK_entities']);
 
 	$newID=$group->add($_POST);
 	logEvent($newID, "groups", 4, "setup", $_SESSION["glpiname"]." ".$LANG["log"][20]." ".$_POST["name"].".");
@@ -51,7 +51,7 @@
 }
 else if (isset($_POST["delete"]))
 {
-	checkRight("group","w");
+	$group->check($_POST["ID"],'w');
 
 	$group->delete($_POST);
 	logEvent($_POST["ID"], "groups", 4, "setup", $_SESSION["glpiname"]." ".$LANG["log"][22]);
@@ -59,7 +59,7 @@
 }
 else if (isset($_POST["update"]))
 {
-	checkRight("group","w");
+	$group->check($_POST["ID"],'w');
 
 	$group->update($_POST);
 	logEvent($_POST["ID"], "groups", 4, "setup", $_SESSION["glpiname"]." ".$LANG["log"][21]);
@@ -67,7 +67,7 @@
 }
 else if (isset($_POST["adduser"]))
 {
-	checkRight("group","w");
+	$group->check($_POST["FK_groups"],'w');
 
 	addUserGroup($_POST["FK_users"],$_POST["FK_groups"]);
 
@@ -76,7 +76,7 @@
 }
 else if (isset($_POST["deleteuser"]))
 {
-	checkRight("group","w");
+	$group->check($_POST["FK_groups"],'w');
 
 	if (count($_POST["item"]))
 		foreach ($_POST["item"] as $key => $val)
@@ -87,7 +87,7 @@
 }
 else
 {
-	checkRight("group","r");
+	$group->check($_GET["ID"],'r');
 
 	if (!isset($_SESSION['glpi_onglet'])) $_SESSION['glpi_onglet']=1;
 	if (isset($_GET['onglet'])) {
Index: front/massiveaction.php
===================================================================
--- front/massiveaction.php	(révision 7477)
+++ front/massiveaction.php	(copie de travail)
@@ -228,7 +228,7 @@
 					} else { // Not infocoms
 						$ci=new CommonItem();
 						$ci->setType($_POST["device_type"],1);
-						$link_entity_type=-1;
+						$link_entity_type=array();
 						// Specific entity item
 						
 						if ($searchopt[$_POST["id_field"]]["table"]!=$LINK_ID_TABLE[$_POST["device_type"]]
@@ -240,7 +240,11 @@
 		
 							if ($ci2->getFromDB($_POST[$_POST["field"]])){
 								if (isset($ci2->fields["FK_entities"])&&$ci2->fields["FK_entities"]>=0){
-									$link_entity_type=$ci2->fields["FK_entities"];
+									if (isset($ci2->fields["recursive"])&&$ci2->fields["recursive"]){
+										$link_entity_type=getEntitySons($ci2->fields["FK_entities"]);
+									} else {
+										$link_entity_type[]=$ci2->fields["FK_entities"];
+									}
 								}
 		
 							}
@@ -249,11 +253,11 @@
 						foreach ($_POST["item"] as $key => $val){
 							if ($val==1) {
 								if ($ci->getFromDB($_POST["device_type"],$key)){
-									if ($link_entity_type<0
-									||$link_entity_type==$ci->obj->fields["FK_entities"]){
+									if (count($link_entity_type)==0
+										|| in_array($ci->obj->fields["FK_entities"], $link_entity_type)){
 										$ci->obj->update(array("ID"=>$key,$_POST["field"] => $_POST[$_POST["field"]]));
 									}
-									// else error_log("Access denied for update (not infocom)");
+									// else error_log("Access denied for update (not infocom) ci->obj->fields[FK_entities]=".$ci->obj->fields["FK_entities"]."  link_entity_type=".implode(",", $link_entity_type));
 								} 
 							}
 						}
Index: config/define.php
===================================================================
--- config/define.php	(révision 7477)
+++ config/define.php	(copie de travail)
@@ -362,5 +362,5 @@
 	$CFG_GLPI["union_search_type"]=array(RESERVATION_TYPE=>"reservation_types",STATE_TYPE=>"state_types");
 
 	$CFG_GLPI["recursive_type"]=array(CONTACT_TYPE=>"glpi_contacts", ENTERPRISE_TYPE=>"glpi_enterprises", CONTRACT_TYPE=>"glpi_contracts", 
-		DOCUMENT_TYPE=>"glpi_docs", KNOWBASE_TYPE=>"glpi_kbitems");
-?>
\ No newline at end of file
+		DOCUMENT_TYPE=>"glpi_docs", KNOWBASE_TYPE=>"glpi_kbitems", GROUP_TYPE => "glpi_groups");
+?>
Index: install/mysql/glpi-0.71-empty.sql
===================================================================
--- install/mysql/glpi-0.71-empty.sql	(révision 7477)
+++ install/mysql/glpi-0.71-empty.sql	(copie de travail)
@@ -1424,6 +1424,7 @@
 CREATE TABLE `glpi_groups` (
   `ID` int(11) NOT NULL auto_increment,
   `FK_entities` int(11) NOT NULL default '0',
+  `recursive` tinyint(1) NOT NULL default '0',
   `name` varchar(255) collate utf8_unicode_ci default NULL,
   `comments` text collate utf8_unicode_ci,
   `FK_users` int(11) NOT NULL default '0',
Index: install/update_071_0712.php
===================================================================
--- install/update_071_0712.php	(révision 7477)
+++ install/update_071_0712.php	(copie de travail)
@@ -46,5 +46,11 @@
 	$query="UPDATE glpi_rules_actions SET field='_ignore_ocs_import' WHERE action_type='ignore'"	;
 	$DB->query($query) or die("0.71.2 Update ignore field for soft dict ".$LANG["update"][90].$DB->error());
 
+        if (!FieldExists("glpi_groups", "recursive")) {
+                       $query = "ALTER TABLE `glpi_groups` ADD `recursive` TINYINT( 1 ) NOT NULL DEFAULT '0' AFTER `FK_entities`;";
+                       $DB->query($query) or die("0.71 add recursive in glpi_groups" . $LANG["update"][90] . $DB->error());
+       }
+
+
 } // fin 0.71 #####################################################################################
 ?>
Index: inc/user.function.php
===================================================================
--- inc/user.function.php	(révision 7477)
+++ inc/user.function.php	(copie de travail)
@@ -192,51 +192,29 @@
 	if (!haveRight("user","r")||!haveRight("group","r"))	return false;
 
 	$canedit=haveRight("user","w");
-	$strict_entities=getUserEntities($ID,false);
+	$strict_entities=getUserEntities($ID,true);
 	if (!haveAccessToOneOfEntities($strict_entities)&&!isViewAllEntities()){
 		$canedit=false;
 	}
 
 	$nb_per_line=3;
-	if ($canedit) $headerspan=$nb_per_line*2;
-	else $headerspan=$nb_per_line;
-
-	echo "<form name='groupuser_form' id='groupuser_form' method='post' action=\"$target\">";
-
-	if ($canedit){
-		echo "<div class='center'>";
-		echo "<table  class='tab_cadre_fixe'>";
-
-		echo "<tr class='tab_bg_1'><th colspan='2'>".$LANG["setup"][604]."</tr><tr><td class='tab_bg_2' align='center'>";
-		echo "<input type='hidden' name='FK_users' value='$ID'>";
-		$query="SELECT glpi_groups.ID, glpi_groups.name, glpi_entities.completename FROM glpi_groups LEFT JOIN glpi_entities ON (glpi_groups.FK_entities=glpi_entities.ID) WHERE glpi_groups.FK_entities IN (SELECT FK_entities FROM glpi_users_profiles WHERE FK_users = '$ID')";
-		$result=$DB->query($query);
-		if ($DB->numrows($result)>0){
-			$groups=array();
-			while ($data=$DB->fetch_array($result)){
-				$groups[$data['ID']]=$data['name'];
-				if (!empty($data['completename'])){
-					$groups[$data['ID']].= ' - '.$data['completename'];
-				}
-			}
-			dropdownArrayValues("FK_groups",$groups);
-
-		}
-		echo "</td><td align='center' class='tab_bg_2'>";
-		echo "<input type='submit' name='addgroup' value=\"".$LANG["buttons"][8]."\" class='submit'>";
-		echo "</td></tr>";
-
-		echo "</table></div><br>";
+	if ($canedit) {
+		$headerspan=$nb_per_line*2;	
+		echo "<form name='groupuser_form' id='groupuser_form' method='post' action=\"$target\">";
+	} else {
+		$headerspan=$nb_per_line;
 	}
 
 	echo "<div class='center'><table class='tab_cadrehov'><tr><th colspan='$headerspan'>".$LANG["Menu"][36]."</th></tr>";
 	$query="SELECT glpi_groups.*, glpi_users_groups.ID AS IDD,glpi_users_groups.ID as linkID from glpi_users_groups LEFT JOIN glpi_groups ON (glpi_groups.ID = glpi_users_groups.FK_groups) WHERE glpi_users_groups.FK_users='$ID' ORDER BY glpi_groups.name";
 
 	$result=$DB->query($query);
+	$used=array();
 	if ($DB->numrows($result)>0){
 		$i=0;
 
 		while ($data=$DB->fetch_array($result)){
+			$used[]=$data["ID"];
 			if ($i%$nb_per_line==0) {
 				if ($i!=0) echo "</tr>";
 				echo "<tr class='tab_bg_1'>";
@@ -261,6 +239,8 @@
 			$i++;
 		}
 		echo "</tr>";
+	} else {
+		echo "<tr class='tab_bg_1'><td colspan='$headerspan' class='center'>".$LANG["common"][49]."</td></tr>";
 	}
 
 	echo "</table></div>";
@@ -465,7 +445,7 @@
 				$entities[]=$data['FK_entities'];
 			}
 		}
-		return $entities;
+		return array_unique($entities);
 	} 
 
 	return array();
Index: inc/group.function.php
===================================================================
--- inc/group.function.php	(révision 7477)
+++ inc/group.function.php	(copie de travail)
@@ -47,19 +47,22 @@
 	global $DB,$CFG_GLPI, $LANG,$LINK_ID_TABLE,$INFOFORM_PAGES;
 
 	$ci=new CommonItem();
-	echo "<div class='center'><table class='tab_cadre'><tr><th>".$LANG["common"][17]."</th><th>".$LANG["common"][16]."</th></tr>";
+	echo "<div class='center'><table class='tab_cadre'><tr><th>".$LANG["common"][17]."</th>" .
+			"<th>".$LANG["common"][16]."</th><th>".$LANG["entity"][0]."</th></tr>";
 	foreach ($CFG_GLPI["linkuser_types"] as $type){
-		$query="SELECT * from ".$LINK_ID_TABLE[$type]." WHERE FK_groups='$ID'";
+		$query="SELECT * from ".$LINK_ID_TABLE[$type]." WHERE FK_groups='$ID' " .
+			getEntitiesRestrictRequest(" AND ", $LINK_ID_TABLE[$type], '', '', isset($CFG_GLPI["recursive_type"][$type]));
 		$result=$DB->query($query);
 		if ($DB->numrows($result)>0){
 			$ci->setType($type);
 			$type_name=$ci->getType();
 			$cansee=haveTypeRight($type,"r");
 			while ($data=$DB->fetch_array($result)){
-				$link=$data["name"];
+				$link=($data["name"] ? $data["name"] : "(".$data["ID"].")");
 				if ($cansee) $link="<a href='".$CFG_GLPI["root_doc"]."/".$INFOFORM_PAGES[$type]."?ID=".$data["ID"]."'>".$link."</a>";
 				$linktype="";
-				echo "<tr class='tab_bg_1'><td>$type_name</td><td>$link</td></tr>";
+				echo "<tr class='tab_bg_1'><td>$type_name</td><td>$link</td>";
+				echo "<td>".getDropdownName("glpi_entities",$data['FK_entities'])."</td></tr>";
 			}
 		}
 
@@ -78,22 +81,22 @@
 
 	if (!haveRight("user","r")||!haveRight("group","r"))	return false;
 
-	$canedit=haveRight("group","w");
-
-	$nb_per_line=3;
-	if ($canedit) $headerspan=$nb_per_line*2;
-	else $headerspan=$nb_per_line;
-
-
 	$group=new Group();
 
 	if ($group->getFromDB($ID)){
+		$canedit=$group->can($ID,"w");
 	
+		$nb_per_line=3;
 		if ($canedit) {
+			$headerspan=$nb_per_line*2;	
 			echo "<form name='groupuser_form' id='groupuser_form' method='post' action=\"$target\">";
+		} else {
+			$headerspan=$nb_per_line;
 		}
+	
 		echo "<div class='center'><table class='tab_cadrehov'><tr><th colspan='$headerspan'>".$LANG["Menu"][14]."</th></tr>";
-		$query="SELECT glpi_users.*,glpi_users_groups.ID as linkID from glpi_users_groups LEFT JOIN glpi_users ON (glpi_users.ID = glpi_users_groups.FK_users) WHERE glpi_users_groups.FK_groups='$ID' ORDER BY glpi_users.name, glpi_users.realname, glpi_users.firstname";
+		$query="SELECT glpi_users.*,glpi_users_groups.ID as linkID from glpi_users_groups LEFT JOIN glpi_users ON (glpi_users.ID = glpi_users_groups.FK_users) WHERE glpi_users_groups.FK_groups='$ID' " .
+				"ORDER BY glpi_users.name, glpi_users.realname, glpi_users.firstname";
 	
 		$used = array();
 
@@ -145,14 +148,22 @@
 			echo "</table>";
 			echo "</div>";
 
-			$res=dropdownUsersSelect (true, "all", $group->fields["FK_entities"], 0, $used);
+			if ($group->fields["recursive"]) {
+				$res=dropdownUsersSelect (true, "all", getEntitySons($group->fields["FK_entities"]), 0, $used);
+			} else {
+				$res=dropdownUsersSelect (true, "all", $group->fields["FK_entities"], 0, $used);
+			}		
 			$nb=($res ? $DB->result($res,0,"CPT") : 0);
 			
 			if ($nb) {		
 				echo "<div class='center'>";
 				echo "<table  class='tab_cadre_fixe'>";
 				echo "<tr class='tab_bg_1'><th colspan='2'>".$LANG["setup"][603]."</tr><tr><td class='tab_bg_2' align='center'>";
-				dropdownUsers("FK_users",0,"all",-1,1,$group->fields["FK_entities"],0,$used);
+				if ($group->fields["recursive"]) {
+					dropdownUsers("FK_users",0,"all",-1,1,getEntitySons($group->fields["FK_entities"]),0,$used);
+				} else {
+					dropdownUsers("FK_users",0,"all",-1,1,$group->fields["FK_entities"],0,$used);
+				}
 				//dropdownAllUsers("FK_users",0,1,$group->fields["FK_entities"],0,$used);
 				echo "</td><td align='center' class='tab_bg_2'>";
 				echo "<input type='submit' name='adduser' value=\"".$LANG["buttons"][8]."\" class='submit'>";
Index: inc/dropdown.function.php
===================================================================
--- inc/dropdown.function.php	(révision 7477)
+++ inc/dropdown.function.php	(copie de travail)
@@ -310,7 +310,7 @@
 /**
  * Execute the query to select box with all glpi users where select key = name
  * 
- * Internaly used by dropdownUsersSelect and ajax/dropdownUsers.php
+ * Internaly used by showGroupUsers, dropdownUsers and ajax/dropdownUsers.php
  *
  * @param $count true if execute an count(*), 
  * @param $right limit user who have specific right
Index: inc/auth.function.php
===================================================================
--- inc/auth.function.php	(révision 7477)
+++ inc/auth.function.php	(copie de travail)
@@ -677,7 +677,7 @@
 
 	$_SESSION["glpigroups"] = array ();
 
-
+/*
 	$first=true;
 	$entities="";
 	foreach ($_SESSION['glpiactiveentities'] as $val){
@@ -685,10 +685,11 @@
 		else $first=false;
 		$entities.=$val;
 	}			
+*/
 
+	$query_gp = "SELECT FK_groups FROM glpi_users_groups LEFT JOIN glpi_groups ON (glpi_users_groups.FK_groups = glpi_groups.ID) WHERE glpi_users_groups.FK_users='" . $_SESSION['glpiID'] . "' " .
+			getEntitiesRestrictRequest(" AND ","glpi_groups","FK_entities",$_SESSION['glpiactiveentities'],true);
 
-	$query_gp = "SELECT FK_groups FROM glpi_users_groups LEFT JOIN glpi_groups ON (glpi_users_groups.FK_groups = glpi_groups.ID) WHERE glpi_users_groups.FK_users='" . $_SESSION['glpiID'] . "' AND glpi_groups.FK_entities IN ($entities)";
-
 	$result_gp = $DB->query($query_gp);
 	if ($DB->numrows($result_gp)) {
 		while ($data = $DB->fetch_array($result_gp)) {
Index: inc/group.class.php
===================================================================
--- inc/group.class.php	(révision 7477)
+++ inc/group.class.php	(copie de travail)
@@ -47,6 +47,8 @@
 	function Group () {
 		$this->table="glpi_groups";
 		$this->type=GROUP_TYPE;
+		$this->entity_assign=true;
+		$this->may_be_recursive=true;
 	}
 
 	function cleanDBonPurge($ID) {
@@ -95,23 +97,25 @@
 		$con_spotted=false;
 
 		if (empty($ID)) {
-
-			if($this->getEmpty()) $con_spotted = true;
+			$con_spotted = ($this->getEmpty() && $this->can(-1,'w'));
 		} else {
-			if($this->getFromDB($ID)&&haveAccessToEntity($this->fields["FK_entities"])) $con_spotted = true;
+			$con_spotted = $this->can($ID,'r');
 		}
 
 		if ($con_spotted){
+			$canedit=$this->can($ID,'w');
 
 			$this->showOnglets($ID, $withtemplate,$_SESSION['glpi_onglet']);
 
-			echo "<form method='post' name=form action=\"$target\"><div class='center'>";
-			if (empty($ID)){
-				echo "<input type='hidden' name='FK_entities' value='".$_SESSION["glpiactive_entity"]."'>";
+			if ($canedit) {
+				echo "<form method='post' name=form action=\"$target\"><div class='center'>";
+				if (empty($ID)){
+					echo "<input type='hidden' name='FK_entities' value='".$_SESSION["glpiactive_entity"]."'>";
+				}
 			}
 
 			echo "<table class='tab_cadre_fixe' cellpadding='2' >";
-			echo "<tr><th colspan='2'>";
+			echo "<tr><th>";
 			if (empty($ID)) {
 				echo $LANG["setup"][605];
 
@@ -121,7 +125,18 @@
 			if (isMultiEntitiesMode()){
 				echo "&nbsp;(".getDropdownName("glpi_entities",$this->fields["FK_entities"]).")";
 			}
-
+			echo "</th><th>";
+			if (isMultiEntitiesMode()){
+				echo $LANG["entity"][9].":&nbsp;";
+			
+				if ($this->can($ID,'recursive')) {
+					dropdownYesNo("recursive",$this->fields["recursive"]);					
+				} else {
+					echo getYesNo($this->fields["recursive"]);
+				}
+			} else {
+				echo "&nbsp;";
+			}
 			echo "</th></tr>";
 
 			echo "<tr><td class='tab_bg_1' valign='top'>";
@@ -135,6 +150,8 @@
 
 			echo "<tr><td>".$LANG["common"][64].":	</td>";
 			echo "<td>";
+			// Manager must be in the same entity
+			// TODO for a recursive group the manager need to have a recursive right ?
 			dropdownUsers('FK_users',$this->fields["FK_users"],'all',0,1,$this->fields["FK_entities"]);
 			echo "</td></tr>";
 
@@ -176,7 +193,7 @@
 			echo "</td>";
 			echo "</tr>";
 
-			if (haveRight("group","w")) 
+			if ($canedit) {
 				if ($ID=="") {
 
 					echo "<tr>";
@@ -185,7 +202,6 @@
 					echo "</td>";
 					echo "</tr>";
 
-
 				} else {
 
 					echo "<tr>";
@@ -200,7 +216,10 @@
 					echo "</tr>";
 
 				}
-			echo "</table></div></form>";
+				echo "</table></div></form>";
+			} else {
+				echo "</table></div>";
+			}
 
 		} else {
 			echo "<div class='center'><strong>".$LANG["common"][54]."</strong></div>";
