Index: trunk/expressoAdmin1_2/inc/class.db_functions.inc.php
===================================================================
--- trunk/expressoAdmin1_2/inc/class.db_functions.inc.php	(revision 2)
+++ trunk/expressoAdmin1_2/inc/class.db_functions.inc.php	(revision 9)
@@ -503,4 +503,17 @@
 		return $serverList;
 	}
+	
+	function get_apps($account_lid)
+	{
+		$this->db->query("SELECT * FROM phpgw_expressoadmin_apps WHERE manager_lid = '".$account_lid."'");
+		
+		while($this->db->next_record())
+		{
+			$tmp = $this->db->row();
+			$availableApps[$tmp['app']] = 'run'; 
+		}
+			
+		return $availableApps;
+	}
 }
 ?>
Index: trunk/expressoAdmin1_2/inc/class.uiaccounts.inc.php
===================================================================
--- trunk/expressoAdmin1_2/inc/class.uiaccounts.inc.php	(revision 2)
+++ trunk/expressoAdmin1_2/inc/class.uiaccounts.inc.php	(revision 9)
@@ -26,4 +26,5 @@
 		var $functions;
 		var $current_config;
+		var $ldap_functions;
 
 		function uiaccounts()
@@ -32,4 +33,5 @@
 			$this->nextmatchs	= CreateObject('phpgwapi.nextmatchs');
 			$this->functions	= CreateObject('expressoAdmin1_2.functions');
+			$this->ldap_functions = CreateObject('expressoAdmin1_2.ldap_functions');
 			
 			$c = CreateObject('phpgwapi.config','expressoAdmin1_2');
@@ -299,4 +301,5 @@
 			$tmp = $this->functions->read_acl($manager_account_lid);
 			$manager_context = $tmp[0]['context'];
+			$alert_warning = '';
 			
 			// Verifica se tem acesso a este modulo
@@ -339,5 +342,4 @@
 			$GLOBALS['phpgw']->js->validate_file("jscode","users","expressoAdmin1_2");
 			$GLOBALS['phpgw']->js->set_onload("get_available_groups(document.forms[0].context.value);");
-			//$GLOBALS['phpgw']->js->set_onload("get_sectors(document.forms[0].context.value, '".$user_info['context']."','".$user_info['departmentnumber']."');");
 			$GLOBALS['phpgw']->js->set_onload("get_available_maillists(document.forms[0].context.value);");
 			$GLOBALS['phpgw']->js->set_onload("use_samba_attrs('".$user_info['sambaUser']."');");
@@ -368,5 +370,18 @@
 				foreach ($array_groups as $gidnumber=>$cn)
 				{
-					$ea_select_user_groups_options .= "<option value=" . $gidnumber . ">" . $cn . "</option>";
+					
+					// O memberUid do usuário está somente no Banco, então adicionamos o memberUid no Ldap.
+					if (is_null($user_info['groups_ldap'][$gidnumber]))
+					{
+						$this->ldap_functions->add_user2group($gidnumber, $user_info['uid']);
+						$ea_select_user_groups_options .= "<option value=" . $gidnumber . ">" . $cn . " [Incluido memberUid no Ldap]</option>";
+						
+						if ($alert_warning == '')
+							$alert_warning = "O expressoAdmin corrigiu as seguintes inconsistências:\\n";
+						$alert_warning .= "Incluido atributo memberUid do usuário no grupo $cn.\\n";
+					}
+					else
+						$ea_select_user_groups_options .= "<option value=" . $gidnumber . ">" . $cn . "3</option>";
+					
 					if ($gidnumber == $user_info['gidnumber'])
 					{
@@ -378,4 +393,17 @@
 					}
 				}
+				
+				// O memberUid do usuário está somente no Ldap, então removemos ele.
+				$groups_db = array_flip($user_info['groups']);
+				foreach ($user_info['groups_ldap'] as $gidnumber=>$cn)
+				{
+					if (is_null($groups_db[$gidnumber]))
+					{
+						$this->ldap_functions->remove_user2group($gidnumber, $user_info['uid']);
+						if ($alert_warning == '')
+							$alert_warning = "O expressoAdmin corrigiu as seguintes inconsistências:\\n";
+						$alert_warning .= "Removido atributo memberUid do usuário do grupo $cn.\\n";
+					}
+				}	
 			}
 			
@@ -412,10 +440,7 @@
 			}
 
