dolibarr 19.0.4
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 (getDolGlobalString('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 //For compatibility with Samba 4 AD
93 if ($ldap->serverType == "activedirectory") {
94 $info['userAccountControl'] = $conf->global->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->connect_bind();
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->connect_bind();
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->connect_bind();
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 $resUpdate = $ldap->update($dn, $info, $user, $dn);
237 if ($resUpdate < 0) {
238 $this->error = "ErrorLDAP " . $ldap->error;
239 }
240 }
241 } else {
242 $this->error = "ErrorLDAP " . $ldap->error;
243 }
244 }
245 } elseif ($action == 'USER_DELETE') {
246 dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
247 if (getDolGlobalString('LDAP_SYNCHRO_ACTIVE') && getDolGlobalInt('LDAP_SYNCHRO_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
248 $ldap = new Ldap();
249 $result = $ldap->connect_bind();
250
251 if ($result > 0) {
252 $info = $object->_load_ldap_info();
253 $dn = $object->_load_ldap_dn($info);
254
255 $result = $ldap->delete($dn);
256 }
257
258 if ($result < 0) {
259 $this->error = "ErrorLDAP ".$ldap->error;
260 }
261 }
262 } elseif ($action == 'USERGROUP_CREATE') {
263 // Groupes
264 dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
265 if (getDolGlobalString('LDAP_SYNCHRO_ACTIVE') && getDolGlobalInt('LDAP_SYNCHRO_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
266 $ldap = new Ldap();
267 $result = $ldap->connect_bind();
268
269 if ($result > 0) {
270 $info = $object->_load_ldap_info();
271 $dn = $object->_load_ldap_dn($info);
272
273 // Get a gid number for objectclass PosixGroup if none was provided
274 if (empty($info[getDolGlobalString('LDAP_GROUP_FIELD_GROUPID')]) && in_array('posixGroup', $info['objectclass'])) {
275 $info['gidNumber'] = $ldap->getNextGroupGid('LDAP_KEY_GROUPS');
276 }
277
278 // Avoid Ldap error due to empty member
279 if (isset($info['member']) && empty($info['member'])) {
280 unset($info['member']);
281 }
282
283 $result = $ldap->add($dn, $info, $user);
284 }
285
286 if ($ldap->serverType == "activedirectory") {
287 $info['sAMAccountName'] = $object->name;
288 }
289
290 if ($result < 0) {
291 $this->error = "ErrorLDAP ".$ldap->error;
292 }
293 }
294 } elseif ($action == 'USERGROUP_MODIFY') {
295 dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
296 if (getDolGlobalString('LDAP_SYNCHRO_ACTIVE') && getDolGlobalInt('LDAP_SYNCHRO_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
297 $ldap = new Ldap();
298 $result = $ldap->connect_bind();
299
300 if ($result > 0) {
301 if (empty($object->oldcopy) || !is_object($object->oldcopy)) {
302 dol_syslog("Trigger ".$action." was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
303 $object->oldcopy = clone $object;
304 }
305
306 $oldinfo = $object->oldcopy->_load_ldap_info();
307 $olddn = $object->oldcopy->_load_ldap_dn($oldinfo);
308
309 // Verify if entry exist
310 $container = $object->oldcopy->_load_ldap_dn($oldinfo, 1);
311 $search = "(".$object->oldcopy->_load_ldap_dn($oldinfo, 2).")";
312 $records = $ldap->search($container, $search);
313 if (count($records) && $records['count'] == 0) {
314 $olddn = '';
315 }
316
317 $info = $object->_load_ldap_info();
318 $dn = $object->_load_ldap_dn($info);
319
320 $result = $ldap->update($dn, $info, $user, $olddn);
321 }
322
323 if ($result < 0) {
324 $this->error = "ErrorLDAP ".$ldap->error;
325 }
326 }
327 } elseif ($action == 'USERGROUP_DELETE') {
328 dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
329 if (getDolGlobalString('LDAP_SYNCHRO_ACTIVE') && getDolGlobalInt('LDAP_SYNCHRO_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
330 $ldap = new Ldap();
331 $result = $ldap->connect_bind();
332
333 if ($result > 0) {
334 $info = $object->_load_ldap_info();
335 $dn = $object->_load_ldap_dn($info);
336
337 $result = $ldap->delete($dn);
338 }
339
340 if ($result < 0) {
341 $this->error = "ErrorLDAP ".$ldap->error;
342 }
343 }
344 } elseif ($action == 'CONTACT_CREATE') {
345 // Contacts
346 dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
347 if (getDolGlobalString('LDAP_CONTACT_ACTIVE')) {
348 $ldap = new Ldap();
349 $result = $ldap->connect_bind();
350
351 if ($result > 0) {
352 $info = $object->_load_ldap_info();
353 $dn = $object->_load_ldap_dn($info);
354
355 $result = $ldap->add($dn, $info, $user);
356 }
357
358 if ($result < 0) {
359 $this->error = "ErrorLDAP ".$ldap->error;
360 }
361 }
362 } elseif ($action == 'CONTACT_MODIFY') {
363 dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
364 if (getDolGlobalString('LDAP_CONTACT_ACTIVE')) {
365 $ldap = new Ldap();
366 $result = $ldap->connect_bind();
367
368 if ($result > 0) {
369 if (empty($object->oldcopy) || !is_object($object->oldcopy)) {
370 dol_syslog("Trigger ".$action." was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
371 $object->oldcopy = clone $object;
372 }
373
374 $oldinfo = $object->oldcopy->_load_ldap_info();
375 $olddn = $object->oldcopy->_load_ldap_dn($oldinfo);
376
377 // Verify if entry exist
378 $container = $object->oldcopy->_load_ldap_dn($oldinfo, 1);
379 $search = "(".$object->oldcopy->_load_ldap_dn($oldinfo, 2).")";
380 $records = $ldap->search($container, $search);
381 if (count($records) && $records['count'] == 0) {
382 $olddn = '';
383 }
384
385 $info = $object->_load_ldap_info();
386 $dn = $object->_load_ldap_dn($info);
387
388 $result = $ldap->update($dn, $info, $user, $olddn);
389 }
390
391 if ($result < 0) {
392 $this->error = "ErrorLDAP ".$ldap->error;
393 }
394 }
395 } elseif ($action == 'CONTACT_DELETE') {
396 dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
397 if (getDolGlobalString('LDAP_CONTACT_ACTIVE')) {
398 $ldap = new Ldap();
399 $result = $ldap->connect_bind();
400
401 if ($result > 0) {
402 $info = $object->_load_ldap_info();
403 $dn = $object->_load_ldap_dn($info);
404
405 $result = $ldap->delete($dn);
406 }
407
408 if ($result < 0) {
409 $this->error = "ErrorLDAP ".$ldap->error;
410 }
411 }
412 } elseif ($action == 'MEMBER_CREATE') {
413 // Members
414 dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
415 if (getDolGlobalString('LDAP_MEMBER_ACTIVE') && getDolGlobalInt('LDAP_MEMBER_ACTIVE') == Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
416 $ldap = new Ldap();
417 $result = $ldap->connect_bind();
418
419 if ($result > 0) {
420 $info = $object->_load_ldap_info();
421 $dn = $object->_load_ldap_dn($info);
422
423 $result = $ldap->add($dn, $info, $user);
424
425 // For member type
426 if (getDolGlobalString('LDAP_MEMBER_TYPE_ACTIVE') && getDolGlobalInt('LDAP_MEMBER_TYPE_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
427 if ($object->typeid > 0) {
428 require_once DOL_DOCUMENT_ROOT."/adherents/class/adherent_type.class.php";
429 $membertype = new AdherentType($this->db);
430 $membertype->fetch($object->typeid);
431 $membertype->listMembersForMemberType('', 1);
432
433 $oldinfo = $membertype->_load_ldap_info();
434 $olddn = $membertype->_load_ldap_dn($oldinfo);
435
436 // Verify if entry exist
437 $container = $membertype->_load_ldap_dn($oldinfo, 1);
438 $search = "(".$membertype->_load_ldap_dn($oldinfo, 2).")";
439 $records = $ldap->search($container, $search);
440 if (count($records) && $records['count'] == 0) {
441 $olddn = '';
442 }
443
444 $info = $membertype->_load_ldap_info(); // Contains all members, included the new one (insert already done before trigger call)
445 $dn = $membertype->_load_ldap_dn($info);
446
447 $result = $ldap->update($dn, $info, $user, $olddn);
448 }
449 }
450 }
451
452 if ($result < 0) {
453 $this->error = "ErrorLDAP ".$ldap->error;
454 }
455 }
456 } elseif ($action == 'MEMBER_VALIDATE') {
457 dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
458 if (getDolGlobalString('LDAP_MEMBER_ACTIVE') && getDolGlobalInt('LDAP_MEMBER_ACTIVE') == Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
459 // If status field is setup to be synchronized
460 if (getDolGlobalString('LDAP_FIELD_MEMBER_STATUS')) {
461 $ldap = new Ldap();
462 $result = $ldap->connect_bind();
463
464 if ($result > 0) {
465 $info = $object->_load_ldap_info();
466 $dn = $object->_load_ldap_dn($info);
467 $olddn = $dn; // We know olddn=dn as we change only status
468
469 $result = $ldap->update($dn, $info, $user, $olddn);
470 }
471
472 if ($result < 0) {
473 $this->error = "ErrorLDAP ".$ldap->error;
474 }
475 }
476 }
477 } elseif ($action == 'MEMBER_SUBSCRIPTION') {
478 dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
479 if (getDolGlobalString('LDAP_MEMBER_ACTIVE') && getDolGlobalInt('LDAP_MEMBER_ACTIVE') == Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
480 // If subscriptions fields are setup to be synchronized
481 if (getDolGlobalString('LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE')
482 || getDolGlobalString('LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_AMOUNT')
483 || getDolGlobalString('LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_DATE')
484 || getDolGlobalString('LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_AMOUNT')
485 || getDolGlobalString('LDAP_FIELD_MEMBER_END_LASTSUBSCRIPTION')) {
486 $ldap = new Ldap();
487 $result = $ldap->connect_bind();
488
489 if ($result > 0) {
490 $info = $object->_load_ldap_info();
491 $dn = $object->_load_ldap_dn($info);
492 $olddn = $dn; // We know olddn=dn as we change only subscriptions
493
494 $result = $ldap->update($dn, $info, $user, $olddn);
495 }
496
497 if ($result < 0) {
498 $this->error = "ErrorLDAP ".$ldap->error;
499 }
500 }
501 }
502 } elseif ($action == 'MEMBER_MODIFY') {
503 dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
504 if (getDolGlobalString('LDAP_MEMBER_ACTIVE') && getDolGlobalInt('LDAP_MEMBER_ACTIVE') == Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
505 $ldap = new Ldap();
506 $result = $ldap->connect_bind();
507
508 if ($result > 0) {
509 if (empty($object->oldcopy) || !is_object($object->oldcopy)) {
510 dol_syslog("Trigger ".$action." was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
511 $object->oldcopy = clone $object;
512 }
513
514 $oldinfo = $object->oldcopy->_load_ldap_info();
515 $olddn = $object->oldcopy->_load_ldap_dn($oldinfo);
516
517 // Verify if entry exist
518 $container = $object->oldcopy->_load_ldap_dn($oldinfo, 1);
519 $search = "(".$object->oldcopy->_load_ldap_dn($oldinfo, 2).")";
520 $records = $ldap->search($container, $search);
521 if (count($records) && $records['count'] == 0) {
522 $olddn = '';
523 }
524
525 $info = $object->_load_ldap_info();
526 $dn = $object->_load_ldap_dn($info);
527 $newrdn = $object->_load_ldap_dn($info, 2);
528 $newparent = $object->_load_ldap_dn($info, 1);
529
530 $result = $ldap->update($dn, $info, $user, $olddn, $newrdn, $newparent);
531
532 // For member type
533 if (getDolGlobalString('LDAP_MEMBER_TYPE_ACTIVE') && getDolGlobalInt('LDAP_MEMBER_TYPE_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
534 require_once DOL_DOCUMENT_ROOT."/adherents/class/adherent_type.class.php";
535
536 /*
537 * Change member info
538 */
539 $newmembertype = new AdherentType($this->db);
540 $newmembertype->fetch($object->typeid);
541 $newmembertype->listMembersForMemberType('', 1);
542
543 $oldinfo = $newmembertype->_load_ldap_info();
544 $olddn = $newmembertype->_load_ldap_dn($oldinfo);
545
546 // Verify if entry exist
547 $container = $newmembertype->_load_ldap_dn($oldinfo, 1);
548 $search = "(".$newmembertype->_load_ldap_dn($oldinfo, 2).")";
549 $records = $ldap->search($container, $search);
550 if (count($records) && $records['count'] == 0) {
551 $olddn = '';
552 }
553
554 $info = $newmembertype->_load_ldap_info(); // Contains all members, included the new one (insert already done before trigger call)
555 $dn = $newmembertype->_load_ldap_dn($info);
556
557 $result = $ldap->update($dn, $info, $user, $olddn);
558
559 if ($object->oldcopy->typeid != $object->typeid) {
560 /*
561 * Remove member in old member type
562 */
563 $oldmembertype = new AdherentType($this->db);
564 $oldmembertype->fetch($object->oldcopy->typeid);
565 $oldmembertype->listMembersForMemberType('', 1);
566
567 $oldinfo = $oldmembertype->_load_ldap_info();
568 $olddn = $oldmembertype->_load_ldap_dn($oldinfo);
569
570 // Verify if entry exist
571 $container = $oldmembertype->_load_ldap_dn($oldinfo, 1);
572 $search = "(".$oldmembertype->_load_ldap_dn($oldinfo, 2).")";
573 $records = $ldap->search($container, $search);
574 if (count($records) && $records['count'] == 0) {
575 $olddn = '';
576 }
577
578 $info = $oldmembertype->_load_ldap_info(); // Contains all members, included the new one (insert already done before trigger call)
579 $dn = $oldmembertype->_load_ldap_dn($info);
580
581 $result = $ldap->update($dn, $info, $user, $olddn);
582 }
583 }
584 }
585
586 if ($result <= 0) {
587 $this->errors[] = "ErrorLDAP ".$ldap->error;
588 }
589 }
590 } elseif ($action == 'MEMBER_NEW_PASSWORD') {
591 dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
592 if (getDolGlobalString('LDAP_MEMBER_ACTIVE') && getDolGlobalInt('LDAP_MEMBER_ACTIVE') == Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
593 // If password field is setup to be synchronized
594 if (getDolGlobalString('LDAP_FIELD_PASSWORD') || getDolGlobalString('LDAP_FIELD_PASSWORD_CRYPTED')) {
595 $ldap = new Ldap();
596 $result = $ldap->connect_bind();
597
598 if ($result > 0) {
599 $info = $object->_load_ldap_info();
600 $dn = $object->_load_ldap_dn($info);
601 $olddn = $dn; // We know olddn=dn as we change only password
602
603 $result = $ldap->update($dn, $info, $user, $olddn);
604 }
605
606 if ($result <= 0) {
607 $this->errors[] = "ErrorLDAP ".$ldap->error;
608 }
609 }
610 }
611 } elseif ($action == 'MEMBER_RESILIATE') {
612 dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
613 if (getDolGlobalString('LDAP_MEMBER_ACTIVE') && getDolGlobalInt('LDAP_MEMBER_ACTIVE') == Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
614 // If status field is setup to be synchronized
615 if (getDolGlobalString('LDAP_FIELD_MEMBER_STATUS')) {
616 $ldap = new Ldap();
617 $result = $ldap->connect_bind();
618
619 if ($result > 0) {
620 $info = $object->_load_ldap_info();
621 $dn = $object->_load_ldap_dn($info);
622 $olddn = $dn; // We know olddn=dn as we change only status
623
624 $result = $ldap->update($dn, $info, $user, $olddn);
625 }
626
627 if ($result <= 0) {
628 $this->errors[] = "ErrorLDAP ".$ldap->error;
629 }
630 }
631 }
632 } elseif ($action == 'MEMBER_DELETE') {
633 dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
634 if (getDolGlobalString('LDAP_MEMBER_ACTIVE') && getDolGlobalInt('LDAP_MEMBER_ACTIVE') == Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
635 $ldap = new Ldap();
636 $result = $ldap->connect_bind();
637
638 if ($result > 0) {
639 $info = $object->_load_ldap_info();
640 $dn = $object->_load_ldap_dn($info);
641
642 $result = $ldap->delete($dn);
643
644 // For member type
645 if (getDolGlobalString('LDAP_MEMBER_TYPE_ACTIVE') && getDolGlobalInt('LDAP_MEMBER_TYPE_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
646 if ($object->typeid > 0) {
647 require_once DOL_DOCUMENT_ROOT."/adherents/class/adherent_type.class.php";
648
649 /*
650 * Remove member in member type
651 */
652 $membertype = new AdherentType($this->db);
653 $membertype->fetch($object->typeid);
654 $membertype->listMembersForMemberType('a.rowid != '.$object->id, 1); // remove deleted member from the list
655
656 $oldinfo = $membertype->_load_ldap_info();
657 $olddn = $membertype->_load_ldap_dn($oldinfo);
658
659 // Verify if entry exist
660 $container = $membertype->_load_ldap_dn($oldinfo, 1);
661 $search = "(".$membertype->_load_ldap_dn($oldinfo, 2).")";
662 $records = $ldap->search($container, $search);
663 if (count($records) && $records['count'] == 0) {
664 $olddn = '';
665 }
666
667 $info = $membertype->_load_ldap_info(); // Contains all members, included the new one (insert already done before trigger call)
668 $dn = $membertype->_load_ldap_dn($info);
669
670 $result = $ldap->update($dn, $info, $user, $olddn);
671 }
672 }
673 }
674
675 if ($result <= 0) {
676 $this->errors[] = "ErrorLDAP ".$ldap->error;
677 }
678 }
679 } elseif ($action == 'MEMBER_TYPE_CREATE') {
680 // Members types
681 dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
682 if (getDolGlobalString('LDAP_MEMBER_TYPE_ACTIVE') && getDolGlobalInt('LDAP_MEMBER_TYPE_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
683 $ldap = new Ldap();
684 $result = $ldap->connect_bind();
685
686 if ($result > 0) {
687 $info = $object->_load_ldap_info();
688 $dn = $object->_load_ldap_dn($info);
689
690 // Get a gid number for objectclass PosixGroup
691 if (in_array('posixGroup', $info['objectclass'])) {
692 $info['gidNumber'] = $ldap->getNextGroupGid('LDAP_KEY_MEMBERS_TYPE');
693 }
694
695 $result = $ldap->add($dn, $info, $user);
696 }
697
698 if ($result <= 0) {
699 $this->errors[] = "ErrorLDAP ".$ldap->error;
700 }
701 }
702 } elseif ($action == 'MEMBER_TYPE_MODIFY') {
703 dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
704 if (getDolGlobalString('LDAP_MEMBER_TYPE_ACTIVE') && getDolGlobalInt('LDAP_MEMBER_TYPE_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
705 $ldap = new Ldap();
706 $result = $ldap->connect_bind();
707
708 if ($result > 0) {
709 if (empty($object->oldcopy) || !is_object($object->oldcopy)) {
710 dol_syslog("Trigger ".$action." was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
711 $object->oldcopy = clone $object;
712 }
713
714 $object->oldcopy->listMembersForMemberType('', 1);
715
716 $oldinfo = $object->oldcopy->_load_ldap_info();
717 $olddn = $object->oldcopy->_load_ldap_dn($oldinfo);
718
719 // Verify if entry exist
720 $container = $object->oldcopy->_load_ldap_dn($oldinfo, 1);
721 $search = "(".$object->oldcopy->_load_ldap_dn($oldinfo, 2).")";
722 $records = $ldap->search($container, $search);
723 if (count($records) && $records['count'] == 0) {
724 $olddn = '';
725 }
726
727 $object->listMembersForMemberType('', 1);
728
729 $info = $object->_load_ldap_info();
730 $dn = $object->_load_ldap_dn($info);
731
732 $result = $ldap->update($dn, $info, $user, $olddn);
733 }
734
735 if ($result <= 0) {
736 $this->errors[] = "ErrorLDAP ".$ldap->error;
737 }
738 }
739 } elseif ($action == 'MEMBER_TYPE_DELETE') {
740 dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
741 if (getDolGlobalString('LDAP_MEMBER_TYPE_ACTIVE') && getDolGlobalInt('LDAP_MEMBER_TYPE_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
742 $ldap = new Ldap();
743 $result = $ldap->connect_bind();
744
745 if ($result > 0) {
746 $info = $object->_load_ldap_info();
747 $dn = $object->_load_ldap_dn($info);
748
749 $result = $ldap->delete($dn);
750 }
751
752 if ($result <= 0) {
753 $this->errors[] = "ErrorLDAP ".$ldap->error;
754 }
755 }
756 }
757
758 return $result;
759 }
760}
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 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:124