interface_50_modLdap_Ldapsynchro.class.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. <?php
  2. /* Copyright (C) 2005-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2005-2021 Regis Houssin <regis.houssin@inodbox.com>
  4. * Copyright (C) 2014 Marcos García <marcosgdf@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/core/triggers/interface_50_modLdap_Ldapsynchro.class.php
  21. * \ingroup core
  22. * \brief Fichier de gestion des triggers LDAP
  23. */
  24. require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php';
  25. /**
  26. * Class of triggers for ldap module
  27. */
  28. class InterfaceLdapsynchro extends DolibarrTriggers
  29. {
  30. /**
  31. * Constructor
  32. *
  33. * @param DoliDB $db Database handler
  34. */
  35. public function __construct($db)
  36. {
  37. $this->db = $db;
  38. $this->name = preg_replace('/^Interface/i', '', get_class($this));
  39. $this->family = "ldap";
  40. $this->description = "Triggers of this module allows to synchronize Dolibarr toward a LDAP database.";
  41. // 'development', 'experimental', 'dolibarr' or version
  42. $this->version = self::VERSION_DOLIBARR;
  43. $this->picto = 'technic';
  44. }
  45. /**
  46. * Function called when a Dolibarrr business event is done.
  47. * All functions "runTrigger" are triggered if file is inside directory htdocs/core/triggers or htdocs/module/code/triggers (and declared)
  48. *
  49. * @param string $action Event action code
  50. * @param Object $object Object
  51. * @param User $user Object user
  52. * @param Translate $langs Object langs
  53. * @param conf $conf Object conf
  54. * @return int <0 if KO, 0 if no triggered ran, >0 if OK
  55. */
  56. public function runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
  57. {
  58. if (empty($conf->ldap) || empty($conf->ldap->enabled)) {
  59. return 0; // Module not active, we do nothing
  60. }
  61. if (defined('DISABLE_LDAP_SYNCHRO')) {
  62. return 0; // If constant defined, we do nothing
  63. }
  64. if (!function_exists('ldap_connect')) {
  65. dol_syslog("Warning, module LDAP is enabled but LDAP functions not available in this PHP", LOG_WARNING);
  66. return 0;
  67. }
  68. require_once DOL_DOCUMENT_ROOT."/core/class/ldap.class.php";
  69. require_once DOL_DOCUMENT_ROOT."/user/class/usergroup.class.php";
  70. $result = 0;
  71. // Users
  72. if ($action == 'USER_CREATE') {
  73. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  74. if (!empty($conf->global->LDAP_SYNCHRO_ACTIVE) && getDolGlobalInt('LDAP_SYNCHRO_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
  75. $ldap = new Ldap();
  76. $result = $ldap->connect_bind();
  77. if ($result > 0) {
  78. $info = $object->_load_ldap_info();
  79. $dn = $object->_load_ldap_dn($info);
  80. $result = $ldap->add($dn, $info, $user);
  81. }
  82. if ($result < 0) {
  83. $this->error = "ErrorLDAP ".$ldap->error;
  84. }
  85. }
  86. } elseif ($action == 'USER_MODIFY') {
  87. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  88. if (!empty($conf->global->LDAP_SYNCHRO_ACTIVE) && getDolGlobalInt('LDAP_SYNCHRO_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
  89. $ldap = new Ldap();
  90. $result = $ldap->connect_bind();
  91. if ($result > 0) {
  92. if (empty($object->oldcopy) || !is_object($object->oldcopy)) {
  93. dol_syslog("Trigger ".$action." was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
  94. $object->oldcopy = clone $object;
  95. }
  96. $oldinfo = $object->oldcopy->_load_ldap_info();
  97. $olddn = $object->oldcopy->_load_ldap_dn($oldinfo);
  98. // Verify if entry exist
  99. $container = $object->oldcopy->_load_ldap_dn($oldinfo, 1);
  100. $search = "(".$object->oldcopy->_load_ldap_dn($oldinfo, 2).")";
  101. $records = $ldap->search($container, $search);
  102. if (count($records) && $records['count'] == 0) {
  103. $olddn = '';
  104. }
  105. $info = $object->_load_ldap_info();
  106. $dn = $object->_load_ldap_dn($info);
  107. $newrdn = $object->_load_ldap_dn($info, 2);
  108. $newparent = $object->_load_ldap_dn($info, 1);
  109. $result = $ldap->update($dn, $info, $user, $olddn, $newrdn, $newparent);
  110. if ($result > 0 && !empty($object->context['newgroupid'])) { // We are in context of adding a new group to user
  111. $usergroup = new Usergroup($this->db);
  112. $usergroup->fetch($object->context['newgroupid']);
  113. $oldinfo = $usergroup->_load_ldap_info();
  114. $olddn = $usergroup->_load_ldap_dn($oldinfo);
  115. // Verify if entry exist
  116. $container = $usergroup->_load_ldap_dn($oldinfo, 1);
  117. $search = "(".$usergroup->_load_ldap_dn($oldinfo, 2).")";
  118. $records = $ldap->search($container, $search);
  119. if (count($records) && $records['count'] == 0) {
  120. $olddn = '';
  121. }
  122. $info = $usergroup->_load_ldap_info(); // Contains all members, included the new one (insert already done before trigger call)
  123. $dn = $usergroup->_load_ldap_dn($info);
  124. $result = $ldap->update($dn, $info, $user, $olddn);
  125. }
  126. if ($result > 0 && !empty($object->context['oldgroupid'])) { // We are in context of removing a group from user
  127. $usergroup = new Usergroup($this->db);
  128. $usergroup->fetch($object->context['oldgroupid']);
  129. $oldinfo = $usergroup->_load_ldap_info();
  130. $olddn = $usergroup->_load_ldap_dn($oldinfo);
  131. // Verify if an entry exists
  132. $container = $usergroup->_load_ldap_dn($oldinfo, 1);
  133. $search = "(".$usergroup->_load_ldap_dn($oldinfo, 2).")";
  134. $records = $ldap->search($container, $search);
  135. if (count($records) && $records['count'] == 0) {
  136. $olddn = '';
  137. }
  138. $info = $usergroup->_load_ldap_info(); // Contains all members, except the old one (remove already done before trigger call)
  139. $dn = $usergroup->_load_ldap_dn($info);
  140. $result = $ldap->update($dn, $info, $user, $olddn);
  141. }
  142. }
  143. if ($result < 0) {
  144. $this->error = "ErrorLDAP ".$ldap->error;
  145. }
  146. }
  147. } elseif ($action == 'USER_NEW_PASSWORD') {
  148. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  149. if (!empty($conf->global->LDAP_SYNCHRO_ACTIVE) && getDolGlobalInt('LDAP_SYNCHRO_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
  150. $ldap = new Ldap();
  151. $result = $ldap->connect_bind();
  152. if ($result > 0) {
  153. if (empty($object->oldcopy) || !is_object($object->oldcopy)) {
  154. dol_syslog("Trigger ".$action." was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
  155. $object->oldcopy = clone $object;
  156. }
  157. $oldinfo = $object->oldcopy->_load_ldap_info();
  158. $olddn = $object->oldcopy->_load_ldap_dn($oldinfo);
  159. // Verify if entry exist
  160. $container = $object->oldcopy->_load_ldap_dn($oldinfo, 1);
  161. $search = "(".$object->oldcopy->_load_ldap_dn($oldinfo, 2).")";
  162. $records = $ldap->search($container, $search);
  163. if (count($records) && $records['count'] == 0) {
  164. $olddn = '';
  165. }
  166. $info = $object->_load_ldap_info();
  167. $dn = $object->_load_ldap_dn($info);
  168. $result = $ldap->update($dn, $info, $user, $olddn);
  169. }
  170. if ($result < 0) {
  171. $this->error = "ErrorLDAP ".$ldap->error;
  172. }
  173. }
  174. } elseif ($action == 'USER_ENABLEDISABLE') {
  175. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  176. } elseif ($action == 'USER_DELETE') {
  177. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  178. if (!empty($conf->global->LDAP_SYNCHRO_ACTIVE) && getDolGlobalInt('LDAP_SYNCHRO_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
  179. $ldap = new Ldap();
  180. $result = $ldap->connect_bind();
  181. if ($result > 0) {
  182. $info = $object->_load_ldap_info();
  183. $dn = $object->_load_ldap_dn($info);
  184. $result = $ldap->delete($dn);
  185. }
  186. if ($result < 0) {
  187. $this->error = "ErrorLDAP ".$ldap->error;
  188. }
  189. }
  190. /*} elseif ($action == 'USER_SETINGROUP') {
  191. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  192. if (!empty($conf->global->LDAP_SYNCHRO_ACTIVE) && getDolGlobalInt('LDAP_SYNCHRO_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
  193. $ldap = new Ldap();
  194. $result = $ldap->connect_bind();
  195. if ($result > 0) {
  196. // Must edit $object->newgroupid
  197. $usergroup = new UserGroup($this->db);
  198. if ($object->newgroupid > 0) {
  199. $usergroup->fetch($object->newgroupid);
  200. $oldinfo = $usergroup->_load_ldap_info();
  201. $olddn = $usergroup->_load_ldap_dn($oldinfo);
  202. // Verify if entry exist
  203. $container = $usergroup->_load_ldap_dn($oldinfo, 1);
  204. $search = "(".$usergroup->_load_ldap_dn($oldinfo, 2).")";
  205. $records = $ldap->search($container, $search);
  206. if (count($records) && $records['count'] == 0) {
  207. $olddn = '';
  208. }
  209. $info = $usergroup->_load_ldap_info(); // Contains all members, included the new one (insert already done before trigger call)
  210. $dn = $usergroup->_load_ldap_dn($info);
  211. $result = $ldap->update($dn, $info, $user, $olddn);
  212. }
  213. }
  214. if ($result < 0) {
  215. $this->error = "ErrorLDAP ".$ldap->error;
  216. }
  217. }
  218. } elseif ($action == 'USER_REMOVEFROMGROUP') {
  219. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  220. if (!empty($conf->global->LDAP_SYNCHRO_ACTIVE) && getDolGlobalInt('LDAP_SYNCHRO_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
  221. $ldap = new Ldap();
  222. $result = $ldap->connect_bind();
  223. if ($result > 0) {
  224. // Must edit $object->newgroupid
  225. $usergroup = new UserGroup($this->db);
  226. if ($object->oldgroupid > 0) {
  227. $usergroup->fetch($object->oldgroupid);
  228. $oldinfo = $usergroup->_load_ldap_info();
  229. $olddn = $usergroup->_load_ldap_dn($oldinfo);
  230. // Verify if entry exist
  231. $container = $usergroup->_load_ldap_dn($oldinfo, 1);
  232. $search = "(".$usergroup->_load_ldap_dn($oldinfo, 2).")";
  233. $records = $ldap->search($container, $search);
  234. if (count($records) && $records['count'] == 0) {
  235. $olddn = '';
  236. }
  237. $info = $usergroup->_load_ldap_info(); // Contains all members, included the new one (insert already done before trigger call)
  238. $dn = $usergroup->_load_ldap_dn($info);
  239. $result = $ldap->update($dn, $info, $user, $olddn);
  240. }
  241. }
  242. if ($result < 0) {
  243. $this->error = "ErrorLDAP ".$ldap->error;
  244. }
  245. } */
  246. } elseif ($action == 'USERGROUP_CREATE') {
  247. // Groupes
  248. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  249. if (!empty($conf->global->LDAP_SYNCHRO_ACTIVE) && getDolGlobalInt('LDAP_SYNCHRO_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
  250. $ldap = new Ldap();
  251. $result = $ldap->connect_bind();
  252. if ($result > 0) {
  253. $info = $object->_load_ldap_info();
  254. $dn = $object->_load_ldap_dn($info);
  255. // Get a gid number for objectclass PosixGroup if none was provided
  256. if (empty($info[$conf->global->LDAP_GROUP_FIELD_GROUPID]) && in_array('posixGroup', $info['objectclass'])) {
  257. $info['gidNumber'] = $ldap->getNextGroupGid('LDAP_KEY_GROUPS');
  258. }
  259. $result = $ldap->add($dn, $info, $user);
  260. }
  261. if ($result < 0) {
  262. $this->error = "ErrorLDAP ".$ldap->error;
  263. }
  264. }
  265. } elseif ($action == 'USERGROUP_MODIFY') {
  266. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  267. if (!empty($conf->global->LDAP_SYNCHRO_ACTIVE) && getDolGlobalInt('LDAP_SYNCHRO_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
  268. $ldap = new Ldap();
  269. $result = $ldap->connect_bind();
  270. if ($result > 0) {
  271. if (empty($object->oldcopy) || !is_object($object->oldcopy)) {
  272. dol_syslog("Trigger ".$action." was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
  273. $object->oldcopy = clone $object;
  274. }
  275. $oldinfo = $object->oldcopy->_load_ldap_info();
  276. $olddn = $object->oldcopy->_load_ldap_dn($oldinfo);
  277. // Verify if entry exist
  278. $container = $object->oldcopy->_load_ldap_dn($oldinfo, 1);
  279. $search = "(".$object->oldcopy->_load_ldap_dn($oldinfo, 2).")";
  280. $records = $ldap->search($container, $search);
  281. if (count($records) && $records['count'] == 0) {
  282. $olddn = '';
  283. }
  284. $info = $object->_load_ldap_info();
  285. $dn = $object->_load_ldap_dn($info);
  286. $result = $ldap->update($dn, $info, $user, $olddn);
  287. }
  288. if ($result < 0) {
  289. $this->error = "ErrorLDAP ".$ldap->error;
  290. }
  291. }
  292. } elseif ($action == 'USERGROUP_DELETE') {
  293. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  294. if (!empty($conf->global->LDAP_SYNCHRO_ACTIVE) && getDolGlobalInt('LDAP_SYNCHRO_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
  295. $ldap = new Ldap();
  296. $result = $ldap->connect_bind();
  297. if ($result > 0) {
  298. $info = $object->_load_ldap_info();
  299. $dn = $object->_load_ldap_dn($info);
  300. $result = $ldap->delete($dn);
  301. }
  302. if ($result < 0) {
  303. $this->error = "ErrorLDAP ".$ldap->error;
  304. }
  305. }
  306. } elseif ($action == 'CONTACT_CREATE') {
  307. // Contacts
  308. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  309. if (!empty($conf->global->LDAP_CONTACT_ACTIVE)) {
  310. $ldap = new Ldap();
  311. $result = $ldap->connect_bind();
  312. if ($result > 0) {
  313. $info = $object->_load_ldap_info();
  314. $dn = $object->_load_ldap_dn($info);
  315. $result = $ldap->add($dn, $info, $user);
  316. }
  317. if ($result < 0) {
  318. $this->error = "ErrorLDAP ".$ldap->error;
  319. }
  320. }
  321. } elseif ($action == 'CONTACT_MODIFY') {
  322. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  323. if (!empty($conf->global->LDAP_CONTACT_ACTIVE)) {
  324. $ldap = new Ldap();
  325. $result = $ldap->connect_bind();
  326. if ($result > 0) {
  327. if (empty($object->oldcopy) || !is_object($object->oldcopy)) {
  328. dol_syslog("Trigger ".$action." was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
  329. $object->oldcopy = clone $object;
  330. }
  331. $oldinfo = $object->oldcopy->_load_ldap_info();
  332. $olddn = $object->oldcopy->_load_ldap_dn($oldinfo);
  333. // Verify if entry exist
  334. $container = $object->oldcopy->_load_ldap_dn($oldinfo, 1);
  335. $search = "(".$object->oldcopy->_load_ldap_dn($oldinfo, 2).")";
  336. $records = $ldap->search($container, $search);
  337. if (count($records) && $records['count'] == 0) {
  338. $olddn = '';
  339. }
  340. $info = $object->_load_ldap_info();
  341. $dn = $object->_load_ldap_dn($info);
  342. $result = $ldap->update($dn, $info, $user, $olddn);
  343. }
  344. if ($result < 0) {
  345. $this->error = "ErrorLDAP ".$ldap->error;
  346. }
  347. }
  348. } elseif ($action == 'CONTACT_DELETE') {
  349. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  350. if (!empty($conf->global->LDAP_CONTACT_ACTIVE)) {
  351. $ldap = new Ldap();
  352. $result = $ldap->connect_bind();
  353. if ($result > 0) {
  354. $info = $object->_load_ldap_info();
  355. $dn = $object->_load_ldap_dn($info);
  356. $result = $ldap->delete($dn);
  357. }
  358. if ($result < 0) {
  359. $this->error = "ErrorLDAP ".$ldap->error;
  360. }
  361. }
  362. } elseif ($action == 'MEMBER_CREATE') {
  363. // Members
  364. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  365. if (!empty($conf->global->LDAP_MEMBER_ACTIVE) && getDolGlobalInt('LDAP_MEMBER_ACTIVE') == Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
  366. $ldap = new Ldap();
  367. $result = $ldap->connect_bind();
  368. if ($result > 0) {
  369. $info = $object->_load_ldap_info();
  370. $dn = $object->_load_ldap_dn($info);
  371. $result = $ldap->add($dn, $info, $user);
  372. // For member type
  373. if (!empty($conf->global->LDAP_MEMBER_TYPE_ACTIVE) && getDolGlobalInt('LDAP_MEMBER_TYPE_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
  374. if ($object->typeid > 0) {
  375. require_once DOL_DOCUMENT_ROOT."/adherents/class/adherent_type.class.php";
  376. $membertype = new AdherentType($this->db);
  377. $membertype->fetch($object->typeid);
  378. $membertype->listMembersForMemberType('', 1);
  379. $oldinfo = $membertype->_load_ldap_info();
  380. $olddn = $membertype->_load_ldap_dn($oldinfo);
  381. // Verify if entry exist
  382. $container = $membertype->_load_ldap_dn($oldinfo, 1);
  383. $search = "(".$membertype->_load_ldap_dn($oldinfo, 2).")";
  384. $records = $ldap->search($container, $search);
  385. if (count($records) && $records['count'] == 0) {
  386. $olddn = '';
  387. }
  388. $info = $membertype->_load_ldap_info(); // Contains all members, included the new one (insert already done before trigger call)
  389. $dn = $membertype->_load_ldap_dn($info);
  390. $result = $ldap->update($dn, $info, $user, $olddn);
  391. }
  392. }
  393. }
  394. if ($result < 0) {
  395. $this->error = "ErrorLDAP ".$ldap->error;
  396. }
  397. }
  398. } elseif ($action == 'MEMBER_VALIDATE') {
  399. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  400. if (!empty($conf->global->LDAP_MEMBER_ACTIVE) && getDolGlobalInt('LDAP_MEMBER_ACTIVE') == Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
  401. // If status field is setup to be synchronized
  402. if (!empty($conf->global->LDAP_FIELD_MEMBER_STATUS)) {
  403. $ldap = new Ldap();
  404. $result = $ldap->connect_bind();
  405. if ($result > 0) {
  406. $info = $object->_load_ldap_info();
  407. $dn = $object->_load_ldap_dn($info);
  408. $olddn = $dn; // We know olddn=dn as we change only status
  409. $result = $ldap->update($dn, $info, $user, $olddn);
  410. }
  411. if ($result < 0) {
  412. $this->error = "ErrorLDAP ".$ldap->error;
  413. }
  414. }
  415. }
  416. } elseif ($action == 'MEMBER_SUBSCRIPTION') {
  417. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  418. if (!empty($conf->global->LDAP_MEMBER_ACTIVE) && getDolGlobalInt('LDAP_MEMBER_ACTIVE') == Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
  419. // If subscriptions fields are setup to be synchronized
  420. if (!empty($conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE)
  421. || !empty($conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_AMOUNT)
  422. || !empty($conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_DATE)
  423. || !empty($conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_AMOUNT)
  424. || !empty($conf->global->LDAP_FIELD_MEMBER_END_LASTSUBSCRIPTION)) {
  425. $ldap = new Ldap();
  426. $result = $ldap->connect_bind();
  427. if ($result > 0) {
  428. $info = $object->_load_ldap_info();
  429. $dn = $object->_load_ldap_dn($info);
  430. $olddn = $dn; // We know olddn=dn as we change only subscriptions
  431. $result = $ldap->update($dn, $info, $user, $olddn);
  432. }
  433. if ($result < 0) {
  434. $this->error = "ErrorLDAP ".$ldap->error;
  435. }
  436. }
  437. }
  438. } elseif ($action == 'MEMBER_MODIFY') {
  439. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  440. if (!empty($conf->global->LDAP_MEMBER_ACTIVE) && getDolGlobalInt('LDAP_MEMBER_ACTIVE') == Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
  441. $ldap = new Ldap();
  442. $result = $ldap->connect_bind();
  443. if ($result > 0) {
  444. if (empty($object->oldcopy) || !is_object($object->oldcopy)) {
  445. dol_syslog("Trigger ".$action." was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
  446. $object->oldcopy = clone $object;
  447. }
  448. $oldinfo = $object->oldcopy->_load_ldap_info();
  449. $olddn = $object->oldcopy->_load_ldap_dn($oldinfo);
  450. // Verify if entry exist
  451. $container = $object->oldcopy->_load_ldap_dn($oldinfo, 1);
  452. $search = "(".$object->oldcopy->_load_ldap_dn($oldinfo, 2).")";
  453. $records = $ldap->search($container, $search);
  454. if (count($records) && $records['count'] == 0) {
  455. $olddn = '';
  456. }
  457. $info = $object->_load_ldap_info();
  458. $dn = $object->_load_ldap_dn($info);
  459. $newrdn = $object->_load_ldap_dn($info, 2);
  460. $newparent = $object->_load_ldap_dn($info, 1);
  461. $result = $ldap->update($dn, $info, $user, $olddn, $newrdn, $newparent);
  462. // For member type
  463. if (!empty($conf->global->LDAP_MEMBER_TYPE_ACTIVE) && getDolGlobalInt('LDAP_MEMBER_TYPE_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
  464. require_once DOL_DOCUMENT_ROOT."/adherents/class/adherent_type.class.php";
  465. /*
  466. * Change member info
  467. */
  468. $newmembertype = new AdherentType($this->db);
  469. $newmembertype->fetch($object->typeid);
  470. $newmembertype->listMembersForMemberType('', 1);
  471. $oldinfo = $newmembertype->_load_ldap_info();
  472. $olddn = $newmembertype->_load_ldap_dn($oldinfo);
  473. // Verify if entry exist
  474. $container = $newmembertype->_load_ldap_dn($oldinfo, 1);
  475. $search = "(".$newmembertype->_load_ldap_dn($oldinfo, 2).")";
  476. $records = $ldap->search($container, $search);
  477. if (count($records) && $records['count'] == 0) {
  478. $olddn = '';
  479. }
  480. $info = $newmembertype->_load_ldap_info(); // Contains all members, included the new one (insert already done before trigger call)
  481. $dn = $newmembertype->_load_ldap_dn($info);
  482. $result = $ldap->update($dn, $info, $user, $olddn);
  483. if ($object->oldcopy->typeid != $object->typeid) {
  484. /*
  485. * Remove member in old member type
  486. */
  487. $oldmembertype = new AdherentType($this->db);
  488. $oldmembertype->fetch($object->oldcopy->typeid);
  489. $oldmembertype->listMembersForMemberType('', 1);
  490. $oldinfo = $oldmembertype->_load_ldap_info();
  491. $olddn = $oldmembertype->_load_ldap_dn($oldinfo);
  492. // Verify if entry exist
  493. $container = $oldmembertype->_load_ldap_dn($oldinfo, 1);
  494. $search = "(".$oldmembertype->_load_ldap_dn($oldinfo, 2).")";
  495. $records = $ldap->search($container, $search);
  496. if (count($records) && $records['count'] == 0) {
  497. $olddn = '';
  498. }
  499. $info = $oldmembertype->_load_ldap_info(); // Contains all members, included the new one (insert already done before trigger call)
  500. $dn = $oldmembertype->_load_ldap_dn($info);
  501. $result = $ldap->update($dn, $info, $user, $olddn);
  502. }
  503. }
  504. }
  505. if ($result <= 0) {
  506. $this->errors[] = "ErrorLDAP ".$ldap->error;
  507. }
  508. }
  509. } elseif ($action == 'MEMBER_NEW_PASSWORD') {
  510. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  511. if (!empty($conf->global->LDAP_MEMBER_ACTIVE) && getDolGlobalInt('LDAP_MEMBER_ACTIVE') == Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
  512. // If password field is setup to be synchronized
  513. if (!empty($conf->global->LDAP_FIELD_PASSWORD) || !empty($conf->global->LDAP_FIELD_PASSWORD_CRYPTED)) {
  514. $ldap = new Ldap();
  515. $result = $ldap->connect_bind();
  516. if ($result > 0) {
  517. $info = $object->_load_ldap_info();
  518. $dn = $object->_load_ldap_dn($info);
  519. $olddn = $dn; // We know olddn=dn as we change only password
  520. $result = $ldap->update($dn, $info, $user, $olddn);
  521. }
  522. if ($result <= 0) {
  523. $this->errors[] = "ErrorLDAP ".$ldap->error;
  524. }
  525. }
  526. }
  527. } elseif ($action == 'MEMBER_RESILIATE') {
  528. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  529. if (!empty($conf->global->LDAP_MEMBER_ACTIVE) && getDolGlobalInt('LDAP_MEMBER_ACTIVE') == Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
  530. // If status field is setup to be synchronized
  531. if (!empty($conf->global->LDAP_FIELD_MEMBER_STATUS)) {
  532. $ldap = new Ldap();
  533. $result = $ldap->connect_bind();
  534. if ($result > 0) {
  535. $info = $object->_load_ldap_info();
  536. $dn = $object->_load_ldap_dn($info);
  537. $olddn = $dn; // We know olddn=dn as we change only status
  538. $result = $ldap->update($dn, $info, $user, $olddn);
  539. }
  540. if ($result <= 0) {
  541. $this->errors[] = "ErrorLDAP ".$ldap->error;
  542. }
  543. }
  544. }
  545. } elseif ($action == 'MEMBER_DELETE') {
  546. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  547. if (!empty($conf->global->LDAP_MEMBER_ACTIVE) && getDolGlobalInt('LDAP_MEMBER_ACTIVE') == Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
  548. $ldap = new Ldap();
  549. $result = $ldap->connect_bind();
  550. if ($result > 0) {
  551. $info = $object->_load_ldap_info();
  552. $dn = $object->_load_ldap_dn($info);
  553. $result = $ldap->delete($dn);
  554. // For member type
  555. if (!empty($conf->global->LDAP_MEMBER_TYPE_ACTIVE) && getDolGlobalInt('LDAP_MEMBER_TYPE_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
  556. if ($object->typeid > 0) {
  557. require_once DOL_DOCUMENT_ROOT."/adherents/class/adherent_type.class.php";
  558. /*
  559. * Remove member in member type
  560. */
  561. $membertype = new AdherentType($this->db);
  562. $membertype->fetch($object->typeid);
  563. $membertype->listMembersForMemberType('a.rowid != '.$object->id, 1); // remove deleted member from the list
  564. $oldinfo = $membertype->_load_ldap_info();
  565. $olddn = $membertype->_load_ldap_dn($oldinfo);
  566. // Verify if entry exist
  567. $container = $membertype->_load_ldap_dn($oldinfo, 1);
  568. $search = "(".$membertype->_load_ldap_dn($oldinfo, 2).")";
  569. $records = $ldap->search($container, $search);
  570. if (count($records) && $records['count'] == 0) {
  571. $olddn = '';
  572. }
  573. $info = $membertype->_load_ldap_info(); // Contains all members, included the new one (insert already done before trigger call)
  574. $dn = $membertype->_load_ldap_dn($info);
  575. $result = $ldap->update($dn, $info, $user, $olddn);
  576. }
  577. }
  578. }
  579. if ($result <= 0) {
  580. $this->errors[] = "ErrorLDAP ".$ldap->error;
  581. }
  582. }
  583. } elseif ($action == 'MEMBER_TYPE_CREATE') {
  584. // Members types
  585. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  586. if (!empty($conf->global->LDAP_MEMBER_TYPE_ACTIVE) && getDolGlobalInt('LDAP_MEMBER_TYPE_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
  587. $ldap = new Ldap();
  588. $result = $ldap->connect_bind();
  589. if ($result > 0) {
  590. $info = $object->_load_ldap_info();
  591. $dn = $object->_load_ldap_dn($info);
  592. // Get a gid number for objectclass PosixGroup
  593. if (in_array('posixGroup', $info['objectclass'])) {
  594. $info['gidNumber'] = $ldap->getNextGroupGid('LDAP_KEY_MEMBERS_TYPE');
  595. }
  596. $result = $ldap->add($dn, $info, $user);
  597. }
  598. if ($result <= 0) {
  599. $this->errors[] = "ErrorLDAP ".$ldap->error;
  600. }
  601. }
  602. } elseif ($action == 'MEMBER_TYPE_MODIFY') {
  603. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  604. if (!empty($conf->global->LDAP_MEMBER_TYPE_ACTIVE) && getDolGlobalInt('LDAP_MEMBER_TYPE_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
  605. $ldap = new Ldap();
  606. $result = $ldap->connect_bind();
  607. if ($result > 0) {
  608. if (empty($object->oldcopy) || !is_object($object->oldcopy)) {
  609. dol_syslog("Trigger ".$action." was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
  610. $object->oldcopy = clone $object;
  611. }
  612. $object->oldcopy->listMembersForMemberType('', 1);
  613. $oldinfo = $object->oldcopy->_load_ldap_info();
  614. $olddn = $object->oldcopy->_load_ldap_dn($oldinfo);
  615. // Verify if entry exist
  616. $container = $object->oldcopy->_load_ldap_dn($oldinfo, 1);
  617. $search = "(".$object->oldcopy->_load_ldap_dn($oldinfo, 2).")";
  618. $records = $ldap->search($container, $search);
  619. if (count($records) && $records['count'] == 0) {
  620. $olddn = '';
  621. }
  622. $object->listMembersForMemberType('', 1);
  623. $info = $object->_load_ldap_info();
  624. $dn = $object->_load_ldap_dn($info);
  625. $result = $ldap->update($dn, $info, $user, $olddn);
  626. }
  627. if ($result <= 0) {
  628. $this->errors[] = "ErrorLDAP ".$ldap->error;
  629. }
  630. }
  631. } elseif ($action == 'MEMBER_TYPE_DELETE') {
  632. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  633. if (!empty($conf->global->LDAP_MEMBER_TYPE_ACTIVE) && getDolGlobalInt('LDAP_MEMBER_TYPE_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
  634. $ldap = new Ldap();
  635. $result = $ldap->connect_bind();
  636. if ($result > 0) {
  637. $info = $object->_load_ldap_info();
  638. $dn = $object->_load_ldap_dn($info);
  639. $result = $ldap->delete($dn);
  640. }
  641. if ($result <= 0) {
  642. $this->errors[] = "ErrorLDAP ".$ldap->error;
  643. }
  644. }
  645. }
  646. return $result;
  647. }
  648. }