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