dolibarr  21.0.0-alpha
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-2021 Regis Houssin <regis.houssin@inodbox.com>
4  * Copyright (C) 2014 Marcos GarcĂ­a <marcosgdf@gmail.com>
5  * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
27 require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php';
28 
29 
34 {
40  public function __construct($db)
41  {
42  $this->db = $db;
43 
44  $this->name = preg_replace('/^Interface/i', '', get_class($this));
45  $this->family = "ldap";
46  $this->description = "Triggers of this module allows to synchronize Dolibarr toward a LDAP database.";
47  $this->version = self::VERSIONS['prod'];
48  $this->picto = 'technic';
49  }
50 
62  public function runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
63  {
64  if (empty($conf->ldap) || empty($conf->ldap->enabled)) {
65  return 0; // Module not active, we do nothing
66  }
67  if (defined('DISABLE_LDAP_SYNCHRO')) {
68  return 0; // If constant defined, we do nothing
69  }
70 
71  if (!function_exists('ldap_connect')) {
72  dol_syslog("Warning, module LDAP is enabled but LDAP functions not available in this PHP", LOG_WARNING);
73  return 0;
74  }
75 
76  require_once DOL_DOCUMENT_ROOT."/core/class/ldap.class.php";
77  require_once DOL_DOCUMENT_ROOT."/user/class/usergroup.class.php";
78 
79  $result = 0;
80 
81  // Users
82  if ($action == 'USER_CREATE') {
83  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
84  if (getDolGlobalString('LDAP_SYNCHRO_ACTIVE') && getDolGlobalInt('LDAP_SYNCHRO_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
85  $ldap = new Ldap();
86  $result = $ldap->connectBind();
87 
88  if ($result > 0) {
89  $info = $object->_load_ldap_info();
90  $dn = $object->_load_ldap_dn($info);
91 
92  //For compatibility with Samba 4 AD
93  if ($ldap->serverType == "activedirectory") {
94  $info['userAccountControl'] = getDolGlobalString('LDAP_USERACCOUNTCONTROL');
95  }
96 
97  $result = $ldap->add($dn, $info, $user);
98  }
99 
100  if ($result < 0) {
101  $this->error = "ErrorLDAP ".$ldap->error;
102  }
103  }
104  } elseif ($action == 'USER_MODIFY') {
105  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
106  if (getDolGlobalString('LDAP_SYNCHRO_ACTIVE') && getDolGlobalInt('LDAP_SYNCHRO_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
107  $ldap = new Ldap();
108  $result = $ldap->connectBind();
109 
110  if ($result > 0) {
111  if (empty($object->oldcopy) || !is_object($object->oldcopy)) {
112  dol_syslog("Trigger ".$action." was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
113  $object->oldcopy = clone $object;
114  }
115 
116  $oldinfo = $object->oldcopy->_load_ldap_info();
117  $olddn = $object->oldcopy->_load_ldap_dn($oldinfo);
118 
119  // Verify if entry exist
120  $container = $object->oldcopy->_load_ldap_dn($oldinfo, 1);
121  $search = "(".$object->oldcopy->_load_ldap_dn($oldinfo, 2).")";
122  $records = $ldap->search($container, $search);
123  if (count($records) && $records['count'] == 0) {
124  $olddn = '';
125  }
126 
127  $info = $object->_load_ldap_info();
128  $dn = $object->_load_ldap_dn($info);
129  $newrdn = $object->_load_ldap_dn($info, 2);
130  $newparent = $object->_load_ldap_dn($info, 1);
131 
132  $result = $ldap->update($dn, $info, $user, $olddn, $newrdn, $newparent);
133 
134  if ($result > 0 && !empty($object->context['newgroupid'])) { // We are in context of adding a new group to user
135  $usergroup = new UserGroup($this->db);
136 
137  $usergroup->fetch($object->context['newgroupid'], '', true);
138 
139  $oldinfo = $usergroup->_load_ldap_info();
140  $olddn = $usergroup->_load_ldap_dn($oldinfo);
141 
142  // Verify if entry exist
143  $container = $usergroup->_load_ldap_dn($oldinfo, 1);
144  $search = "(".$usergroup->_load_ldap_dn($oldinfo, 2).")";
145  $records = $ldap->search($container, $search);
146  if (count($records) && $records['count'] == 0) {
147  $olddn = '';
148  }
149 
150  $info = $usergroup->_load_ldap_info(); // Contains all members, included the new one (insert already done before trigger call)
151  $dn = $usergroup->_load_ldap_dn($info);
152 
153  $result = $ldap->update($dn, $info, $user, $olddn);
154  }
155 
156  if ($result > 0 && !empty($object->context['oldgroupid'])) { // We are in context of removing a group from user
157  $usergroup = new UserGroup($this->db);
158 
159  $usergroup->fetch($object->context['oldgroupid'], '', true);
160 
161  $oldinfo = $usergroup->_load_ldap_info();
162  $olddn = $usergroup->_load_ldap_dn($oldinfo);
163 
164  // Verify if an entry exists
165  $container = $usergroup->_load_ldap_dn($oldinfo, 1);
166  $search = "(".$usergroup->_load_ldap_dn($oldinfo, 2).")";
167  $records = $ldap->search($container, $search);
168  if (count($records) && $records['count'] == 0) {
169  $olddn = '';
170  }
171 
172  $info = $usergroup->_load_ldap_info(); // Contains all members, except the old one (remove already done before trigger call)
173  $dn = $usergroup->_load_ldap_dn($info);
174 
175  $result = $ldap->update($dn, $info, $user, $olddn);
176  }
177  }
178 
179  if ($result < 0) {
180  $this->error = "ErrorLDAP ".$ldap->error;
181  }
182  }
183  } elseif ($action == 'USER_NEW_PASSWORD') {
184  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
185  if (getDolGlobalString('LDAP_SYNCHRO_ACTIVE') && getDolGlobalInt('LDAP_SYNCHRO_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
186  $ldap = new Ldap();
187  $result = $ldap->connectBind();
188 
189  if ($result > 0) {
190  if (empty($object->oldcopy) || !is_object($object->oldcopy)) {
191  dol_syslog("Trigger ".$action." was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
192  $object->oldcopy = clone $object;
193  }
194 
195  $oldinfo = $object->oldcopy->_load_ldap_info();
196  $olddn = $object->oldcopy->_load_ldap_dn($oldinfo);
197 
198  // Verify if entry exist
199  $container = $object->oldcopy->_load_ldap_dn($oldinfo, 1);
200  $search = "(".$object->oldcopy->_load_ldap_dn($oldinfo, 2).")";
201  $records = $ldap->search($container, $search);
202  if (count($records) && $records['count'] == 0) {
203  $olddn = '';
204  }
205 
206  $info = $object->_load_ldap_info();
207  $dn = $object->_load_ldap_dn($info);
208 
209  $result = $ldap->update($dn, $info, $user, $olddn);
210  }
211 
212  if ($result < 0) {
213  $this->error = "ErrorLDAP ".$ldap->error;
214  }
215  }
216  } elseif ($action == 'USER_ENABLEDISABLE') {
217  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
218  if (getDolGlobalInt("LDAP_SYNCHRO_ACTIVE") === Ldap::SYNCHRO_DOLIBARR_TO_LDAP && getDolGlobalString('LDAP_SERVER_TYPE') == "activedirectory") {
219  $ldap = new Ldap();
220  $result = $ldap->connectBind();
221  if ($result > 0) {
222  $info = $object->_load_ldap_info();
223  $dn = $object->_load_ldap_dn($info);
224  $search = "(" . $object->_load_ldap_dn($info, 2) . ")";
225  $uAC = $ldap->getAttributeValues($search, "userAccountControl");
226  if ($uAC["count"] == 1) {
227  $userAccountControl = intval($uAC[0]);
228  $enabledBitMask = 0x2;
229  $isEnabled = ($userAccountControl & $enabledBitMask) === 0;
230  if ($isEnabled && intval($object->statut) === 1) {
231  $userAccountControl += 2;
232  } elseif (!$isEnabled && intval($object->statut) === 0) {
233  $userAccountControl -= 2;
234  }
235  $info['userAccountControl'] = $userAccountControl;
236  // @phan-suppress-next-line PhanPluginSuspiciousParamPosition
237  $resUpdate = $ldap->update($dn, $info, $user, $dn);
238  if ($resUpdate < 0) {
239  $this->error = "ErrorLDAP " . $ldap->error;
240  }
241  }
242  } else {
243  $this->error = "ErrorLDAP " . $ldap->error;
244  }
245  }
246  } elseif ($action == 'USER_DELETE') {
247  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
248  if (getDolGlobalString('LDAP_SYNCHRO_ACTIVE') && getDolGlobalInt('LDAP_SYNCHRO_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
249  $ldap = new Ldap();
250  $result = $ldap->connectBind();
251 
252  if ($result > 0) {
253  $info = $object->_load_ldap_info();
254  $dn = $object->_load_ldap_dn($info);
255 
256  $result = $ldap->delete($dn);
257  }
258 
259  if ($result < 0) {
260  $this->error = "ErrorLDAP ".$ldap->error;
261  }
262  }
263  } elseif ($action == 'USERGROUP_CREATE') {
264  // Groups
265  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
266  if (getDolGlobalString('LDAP_SYNCHRO_ACTIVE') && getDolGlobalInt('LDAP_SYNCHRO_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
267  $ldap = new Ldap();
268  $result = $ldap->connectBind();
269 
270  if ($result > 0) {
271  $info = $object->_load_ldap_info();
272  $dn = $object->_load_ldap_dn($info);
273 
274  // Get a gid number for objectclass PosixGroup if none was provided
275  if (empty($info[getDolGlobalString('LDAP_GROUP_FIELD_GROUPID')]) && in_array('posixGroup', $info['objectclass'])) {
276  $info['gidNumber'] = $ldap->getNextGroupGid('LDAP_KEY_GROUPS');
277  }
278 
279  // Avoid Ldap error due to empty member
280  if (isset($info['member']) && empty($info['member'])) {
281  unset($info['member']);
282  }
283 
284  $result = $ldap->add($dn, $info, $user);
285  }
286 
287  if ($ldap->serverType == "activedirectory") {
288  $info['sAMAccountName'] = $object->name;
289  }
290 
291  if ($result < 0) {
292  $this->error = "ErrorLDAP ".$ldap->error;
293  }
294  }
295  } elseif ($action == 'USERGROUP_MODIFY') {
296  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
297  if (getDolGlobalString('LDAP_SYNCHRO_ACTIVE') && getDolGlobalInt('LDAP_SYNCHRO_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
298  $ldap = new Ldap();
299  $result = $ldap->connectBind();
300 
301  if ($result > 0) {
302  if (empty($object->oldcopy) || !is_object($object->oldcopy)) {
303  dol_syslog("Trigger ".$action." was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
304  $object->oldcopy = clone $object;
305  }
306 
307  $oldinfo = $object->oldcopy->_load_ldap_info();
308  $olddn = $object->oldcopy->_load_ldap_dn($oldinfo);
309 
310  // Verify if entry exist
311  $container = $object->oldcopy->_load_ldap_dn($oldinfo, 1);
312  $search = "(".$object->oldcopy->_load_ldap_dn($oldinfo, 2).")";
313  $records = $ldap->search($container, $search);
314  if (count($records) && $records['count'] == 0) {
315  $olddn = '';
316  }
317 
318  $info = $object->_load_ldap_info();
319  $dn = $object->_load_ldap_dn($info);
320 
321  $result = $ldap->update($dn, $info, $user, $olddn);
322  }
323 
324  if ($result < 0) {
325  $this->error = "ErrorLDAP ".$ldap->error;
326  }
327  }
328  } elseif ($action == 'USERGROUP_DELETE') {
329  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
330  if (getDolGlobalString('LDAP_SYNCHRO_ACTIVE') && getDolGlobalInt('LDAP_SYNCHRO_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
331  $ldap = new Ldap();
332  $result = $ldap->connectBind();
333 
334  if ($result > 0) {
335  $info = $object->_load_ldap_info();
336  $dn = $object->_load_ldap_dn($info);
337 
338  $result = $ldap->delete($dn);
339  }
340 
341  if ($result < 0) {
342  $this->error = "ErrorLDAP ".$ldap->error;
343  }
344  }
345  } elseif ($action == 'CONTACT_CREATE') {
346  // Contacts
347  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
348  if (getDolGlobalString('LDAP_CONTACT_ACTIVE')) {
349  $ldap = new Ldap();
350  $result = $ldap->connectBind();
351 
352  if ($result > 0) {
353  $info = $object->_load_ldap_info();
354  $dn = $object->_load_ldap_dn($info);
355 
356  $result = $ldap->add($dn, $info, $user);
357  }
358 
359  if ($result < 0) {
360  $this->error = "ErrorLDAP ".$ldap->error;
361  }
362  }
363  } elseif ($action == 'CONTACT_MODIFY') {
364  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
365  if (getDolGlobalString('LDAP_CONTACT_ACTIVE')) {
366  $ldap = new Ldap();
367  $result = $ldap->connectBind();
368 
369  if ($result > 0) {
370  if (empty($object->oldcopy) || !is_object($object->oldcopy)) {
371  dol_syslog("Trigger ".$action." was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
372  $object->oldcopy = clone $object;
373  }
374 
375  $oldinfo = $object->oldcopy->_load_ldap_info();
376  $olddn = $object->oldcopy->_load_ldap_dn($oldinfo);
377 
378  // Verify if entry exist
379  $container = $object->oldcopy->_load_ldap_dn($oldinfo, 1);
380  $search = "(".$object->oldcopy->_load_ldap_dn($oldinfo, 2).")";
381  $records = $ldap->search($container, $search);
382  if (count($records) && $records['count'] == 0) {
383  $olddn = '';
384  }
385 
386  $info = $object->_load_ldap_info();
387  $dn = $object->_load_ldap_dn($info);
388 
389  $result = $ldap->update($dn, $info, $user, $olddn);
390  }
391 
392  if ($result < 0) {
393  $this->error = "ErrorLDAP ".$ldap->error;
394  }
395  }
396  } elseif ($action == 'CONTACT_DELETE') {
397  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
398  if (getDolGlobalString('LDAP_CONTACT_ACTIVE')) {
399  $ldap = new Ldap();
400  $result = $ldap->connectBind();
401 
402  if ($result > 0) {
403  $info = $object->_load_ldap_info();
404  $dn = $object->_load_ldap_dn($info);
405 
406  $result = $ldap->delete($dn);
407  }
408 
409  if ($result < 0) {
410  $this->error = "ErrorLDAP ".$ldap->error;
411  }
412  }
413  } elseif ($action == 'MEMBER_CREATE') {
414  // Members
415  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
416  if (getDolGlobalString('LDAP_MEMBER_ACTIVE') && getDolGlobalInt('LDAP_MEMBER_ACTIVE') == Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
417  $ldap = new Ldap();
418  $result = $ldap->connectBind();
419 
420  if ($result > 0) {
421  $info = $object->_load_ldap_info();
422  $dn = $object->_load_ldap_dn($info);
423 
424  $result = $ldap->add($dn, $info, $user);
425 
426  // For member type
427  if (getDolGlobalString('LDAP_MEMBER_TYPE_ACTIVE') && getDolGlobalInt('LDAP_MEMBER_TYPE_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
428  if ($object->typeid > 0) {
429  require_once DOL_DOCUMENT_ROOT."/adherents/class/adherent_type.class.php";
430  $membertype = new AdherentType($this->db);
431  $membertype->fetch($object->typeid);
432  $membertype->listMembersForMemberType('', 1);
433 
434  $oldinfo = $membertype->_load_ldap_info();
435  $olddn = $membertype->_load_ldap_dn($oldinfo);
436 
437  // Verify if entry exist
438  $container = $membertype->_load_ldap_dn($oldinfo, 1);
439  $search = "(".$membertype->_load_ldap_dn($oldinfo, 2).")";
440  $records = $ldap->search($container, $search);
441  if (count($records) && $records['count'] == 0) {
442  $olddn = '';
443  }
444 
445  $info = $membertype->_load_ldap_info(); // Contains all members, included the new one (insert already done before trigger call)
446  $dn = $membertype->_load_ldap_dn($info);
447 
448  $result = $ldap->update($dn, $info, $user, $olddn);
449  }
450  }
451  }
452 
453  if ($result < 0) {
454  $this->error = "ErrorLDAP ".$ldap->error;
455  }
456  }
457  } elseif ($action == 'MEMBER_VALIDATE') {
458  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
459  if (getDolGlobalString('LDAP_MEMBER_ACTIVE') && getDolGlobalInt('LDAP_MEMBER_ACTIVE') == Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
460  // If status field is setup to be synchronized
461  if (getDolGlobalString('LDAP_FIELD_MEMBER_STATUS')) {
462  $ldap = new Ldap();
463  $result = $ldap->connectBind();
464 
465  if ($result > 0) {
466  $info = $object->_load_ldap_info();
467  $dn = $object->_load_ldap_dn($info);
468  $olddn = $dn; // We know olddn=dn as we change only status
469 
470  $result = $ldap->update($dn, $info, $user, $olddn);
471  }
472 
473  if ($result < 0) {
474  $this->error = "ErrorLDAP ".$ldap->error;
475  }
476  }
477  }
478  } elseif ($action == 'MEMBER_SUBSCRIPTION') {
479  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
480  if (getDolGlobalString('LDAP_MEMBER_ACTIVE') && getDolGlobalInt('LDAP_MEMBER_ACTIVE') == Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
481  // If subscriptions fields are setup to be synchronized
482  if (getDolGlobalString('LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE')
483  || getDolGlobalString('LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_AMOUNT')
484  || getDolGlobalString('LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_DATE')
485  || getDolGlobalString('LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_AMOUNT')
486  || getDolGlobalString('LDAP_FIELD_MEMBER_END_LASTSUBSCRIPTION')) {
487  $ldap = new Ldap();
488  $result = $ldap->connectBind();
489 
490  if ($result > 0) {
491  $info = $object->_load_ldap_info();
492  $dn = $object->_load_ldap_dn($info);
493  $olddn = $dn; // We know olddn=dn as we change only subscriptions
494 
495  $result = $ldap->update($dn, $info, $user, $olddn);
496  }
497 
498  if ($result < 0) {
499  $this->error = "ErrorLDAP ".$ldap->error;
500  }
501  }
502  }
503  } elseif ($action == 'MEMBER_MODIFY') {
504  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
505  if (getDolGlobalString('LDAP_MEMBER_ACTIVE') && getDolGlobalInt('LDAP_MEMBER_ACTIVE') == Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
506  $ldap = new Ldap();
507  $result = $ldap->connectBind();
508 
509  if ($result > 0) {
510  if (empty($object->oldcopy) || !is_object($object->oldcopy)) {
511  dol_syslog("Trigger ".$action." was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
512  $object->oldcopy = clone $object;
513  }
514 
515  $oldinfo = $object->oldcopy->_load_ldap_info();
516  $olddn = $object->oldcopy->_load_ldap_dn($oldinfo);
517 
518  // Verify if entry exist
519  $container = $object->oldcopy->_load_ldap_dn($oldinfo, 1);
520  $search = "(".$object->oldcopy->_load_ldap_dn($oldinfo, 2).")";
521  $records = $ldap->search($container, $search);
522  if (count($records) && $records['count'] == 0) {
523  $olddn = '';
524  }
525 
526  $info = $object->_load_ldap_info();
527  $dn = $object->_load_ldap_dn($info);
528  $newrdn = $object->_load_ldap_dn($info, 2);
529  $newparent = $object->_load_ldap_dn($info, 1);
530 
531  $result = $ldap->update($dn, $info, $user, $olddn, $newrdn, $newparent);
532 
533  // For member type
534  if (getDolGlobalString('LDAP_MEMBER_TYPE_ACTIVE') && getDolGlobalInt('LDAP_MEMBER_TYPE_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
535  require_once DOL_DOCUMENT_ROOT."/adherents/class/adherent_type.class.php";
536 
537  /*
538  * Change member info
539  */
540  $newmembertype = new AdherentType($this->db);
541  $newmembertype->fetch($object->typeid);
542  $newmembertype->listMembersForMemberType('', 1);
543 
544  $oldinfo = $newmembertype->_load_ldap_info();
545  $olddn = $newmembertype->_load_ldap_dn($oldinfo);
546 
547  // Verify if entry exist
548  $container = $newmembertype->_load_ldap_dn($oldinfo, 1);
549  $search = "(".$newmembertype->_load_ldap_dn($oldinfo, 2).")";
550  $records = $ldap->search($container, $search);
551  if (count($records) && $records['count'] == 0) {
552  $olddn = '';
553  }
554 
555  $info = $newmembertype->_load_ldap_info(); // Contains all members, included the new one (insert already done before trigger call)
556  $dn = $newmembertype->_load_ldap_dn($info);
557 
558  $result = $ldap->update($dn, $info, $user, $olddn);
559 
560  if ($object->oldcopy->typeid != $object->typeid) {
561  /*
562  * Remove member in old member type
563  */
564  $oldmembertype = new AdherentType($this->db);
565  $oldmembertype->fetch($object->oldcopy->typeid);
566  $oldmembertype->listMembersForMemberType('', 1);
567 
568  $oldinfo = $oldmembertype->_load_ldap_info();
569  $olddn = $oldmembertype->_load_ldap_dn($oldinfo);
570 
571  // Verify if entry exist
572  $container = $oldmembertype->_load_ldap_dn($oldinfo, 1);
573  $search = "(".$oldmembertype->_load_ldap_dn($oldinfo, 2).")";
574  $records = $ldap->search($container, $search);
575  if (count($records) && $records['count'] == 0) {
576  $olddn = '';
577  }
578 
579  $info = $oldmembertype->_load_ldap_info(); // Contains all members, included the new one (insert already done before trigger call)
580  $dn = $oldmembertype->_load_ldap_dn($info);
581 
582  $result = $ldap->update($dn, $info, $user, $olddn);
583  }
584  }
585  }
586 
587  if ($result <= 0) {
588  $this->errors[] = "ErrorLDAP ".$ldap->error;
589  }
590  }
591  } elseif ($action == 'MEMBER_NEW_PASSWORD') {
592  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
593  if (getDolGlobalString('LDAP_MEMBER_ACTIVE') && getDolGlobalInt('LDAP_MEMBER_ACTIVE') == Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
594  // If password field is setup to be synchronized
595  if (getDolGlobalString('LDAP_FIELD_PASSWORD') || getDolGlobalString('LDAP_FIELD_PASSWORD_CRYPTED')) {
596  $ldap = new Ldap();
597  $result = $ldap->connectBind();
598 
599  if ($result > 0) {
600  $info = $object->_load_ldap_info();
601  $dn = $object->_load_ldap_dn($info);
602  $olddn = $dn; // We know olddn=dn as we change only password
603 
604  $result = $ldap->update($dn, $info, $user, $olddn);
605  }
606 
607  if ($result <= 0) {
608  $this->errors[] = "ErrorLDAP ".$ldap->error;
609  }
610  }
611  }
612  } elseif ($action == 'MEMBER_RESILIATE') {
613  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
614  if (getDolGlobalString('LDAP_MEMBER_ACTIVE') && getDolGlobalInt('LDAP_MEMBER_ACTIVE') == Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
615  // If status field is setup to be synchronized
616  if (getDolGlobalString('LDAP_FIELD_MEMBER_STATUS')) {
617  $ldap = new Ldap();
618  $result = $ldap->connectBind();
619 
620  if ($result > 0) {
621  $info = $object->_load_ldap_info();
622  $dn = $object->_load_ldap_dn($info);
623  $olddn = $dn; // We know olddn=dn as we change only status
624 
625  $result = $ldap->update($dn, $info, $user, $olddn);
626  }
627 
628  if ($result <= 0) {
629  $this->errors[] = "ErrorLDAP ".$ldap->error;
630  }
631  }
632  }
633  } elseif ($action == 'MEMBER_DELETE') {
634  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
635  if (getDolGlobalString('LDAP_MEMBER_ACTIVE') && getDolGlobalInt('LDAP_MEMBER_ACTIVE') == Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
636  $ldap = new Ldap();
637  $result = $ldap->connectBind();
638 
639  if ($result > 0) {
640  $info = $object->_load_ldap_info();
641  $dn = $object->_load_ldap_dn($info);
642 
643  $result = $ldap->delete($dn);
644 
645  // For member type
646  if (getDolGlobalString('LDAP_MEMBER_TYPE_ACTIVE') && getDolGlobalInt('LDAP_MEMBER_TYPE_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
647  if ($object->typeid > 0) {
648  require_once DOL_DOCUMENT_ROOT."/adherents/class/adherent_type.class.php";
649 
650  /*
651  * Remove member in member type
652  */
653  $membertype = new AdherentType($this->db);
654  $membertype->fetch($object->typeid);
655  $membertype->listMembersForMemberType('a.rowid != '.$object->id, 1); // remove deleted member from the list
656 
657  $oldinfo = $membertype->_load_ldap_info();
658  $olddn = $membertype->_load_ldap_dn($oldinfo);
659 
660  // Verify if entry exist
661  $container = $membertype->_load_ldap_dn($oldinfo, 1);
662  $search = "(".$membertype->_load_ldap_dn($oldinfo, 2).")";
663  $records = $ldap->search($container, $search);
664  if (count($records) && $records['count'] == 0) {
665  $olddn = '';
666  }
667 
668  $info = $membertype->_load_ldap_info(); // Contains all members, included the new one (insert already done before trigger call)
669  $dn = $membertype->_load_ldap_dn($info);
670 
671  $result = $ldap->update($dn, $info, $user, $olddn);
672  }
673  }
674  }
675 
676  if ($result <= 0) {
677  $this->errors[] = "ErrorLDAP ".$ldap->error;
678  }
679  }
680  } elseif ($action == 'MEMBER_TYPE_CREATE') {
681  // Members types
682  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
683  if (getDolGlobalString('LDAP_MEMBER_TYPE_ACTIVE') && getDolGlobalInt('LDAP_MEMBER_TYPE_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
684  $ldap = new Ldap();
685  $result = $ldap->connectBind();
686 
687  if ($result > 0) {
688  $info = $object->_load_ldap_info();
689  $dn = $object->_load_ldap_dn($info);
690 
691  // Get a gid number for objectclass PosixGroup
692  if (in_array('posixGroup', $info['objectclass'])) {
693  $info['gidNumber'] = $ldap->getNextGroupGid('LDAP_KEY_MEMBERS_TYPE');
694  }
695 
696  $result = $ldap->add($dn, $info, $user);
697  }
698 
699  if ($result <= 0) {
700  $this->errors[] = "ErrorLDAP ".$ldap->error;
701  }
702  }
703  } elseif ($action == 'MEMBER_TYPE_MODIFY') {
704  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
705  if (getDolGlobalString('LDAP_MEMBER_TYPE_ACTIVE') && getDolGlobalInt('LDAP_MEMBER_TYPE_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
706  $ldap = new Ldap();
707  $result = $ldap->connectBind();
708 
709  if ($result > 0) {
710  if (empty($object->oldcopy) || !is_object($object->oldcopy)) {
711  dol_syslog("Trigger ".$action." was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
712  $object->oldcopy = clone $object;
713  }
714 
715  $object->oldcopy->listMembersForMemberType('', 1);
716 
717  $oldinfo = $object->oldcopy->_load_ldap_info();
718  $olddn = $object->oldcopy->_load_ldap_dn($oldinfo);
719 
720  // Verify if entry exist
721  $container = $object->oldcopy->_load_ldap_dn($oldinfo, 1);
722  $search = "(".$object->oldcopy->_load_ldap_dn($oldinfo, 2).")";
723  $records = $ldap->search($container, $search);
724  if (count($records) && $records['count'] == 0) {
725  $olddn = '';
726  }
727 
728  $object->listMembersForMemberType('', 1);
729 
730  $info = $object->_load_ldap_info();
731  $dn = $object->_load_ldap_dn($info);
732 
733  $result = $ldap->update($dn, $info, $user, $olddn);
734  }
735 
736  if ($result <= 0) {
737  $this->errors[] = "ErrorLDAP ".$ldap->error;
738  }
739  }
740  } elseif ($action == 'MEMBER_TYPE_DELETE') {
741  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
742  if (getDolGlobalString('LDAP_MEMBER_TYPE_ACTIVE') && getDolGlobalInt('LDAP_MEMBER_TYPE_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
743  $ldap = new Ldap();
744  $result = $ldap->connectBind();
745 
746  if ($result > 0) {
747  $info = $object->_load_ldap_info();
748  $dn = $object->_load_ldap_dn($info);
749 
750  $result = $ldap->delete($dn);
751  }
752 
753  if ($result <= 0) {
754  $this->errors[] = "ErrorLDAP ".$ldap->error;
755  }
756  }
757  }
758 
759  return $result;
760  }
761 }
if($user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition: card.php:58
Class to manage members type.
Class to stock current configuration.
Definition: conf.class.php:34
Class that all triggers must inherit.
Class of triggers for ldap module.
runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
Function called when a Dolibarr business event is done.
Class to manage LDAP features.
Definition: ldap.class.php:37
Class to manage translations.
Class to manage user groups.
Class to manage Dolibarr users.
Definition: user.class.php:50
print *****$script_file(".$version.") pid cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition: repair.php:142