ldap.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /* Copyright (C) 2006-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2006-2021 Regis Houssin <regis.houssin@inodbox.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/user/ldap.php
  20. * \ingroup ldap
  21. * \brief Page fiche LDAP utilisateur
  22. */
  23. // Load Dolibarr environment
  24. require '../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/lib/usergroups.lib.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/class/ldap.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/ldap.lib.php';
  28. // Load translation files required by page
  29. $langs->loadLangs(array('users', 'admin', 'companies', 'ldap'));
  30. $id = GETPOST('id', 'int');
  31. $action = GETPOST('action', 'aZ09');
  32. $contextpage = GETPOST('contextpage', 'aZ') ?GETPOST('contextpage', 'aZ') : 'userldap'; // To manage different context of search
  33. // Security check
  34. $socid = 0;
  35. if ($user->socid > 0) {
  36. $socid = $user->socid;
  37. }
  38. $feature2 = (($socid && $user->hasRight('user', 'self', 'creer')) ? '' : 'user');
  39. $result = restrictedArea($user, 'user', $id, 'user&user', $feature2);
  40. $object = new User($db);
  41. $object->fetch($id, '', '', 1);
  42. $object->getrights();
  43. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  44. $hookmanager->initHooks(array('usercard', 'userldap', 'globalcard'));
  45. /*
  46. * Actions
  47. */
  48. $parameters = array('id'=>$socid);
  49. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  50. if ($reshook < 0) {
  51. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  52. }
  53. if (empty($reshook)) {
  54. if ($action == 'dolibarr2ldap') {
  55. $ldap = new Ldap();
  56. $result = $ldap->connect_bind();
  57. if ($result > 0) {
  58. $info = $object->_load_ldap_info();
  59. $dn = $object->_load_ldap_dn($info);
  60. $olddn = $dn; // We can say that old dn = dn as we force synchro
  61. $result = $ldap->update($dn, $info, $user, $olddn);
  62. }
  63. if ($result >= 0) {
  64. setEventMessages($langs->trans("UserSynchronized"), null, 'mesgs');
  65. } else {
  66. setEventMessages($ldap->error, $ldap->errors, 'errors');
  67. }
  68. }
  69. }
  70. /*
  71. * View
  72. */
  73. $form = new Form($db);
  74. $person_name = !empty($object->firstname) ? $object->lastname.", ".$object->firstname : $object->lastname;
  75. $title = $person_name." - ".$langs->trans('LDAP');
  76. $help_url = '';
  77. llxHeader('', $title, $help_url);
  78. $head = user_prepare_head($object);
  79. $title = $langs->trans("User");
  80. print dol_get_fiche_head($head, 'ldap', $title, -1, 'user');
  81. $linkback = '';
  82. if ($user->hasRight('user', 'user', 'lire') || !empty($user->admin)) {
  83. $linkback = '<a href="'.DOL_URL_ROOT.'/user/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  84. }
  85. dol_banner_tab($object, 'id', $linkback, $user->hasRight('user', 'user', 'lire') || $user->admin);
  86. print '<div class="fichecenter">';
  87. print '<div class="underbanner clearboth"></div>';
  88. print '<table class="border centpercent tableforfield">';
  89. // Login
  90. print '<tr><td class="titlefield">'.$langs->trans("Login").'</td>';
  91. if ($object->ldap_sid) {
  92. print '<td class="warning">'.$langs->trans("LoginAccountDisableInDolibarr").'</td>';
  93. } else {
  94. print '<td>'.$object->login.'</td>';
  95. }
  96. print '</tr>';
  97. if (getDolGlobalString('LDAP_SERVER_TYPE') == "activedirectory") {
  98. $ldap = new Ldap();
  99. $result = $ldap->connect_bind();
  100. if ($result > 0) {
  101. $userSID = $ldap->getObjectSid($object->login);
  102. }
  103. print '<tr><td class="valigntop">'.$langs->trans("SID").'</td>';
  104. print '<td>'.$userSID.'</td>';
  105. print "</tr>\n";
  106. }
  107. // LDAP DN
  108. print '<tr><td>LDAP '.$langs->trans("LDAPUserDn").'</td><td class="valeur">'.getDolGlobalString('LDAP_USER_DN')."</td></tr>\n";
  109. // LDAP Cle
  110. print '<tr><td>LDAP '.$langs->trans("LDAPNamingAttribute").'</td><td class="valeur">'.getDolGlobalString('LDAP_KEY_USERS')."</td></tr>\n";
  111. // LDAP Server
  112. print '<tr><td>LDAP '.$langs->trans("Type").'</td><td class="valeur">'.getDolGlobalString('LDAP_SERVER_TYPE')."</td></tr>\n";
  113. print '<tr><td>LDAP '.$langs->trans("Version").'</td><td class="valeur">'.getDolGlobalString('LDAP_SERVER_PROTOCOLVERSION')."</td></tr>\n";
  114. print '<tr><td>LDAP '.$langs->trans("LDAPPrimaryServer").'</td><td class="valeur">'.getDolGlobalString('LDAP_SERVER_HOST')."</td></tr>\n";
  115. print '<tr><td>LDAP '.$langs->trans("LDAPSecondaryServer").'</td><td class="valeur">'.getDolGlobalString('LDAP_SERVER_HOST_SLAVE')."</td></tr>\n";
  116. print '<tr><td>LDAP '.$langs->trans("LDAPServerPort").'</td><td class="valeur">'.getDolGlobalString('LDAP_SERVER_PORT')."</td></tr>\n";
  117. print '</table>';
  118. print '</div>';
  119. print dol_get_fiche_end();
  120. /*
  121. * Action bar
  122. */
  123. print '<div class="tabsAction">';
  124. if (getDolGlobalInt('LDAP_SYNCHRO_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
  125. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=dolibarr2ldap">'.$langs->trans("ForceSynchronize").'</a>';
  126. }
  127. print "</div>\n";
  128. if (getDolGlobalInt('LDAP_SYNCHRO_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
  129. print "<br>\n";
  130. }
  131. // Affichage attributs LDAP
  132. print load_fiche_titre($langs->trans("LDAPInformationsForThisUser"));
  133. print '<table class="noborder centpercent">';
  134. print '<tr class="liste_titre">';
  135. print '<td>'.$langs->trans("LDAPAttributes").'</td>';
  136. print '<td>'.$langs->trans("Value").'</td>';
  137. print '</tr>';
  138. // Lecture LDAP
  139. $ldap = new Ldap();
  140. $result = $ldap->connect_bind();
  141. if ($result > 0) {
  142. $info = $object->_load_ldap_info();
  143. $dn = $object->_load_ldap_dn($info, 1);
  144. $search = "(".$object->_load_ldap_dn($info, 2).")";
  145. $records = $ldap->getAttribute($dn, $search);
  146. //print_r($records);
  147. // Affichage arbre
  148. if (((!is_numeric($records)) || $records != 0) && (!isset($records['count']) || $records['count'] > 0)) {
  149. if (!is_array($records)) {
  150. print '<tr class="oddeven"><td colspan="2"><span class="error">'.$langs->trans("ErrorFailedToReadLDAP").'</span></td></tr>';
  151. } else {
  152. $result = show_ldap_content($records, 0, $records['count'], true);
  153. }
  154. } else {
  155. print '<tr class="oddeven"><td colspan="2">'.$langs->trans("LDAPRecordNotFound").' (dn='.dol_escape_htmltag($dn).' - search='.dol_escape_htmltag($search).')</td></tr>';
  156. }
  157. $ldap->unbind();
  158. } else {
  159. setEventMessages($ldap->error, $ldap->errors, 'errors');
  160. }
  161. print '</table>';
  162. // End of page
  163. llxFooter();
  164. $db->close();