-			/*
-			echo '<pre>';
-			_debug_array($user_info);
-			echo '</pre>';
-			*/
-			
+			if ($alert_warning != '')
+				$alert_warning = "alert('". $alert_warning ."')";
+
 			$var = Array(
 				'uidnumber'					=> $_GET['account_id'],
@@ -438,4 +463,5 @@
 				
 				// First ABA
+				'alert_warning'					=> "$alert_warning",
 				'display_input_account_lid'		=> 'display:none',
 				'lang_general_information'		=> lang('General Information'),
Index: trunk/expressoAdmin1_2/inc/class.user.inc.php
===================================================================
--- trunk/expressoAdmin1_2/inc/class.user.inc.php	(revision 2)
+++ trunk/expressoAdmin1_2/inc/class.user.inc.php	(revision 9)
@@ -655,10 +655,22 @@
 				{
 					$this->db_functions->add_id2apps($new_values['uidnumber'], $add_apps);
-					$this->db_functions->write_log("adicionado app ao usuario",'',$dn,'','');
+
+					foreach ($add_apps as $app => $index)
+						$this->db_functions->write_log("Adicionado aplicativo $app ao usuário $dn",'',$dn,'','');
 				}
 				if (count($remove_apps)>0)
 				{
-					$this->db_functions->remove_id2apps($new_values['uidnumber'], $remove_apps);
-					$this->db_functions->write_log("removido app do usuario",'',$dn,'','');
+					//Verifica se o gerente tem acesso a aplicação antes de remove-la do usuario.
+					$manager_apps = $this->db_functions->get_apps($_SESSION['phpgw_session']['session_lid']);
+					
+					foreach ($remove_apps as $app => $app_index)
+					{
+						if ($manager_apps[$app] == 'run')
+							$remove_apps2[$app] = $app_index;
+					}
+					$this->db_functions->remove_id2apps($new_values['uidnumber'], $remove_apps2);
+					
+					foreach ($remove_apps2 as $app => $access)
+						$this->db_functions->write_log("Removido aplicativo $app do usuário $dn",'',$dn,'','');
 				}
 				//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Index: trunk/expressoAdmin1_2/inc/class.group.inc.php
===================================================================
--- trunk/expressoAdmin1_2/inc/class.group.inc.php	(revision 2)
+++ trunk/expressoAdmin1_2/inc/class.group.inc.php	(revision 9)
@@ -282,14 +282,30 @@
 				}
 			}
-			$add_apps    = array_diff($new_values2, $old_values2);
-			$remove_apps = array_diff($old_values2, $new_values2);
+			
+			$add_apps    = array_flip(array_diff($new_values2, $old_values2));
+			$remove_apps = array_flip(array_diff($old_values2, $new_values2));
 
 			if (count($add_apps)>0)
 			{
-				$this->db_functions->add_id2apps($new_values['gidnumber'], array_flip($add_apps));
-			}
+				$this->db_functions->add_id2apps($new_values['gidnumber'], $add_apps);
+				
+				foreach ($add_apps as $app => $index)
+					$this->db_functions->write_log("Adicionado aplicativo $app ao grupo $dn",'',$dn,'','');
+			}
+			
 			if (count($remove_apps)>0)
 			{
-				$this->db_functions->remove_id2apps($new_values['gidnumber'], array_flip($remove_apps));
+				//Verifica se o gerente tem acesso a aplicação antes de remove-la do usuario.
+				$manager_apps = $this->db_functions->get_apps($_SESSION['phpgw_session']['session_lid']);
+					
+				foreach ($remove_apps as $app => $app_index)
+				{
+					if ($manager_apps[$app] == 'run')
+						$remove_apps2[$app] = $app_index;
+				}
+				$this->db_functions->remove_id2apps($new_values['gidnumber'], $remove_apps2);
+					
+				foreach ($remove_apps2 as $app => $access)
+					$this->db_functions->write_log("Removido aplicativo $app do grupo $dn",'',$dn,'','');
 			}
 			//////////////////////////////////////////////////////////////////////////////////////////////////////////////////			
Index: trunk/expressoAdmin1_2/inc/class.ldap_functions.inc.php
===================================================================
--- trunk/expressoAdmin1_2/inc/class.ldap_functions.inc.php	(revision 2)
+++ trunk/expressoAdmin1_2/inc/class.ldap_functions.inc.php	(revision 9)
@@ -355,8 +355,4 @@
 		$group_dn = $entry[0]['dn'];
 		$attrs['memberUid'] = $memberUid;
-		
-		/*echo 'usuarios recebidos para adicao no ldap';
-		echo '<pre>';
-		print_r($memberUid);*/
 		
 		$res = @ldap_mod_add($this->ldap, $group_dn, $attrs);
@@ -527,4 +523,15 @@
     	}
     	
+    	// Groups
+		$justthese = array("gidnumber","cn");
+		$filter="(&(phpgwAccountType=g)(memberuid=".$result['uid']."))";
+		$search = ldap_search($this->ldap, $context, $filter, $justthese);
+    	ldap_sort($this->ldap, $search, "cn");
+    	$entries = ldap_get_entries($this->ldap, $search);
+    	for ($i=0; $i<$entries['count']; $i++)
+    	{
+    		$result['groups_ldap'][ $entries[$i]['gidnumber'][0] ] = $entries[$i]['cn'][0];
+    	}
+    	
 		return $result;		
 	}
