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 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
86 $result = $ldap->connect_bind();
89 $info = $object->_load_ldap_info();
90 $dn = $object->_load_ldap_dn($info);
93 if ($ldap->serverType ==
"activedirectory") {
94 $info[
'userAccountControl'] = $conf->global->LDAP_USERACCOUNTCONTROL;
97 $result = $ldap->add($dn, $info, $user);
101 $this->error =
"ErrorLDAP ".$ldap->error;
104 } elseif ($action ==
'USER_MODIFY') {
105 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
108 $result = $ldap->connect_bind();
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;
116 $oldinfo = $object->oldcopy->_load_ldap_info();
117 $olddn = $object->oldcopy->_load_ldap_dn($oldinfo);
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) {
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);
132 $result = $ldap->update($dn, $info, $user, $olddn, $newrdn, $newparent);
134 if ($result > 0 && !empty($object->context[
'newgroupid'])) {
137 $usergroup->fetch($object->context[
'newgroupid'],
'',
true);
139 $oldinfo = $usergroup->_load_ldap_info();
140 $olddn = $usergroup->_load_ldap_dn($oldinfo);
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) {
150 $info = $usergroup->_load_ldap_info();
151 $dn = $usergroup->_load_ldap_dn($info);
153 $result = $ldap->update($dn, $info, $user, $olddn);
156 if ($result > 0 && !empty($object->context[
'oldgroupid'])) {
159 $usergroup->fetch($object->context[
'oldgroupid'],
'',
true);
161 $oldinfo = $usergroup->_load_ldap_info();
162 $olddn = $usergroup->_load_ldap_dn($oldinfo);
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) {
172 $info = $usergroup->_load_ldap_info();
173 $dn = $usergroup->_load_ldap_dn($info);
175 $result = $ldap->update($dn, $info, $user, $olddn);
180 $this->error =
"ErrorLDAP ".$ldap->error;
183 } elseif ($action ==
'USER_NEW_PASSWORD') {
184 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
187 $result = $ldap->connect_bind();
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;
195 $oldinfo = $object->oldcopy->_load_ldap_info();
196 $olddn = $object->oldcopy->_load_ldap_dn($oldinfo);
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) {
206 $info = $object->_load_ldap_info();
207 $dn = $object->_load_ldap_dn($info);
209 $result = $ldap->update($dn, $info, $user, $olddn);
213 $this->error =
"ErrorLDAP ".$ldap->error;
216 } elseif ($action ==
'USER_ENABLEDISABLE') {
217 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
220 $result = $ldap->connect_bind();
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;
235 $info[
'userAccountControl'] = $userAccountControl;
236 $resUpdate = $ldap->update($dn, $info, $user, $dn);
237 if ($resUpdate < 0) {
238 $this->error =
"ErrorLDAP " . $ldap->error;
242 $this->error =
"ErrorLDAP " . $ldap->error;
245 } elseif ($action ==
'USER_DELETE') {
246 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
249 $result = $ldap->connect_bind();
252 $info = $object->_load_ldap_info();
253 $dn = $object->_load_ldap_dn($info);
255 $result = $ldap->delete($dn);
259 $this->error =
"ErrorLDAP ".$ldap->error;
262 } elseif ($action ==
'USERGROUP_CREATE') {
264 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
267 $result = $ldap->connect_bind();
270 $info = $object->_load_ldap_info();
271 $dn = $object->_load_ldap_dn($info);
274 if (empty($info[
getDolGlobalString(
'LDAP_GROUP_FIELD_GROUPID')]) && in_array(
'posixGroup', $info[
'objectclass'])) {
275 $info[
'gidNumber'] = $ldap->getNextGroupGid(
'LDAP_KEY_GROUPS');
279 if (isset($info[
'member']) && empty($info[
'member'])) {
280 unset($info[
'member']);
283 $result = $ldap->add($dn, $info, $user);
286 if ($ldap->serverType ==
"activedirectory") {
287 $info[
'sAMAccountName'] = $object->name;
291 $this->error =
"ErrorLDAP ".$ldap->error;
294 } elseif ($action ==
'USERGROUP_MODIFY') {
295 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
298 $result = $ldap->connect_bind();
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;
306 $oldinfo = $object->oldcopy->_load_ldap_info();
307 $olddn = $object->oldcopy->_load_ldap_dn($oldinfo);
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) {
317 $info = $object->_load_ldap_info();
318 $dn = $object->_load_ldap_dn($info);
320 $result = $ldap->update($dn, $info, $user, $olddn);
324 $this->error =
"ErrorLDAP ".$ldap->error;
327 } elseif ($action ==
'USERGROUP_DELETE') {
328 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
331 $result = $ldap->connect_bind();
334 $info = $object->_load_ldap_info();
335 $dn = $object->_load_ldap_dn($info);
337 $result = $ldap->delete($dn);
341 $this->error =
"ErrorLDAP ".$ldap->error;
344 } elseif ($action ==
'CONTACT_CREATE') {
346 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
349 $result = $ldap->connect_bind();
352 $info = $object->_load_ldap_info();
353 $dn = $object->_load_ldap_dn($info);
355 $result = $ldap->add($dn, $info, $user);
359 $this->error =
"ErrorLDAP ".$ldap->error;
362 } elseif ($action ==
'CONTACT_MODIFY') {
363 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
366 $result = $ldap->connect_bind();
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;
374 $oldinfo = $object->oldcopy->_load_ldap_info();
375 $olddn = $object->oldcopy->_load_ldap_dn($oldinfo);
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) {
385 $info = $object->_load_ldap_info();
386 $dn = $object->_load_ldap_dn($info);
388 $result = $ldap->update($dn, $info, $user, $olddn);
392 $this->error =
"ErrorLDAP ".$ldap->error;
395 } elseif ($action ==
'CONTACT_DELETE') {
396 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
399 $result = $ldap->connect_bind();
402 $info = $object->_load_ldap_info();
403 $dn = $object->_load_ldap_dn($info);
405 $result = $ldap->delete($dn);
409 $this->error =
"ErrorLDAP ".$ldap->error;
412 } elseif ($action ==
'MEMBER_CREATE') {
414 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
417 $result = $ldap->connect_bind();
420 $info = $object->_load_ldap_info();
421 $dn = $object->_load_ldap_dn($info);
423 $result = $ldap->add($dn, $info, $user);
427 if ($object->typeid > 0) {
428 require_once DOL_DOCUMENT_ROOT.
"/adherents/class/adherent_type.class.php";
430 $membertype->fetch($object->typeid);
431 $membertype->listMembersForMemberType(
'', 1);
433 $oldinfo = $membertype->_load_ldap_info();
434 $olddn = $membertype->_load_ldap_dn($oldinfo);
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) {
444 $info = $membertype->_load_ldap_info();
445 $dn = $membertype->_load_ldap_dn($info);
447 $result = $ldap->update($dn, $info, $user, $olddn);
453 $this->error =
"ErrorLDAP ".$ldap->error;
456 } elseif ($action ==
'MEMBER_VALIDATE') {
457 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
462 $result = $ldap->connect_bind();
465 $info = $object->_load_ldap_info();
466 $dn = $object->_load_ldap_dn($info);
469 $result = $ldap->update($dn, $info, $user, $olddn);
473 $this->error =
"ErrorLDAP ".$ldap->error;
477 } elseif ($action ==
'MEMBER_SUBSCRIPTION') {
478 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
487 $result = $ldap->connect_bind();
490 $info = $object->_load_ldap_info();
491 $dn = $object->_load_ldap_dn($info);
494 $result = $ldap->update($dn, $info, $user, $olddn);
498 $this->error =
"ErrorLDAP ".$ldap->error;
502 } elseif ($action ==
'MEMBER_MODIFY') {
503 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
506 $result = $ldap->connect_bind();
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;
514 $oldinfo = $object->oldcopy->_load_ldap_info();
515 $olddn = $object->oldcopy->_load_ldap_dn($oldinfo);
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) {
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);
530 $result = $ldap->update($dn, $info, $user, $olddn, $newrdn, $newparent);
534 require_once DOL_DOCUMENT_ROOT.
"/adherents/class/adherent_type.class.php";
540 $newmembertype->fetch($object->typeid);
541 $newmembertype->listMembersForMemberType(
'', 1);
543 $oldinfo = $newmembertype->_load_ldap_info();
544 $olddn = $newmembertype->_load_ldap_dn($oldinfo);
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) {
554 $info = $newmembertype->_load_ldap_info();
555 $dn = $newmembertype->_load_ldap_dn($info);
557 $result = $ldap->update($dn, $info, $user, $olddn);
559 if ($object->oldcopy->typeid != $object->typeid) {
564 $oldmembertype->fetch($object->oldcopy->typeid);
565 $oldmembertype->listMembersForMemberType(
'', 1);
567 $oldinfo = $oldmembertype->_load_ldap_info();
568 $olddn = $oldmembertype->_load_ldap_dn($oldinfo);
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) {
578 $info = $oldmembertype->_load_ldap_info();
579 $dn = $oldmembertype->_load_ldap_dn($info);
581 $result = $ldap->update($dn, $info, $user, $olddn);
587 $this->errors[] =
"ErrorLDAP ".$ldap->error;
590 } elseif ($action ==
'MEMBER_NEW_PASSWORD') {
591 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
596 $result = $ldap->connect_bind();
599 $info = $object->_load_ldap_info();
600 $dn = $object->_load_ldap_dn($info);
603 $result = $ldap->update($dn, $info, $user, $olddn);
607 $this->errors[] =
"ErrorLDAP ".$ldap->error;
611 } elseif ($action ==
'MEMBER_RESILIATE') {
612 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
617 $result = $ldap->connect_bind();
620 $info = $object->_load_ldap_info();
621 $dn = $object->_load_ldap_dn($info);
624 $result = $ldap->update($dn, $info, $user, $olddn);
628 $this->errors[] =
"ErrorLDAP ".$ldap->error;
632 } elseif ($action ==
'MEMBER_DELETE') {
633 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
636 $result = $ldap->connect_bind();
639 $info = $object->_load_ldap_info();
640 $dn = $object->_load_ldap_dn($info);
642 $result = $ldap->delete($dn);
646 if ($object->typeid > 0) {
647 require_once DOL_DOCUMENT_ROOT.
"/adherents/class/adherent_type.class.php";
653 $membertype->fetch($object->typeid);
654 $membertype->listMembersForMemberType(
'a.rowid != '.$object->id, 1);
656 $oldinfo = $membertype->_load_ldap_info();
657 $olddn = $membertype->_load_ldap_dn($oldinfo);
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) {
667 $info = $membertype->_load_ldap_info();
668 $dn = $membertype->_load_ldap_dn($info);
670 $result = $ldap->update($dn, $info, $user, $olddn);
676 $this->errors[] =
"ErrorLDAP ".$ldap->error;
679 } elseif ($action ==
'MEMBER_TYPE_CREATE') {
681 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
684 $result = $ldap->connect_bind();
687 $info = $object->_load_ldap_info();
688 $dn = $object->_load_ldap_dn($info);
691 if (in_array(
'posixGroup', $info[
'objectclass'])) {
692 $info[
'gidNumber'] = $ldap->getNextGroupGid(
'LDAP_KEY_MEMBERS_TYPE');
695 $result = $ldap->add($dn, $info, $user);
699 $this->errors[] =
"ErrorLDAP ".$ldap->error;
702 } elseif ($action ==
'MEMBER_TYPE_MODIFY') {
703 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
706 $result = $ldap->connect_bind();
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;
714 $object->oldcopy->listMembersForMemberType(
'', 1);
716 $oldinfo = $object->oldcopy->_load_ldap_info();
717 $olddn = $object->oldcopy->_load_ldap_dn($oldinfo);
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) {
727 $object->listMembersForMemberType(
'', 1);
729 $info = $object->_load_ldap_info();
730 $dn = $object->_load_ldap_dn($info);
732 $result = $ldap->update($dn, $info, $user, $olddn);
736 $this->errors[] =
"ErrorLDAP ".$ldap->error;
739 } elseif ($action ==
'MEMBER_TYPE_DELETE') {
740 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
743 $result = $ldap->connect_bind();
746 $info = $object->_load_ldap_info();
747 $dn = $object->_load_ldap_dn($info);
749 $result = $ldap->delete($dn);
753 $this->errors[] =
"ErrorLDAP ".$ldap->error;