dolibarr  9.0.0
interface_50_modLdap_Ldapsynchro.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2005-2012 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2005-2017 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 <http://www.gnu.org/licenses/>.
18  */
19 
26 require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php';
27 
28 
33 {
34  public $family = 'ldap';
35  public $description = "Triggers of this module allows to synchronize Dolibarr toward a LDAP database.";
36 
41  public $version = self::VERSION_DOLIBARR;
42 
46  public $picto = 'technic';
47 
59  public function runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
60  {
61  if (empty($conf->ldap->enabled)) return 0; // Module not active, we do nothing
62  if (defined('DISABLE_LDAP_SYNCHRO')) return 0; // If constant defined, we do nothing
63 
64  if (! function_exists('ldap_connect'))
65  {
66  dol_syslog("Warning, module LDAP is enabled but LDAP functions not available in this PHP", LOG_WARNING);
67  return 0;
68  }
69 
70  require_once DOL_DOCUMENT_ROOT."/core/class/ldap.class.php";
71  require_once DOL_DOCUMENT_ROOT."/user/class/usergroup.class.php";
72 
73  $result=0;
74 
75  // Users
76  if ($action == 'USER_CREATE')
77  {
78  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
79  if (! empty($conf->global->LDAP_SYNCHRO_ACTIVE) && $conf->global->LDAP_SYNCHRO_ACTIVE === 'dolibarr2ldap')
80  {
81  $ldap=new Ldap();
82  $result=$ldap->connect_bind();
83 
84  if ($result > 0)
85  {
86  $info=$object->_load_ldap_info();
87  $dn=$object->_load_ldap_dn($info);
88 
89  $result=$ldap->add($dn,$info,$user);
90  }
91 
92  if ($result < 0) $this->error="ErrorLDAP ".$ldap->error;
93  }
94  }
95  elseif ($action == 'USER_MODIFY')
96  {
97  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
98  if (! empty($conf->global->LDAP_SYNCHRO_ACTIVE) && $conf->global->LDAP_SYNCHRO_ACTIVE === 'dolibarr2ldap')
99  {
100  $ldap=new Ldap();
101  $result=$ldap->connect_bind();
102 
103  if ($result > 0)
104  {
105  if (empty($object->oldcopy) || ! is_object($object->oldcopy))
106  {
107  dol_syslog("Trigger ".$action." was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
108  $object->oldcopy = clone $object;
109  }
110 
111  $oldinfo=$object->oldcopy->_load_ldap_info();
112  $olddn=$object->oldcopy->_load_ldap_dn($oldinfo);
113 
114  // Verify if entry exist
115  $container=$object->oldcopy->_load_ldap_dn($oldinfo,1);
116  $search = "(".$object->oldcopy->_load_ldap_dn($oldinfo,2).")";
117  $records=$ldap->search($container,$search);
118  if (count($records) && $records['count'] == 0)
119  {
120  $olddn = '';
121  }
122 
123  $info=$object->_load_ldap_info();
124  $dn=$object->_load_ldap_dn($info);
125  $newrdn=$object->_load_ldap_dn($info,2);
126  $newparent=$object->_load_ldap_dn($info,1);
127 
128  $result=$ldap->update($dn,$info,$user,$olddn,$newrdn,$newparent);
129  }
130 
131  if ($result < 0) $this->error="ErrorLDAP ".$ldap->error;
132  }
133  }
134  elseif ($action == 'USER_NEW_PASSWORD')
135  {
136  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
137  if (! empty($conf->global->LDAP_SYNCHRO_ACTIVE) && $conf->global->LDAP_SYNCHRO_ACTIVE === 'dolibarr2ldap')
138  {
139  $ldap=new Ldap();
140  $result=$ldap->connect_bind();
141 
142  if ($result > 0)
143  {
144  if (empty($object->oldcopy) || ! is_object($object->oldcopy))
145  {
146  dol_syslog("Trigger ".$action." was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
147  $object->oldcopy = clone $object;
148  }
149 
150  $oldinfo=$object->oldcopy->_load_ldap_info();
151  $olddn=$object->oldcopy->_load_ldap_dn($oldinfo);
152 
153  // Verify if entry exist
154  $container=$object->oldcopy->_load_ldap_dn($oldinfo,1);
155  $search = "(".$object->oldcopy->_load_ldap_dn($oldinfo,2).")";
156  $records=$ldap->search($container,$search);
157  if (count($records) && $records['count'] == 0)
158  {
159  $olddn = '';
160  }
161 
162  $info=$object->_load_ldap_info();
163  $dn=$object->_load_ldap_dn($info);
164 
165  $result=$ldap->update($dn,$info,$user,$olddn);
166  }
167 
168  if ($result < 0) $this->error="ErrorLDAP ".$ldap->error;
169  }
170  }
171  elseif ($action == 'USER_ENABLEDISABLE')
172  {
173  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
174  }
175  elseif ($action == 'USER_DELETE')
176  {
177  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
178  if (! empty($conf->global->LDAP_SYNCHRO_ACTIVE) && $conf->global->LDAP_SYNCHRO_ACTIVE === 'dolibarr2ldap')
179  {
180  $ldap=new Ldap();
181  $result=$ldap->connect_bind();
182 
183  if ($result > 0)
184  {
185  $info=$object->_load_ldap_info();
186  $dn=$object->_load_ldap_dn($info);
187 
188  $result=$ldap->delete($dn);
189  }
190 
191  if ($result < 0) $this->error="ErrorLDAP ".$ldap->error;
192  }
193  }
194  elseif ($action == 'USER_SETINGROUP')
195  {
196  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
197  if (! empty($conf->global->LDAP_SYNCHRO_ACTIVE) && $conf->global->LDAP_SYNCHRO_ACTIVE === 'dolibarr2ldap')
198  {
199  $ldap=new Ldap();
200  $result=$ldap->connect_bind();
201 
202  if ($result > 0)
203  {
204  // Must edit $object->newgroupid
205  $usergroup=new UserGroup($this->db);
206  if ($object->newgroupid > 0)
207  {
208  $usergroup->fetch($object->newgroupid);
209 
210  $oldinfo=$usergroup->_load_ldap_info();
211  $olddn=$usergroup->_load_ldap_dn($oldinfo);
212 
213  // Verify if entry exist
214  $container=$usergroup->_load_ldap_dn($oldinfo,1);
215  $search = "(".$usergroup->_load_ldap_dn($oldinfo,2).")";
216  $records=$ldap->search($container,$search);
217  if (count($records) && $records['count'] == 0)
218  {
219  $olddn = '';
220  }
221 
222  $info=$usergroup->_load_ldap_info(); // Contains all members, included the new one (insert already done before trigger call)
223  $dn=$usergroup->_load_ldap_dn($info);
224 
225  $result=$ldap->update($dn,$info,$user,$olddn);
226  }
227  }
228 
229  if ($result < 0) $this->error="ErrorLDAP ".$ldap->error;
230  }
231  }
232  elseif ($action == 'USER_REMOVEFROMGROUP')
233  {
234  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
235  if (! empty($conf->global->LDAP_SYNCHRO_ACTIVE) && $conf->global->LDAP_SYNCHRO_ACTIVE === 'dolibarr2ldap')
236  {
237  $ldap=new Ldap();
238  $result=$ldap->connect_bind();
239 
240  if ($result > 0)
241  {
242  // Must edit $object->newgroupid
243  $usergroup=new UserGroup($this->db);
244  if ($object->oldgroupid > 0)
245  {
246  $usergroup->fetch($object->oldgroupid);
247 
248  $oldinfo=$usergroup->_load_ldap_info();
249  $olddn=$usergroup->_load_ldap_dn($oldinfo);
250 
251  // Verify if entry exist
252  $container=$usergroup->_load_ldap_dn($oldinfo,1);
253  $search = "(".$usergroup->_load_ldap_dn($oldinfo,2).")";
254  $records=$ldap->search($container,$search);
255  if (count($records) && $records['count'] == 0)
256  {
257  $olddn = '';
258  }
259 
260  $info=$usergroup->_load_ldap_info(); // Contains all members, included the new one (insert already done before trigger call)
261  $dn=$usergroup->_load_ldap_dn($info);
262 
263  $result=$ldap->update($dn,$info,$user,$olddn);
264  }
265  }
266 
267  if ($result < 0) $this->error="ErrorLDAP ".$ldap->error;
268  }
269  }
270 
271  // Groupes
272  elseif ($action == 'GROUP_CREATE')
273  {
274  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
275  if (! empty($conf->global->LDAP_SYNCHRO_ACTIVE) && $conf->global->LDAP_SYNCHRO_ACTIVE === 'dolibarr2ldap')
276  {
277  $ldap=new Ldap();
278  $result=$ldap->connect_bind();
279 
280  if ($result > 0)
281  {
282  $info=$object->_load_ldap_info();
283  $dn=$object->_load_ldap_dn($info);
284 
285  // Get a gid number for objectclass PosixGroup
286  if (in_array('posixGroup',$info['objectclass'])) {
287  $info['gidNumber'] = $ldap->getNextGroupGid('LDAP_KEY_GROUPS');
288  }
289 
290  $result=$ldap->add($dn,$info,$user);
291  }
292 
293  if ($result < 0) $this->error="ErrorLDAP ".$ldap->error;
294  }
295  }
296  elseif ($action == 'GROUP_MODIFY')
297  {
298  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
299  if (! empty($conf->global->LDAP_SYNCHRO_ACTIVE) && $conf->global->LDAP_SYNCHRO_ACTIVE === 'dolibarr2ldap')
300  {
301  $ldap=new Ldap();
302  $result=$ldap->connect_bind();
303 
304  if ($result > 0)
305  {
306  if (empty($object->oldcopy) || ! is_object($object->oldcopy))
307  {
308  dol_syslog("Trigger ".$action." was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
309  $object->oldcopy = clone $object;
310  }
311 
312  $oldinfo=$object->oldcopy->_load_ldap_info();
313  $olddn=$object->oldcopy->_load_ldap_dn($oldinfo);
314 
315  // Verify if entry exist
316  $container=$object->oldcopy->_load_ldap_dn($oldinfo,1);
317  $search = "(".$object->oldcopy->_load_ldap_dn($oldinfo,2).")";
318  $records=$ldap->search($container,$search);
319  if (count($records) && $records['count'] == 0)
320  {
321  $olddn = '';
322  }
323 
324  $info=$object->_load_ldap_info();
325  $dn=$object->_load_ldap_dn($info);
326 
327  $result=$ldap->update($dn,$info,$user,$olddn);
328  }
329 
330  if ($result < 0) $this->error="ErrorLDAP ".$ldap->error;
331  }
332  }
333  elseif ($action == 'GROUP_DELETE')
334  {
335  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
336  if (! empty($conf->global->LDAP_SYNCHRO_ACTIVE) && $conf->global->LDAP_SYNCHRO_ACTIVE === 'dolibarr2ldap')
337  {
338  $ldap=new Ldap();
339  $result=$ldap->connect_bind();
340 
341  if ($result > 0)
342  {
343  $info=$object->_load_ldap_info();
344  $dn=$object->_load_ldap_dn($info);
345 
346  $result=$ldap->delete($dn);
347  }
348 
349  if ($result < 0) $this->error="ErrorLDAP ".$ldap->error;
350  }
351  }
352 
353  // Contacts
354  elseif ($action == 'CONTACT_CREATE')
355  {
356  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
357  if (! empty($conf->global->LDAP_CONTACT_ACTIVE))
358  {
359  $ldap=new Ldap();
360  $result=$ldap->connect_bind();
361 
362  if ($result > 0)
363  {
364  $info=$object->_load_ldap_info();
365  $dn=$object->_load_ldap_dn($info);
366 
367  $result=$ldap->add($dn,$info,$user);
368  }
369 
370  if ($result < 0) $this->error="ErrorLDAP ".$ldap->error;
371  }
372  }
373  elseif ($action == 'CONTACT_MODIFY')
374  {
375  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
376  if (! empty($conf->global->LDAP_CONTACT_ACTIVE))
377  {
378  $ldap=new Ldap();
379  $result=$ldap->connect_bind();
380 
381  if ($result > 0)
382  {
383  if (empty($object->oldcopy) || ! is_object($object->oldcopy))
384  {
385  dol_syslog("Trigger ".$action." was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
386  $object->oldcopy = clone $object;
387  }
388 
389  $oldinfo=$object->oldcopy->_load_ldap_info();
390  $olddn=$object->oldcopy->_load_ldap_dn($oldinfo);
391 
392  // Verify if entry exist
393  $container=$object->oldcopy->_load_ldap_dn($oldinfo,1);
394  $search = "(".$object->oldcopy->_load_ldap_dn($oldinfo,2).")";
395  $records=$ldap->search($container,$search);
396  if (count($records) && $records['count'] == 0)
397  {
398  $olddn = '';
399  }
400 
401  $info=$object->_load_ldap_info();
402  $dn=$object->_load_ldap_dn($info);
403 
404  $result=$ldap->update($dn,$info,$user,$olddn);
405  }
406 
407  if ($result < 0) $this->error="ErrorLDAP ".$ldap->error;
408  }
409  }
410  elseif ($action == 'CONTACT_DELETE')
411  {
412  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
413  if (! empty($conf->global->LDAP_CONTACT_ACTIVE))
414  {
415  $ldap=new Ldap();
416  $result=$ldap->connect_bind();
417 
418  if ($result > 0)
419  {
420  $info=$object->_load_ldap_info();
421  $dn=$object->_load_ldap_dn($info);
422 
423  $result=$ldap->delete($dn);
424  }
425 
426  if ($result < 0) $this->error="ErrorLDAP ".$ldap->error;
427  }
428  }
429 
430  // Members
431  elseif ($action == 'MEMBER_CREATE')
432  {
433  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
434  if (! empty($conf->global->LDAP_MEMBER_ACTIVE) && (string) $conf->global->LDAP_MEMBER_ACTIVE == '1')
435  {
436  $ldap=new Ldap();
437  $result=$ldap->connect_bind();
438 
439  if ($result > 0)
440  {
441  $info=$object->_load_ldap_info();
442  $dn=$object->_load_ldap_dn($info);
443 
444  $result=$ldap->add($dn,$info,$user);
445 
446  // For member type
447  if (! empty($conf->global->LDAP_MEMBER_TYPE_ACTIVE) && (string) $conf->global->LDAP_MEMBER_TYPE_ACTIVE == '1')
448  {
449  if ($object->typeid > 0)
450  {
451  require_once DOL_DOCUMENT_ROOT."/adherents/class/adherent_type.class.php";
452  $membertype=new AdherentType($this->db);
453  $membertype->fetch($object->typeid);
454  $membertype->listMembersForMemberType('', 1);
455 
456  $oldinfo=$membertype->_load_ldap_info();
457  $olddn=$membertype->_load_ldap_dn($oldinfo);
458 
459  // Verify if entry exist
460  $container=$membertype->_load_ldap_dn($oldinfo,1);
461  $search = "(".$membertype->_load_ldap_dn($oldinfo,2).")";
462  $records=$ldap->search($container,$search);
463  if (count($records) && $records['count'] == 0)
464  {
465  $olddn = '';
466  }
467 
468  $info=$membertype->_load_ldap_info(); // Contains all members, included the new one (insert already done before trigger call)
469  $dn=$membertype->_load_ldap_dn($info);
470 
471  $result=$ldap->update($dn,$info,$user,$olddn);
472  }
473  }
474  }
475 
476  if ($result < 0) $this->error="ErrorLDAP ".$ldap->error;
477  }
478  }
479  elseif ($action == 'MEMBER_VALIDATE')
480  {
481  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
482  if (! empty($conf->global->LDAP_MEMBER_ACTIVE) && (string) $conf->global->LDAP_MEMBER_ACTIVE == '1')
483  {
484  // If status field is setup to be synchronized
485  if (! empty($conf->global->LDAP_FIELD_MEMBER_STATUS))
486  {
487  $ldap=new Ldap();
488  $result=$ldap->connect_bind();
489 
490  if ($result > 0)
491  {
492  $info=$object->_load_ldap_info();
493  $dn=$object->_load_ldap_dn($info);
494  $olddn=$dn; // We know olddn=dn as we change only status
495 
496  $result=$ldap->update($dn,$info,$user,$olddn);
497  }
498 
499  if ($result < 0) $this->error="ErrorLDAP ".$ldap->error;
500  }
501  }
502  }
503  elseif ($action == 'MEMBER_SUBSCRIPTION')
504  {
505  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
506  if (! empty($conf->global->LDAP_MEMBER_ACTIVE) && (string) $conf->global->LDAP_MEMBER_ACTIVE == '1')
507  {
508  // If subscriptions fields are setup to be synchronized
509  if ($conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE
510  || $conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_AMOUNT
511  || $conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_DATE
512  || $conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_AMOUNT
513  || $conf->global->LDAP_FIELD_MEMBER_END_LASTSUBSCRIPTION)
514  {
515  $ldap=new Ldap();
516  $result=$ldap->connect_bind();
517 
518  if ($result > 0)
519  {
520  $info=$object->_load_ldap_info();
521  $dn=$object->_load_ldap_dn($info);
522  $olddn=$dn; // We know olddn=dn as we change only subscriptions
523 
524  $result=$ldap->update($dn,$info,$user,$olddn);
525  }
526 
527  if ($result < 0) $this->error="ErrorLDAP ".$ldap->error;
528  }
529  }
530  }
531  elseif ($action == 'MEMBER_MODIFY')
532  {
533  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
534  if (! empty($conf->global->LDAP_MEMBER_ACTIVE) && (string) $conf->global->LDAP_MEMBER_ACTIVE == '1')
535  {
536  $ldap=new Ldap();
537  $result=$ldap->connect_bind();
538 
539  if ($result > 0)
540  {
541  if (empty($object->oldcopy) || ! is_object($object->oldcopy))
542  {
543  dol_syslog("Trigger ".$action." was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
544  $object->oldcopy = clone $object;
545  }
546 
547  $oldinfo=$object->oldcopy->_load_ldap_info();
548  $olddn=$object->oldcopy->_load_ldap_dn($oldinfo);
549 
550  // Verify if entry exist
551  $container=$object->oldcopy->_load_ldap_dn($oldinfo,1);
552  $search = "(".$object->oldcopy->_load_ldap_dn($oldinfo,2).")";
553  $records=$ldap->search($container,$search);
554  if (count($records) && $records['count'] == 0)
555  {
556  $olddn = '';
557  }
558 
559  $info=$object->_load_ldap_info();
560  $dn=$object->_load_ldap_dn($info);
561  $newrdn=$object->_load_ldap_dn($info,2);
562  $newparent=$object->_load_ldap_dn($info,1);
563 
564  $result=$ldap->update($dn,$info,$user,$olddn,$newrdn,$newparent);
565 
566  // For member type
567  if (! empty($conf->global->LDAP_MEMBER_TYPE_ACTIVE) && (string) $conf->global->LDAP_MEMBER_TYPE_ACTIVE == '1')
568  {
569  require_once DOL_DOCUMENT_ROOT."/adherents/class/adherent_type.class.php";
570 
571  /*
572  * Change member info
573  */
574  $newmembertype=new AdherentType($this->db);
575  $newmembertype->fetch($object->typeid);
576  $newmembertype->listMembersForMemberType('', 1);
577 
578  $oldinfo=$newmembertype->_load_ldap_info();
579  $olddn=$newmembertype->_load_ldap_dn($oldinfo);
580 
581  // Verify if entry exist
582  $container=$newmembertype->_load_ldap_dn($oldinfo,1);
583  $search = "(".$newmembertype->_load_ldap_dn($oldinfo,2).")";
584  $records=$ldap->search($container,$search);
585  if (count($records) && $records['count'] == 0)
586  {
587  $olddn = '';
588  }
589 
590  $info=$newmembertype->_load_ldap_info(); // Contains all members, included the new one (insert already done before trigger call)
591  $dn=$newmembertype->_load_ldap_dn($info);
592 
593  $result=$ldap->update($dn,$info,$user,$olddn);
594 
595  if ($object->oldcopy->typeid != $object->typeid)
596  {
597  /*
598  * Remove member in old member type
599  */
600  $oldmembertype=new AdherentType($this->db);
601  $oldmembertype->fetch($object->oldcopy->typeid);
602  $oldmembertype->listMembersForMemberType('', 1);
603 
604  $oldinfo=$oldmembertype->_load_ldap_info();
605  $olddn=$oldmembertype->_load_ldap_dn($oldinfo);
606 
607  // Verify if entry exist
608  $container=$oldmembertype->_load_ldap_dn($oldinfo,1);
609  $search = "(".$oldmembertype->_load_ldap_dn($oldinfo,2).")";
610  $records=$ldap->search($container,$search);
611  if (count($records) && $records['count'] == 0)
612  {
613  $olddn = '';
614  }
615 
616  $info=$oldmembertype->_load_ldap_info(); // Contains all members, included the new one (insert already done before trigger call)
617  $dn=$oldmembertype->_load_ldap_dn($info);
618 
619  $result=$ldap->update($dn,$info,$user,$olddn);
620  }
621  }
622  }
623 
624  if ($result < 0) $this->error="ErrorLDAP ".$ldap->error;
625  }
626  }
627  elseif ($action == 'MEMBER_NEW_PASSWORD')
628  {
629  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
630  if (! empty($conf->global->LDAP_MEMBER_ACTIVE) && (string) $conf->global->LDAP_MEMBER_ACTIVE == '1')
631  {
632  // If password field is setup to be synchronized
633  if ($conf->global->LDAP_FIELD_PASSWORD || $conf->global->LDAP_FIELD_PASSWORD_CRYPTED)
634  {
635  $ldap=new Ldap();
636  $result=$ldap->connect_bind();
637 
638  if ($result > 0)
639  {
640  $info=$object->_load_ldap_info();
641  $dn=$object->_load_ldap_dn($info);
642  $olddn=$dn; // We know olddn=dn as we change only password
643 
644  $result=$ldap->update($dn,$info,$user,$olddn);
645  }
646 
647  if ($result < 0) $this->error="ErrorLDAP ".$ldap->error;
648  }
649  }
650  }
651  elseif ($action == 'MEMBER_RESILIATE')
652  {
653  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
654  if (! empty($conf->global->LDAP_MEMBER_ACTIVE) && (string) $conf->global->LDAP_MEMBER_ACTIVE == '1')
655  {
656  // If status field is setup to be synchronized
657  if (! empty($conf->global->LDAP_FIELD_MEMBER_STATUS))
658  {
659  $ldap=new Ldap();
660  $result=$ldap->connect_bind();
661 
662  if ($result > 0)
663  {
664  $info=$object->_load_ldap_info();
665  $dn=$object->_load_ldap_dn($info);
666  $olddn=$dn; // We know olddn=dn as we change only status
667 
668  $result=$ldap->update($dn,$info,$user,$olddn);
669  }
670 
671  if ($result < 0) $this->error="ErrorLDAP ".$ldap->error;
672  }
673  }
674  }
675  elseif ($action == 'MEMBER_DELETE')
676  {
677  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
678  if (! empty($conf->global->LDAP_MEMBER_ACTIVE) && (string) $conf->global->LDAP_MEMBER_ACTIVE == '1')
679  {
680  $ldap=new Ldap();
681  $result=$ldap->connect_bind();
682 
683  if ($result > 0)
684  {
685  $info=$object->_load_ldap_info();
686  $dn=$object->_load_ldap_dn($info);
687 
688  $result=$ldap->delete($dn);
689 
690  // For member type
691  if (! empty($conf->global->LDAP_MEMBER_TYPE_ACTIVE) && (string) $conf->global->LDAP_MEMBER_TYPE_ACTIVE == '1')
692  {
693  if ($object->typeid > 0)
694  {
695  require_once DOL_DOCUMENT_ROOT."/adherents/class/adherent_type.class.php";
696 
697  /*
698  * Remove member in member type
699  */
700  $membertype=new AdherentType($this->db);
701  $membertype->fetch($object->typeid);
702  $membertype->listMembersForMemberType('a.rowid != ' . $object->id, 1); // remove deleted member from the list
703 
704  $oldinfo=$membertype->_load_ldap_info();
705  $olddn=$membertype->_load_ldap_dn($oldinfo);
706 
707  // Verify if entry exist
708  $container=$membertype->_load_ldap_dn($oldinfo,1);
709  $search = "(".$membertype->_load_ldap_dn($oldinfo,2).")";
710  $records=$ldap->search($container,$search);
711  if (count($records) && $records['count'] == 0)
712  {
713  $olddn = '';
714  }
715 
716  $info=$membertype->_load_ldap_info(); // Contains all members, included the new one (insert already done before trigger call)
717  $dn=$membertype->_load_ldap_dn($info);
718 
719  $result=$ldap->update($dn,$info,$user,$olddn);
720  }
721  }
722  }
723 
724  if ($result < 0) $this->error="ErrorLDAP ".$ldap->error;
725  }
726  }
727 
728  // Members types
729  elseif ($action == 'MEMBER_TYPE_CREATE')
730  {
731  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
732  if (! empty($conf->global->LDAP_MEMBER_TYPE_ACTIVE) && (string) $conf->global->LDAP_MEMBER_TYPE_ACTIVE == '1')
733  {
734  $ldap=new Ldap();
735  $result=$ldap->connect_bind();
736 
737  if ($result > 0)
738  {
739  $info=$object->_load_ldap_info();
740  $dn=$object->_load_ldap_dn($info);
741 
742  // Get a gid number for objectclass PosixGroup
743  if (in_array('posixGroup',$info['objectclass'])) {
744  $info['gidNumber'] = $ldap->getNextGroupGid('LDAP_KEY_MEMBERS_TYPE');
745  }
746 
747  $result=$ldap->add($dn,$info,$user);
748  }
749 
750  if ($result < 0) $this->error="ErrorLDAP ".$ldap->error;
751  }
752  }
753  elseif ($action == 'MEMBER_TYPE_MODIFY')
754  {
755  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
756  if (! empty($conf->global->LDAP_MEMBER_TYPE_ACTIVE) && (string) $conf->global->LDAP_MEMBER_TYPE_ACTIVE == '1')
757  {
758  $ldap=new Ldap();
759  $result=$ldap->connect_bind();
760 
761  if ($result > 0)
762  {
763  if (empty($object->oldcopy) || ! is_object($object->oldcopy))
764  {
765  dol_syslog("Trigger ".$action." was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
766  $object->oldcopy = clone $object;
767  }
768 
769  $object->oldcopy->listMembersForMemberType('', 1);
770 
771  $oldinfo=$object->oldcopy->_load_ldap_info();
772  $olddn=$object->oldcopy->_load_ldap_dn($oldinfo);
773 
774  // Verify if entry exist
775  $container=$object->oldcopy->_load_ldap_dn($oldinfo,1);
776  $search = "(".$object->oldcopy->_load_ldap_dn($oldinfo,2).")";
777  $records=$ldap->search($container,$search);
778  if (count($records) && $records['count'] == 0)
779  {
780  $olddn = '';
781  }
782 
783  $object->listMembersForMemberType('', 1);
784 
785  $info=$object->_load_ldap_info();
786  $dn=$object->_load_ldap_dn($info);
787 
788  $result=$ldap->update($dn,$info,$user,$olddn);
789  }
790 
791  if ($result < 0) $this->error="ErrorLDAP ".$ldap->error;
792  }
793  }
794  elseif ($action == 'MEMBER_TYPE_DELETE')
795  {
796  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
797  if (! empty($conf->global->LDAP_MEMBER_TYPE_ACTIVE) && (string) $conf->global->LDAP_MEMBER_TYPE_ACTIVE == '1')
798  {
799  $ldap=new Ldap();
800  $result=$ldap->connect_bind();
801 
802  if ($result > 0)
803  {
804  $info=$object->_load_ldap_info();
805  $dn=$object->_load_ldap_dn($info);
806 
807  $result=$ldap->delete($dn);
808  }
809 
810  if ($result < 0) $this->error="ErrorLDAP ".$ldap->error;
811  }
812  }
813 
814  return $result;
815  }
816 }
Class to stock current configuration.
Definition: conf.class.php:33
runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
Function called when a Dolibarrr business event is done.
Class to manage Dolibarr users.
Definition: user.class.php:41
Class to manage user groups.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition: repair.php:103
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
Class to manage translations.
Class that all the triggers must extend.
Class to manage members type.
Class to manage LDAP features.
Definition: ldap.class.php:30
Class of triggers for ldap module.