From 13ea9c91fdf0ebaf4ecc033386a77b43b3c52480 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Fri, 11 May 2012 12:47:15 +0200 Subject: [PATCH] Fix: Synchro with group not done when editing group only of a user. --- htdocs/core/class/ldap.class.php | 2 +- ...interface_50_modLdap_Ldapsynchro.class.php | 77 +++++++++++++++++++ htdocs/user/class/user.class.php | 4 + htdocs/user/class/usergroup.class.php | 6 +- 4 files changed, 85 insertions(+), 4 deletions(-) diff --git a/htdocs/core/class/ldap.class.php b/htdocs/core/class/ldap.class.php index 03929787560..e3b24e26e8a 100644 --- a/htdocs/core/class/ldap.class.php +++ b/htdocs/core/class/ldap.class.php @@ -508,7 +508,7 @@ class Ldap * * @param string $dn DN entry key * @param string $info Attributes array - * @param User $user Objet user that delete + * @param User $user Objet user that update * @param string $olddn Old DN entry key (before update) * @return int <0 if KO, >0 if OK */ diff --git a/htdocs/core/triggers/interface_50_modLdap_Ldapsynchro.class.php b/htdocs/core/triggers/interface_50_modLdap_Ldapsynchro.class.php index 2e2bc3642ab..ccc7d65af6d 100755 --- a/htdocs/core/triggers/interface_50_modLdap_Ldapsynchro.class.php +++ b/htdocs/core/triggers/interface_50_modLdap_Ldapsynchro.class.php @@ -21,6 +21,7 @@ * \brief Fichier de gestion des triggers LDAP */ require_once (DOL_DOCUMENT_ROOT."/core/class/ldap.class.php"); +require_once (DOL_DOCUMENT_ROOT."/user/class/usergroup.class.php"); /** @@ -210,6 +211,82 @@ class InterfaceLdapsynchro return $result; } } + elseif ($action == 'USER_SETINGROUP') + { + dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); + if ($conf->ldap->enabled && $conf->global->LDAP_SYNCHRO_ACTIVE == 'dolibarr2ldap') + { + $ldap=new Ldap(); + $ldap->connect_bind(); + + // Must edit $object->newgroupid + $usergroup=new UserGroup($this->db); + if ($object->newgroupid > 0) + { + $usergroup->fetch($object->newgroupid); + + $oldinfo=$usergroup->_load_ldap_info(); + $olddn=$usergroup->_load_ldap_dn($oldinfo); + + // Verify if entry exist + $container=$usergroup->_load_ldap_dn($oldinfo,1); + $search = "(".$usergroup->_load_ldap_dn($oldinfo,2).")"; + $records=$ldap->search($container,$search); + if (count($records) && $records['count'] == 0) + { + $olddn = ''; + } + + $info=$usergroup->_load_ldap_info(); // Contains all members, included the new one (insert already done before trigger call) + $dn=$usergroup->_load_ldap_dn($info); + + $result=$ldap->update($dn,$info,$user,$olddn); + if ($result < 0) + { + $this->error="ErrorLDAP ".$ldap->error; + } + } + return $result; + } + } + elseif ($action == 'USER_REMOVEFROMGROUP') + { + dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); + if ($conf->ldap->enabled && $conf->global->LDAP_SYNCHRO_ACTIVE == 'dolibarr2ldap') + { + $ldap=new Ldap(); + $ldap->connect_bind(); + + // Must edit $object->newgroupid + $usergroup=new UserGroup($this->db); + if ($object->oldgroupid > 0) + { + $usergroup->fetch($object->oldgroupid); + + $oldinfo=$usergroup->_load_ldap_info(); + $olddn=$usergroup->_load_ldap_dn($oldinfo); + + // Verify if entry exist + $container=$usergroup->_load_ldap_dn($oldinfo,1); + $search = "(".$usergroup->_load_ldap_dn($oldinfo,2).")"; + $records=$ldap->search($container,$search); + if (count($records) && $records['count'] == 0) + { + $olddn = ''; + } + + $info=$usergroup->_load_ldap_info(); // Contains all members, included the new one (insert already done before trigger call) + $dn=$usergroup->_load_ldap_dn($info); + + $result=$ldap->update($dn,$info,$user,$olddn); + if ($result < 0) + { + $this->error="ErrorLDAP ".$ldap->error; + } + } + return $result; + } + } // Groupes elseif ($action == 'GROUP_CREATE') diff --git a/htdocs/user/class/user.class.php b/htdocs/user/class/user.class.php index d9af596911e..77a1bc1fb47 100644 --- a/htdocs/user/class/user.class.php +++ b/htdocs/user/class/user.class.php @@ -1576,6 +1576,8 @@ class User extends CommonObject { if (! $error && ! $notrigger) { + $this->newgroupid=$group; + // Appel des triggers include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php"); $interface=new Interfaces($this->db); @@ -1632,6 +1634,8 @@ class User extends CommonObject { if (! $error && ! $notrigger) { + $this->oldgroupid=$group; + // Appel des triggers include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php"); $interface=new Interfaces($this->db); diff --git a/htdocs/user/class/usergroup.class.php b/htdocs/user/class/usergroup.class.php index 3453f4fa379..cb2e616b21f 100644 --- a/htdocs/user/class/usergroup.class.php +++ b/htdocs/user/class/usergroup.class.php @@ -53,11 +53,11 @@ class UserGroup extends CommonObject /** * Constructor de la classe * - * @param DoliDb $DB Database handler + * @param DoliDb $db Database handler */ - function UserGroup($DB) + function UserGroup($db) { - $this->db = $DB; + $this->db = $db; return 0; }