64 if (empty($conf->ldap) || empty($conf->ldap->enabled)) {
67 if (defined(
'DISABLE_LDAP_SYNCHRO')) {
71 if (!function_exists(
'ldap_connect')) {
72 dol_syslog(
"Warning, module LDAP is enabled but LDAP functions not available in this PHP", LOG_WARNING);
76 require_once DOL_DOCUMENT_ROOT.
"/core/class/ldap.class.php";
77 require_once DOL_DOCUMENT_ROOT.
"/user/class/usergroup.class.php";
82 if ($action ==
'USER_CREATE') {
83 '@phan-var-force User $object';
84 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".
$object->id);
87 $result = $ldap->connectBind();
90 $info =
$object->_load_ldap_info();
91 $dn =
$object->_load_ldap_dn($info);
94 if ($ldap->serverType ==
"activedirectory") {
98 $result = $ldap->add($dn, $info, $user);
102 $this->error =
"ErrorLDAP ".$ldap->error;
105 } elseif ($action ==
'USER_MODIFY') {
106 '@phan-var-force User $object';
107 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".
$object->id);
110 $result = $ldap->connectBind();
114 dol_syslog(
"Trigger ".$action.
" was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
118 $oldinfo =
$object->oldcopy->_load_ldap_info();
119 $olddn =
$object->oldcopy->_load_ldap_dn($oldinfo);
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) {
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);
134 $result = $ldap->update($dn, $info, $user, $olddn, $newrdn, $newparent);
136 if ($result > 0 && !empty(
$object->context[
'newgroupid'])) {
139 $usergroup->fetch(
$object->context[
'newgroupid'],
'',
true);
141 $oldinfo = $usergroup->_load_ldap_info();
142 $olddn = $usergroup->_load_ldap_dn($oldinfo);
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) {
152 $info = $usergroup->_load_ldap_info();
153 $dn = $usergroup->_load_ldap_dn($info);
155 $result = $ldap->update($dn, $info, $user, $olddn);
158 if ($result > 0 && !empty(
$object->context[
'oldgroupid'])) {
161 $usergroup->fetch(
$object->context[
'oldgroupid'],
'',
true);
163 $oldinfo = $usergroup->_load_ldap_info();
164 $olddn = $usergroup->_load_ldap_dn($oldinfo);
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) {
174 $info = $usergroup->_load_ldap_info();
175 $dn = $usergroup->_load_ldap_dn($info);
177 $result = $ldap->update($dn, $info, $user, $olddn);
182 $this->error =
"ErrorLDAP ".$ldap->error;
185 } elseif ($action ==
'USER_NEW_PASSWORD') {
186 '@phan-var-force User $object';
187 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".
$object->id);
190 $result = $ldap->connectBind();
194 dol_syslog(
"Trigger ".$action.
" was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
198 $oldinfo =
$object->oldcopy->_load_ldap_info();
199 $olddn =
$object->oldcopy->_load_ldap_dn($oldinfo);
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) {
209 $info =
$object->_load_ldap_info();
210 $dn =
$object->_load_ldap_dn($info);
212 $result = $ldap->update($dn, $info, $user, $olddn);
216 $this->error =
"ErrorLDAP ".$ldap->error;
219 } elseif ($action ==
'USER_ENABLEDISABLE') {
220 '@phan-var-force User $object';
221 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".
$object->id);
224 $result = $ldap->connectBind();
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;
239 $info[
'userAccountControl'] = $userAccountControl;
241 $resUpdate = $ldap->update($dn, $info, $user, $dn);
242 if ($resUpdate < 0) {
243 $this->error =
"ErrorLDAP " . $ldap->error;
247 $this->error =
"ErrorLDAP " . $ldap->error;
250 } elseif ($action ==
'USER_DELETE') {
251 '@phan-var-force User $object';
252 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".
$object->id);
255 $result = $ldap->connectBind();
258 $info =
$object->_load_ldap_info();
259 $dn =
$object->_load_ldap_dn($info);
261 $result = $ldap->delete($dn);
265 $this->error =
"ErrorLDAP ".$ldap->error;
268 } elseif ($action ==
'USERGROUP_CREATE') {
269 '@phan-var-force UserGroup $object';
271 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".
$object->id);
274 $result = $ldap->connectBind();
277 $info =
$object->_load_ldap_info();
278 $dn =
$object->_load_ldap_dn($info);
281 if (empty($info[
getDolGlobalString(
'LDAP_GROUP_FIELD_GROUPID')]) && in_array(
'posixGroup', $info[
'objectclass'])) {
282 $info[
'gidNumber'] = $ldap->getNextGroupGid(
'LDAP_KEY_GROUPS');
286 if (isset($info[
'member']) && empty($info[
'member'])) {
287 unset($info[
'member']);
290 $result = $ldap->add($dn, $info, $user);
293 if ($ldap->serverType ==
"activedirectory") {
294 $info[
'sAMAccountName'] =
$object->name;
298 $this->error =
"ErrorLDAP ".$ldap->error;
301 } elseif ($action ==
'USERGROUP_MODIFY') {
302 '@phan-var-force UserGroup $object';
303 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".
$object->id);
306 $result = $ldap->connectBind();
310 dol_syslog(
"Trigger ".$action.
" was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
314 $oldinfo =
$object->oldcopy->_load_ldap_info();
315 $olddn =
$object->oldcopy->_load_ldap_dn($oldinfo);
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) {
325 $info =
$object->_load_ldap_info();
326 $dn =
$object->_load_ldap_dn($info);
328 $result = $ldap->update($dn, $info, $user, $olddn);
332 $this->error =
"ErrorLDAP ".$ldap->error;
335 } elseif ($action ==
'USERGROUP_DELETE') {
336 '@phan-var-force UserGroup $object';
337 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".
$object->id);
340 $result = $ldap->connectBind();
343 $info =
$object->_load_ldap_info();
344 $dn =
$object->_load_ldap_dn($info);
346 $result = $ldap->delete($dn);
350 $this->error =
"ErrorLDAP ".$ldap->error;
353 } elseif ($action ==
'CONTACT_CREATE') {
354 '@phan-var-force Contact $object';
356 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".
$object->id);
359 $result = $ldap->connectBind();
362 $info =
$object->_load_ldap_info();
363 $dn =
$object->_load_ldap_dn($info);
365 $result = $ldap->add($dn, $info, $user);
369 $this->error =
"ErrorLDAP ".$ldap->error;
372 } elseif ($action ==
'CONTACT_MODIFY') {
373 '@phan-var-force Contact $object';
374 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".
$object->id);
377 $result = $ldap->connectBind();
381 dol_syslog(
"Trigger ".$action.
" was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
385 $oldinfo =
$object->oldcopy->_load_ldap_info();
386 $olddn =
$object->oldcopy->_load_ldap_dn($oldinfo);
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) {
396 $info =
$object->_load_ldap_info();
397 $dn =
$object->_load_ldap_dn($info);
399 $result = $ldap->update($dn, $info, $user, $olddn);
403 $this->error =
"ErrorLDAP ".$ldap->error;
406 } elseif ($action ==
'CONTACT_DELETE') {
407 '@phan-var-force Contact $object';
408 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".
$object->id);
411 $result = $ldap->connectBind();
414 $info =
$object->_load_ldap_info();
415 $dn =
$object->_load_ldap_dn($info);
417 $result = $ldap->delete($dn);
421 $this->error =
"ErrorLDAP ".$ldap->error;
424 } elseif ($action ==
'MEMBER_CREATE') {
425 '@phan-var-force Adherent $object';
427 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".
$object->id);
430 $result = $ldap->connectBind();
433 $info =
$object->_load_ldap_info();
434 $dn =
$object->_load_ldap_dn($info);
436 $result = $ldap->add($dn, $info, $user);
441 require_once DOL_DOCUMENT_ROOT.
"/adherents/class/adherent_type.class.php";
443 $membertype->fetch(
$object->typeid);
444 $membertype->listMembersForMemberType(
'', 1);
446 $oldinfo = $membertype->_load_ldap_info();
447 $olddn = $membertype->_load_ldap_dn($oldinfo);
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) {
457 $info = $membertype->_load_ldap_info();
458 $dn = $membertype->_load_ldap_dn($info);
460 $result = $ldap->update($dn, $info, $user, $olddn);
466 $this->error =
"ErrorLDAP ".$ldap->error;
469 } elseif ($action ==
'MEMBER_VALIDATE') {
470 '@phan-var-force UserGroup $object';
471 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".
$object->id);
476 $result = $ldap->connectBind();
479 $info =
$object->_load_ldap_info();
480 $dn =
$object->_load_ldap_dn($info);
483 $result = $ldap->update($dn, $info, $user, $olddn);
487 $this->error =
"ErrorLDAP ".$ldap->error;
491 } elseif ($action ==
'MEMBER_SUBSCRIPTION') {
492 '@phan-var-force Adherent|User $object';
493 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".
$object->id);
502 $result = $ldap->connectBind();
505 $info =
$object->_load_ldap_info();
506 $dn =
$object->_load_ldap_dn($info);
509 $result = $ldap->update($dn, $info, $user, $olddn);
513 $this->error =
"ErrorLDAP ".$ldap->error;
517 } elseif ($action ==
'MEMBER_MODIFY') {
518 '@phan-var-force Adherent|User $object';
519 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".
$object->id);
522 $result = $ldap->connectBind();
526 dol_syslog(
"Trigger ".$action.
" was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
530 $oldinfo =
$object->oldcopy->_load_ldap_info();
531 $olddn =
$object->oldcopy->_load_ldap_dn($oldinfo);
534 $container =
$object->oldcopy->_load_ldap_dn($oldinfo, 1);
535 $search =
"(".$object->oldcopy->_load_ldap_dn($oldinfo, 2).
")";
536 $records = $ldap->search($container, $search);
537 if (count($records) && $records[
'count'] == 0) {
541 $info =
$object->_load_ldap_info();
542 $dn =
$object->_load_ldap_dn($info);
543 $newrdn =
$object->_load_ldap_dn($info, 2);
544 $newparent =
$object->_load_ldap_dn($info, 1);
546 $result = $ldap->update($dn, $info, $user, $olddn, $newrdn, $newparent);
550 require_once DOL_DOCUMENT_ROOT.
"/adherents/class/adherent_type.class.php";
556 $newmembertype->fetch(
$object->typeid);
557 $newmembertype->listMembersForMemberType(
'', 1);
559 $oldinfo = $newmembertype->_load_ldap_info();
560 $olddn = $newmembertype->_load_ldap_dn($oldinfo);
563 $container = $newmembertype->_load_ldap_dn($oldinfo, 1);
564 $search =
"(".$newmembertype->_load_ldap_dn($oldinfo, 2).
")";
565 $records = $ldap->search($container, $search);
566 if (count($records) && $records[
'count'] == 0) {
570 $info = $newmembertype->_load_ldap_info();
571 $dn = $newmembertype->_load_ldap_dn($info);
573 $result = $ldap->update($dn, $info, $user, $olddn);
580 $oldmembertype->fetch(
$object->oldcopy->typeid);
581 $oldmembertype->listMembersForMemberType(
'', 1);
583 $oldinfo = $oldmembertype->_load_ldap_info();
584 $olddn = $oldmembertype->_load_ldap_dn($oldinfo);
587 $container = $oldmembertype->_load_ldap_dn($oldinfo, 1);
588 $search =
"(".$oldmembertype->_load_ldap_dn($oldinfo, 2).
")";
589 $records = $ldap->search($container, $search);
590 if (count($records) && $records[
'count'] == 0) {
594 $info = $oldmembertype->_load_ldap_info();
595 $dn = $oldmembertype->_load_ldap_dn($info);
597 $result = $ldap->update($dn, $info, $user, $olddn);
603 $this->errors[] =
"ErrorLDAP ".$ldap->error;
606 } elseif ($action ==
'MEMBER_NEW_PASSWORD') {
607 '@phan-var-force Adherent|User $object';
608 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".
$object->id);
613 $result = $ldap->connectBind();
616 $info =
$object->_load_ldap_info();
617 $dn =
$object->_load_ldap_dn($info);
620 $result = $ldap->update($dn, $info, $user, $olddn);
624 $this->errors[] =
"ErrorLDAP ".$ldap->error;
628 } elseif ($action ==
'MEMBER_RESILIATE') {
629 '@phan-var-force Adherent|User $object';
630 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".
$object->id);
635 $result = $ldap->connectBind();
638 $info =
$object->_load_ldap_info();
639 $dn =
$object->_load_ldap_dn($info);
642 $result = $ldap->update($dn, $info, $user, $olddn);
646 $this->errors[] =
"ErrorLDAP ".$ldap->error;
650 } elseif ($action ==
'MEMBER_DELETE') {
651 '@phan-var-force Adherent|User $object';
652 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".
$object->id);
655 $result = $ldap->connectBind();
658 $info =
$object->_load_ldap_info();
659 $dn =
$object->_load_ldap_dn($info);
661 $result = $ldap->delete($dn);
666 require_once DOL_DOCUMENT_ROOT.
"/adherents/class/adherent_type.class.php";
672 $membertype->fetch(
$object->typeid);
673 $membertype->listMembersForMemberType(
'a.rowid != '.
$object->id, 1);
675 $oldinfo = $membertype->_load_ldap_info();
676 $olddn = $membertype->_load_ldap_dn($oldinfo);
679 $container = $membertype->_load_ldap_dn($oldinfo, 1);
680 $search =
"(".$membertype->_load_ldap_dn($oldinfo, 2).
")";
681 $records = $ldap->search($container, $search);
682 if (count($records) && $records[
'count'] == 0) {
686 $info = $membertype->_load_ldap_info();
687 $dn = $membertype->_load_ldap_dn($info);
689 $result = $ldap->update($dn, $info, $user, $olddn);
695 $this->errors[] =
"ErrorLDAP ".$ldap->error;
698 } elseif ($action ==
'MEMBER_TYPE_CREATE') {
699 '@phan-var-force Adherent|User $object';
701 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".
$object->id);
704 $result = $ldap->connectBind();
707 $info =
$object->_load_ldap_info();
708 $dn =
$object->_load_ldap_dn($info);
711 if (in_array(
'posixGroup', $info[
'objectclass'])) {
712 $info[
'gidNumber'] = $ldap->getNextGroupGid(
'LDAP_KEY_MEMBERS_TYPE');
715 $result = $ldap->add($dn, $info, $user);
719 $this->errors[] =
"ErrorLDAP ".$ldap->error;
722 } elseif ($action ==
'MEMBER_TYPE_MODIFY') {
723 '@phan-var-force Adherent|User $object';
724 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".
$object->id);
727 $result = $ldap->connectBind();
731 dol_syslog(
"Trigger ".$action.
" was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
735 $object->oldcopy->listMembersForMemberType(
'', 1);
737 $oldinfo =
$object->oldcopy->_load_ldap_info();
738 $olddn =
$object->oldcopy->_load_ldap_dn($oldinfo);
741 $container =
$object->oldcopy->_load_ldap_dn($oldinfo, 1);
742 $search =
"(".$object->oldcopy->_load_ldap_dn($oldinfo, 2).
")";
743 $records = $ldap->search($container, $search);
744 if (count($records) && $records[
'count'] == 0) {
748 $object->listMembersForMemberType(
'', 1);
750 $info =
$object->_load_ldap_info();
751 $dn =
$object->_load_ldap_dn($info);
753 $result = $ldap->update($dn, $info, $user, $olddn);
757 $this->errors[] =
"ErrorLDAP ".$ldap->error;
760 } elseif ($action ==
'MEMBER_TYPE_DELETE') {
761 '@phan-var-force Adherent|User $object';
764 $result = $ldap->connectBind();
767 $info =
$object->_load_ldap_info();
768 $dn =
$object->_load_ldap_dn($info);
770 $result = $ldap->delete($dn);
774 $this->errors[] =
"ErrorLDAP ".$ldap->error;