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