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