dolibarr 20.0.2
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
27require_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 if (!method_exists($object->oldcopy, '_load_ldap_info')) {
516 dol_syslog("Trigger ".$action." was called by a function that did not set previously the method ->_load_ldap_info onto object", LOG_WARNING);
517 $object->oldcopy = clone $object;
518 }
519
520 $oldinfo = $object->oldcopy->_load_ldap_info();
521 $olddn = $object->oldcopy->_load_ldap_dn($oldinfo);
522
523 // Verify if entry exist
524 $container = $object->oldcopy->_load_ldap_dn($oldinfo, 1);
525 $search = "(".$object->oldcopy->_load_ldap_dn($oldinfo, 2).")";
526 $records = $ldap->search($container, $search);
527 if (count($records) && $records['count'] == 0) {
528 $olddn = '';
529 }
530
531 $info = $object->_load_ldap_info();
532 $dn = $object->_load_ldap_dn($info);
533 $newrdn = $object->_load_ldap_dn($info, 2);
534 $newparent = $object->_load_ldap_dn($info, 1);
535
536 $result = $ldap->update($dn, $info, $user, $olddn, $newrdn, $newparent);
537
538 // For member type
539 if (getDolGlobalString('LDAP_MEMBER_TYPE_ACTIVE') && getDolGlobalInt('LDAP_MEMBER_TYPE_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
540 require_once DOL_DOCUMENT_ROOT."/adherents/class/adherent_type.class.php";
541
542 /*
543 * Change member info
544 */
545 $newmembertype = new AdherentType($this->db);
546 $newmembertype->fetch($object->typeid);
547 $newmembertype->listMembersForMemberType('', 1);
548
549 $oldinfo = $newmembertype->_load_ldap_info();
550 $olddn = $newmembertype->_load_ldap_dn($oldinfo);
551
552 // Verify if entry exist
553 $container = $newmembertype->_load_ldap_dn($oldinfo, 1);
554 $search = "(".$newmembertype->_load_ldap_dn($oldinfo, 2).")";
555 $records = $ldap->search($container, $search);
556 if (count($records) && $records['count'] == 0) {
557 $olddn = '';
558 }
559
560 $info = $newmembertype->_load_ldap_info(); // Contains all members, included the new one (insert already done before trigger call)
561 $dn = $newmembertype->_load_ldap_dn($info);
562
563 $result = $ldap->update($dn, $info, $user, $olddn);
564
565 if ($object->oldcopy->typeid != $object->typeid) {
566 /*
567 * Remove member in old member type
568 */
569 $oldmembertype = new AdherentType($this->db);
570 $oldmembertype->fetch($object->oldcopy->typeid);
571 $oldmembertype->listMembersForMemberType('', 1);
572
573 $oldinfo = $oldmembertype->_load_ldap_info();
574 $olddn = $oldmembertype->_load_ldap_dn($oldinfo);
575
576 // Verify if entry exist
577 $container = $oldmembertype->_load_ldap_dn($oldinfo, 1);
578 $search = "(".$oldmembertype->_load_ldap_dn($oldinfo, 2).")";
579 $records = $ldap->search($container, $search);
580 if (count($records) && $records['count'] == 0) {
581 $olddn = '';
582 }
583
584 $info = $oldmembertype->_load_ldap_info(); // Contains all members, included the new one (insert already done before trigger call)
585 $dn = $oldmembertype->_load_ldap_dn($info);
586
587 $result = $ldap->update($dn, $info, $user, $olddn);
588 }
589 }
590 }
591
592 if ($result <= 0) {
593 $this->errors[] = "ErrorLDAP ".$ldap->error;
594 }
595 }
596 } elseif ($action == 'MEMBER_NEW_PASSWORD') {
597 dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
598 if (getDolGlobalString('LDAP_MEMBER_ACTIVE') && getDolGlobalInt('LDAP_MEMBER_ACTIVE') == Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
599 // If password field is setup to be synchronized
600 if (getDolGlobalString('LDAP_FIELD_PASSWORD') || getDolGlobalString('LDAP_FIELD_PASSWORD_CRYPTED')) {
601 $ldap = new Ldap();
602 $result = $ldap->connectBind();
603
604 if ($result > 0) {
605 $info = $object->_load_ldap_info();
606 $dn = $object->_load_ldap_dn($info);
607 $olddn = $dn; // We know olddn=dn as we change only password
608
609 $result = $ldap->update($dn, $info, $user, $olddn);
610 }
611
612 if ($result <= 0) {
613 $this->errors[] = "ErrorLDAP ".$ldap->error;
614 }
615 }
616 }
617 } elseif ($action == 'MEMBER_RESILIATE') {
618 dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
619 if (getDolGlobalString('LDAP_MEMBER_ACTIVE') && getDolGlobalInt('LDAP_MEMBER_ACTIVE') == Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
620 // If status field is setup to be synchronized
621 if (getDolGlobalString('LDAP_FIELD_MEMBER_STATUS')) {
622 $ldap = new Ldap();
623 $result = $ldap->connectBind();
624
625 if ($result > 0) {
626 $info = $object->_load_ldap_info();
627 $dn = $object->_load_ldap_dn($info);
628 $olddn = $dn; // We know olddn=dn as we change only status
629
630 $result = $ldap->update($dn, $info, $user, $olddn);
631 }
632
633 if ($result <= 0) {
634 $this->errors[] = "ErrorLDAP ".$ldap->error;
635 }
636 }
637 }
638 } elseif ($action == 'MEMBER_DELETE') {
639 dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
640 if (getDolGlobalString('LDAP_MEMBER_ACTIVE') && getDolGlobalInt('LDAP_MEMBER_ACTIVE') == Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
641 $ldap = new Ldap();
642 $result = $ldap->connectBind();
643
644 if ($result > 0) {
645 $info = $object->_load_ldap_info();
646 $dn = $object->_load_ldap_dn($info);
647
648 $result = $ldap->delete($dn);
649
650 // For member type
651 if (getDolGlobalString('LDAP_MEMBER_TYPE_ACTIVE') && getDolGlobalInt('LDAP_MEMBER_TYPE_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
652 if ($object->typeid > 0) {
653 require_once DOL_DOCUMENT_ROOT."/adherents/class/adherent_type.class.php";
654
655 /*
656 * Remove member in member type
657 */
658 $membertype = new AdherentType($this->db);
659 $membertype->fetch($object->typeid);
660 $membertype->listMembersForMemberType('a.rowid != '.$object->id, 1); // remove deleted member from the list
661
662 $oldinfo = $membertype->_load_ldap_info();
663 $olddn = $membertype->_load_ldap_dn($oldinfo);
664
665 // Verify if entry exist
666 $container = $membertype->_load_ldap_dn($oldinfo, 1);
667 $search = "(".$membertype->_load_ldap_dn($oldinfo, 2).")";
668 $records = $ldap->search($container, $search);
669 if (count($records) && $records['count'] == 0) {
670 $olddn = '';
671 }
672
673 $info = $membertype->_load_ldap_info(); // Contains all members, included the new one (insert already done before trigger call)
674 $dn = $membertype->_load_ldap_dn($info);
675
676 $result = $ldap->update($dn, $info, $user, $olddn);
677 }
678 }
679 }
680
681 if ($result <= 0) {
682 $this->errors[] = "ErrorLDAP ".$ldap->error;
683 }
684 }
685 } elseif ($action == 'MEMBER_TYPE_CREATE') {
686 // Members types
687 dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
688 if (getDolGlobalString('LDAP_MEMBER_TYPE_ACTIVE') && getDolGlobalInt('LDAP_MEMBER_TYPE_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
689 $ldap = new Ldap();
690 $result = $ldap->connectBind();
691
692 if ($result > 0) {
693 $info = $object->_load_ldap_info();
694 $dn = $object->_load_ldap_dn($info);
695
696 // Get a gid number for objectclass PosixGroup
697 if (in_array('posixGroup', $info['objectclass'])) {
698 $info['gidNumber'] = $ldap->getNextGroupGid('LDAP_KEY_MEMBERS_TYPE');
699 }
700
701 $result = $ldap->add($dn, $info, $user);
702 }
703
704 if ($result <= 0) {
705 $this->errors[] = "ErrorLDAP ".$ldap->error;
706 }
707 }
708 } elseif ($action == 'MEMBER_TYPE_MODIFY') {
709 dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
710 if (getDolGlobalString('LDAP_MEMBER_TYPE_ACTIVE') && getDolGlobalInt('LDAP_MEMBER_TYPE_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
711 $ldap = new Ldap();
712 $result = $ldap->connectBind();
713
714 if ($result > 0) {
715 if (empty($object->oldcopy) || !is_object($object->oldcopy)) {
716 dol_syslog("Trigger ".$action." was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
717 $object->oldcopy = clone $object;
718 }
719
720 $object->oldcopy->listMembersForMemberType('', 1);
721
722 $oldinfo = $object->oldcopy->_load_ldap_info();
723 $olddn = $object->oldcopy->_load_ldap_dn($oldinfo);
724
725 // Verify if entry exist
726 $container = $object->oldcopy->_load_ldap_dn($oldinfo, 1);
727 $search = "(".$object->oldcopy->_load_ldap_dn($oldinfo, 2).")";
728 $records = $ldap->search($container, $search);
729 if (count($records) && $records['count'] == 0) {
730 $olddn = '';
731 }
732
733 $object->listMembersForMemberType('', 1);
734
735 $info = $object->_load_ldap_info();
736 $dn = $object->_load_ldap_dn($info);
737
738 $result = $ldap->update($dn, $info, $user, $olddn);
739 }
740
741 if ($result <= 0) {
742 $this->errors[] = "ErrorLDAP ".$ldap->error;
743 }
744 }
745 } elseif ($action == 'MEMBER_TYPE_DELETE') {
746 dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
747 if (getDolGlobalString('LDAP_MEMBER_TYPE_ACTIVE') && getDolGlobalInt('LDAP_MEMBER_TYPE_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
748 $ldap = new Ldap();
749 $result = $ldap->connectBind();
750
751 if ($result > 0) {
752 $info = $object->_load_ldap_info();
753 $dn = $object->_load_ldap_dn($info);
754
755 $result = $ldap->delete($dn);
756 }
757
758 if ($result <= 0) {
759 $this->errors[] = "ErrorLDAP ".$ldap->error;
760 }
761 }
762 }
763
764 return $result;
765 }
766}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Class to manage members type.
Class to stock current configuration.
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.
Class to manage translations.
Class to manage user groups.
Class to manage Dolibarr users.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after 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:140