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 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);
535 $oldinfo =
$object->oldcopy->_load_ldap_info();
536 $olddn =
$object->oldcopy->_load_ldap_dn($oldinfo);
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) {
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);
551 $result = $ldap->update($dn, $info, $user, $olddn, $newrdn, $newparent);
555 require_once DOL_DOCUMENT_ROOT.
"/adherents/class/adherent_type.class.php";
561 $newmembertype->fetch(
$object->typeid);
562 $newmembertype->listMembersForMemberType(
'', 1);
564 $oldinfo = $newmembertype->_load_ldap_info();
565 $olddn = $newmembertype->_load_ldap_dn($oldinfo);
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) {
575 $info = $newmembertype->_load_ldap_info();
576 $dn = $newmembertype->_load_ldap_dn($info);
578 $result = $ldap->update($dn, $info, $user, $olddn);
585 $oldmembertype->fetch(
$object->oldcopy->typeid);
586 $oldmembertype->listMembersForMemberType(
'', 1);
588 $oldinfo = $oldmembertype->_load_ldap_info();
589 $olddn = $oldmembertype->_load_ldap_dn($oldinfo);
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) {
599 $info = $oldmembertype->_load_ldap_info();
600 $dn = $oldmembertype->_load_ldap_dn($info);
602 $result = $ldap->update($dn, $info, $user, $olddn);
608 $this->errors[] =
"ErrorLDAP ".$ldap->error;
611 } elseif ($action ==
'MEMBER_NEW_PASSWORD') {
612 '@phan-var-force Adherent|User $object';
613 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".
$object->id);
618 $result = $ldap->connectBind();
621 $info =
$object->_load_ldap_info();
622 $dn =
$object->_load_ldap_dn($info);
625 $result = $ldap->update($dn, $info, $user, $olddn);
629 $this->errors[] =
"ErrorLDAP ".$ldap->error;
633 } elseif ($action ==
'MEMBER_RESILIATE') {
634 '@phan-var-force Adherent|User $object';
635 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".
$object->id);
640 $result = $ldap->connectBind();
643 $info =
$object->_load_ldap_info();
644 $dn =
$object->_load_ldap_dn($info);
647 $result = $ldap->update($dn, $info, $user, $olddn);
651 $this->errors[] =
"ErrorLDAP ".$ldap->error;
655 } elseif ($action ==
'MEMBER_DELETE') {
656 '@phan-var-force Adherent|User $object';
657 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".
$object->id);
660 $result = $ldap->connectBind();
663 $info =
$object->_load_ldap_info();
664 $dn =
$object->_load_ldap_dn($info);
666 $result = $ldap->delete($dn);
671 require_once DOL_DOCUMENT_ROOT.
"/adherents/class/adherent_type.class.php";
677 $membertype->fetch(
$object->typeid);
678 $membertype->listMembersForMemberType(
'a.rowid != '.
$object->id, 1);
680 $oldinfo = $membertype->_load_ldap_info();
681 $olddn = $membertype->_load_ldap_dn($oldinfo);
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) {
691 $info = $membertype->_load_ldap_info();
692 $dn = $membertype->_load_ldap_dn($info);
694 $result = $ldap->update($dn, $info, $user, $olddn);
700 $this->errors[] =
"ErrorLDAP ".$ldap->error;
703 } elseif ($action ==
'MEMBER_TYPE_CREATE') {
704 '@phan-var-force Adherent|User $object';
706 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".
$object->id);
709 $result = $ldap->connectBind();
712 $info =
$object->_load_ldap_info();
713 $dn =
$object->_load_ldap_dn($info);
716 if (in_array(
'posixGroup', $info[
'objectclass'])) {
717 $info[
'gidNumber'] = $ldap->getNextGroupGid(
'LDAP_KEY_MEMBERS_TYPE');
720 $result = $ldap->add($dn, $info, $user);
724 $this->errors[] =
"ErrorLDAP ".$ldap->error;
727 } elseif ($action ==
'MEMBER_TYPE_MODIFY') {
728 '@phan-var-force Adherent|User $object';
729 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".
$object->id);
732 $result = $ldap->connectBind();
736 dol_syslog(
"Trigger ".$action.
" was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
740 $object->oldcopy->listMembersForMemberType(
'', 1);
742 $oldinfo =
$object->oldcopy->_load_ldap_info();
743 $olddn =
$object->oldcopy->_load_ldap_dn($oldinfo);
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) {
753 $object->listMembersForMemberType(
'', 1);
755 $info =
$object->_load_ldap_info();
756 $dn =
$object->_load_ldap_dn($info);
758 $result = $ldap->update($dn, $info, $user, $olddn);
762 $this->errors[] =
"ErrorLDAP ".$ldap->error;
765 } elseif ($action ==
'MEMBER_TYPE_DELETE') {
766 '@phan-var-force Adherent|User $object';
769 $result = $ldap->connectBind();
772 $info =
$object->_load_ldap_info();
773 $dn =
$object->_load_ldap_dn($info);
775 $result = $ldap->delete($dn);
779 $this->errors[] =
"ErrorLDAP ".$ldap->error;