PageRenderTime 60ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/core/triggers/interface_50_modLdap_Ldapsynchro.class.php

https://bitbucket.org/speedealing/speedealing
PHP | 617 lines | 464 code | 73 blank | 80 comment | 143 complexity | 3123b2274d70b8e446a71ce6d8141c92 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* Copyright (C) 2005-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/core/triggers/interface_50_modLdap_Ldapsynchro.class.php
  19. * \ingroup core
  20. * \brief Fichier de gestion des triggers LDAP
  21. */
  22. require_once (DOL_DOCUMENT_ROOT."/core/class/ldap.class.php");
  23. require_once (DOL_DOCUMENT_ROOT."/user/class/usergroup.class.php");
  24. /**
  25. * Class of triggers for ldap module
  26. */
  27. class InterfaceLdapsynchro
  28. {
  29. var $db;
  30. var $error;
  31. /**
  32. * Constructor
  33. *
  34. * @param DoliDB $db Database handler
  35. */
  36. function __construct($db = '')
  37. {
  38. $this->db = $db;
  39. $this->name = preg_replace('/^Interface/i','',get_class($this));
  40. $this->family = "ldap";
  41. $this->description = "Triggers of this module allows to synchronize Dolibarr toward a LDAP database.";
  42. $this->version = 'dolibarr'; // 'experimental' or 'dolibarr' or version
  43. $this->picto = 'technic';
  44. }
  45. /**
  46. * Return name of trigger file
  47. *
  48. * @return string Name of trigger file
  49. */
  50. function getName()
  51. {
  52. return $this->name;
  53. }
  54. /**
  55. * Return description of trigger file
  56. *
  57. * @return string Description of trigger file
  58. */
  59. function getDesc()
  60. {
  61. return $this->description;
  62. }
  63. /**
  64. * Return version of trigger file
  65. *
  66. * @return string Version of trigger file
  67. */
  68. function getVersion()
  69. {
  70. global $langs;
  71. $langs->load("admin");
  72. if ($this->version == 'experimental') return $langs->trans("Experimental");
  73. elseif ($this->version == 'dolibarr') return DOL_VERSION;
  74. elseif ($this->version) return $this->version;
  75. else return $langs->trans("Unknown");
  76. }
  77. /**
  78. * Function called when a Dolibarrr business event is done.
  79. * All functions "run_trigger" are triggered if file is inside directory htdocs/core/triggers
  80. *
  81. * @param string $action Event action code
  82. * @param Object $object Object
  83. * @param User $user Object user
  84. * @param Translate $langs Object langs
  85. * @param conf $conf Object conf
  86. * @return int <0 if KO, 0 if no triggered ran, >0 if OK
  87. */
  88. function run_trigger($action,$object,$user,$langs,$conf)
  89. {
  90. if (empty($conf->ldap->enabled)) return 0; // Module not active, we do nothing
  91. if (! function_exists('ldap_connect'))
  92. {
  93. dol_syslog("Warning, module LDAP is enabled but LDAP functions not available in this PHP", LOG_WARNING);
  94. return 0;
  95. }
  96. // Users
  97. if ($action == 'USER_CREATE')
  98. {
  99. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  100. if (! empty($conf->ldap->enabled) && $conf->global->LDAP_SYNCHRO_ACTIVE == 'dolibarr2ldap')
  101. {
  102. $ldap=new Ldap();
  103. $ldap->connect_bind();
  104. $info=$object->_load_ldap_info();
  105. $dn=$object->_load_ldap_dn($info);
  106. $result=$ldap->add($dn,$info,$user);
  107. if ($result < 0)
  108. {
  109. $this->error="ErrorLDAP ".$ldap->error;
  110. }
  111. return $result;
  112. }
  113. }
  114. elseif ($action == 'USER_MODIFY')
  115. {
  116. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  117. if (! empty($conf->ldap->enabled) && $conf->global->LDAP_SYNCHRO_ACTIVE == 'dolibarr2ldap')
  118. {
  119. $ldap=new Ldap();
  120. $ldap->connect_bind();
  121. $oldinfo=$object->oldcopy->_load_ldap_info();
  122. $olddn=$object->oldcopy->_load_ldap_dn($oldinfo);
  123. // Verify if entry exist
  124. $container=$object->oldcopy->_load_ldap_dn($oldinfo,1);
  125. $search = "(".$object->oldcopy->_load_ldap_dn($oldinfo,2).")";
  126. $records=$ldap->search($container,$search);
  127. if (count($records) && $records['count'] == 0)
  128. {
  129. $olddn = '';
  130. }
  131. $info=$object->_load_ldap_info();
  132. $dn=$object->_load_ldap_dn($info);
  133. $result=$ldap->update($dn,$info,$user,$olddn);
  134. if ($result < 0)
  135. {
  136. $this->error="ErrorLDAP ".$ldap->error;
  137. }
  138. return $result;
  139. }
  140. }
  141. elseif ($action == 'USER_NEW_PASSWORD')
  142. {
  143. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  144. if (! empty($conf->ldap->enabled) && $conf->global->LDAP_SYNCHRO_ACTIVE == 'dolibarr2ldap')
  145. {
  146. $ldap=new Ldap();
  147. $ldap->connect_bind();
  148. $oldinfo=$object->oldcopy->_load_ldap_info();
  149. $olddn=$object->oldcopy->_load_ldap_dn($oldinfo);
  150. // Verify if entry exist
  151. $container=$object->oldcopy->_load_ldap_dn($oldinfo,1);
  152. $search = "(".$object->oldcopy->_load_ldap_dn($oldinfo,2).")";
  153. $records=$ldap->search($container,$search);
  154. if (count($records) && $records['count'] == 0)
  155. {
  156. $olddn = '';
  157. }
  158. $info=$object->_load_ldap_info();
  159. $dn=$object->_load_ldap_dn($info);
  160. $result=$ldap->update($dn,$info,$user,$olddn);
  161. if ($result < 0)
  162. {
  163. $this->error="ErrorLDAP ".$ldap->error;
  164. }
  165. return $result;
  166. }
  167. }
  168. elseif ($action == 'USER_ENABLEDISABLE')
  169. {
  170. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  171. }
  172. elseif ($action == 'USER_DELETE')
  173. {
  174. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  175. if (! empty($conf->ldap->enabled) && $conf->global->LDAP_SYNCHRO_ACTIVE == 'dolibarr2ldap')
  176. {
  177. $ldap=new Ldap();
  178. $ldap->connect_bind();
  179. $info=$object->_load_ldap_info();
  180. $dn=$object->_load_ldap_dn($info);
  181. $result=$ldap->delete($dn,$info,$user);
  182. if ($result < 0)
  183. {
  184. $this->error="ErrorLDAP ".$ldap->error;
  185. }
  186. return $result;
  187. }
  188. }
  189. elseif ($action == 'USER_SETINGROUP')
  190. {
  191. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  192. if (! empty($conf->ldap->enabled) && $conf->global->LDAP_SYNCHRO_ACTIVE == 'dolibarr2ldap')
  193. {
  194. $ldap=new Ldap();
  195. $ldap->connect_bind();
  196. // Must edit $object->newgroupid
  197. $usergroup=new UserGroup($this->db);
  198. if ($object->newgroupid > 0)
  199. {
  200. $usergroup->fetch($object->newgroupid);
  201. $oldinfo=$usergroup->_load_ldap_info();
  202. $olddn=$usergroup->_load_ldap_dn($oldinfo);
  203. // Verify if entry exist
  204. $container=$usergroup->_load_ldap_dn($oldinfo,1);
  205. $search = "(".$usergroup->_load_ldap_dn($oldinfo,2).")";
  206. $records=$ldap->search($container,$search);
  207. if (count($records) && $records['count'] == 0)
  208. {
  209. $olddn = '';
  210. }
  211. $info=$usergroup->_load_ldap_info(); // Contains all members, included the new one (insert already done before trigger call)
  212. $dn=$usergroup->_load_ldap_dn($info);
  213. $result=$ldap->update($dn,$info,$user,$olddn);
  214. if ($result < 0)
  215. {
  216. $this->error="ErrorLDAP ".$ldap->error;
  217. }
  218. }
  219. return $result;
  220. }
  221. }
  222. elseif ($action == 'USER_REMOVEFROMGROUP')
  223. {
  224. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  225. if (! empty($conf->ldap->enabled) && $conf->global->LDAP_SYNCHRO_ACTIVE == 'dolibarr2ldap')
  226. {
  227. $ldap=new Ldap();
  228. $ldap->connect_bind();
  229. // Must edit $object->newgroupid
  230. $usergroup=new UserGroup($this->db);
  231. if ($object->oldgroupid > 0)
  232. {
  233. $usergroup->fetch($object->oldgroupid);
  234. $oldinfo=$usergroup->_load_ldap_info();
  235. $olddn=$usergroup->_load_ldap_dn($oldinfo);
  236. // Verify if entry exist
  237. $container=$usergroup->_load_ldap_dn($oldinfo,1);
  238. $search = "(".$usergroup->_load_ldap_dn($oldinfo,2).")";
  239. $records=$ldap->search($container,$search);
  240. if (count($records) && $records['count'] == 0)
  241. {
  242. $olddn = '';
  243. }
  244. $info=$usergroup->_load_ldap_info(); // Contains all members, included the new one (insert already done before trigger call)
  245. $dn=$usergroup->_load_ldap_dn($info);
  246. $result=$ldap->update($dn,$info,$user,$olddn);
  247. if ($result < 0)
  248. {
  249. $this->error="ErrorLDAP ".$ldap->error;
  250. }
  251. }
  252. return $result;
  253. }
  254. }
  255. // Groupes
  256. elseif ($action == 'GROUP_CREATE')
  257. {
  258. if (! empty($conf->ldap->enabled) && $conf->global->LDAP_SYNCHRO_ACTIVE == 'dolibarr2ldap')
  259. {
  260. $ldap=new Ldap();
  261. $ldap->connect_bind();
  262. $info=$object->_load_ldap_info();
  263. $dn=$object->_load_ldap_dn($info);
  264. // Get a gid number for objectclass PosixGroup
  265. if(in_array('posixGroup',$info['objectclass']))
  266. $info['gidNumber'] = $ldap->getNextGroupGid();
  267. $result=$ldap->add($dn,$info,$user);
  268. if ($result < 0)
  269. {
  270. $this->error="ErrorLDAP ".$ldap->error;
  271. }
  272. return $result;
  273. }
  274. }
  275. elseif ($action == 'GROUP_MODIFY')
  276. {
  277. if (! empty($conf->ldap->enabled) && $conf->global->LDAP_SYNCHRO_ACTIVE == 'dolibarr2ldap')
  278. {
  279. $ldap=new Ldap();
  280. $ldap->connect_bind();
  281. $oldinfo=$object->oldcopy->_load_ldap_info();
  282. $olddn=$object->oldcopy->_load_ldap_dn($oldinfo);
  283. // Verify if entry exist
  284. $container=$object->oldcopy->_load_ldap_dn($oldinfo,1);
  285. $search = "(".$object->oldcopy->_load_ldap_dn($oldinfo,2).")";
  286. $records=$ldap->search($container,$search);
  287. if (count($records) && $records['count'] == 0)
  288. {
  289. $olddn = '';
  290. }
  291. $info=$object->_load_ldap_info();
  292. $dn=$object->_load_ldap_dn($info);
  293. $result=$ldap->update($dn,$info,$user,$olddn);
  294. if ($result < 0)
  295. {
  296. $this->error="ErrorLDAP ".$ldap->error;
  297. }
  298. return $result;
  299. }
  300. }
  301. elseif ($action == 'GROUP_DELETE')
  302. {
  303. if (! empty($conf->ldap->enabled) && $conf->global->LDAP_SYNCHRO_ACTIVE == 'dolibarr2ldap')
  304. {
  305. $ldap=new Ldap();
  306. $ldap->connect_bind();
  307. $info=$object->_load_ldap_info();
  308. $dn=$object->_load_ldap_dn($info);
  309. $result=$ldap->delete($dn,$info,$user);
  310. if ($result < 0)
  311. {
  312. $this->error="ErrorLDAP ".$ldap->error;
  313. }
  314. return $result;
  315. }
  316. }
  317. // Contacts
  318. elseif ($action == 'CONTACT_CREATE')
  319. {
  320. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  321. if (! empty($conf->ldap->enabled) && ! empty($conf->global->LDAP_CONTACT_ACTIVE))
  322. {
  323. $ldap=new Ldap();
  324. $ldap->connect_bind();
  325. $info=$object->_load_ldap_info();
  326. $dn=$object->_load_ldap_dn($info);
  327. $result=$ldap->add($dn,$info,$user);
  328. if ($result < 0)
  329. {
  330. $this->error="ErrorLDAP ".$ldap->error;
  331. }
  332. return $result;
  333. }
  334. }
  335. elseif ($action == 'CONTACT_MODIFY')
  336. {
  337. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  338. if (! empty($conf->ldap->enabled) && ! empty($conf->global->LDAP_CONTACT_ACTIVE))
  339. {
  340. $ldap=new Ldap();
  341. $ldap->connect_bind();
  342. $oldinfo=$object->oldcopy->_load_ldap_info();
  343. $olddn=$object->oldcopy->_load_ldap_dn($oldinfo);
  344. // Verify if entry exist
  345. $container=$object->oldcopy->_load_ldap_dn($oldinfo,1);
  346. $search = "(".$object->oldcopy->_load_ldap_dn($oldinfo,2).")";
  347. $records=$ldap->search($container,$search);
  348. if (count($records) && $records['count'] == 0)
  349. {
  350. $olddn = '';
  351. }
  352. $info=$object->_load_ldap_info();
  353. $dn=$object->_load_ldap_dn($info);
  354. $result=$ldap->update($dn,$info,$user,$olddn);
  355. if ($result < 0)
  356. {
  357. $this->error="ErrorLDAP ".$ldap->error;
  358. }
  359. return $result;
  360. }
  361. }
  362. elseif ($action == 'CONTACT_DELETE')
  363. {
  364. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  365. if (! empty($conf->ldap->enabled) && ! empty($conf->global->LDAP_CONTACT_ACTIVE))
  366. {
  367. $ldap=new Ldap();
  368. $ldap->connect_bind();
  369. $info=$object->_load_ldap_info();
  370. $dn=$object->_load_ldap_dn($info);
  371. $result=$ldap->delete($dn,$info,$user);
  372. if ($result < 0)
  373. {
  374. $this->error="ErrorLDAP ".$ldap->error;
  375. }
  376. return $result;
  377. }
  378. }
  379. // Members
  380. elseif ($action == 'MEMBER_CREATE')
  381. {
  382. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  383. if (! empty($conf->ldap->enabled) && ! empty($conf->global->LDAP_MEMBER_ACTIVE))
  384. {
  385. $ldap=new Ldap();
  386. $ldap->connect_bind();
  387. $info=$object->_load_ldap_info();
  388. $dn=$object->_load_ldap_dn($info);
  389. $result=$ldap->add($dn,$info,$user);
  390. if ($result < 0)
  391. {
  392. $this->error="ErrorLDAP ".$ldap->error;
  393. }
  394. return $result;
  395. }
  396. }
  397. elseif ($action == 'MEMBER_VALIDATE')
  398. {
  399. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  400. if (! empty($conf->ldap->enabled) && ! empty($conf->global->LDAP_MEMBER_ACTIVE))
  401. {
  402. // If status field is setup to be synchronized
  403. if (! empty($conf->global->LDAP_FIELD_MEMBER_STATUS))
  404. {
  405. $ldap=new Ldap();
  406. $ldap->connect_bind();
  407. $info=$object->_load_ldap_info();
  408. $dn=$object->_load_ldap_dn($info);
  409. $olddn=$dn; // We know olddn=dn as we change only status
  410. $result=$ldap->update($dn,$info,$user,$olddn);
  411. if ($result < 0)
  412. {
  413. $this->error="ErrorLDAP ".$ldap->error;
  414. }
  415. return $result;
  416. }
  417. }
  418. }
  419. elseif ($action == 'MEMBER_SUBSCRIPTION')
  420. {
  421. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  422. if (! empty($conf->ldap->enabled) && ! empty($conf->global->LDAP_MEMBER_ACTIVE))
  423. {
  424. // If subscriptions fields are setup to be synchronized
  425. if ($conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE
  426. || $conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_AMOUNT
  427. || $conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_DATE
  428. || $conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_AMOUNT
  429. || $conf->global->LDAP_FIELD_MEMBER_END_LASTSUBSCRIPTION)
  430. {
  431. $ldap=new Ldap();
  432. $ldap->connect_bind();
  433. $info=$object->_load_ldap_info();
  434. $dn=$object->_load_ldap_dn($info);
  435. $olddn=$dn; // We know olddn=dn as we change only subscriptions
  436. $result=$ldap->update($dn,$info,$user,$olddn);
  437. if ($result < 0)
  438. {
  439. $this->error="ErrorLDAP ".$ldap->error;
  440. }
  441. return $result;
  442. }
  443. }
  444. }
  445. elseif ($action == 'MEMBER_MODIFY')
  446. {
  447. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  448. if (! empty($conf->ldap->enabled) && ! empty($conf->global->LDAP_MEMBER_ACTIVE))
  449. {
  450. $ldap=new Ldap();
  451. $ldap->connect_bind();
  452. $oldinfo=$object->oldcopy->_load_ldap_info();
  453. $olddn=$object->oldcopy->_load_ldap_dn($oldinfo);
  454. // Verify if entry exist
  455. $container=$object->oldcopy->_load_ldap_dn($oldinfo,1);
  456. $search = "(".$object->oldcopy->_load_ldap_dn($oldinfo,2).")";
  457. $records=$ldap->search($container,$search);
  458. if (count($records) && $records['count'] == 0)
  459. {
  460. $olddn = '';
  461. }
  462. $info=$object->_load_ldap_info();
  463. $dn=$object->_load_ldap_dn($info);
  464. $result=$ldap->update($dn,$info,$user,$olddn);
  465. if ($result < 0)
  466. {
  467. $this->error="ErrorLDAP ".$ldap->error;
  468. }
  469. return $result;
  470. }
  471. }
  472. elseif ($action == 'MEMBER_NEW_PASSWORD')
  473. {
  474. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  475. if (! empty($conf->ldap->enabled) && ! empty($conf->global->LDAP_MEMBER_ACTIVE))
  476. {
  477. // If password field is setup to be synchronized
  478. if ($conf->global->LDAP_FIELD_PASSWORD || $conf->global->LDAP_FIELD_PASSWORD_CRYPTED)
  479. {
  480. $ldap=new Ldap();
  481. $ldap->connect_bind();
  482. $info=$object->_load_ldap_info();
  483. $dn=$object->_load_ldap_dn($info);
  484. $olddn=$dn; // We know olddn=dn as we change only password
  485. $result=$ldap->update($dn,$info,$user,$olddn);
  486. if ($result < 0)
  487. {
  488. $this->error="ErrorLDAP ".$ldap->error;
  489. }
  490. return $result;
  491. }
  492. }
  493. }
  494. elseif ($action == 'MEMBER_RESILIATE')
  495. {
  496. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  497. if (! empty($conf->ldap->enabled) && ! empty($conf->global->LDAP_MEMBER_ACTIVE))
  498. {
  499. // If status field is setup to be synchronized
  500. if (! empty($conf->global->LDAP_FIELD_MEMBER_STATUS))
  501. {
  502. $ldap=new Ldap();
  503. $ldap->connect_bind();
  504. $info=$object->_load_ldap_info();
  505. $dn=$object->_load_ldap_dn($info);
  506. $olddn=$dn; // We know olddn=dn as we change only status
  507. $result=$ldap->update($dn,$info,$user,$olddn);
  508. if ($result < 0)
  509. {
  510. $this->error="ErrorLDAP ".$ldap->error;
  511. }
  512. return $result;
  513. }
  514. }
  515. }
  516. elseif ($action == 'MEMBER_DELETE')
  517. {
  518. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  519. if (! empty($conf->ldap->enabled) && ! empty($conf->global->LDAP_MEMBER_ACTIVE))
  520. {
  521. $ldap=new Ldap();
  522. $ldap->connect_bind();
  523. $info=$object->_load_ldap_info();
  524. $dn=$object->_load_ldap_dn($info);
  525. $result=$ldap->delete($dn,$info,$user);
  526. if ($result < 0)
  527. {
  528. $this->error="ErrorLDAP ".$ldap->error;
  529. }
  530. return $result;
  531. }
  532. }
  533. // If not found
  534. /*
  535. else
  536. {
  537. dol_syslog("Trigger '".$this->name."' for action '$action' was ran by ".__FILE__." but no handler found for this action.");
  538. return -1;
  539. }
  540. */
  541. return 0;
  542. }
  543. }
  544. ?>