ldap_groups.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <?php
  2. /* Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004 Sebastien Di Cintio <sdicintio@ressource-toi.org>
  4. * Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
  5. * Copyright (C) 2005 Regis Houssin <regis.houssin@inodbox.com>
  6. * Copyright (C) 2006-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  7. * Copyright (C) 2011-2013 Juanjo Menent <jmenent@2byte.es>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/admin/ldap_groups.php
  24. * \ingroup ldap
  25. * \brief Page to setup LDAP synchronization for groups
  26. */
  27. require '../main.inc.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  29. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/user/class/usergroup.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/class/ldap.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/lib/ldap.lib.php';
  33. // Load translation files required by the page
  34. $langs->loadLangs(array("admin", "errors"));
  35. if (!$user->admin) {
  36. accessforbidden();
  37. }
  38. $action = GETPOST('action', 'aZ09');
  39. /*
  40. * Actions
  41. */
  42. if ($action == 'setvalue' && $user->admin) {
  43. $error = 0;
  44. $db->begin();
  45. if (!dolibarr_set_const($db, 'LDAP_GROUP_DN', GETPOST("group", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) {
  46. $error++;
  47. }
  48. if (!dolibarr_set_const($db, 'LDAP_GROUP_OBJECT_CLASS', GETPOST("objectclass", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) {
  49. $error++;
  50. }
  51. if (!dolibarr_set_const($db, 'LDAP_GROUP_FILTER', GETPOST("filter"), 'chaine', 0, '', $conf->entity)) {
  52. $error++;
  53. }
  54. if (!dolibarr_set_const($db, 'LDAP_GROUP_FIELD_FULLNAME', GETPOST("fieldfullname", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) {
  55. $error++;
  56. }
  57. //if (! dolibarr_set_const($db, 'LDAP_GROUP_FIELD_NAME',GETPOST("fieldname", 'alphanohtml'),'chaine',0,'',$conf->entity)) $error++;
  58. if (!dolibarr_set_const($db, 'LDAP_GROUP_FIELD_DESCRIPTION', GETPOST("fielddescription", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) {
  59. $error++;
  60. }
  61. if (!dolibarr_set_const($db, 'LDAP_GROUP_FIELD_GROUPMEMBERS', GETPOST("fieldgroupmembers", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) {
  62. $error++;
  63. }
  64. if (!dolibarr_set_const($db, 'LDAP_GROUP_FIELD_GROUPID', GETPOST("fieldgroupid", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) {
  65. $error++;
  66. }
  67. // This one must be after the others
  68. $valkey = '';
  69. $key = GETPOST("key");
  70. if ($key) {
  71. $valkey = $conf->global->$key;
  72. }
  73. if (!dolibarr_set_const($db, 'LDAP_KEY_GROUPS', $valkey, 'chaine', 0, '', $conf->entity)) {
  74. $error++;
  75. }
  76. if (!$error) {
  77. $db->commit();
  78. setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
  79. } else {
  80. $db->rollback();
  81. dol_print_error($db);
  82. }
  83. }
  84. /*
  85. * View
  86. */
  87. $form = new Form($db);
  88. llxHeader('', $langs->trans("LDAPSetup"), 'EN:Module_LDAP_En|FR:Module_LDAP|ES:M&oacute;dulo_LDAP');
  89. $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
  90. print load_fiche_titre($langs->trans("LDAPSetup"), $linkback, 'title_setup');
  91. $head = ldap_prepare_head();
  92. // Test si fonction LDAP actives
  93. if (!function_exists("ldap_connect")) {
  94. setEventMessages($langs->trans("LDAPFunctionsNotAvailableOnPHP"), null, 'errors');
  95. }
  96. print dol_get_fiche_head($head, 'groups', $langs->trans("LDAPSetup"), -1);
  97. print $langs->trans("LDAPDescGroups").'<br>';
  98. print '<br>';
  99. print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?action=setvalue&token='.newtoken().'">';
  100. print '<input type="hidden" name="token" value="'.newToken().'">';
  101. print '<table class="noborder centpercent">';
  102. print '<tr class="liste_titre">';
  103. print '<td colspan="4">'.$langs->trans("LDAPSynchronizeGroups").'</td>';
  104. print "</tr>\n";
  105. // DN pour les groupes
  106. print '<!-- LDAP_GROUP_DN -->';
  107. print '<tr class="oddeven"><td><span class="fieldrequired">'.$langs->trans("LDAPGroupDn").'</span></td><td>';
  108. print '<input size="48" type="text" name="group" value="'.$conf->global->LDAP_GROUP_DN.'">';
  109. print '</td><td>'.$langs->trans("LDAPGroupDnExample").'</td>';
  110. print '<td>&nbsp;</td>';
  111. print '</tr>';
  112. // List of object class used to define attributes in structure
  113. print '<!-- LDAP_GROUP_OBJECT_CLASS -->';
  114. print '<tr class="oddeven"><td><span class="fieldrequired">'.$langs->trans("LDAPGroupObjectClassList").'</span></td><td>';
  115. print '<input size="48" type="text" name="objectclass" value="'.$conf->global->LDAP_GROUP_OBJECT_CLASS.'">';
  116. print '</td><td>'.$langs->trans("LDAPGroupObjectClassListExample").'</td>';
  117. print '<td>&nbsp;</td>';
  118. print '</tr>';
  119. // Filter, used to filter search
  120. print '<!-- LDAP_GROUP_FILTER -->';
  121. print '<tr class="oddeven"><td>'.$langs->trans("LDAPFilterConnection").'</td><td>';
  122. print '<input size="48" type="text" name="filter" value="'.$conf->global->LDAP_GROUP_FILTER.'">';
  123. print '</td><td>'.$langs->trans("LDAPGroupFilterExample").'</td>';
  124. print '<td></td>';
  125. print '</tr>';
  126. print '</table>';
  127. print '<br>';
  128. print '<table class="noborder centpercent">';
  129. print '<tr class="liste_titre">';
  130. print '<td>'.$langs->trans("LDAPDolibarrMapping").'</td>';
  131. print '<td colspan="2">'.$langs->trans("LDAPLdapMapping").'</td>';
  132. print '<td class="right">'.$langs->trans("LDAPNamingAttribute").'</td>';
  133. print "</tr>\n";
  134. // Filtre
  135. // Common name
  136. print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldName").'</td><td>';
  137. print '<input size="25" type="text" name="fieldfullname" value="'.$conf->global->LDAP_GROUP_FIELD_FULLNAME.'">';
  138. print '</td><td>'.$langs->trans("LDAPFieldCommonNameExample").'</td>';
  139. print '<td class="right"><input type="radio" name="key" value="LDAP_GROUP_FIELD_FULLNAME"'.(($conf->global->LDAP_KEY_GROUPS && $conf->global->LDAP_KEY_GROUPS == $conf->global->LDAP_GROUP_FIELD_FULLNAME) ? ' checked' : '')."></td>";
  140. print '</tr>';
  141. // Name
  142. /*
  143. print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldName").'</td><td>';
  144. print '<input size="25" type="text" name="fieldname" value="'.$conf->global->LDAP_GROUP_FIELD_NAME.'">';
  145. print '</td><td>'.$langs->trans("LDAPFieldNameExample").'</td>';
  146. print '<td class="right"><input type="radio" name="key" value="'.$conf->global->LDAP_GROUP_FIELD_NAME.'"'.($conf->global->LDAP_KEY_GROUPS==$conf->global->LDAP_GROUP_FIELD_NAME?' checked':'')."></td>";
  147. print '</tr>';
  148. */
  149. // Description
  150. print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldDescription").'</td><td>';
  151. print '<input size="25" type="text" name="fielddescription" value="'.$conf->global->LDAP_GROUP_FIELD_DESCRIPTION.'">';
  152. print '</td><td>'.$langs->trans("LDAPFieldDescriptionExample").'</td>';
  153. print '<td class="right"><input type="radio" name="key" value="LDAP_GROUP_FIELD_DESCRIPTION"'.(($conf->global->LDAP_KEY_GROUPS && $conf->global->LDAP_KEY_GROUPS == $conf->global->LDAP_GROUP_FIELD_DESCRIPTION) ? ' checked' : '')."></td>";
  154. print '</tr>';
  155. // User group
  156. print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldGroupMembers").'</td><td>';
  157. print '<input size="25" type="text" name="fieldgroupmembers" value="'.$conf->global->LDAP_GROUP_FIELD_GROUPMEMBERS.'">';
  158. print '</td><td>'.$langs->trans("LDAPFieldGroupMembersExample").'</td>';
  159. print '<td class="right"><input type="radio" name="key" value="LDAP_GROUP_FIELD_GROUPMEMBERS"'.(($conf->global->LDAP_KEY_GROUPS && $conf->global->LDAP_KEY_GROUPS == $conf->global->LDAP_GROUP_FIELD_GROUPMEMBERS) ? ' checked' : '')."></td>";
  160. print '</tr>';
  161. // Group id
  162. print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldGroupid").'</td><td>';
  163. print '<input size="25" type="text" name="fieldgroupid" value="'.$conf->global->LDAP_GROUP_FIELD_GROUPID.'">';
  164. print '</td><td>'.$langs->trans("LDAPFieldGroupidExample").'</td>';
  165. print '<td class="right">&nbsp;</td>';
  166. print '</tr>';
  167. print '</table>';
  168. print info_admin($langs->trans("LDAPDescValues"));
  169. print dol_get_fiche_end();
  170. print $form->buttonsSaveCancel("Modify", '');
  171. print '</form>';
  172. /*
  173. * Test de la connexion
  174. */
  175. if ($conf->global->LDAP_SYNCHRO_ACTIVE == 'dolibarr2ldap') {
  176. $butlabel = $langs->trans("LDAPTestSynchroGroup");
  177. $testlabel = 'testgroup';
  178. $key = $conf->global->LDAP_KEY_GROUPS;
  179. $dn = $conf->global->LDAP_GROUP_DN;
  180. $objectclass = $conf->global->LDAP_GROUP_OBJECT_CLASS;
  181. show_ldap_test_button($butlabel, $testlabel, $key, $dn, $objectclass);
  182. } elseif ($conf->global->LDAP_SYNCHRO_ACTIVE == 'ldap2dolibarr') {
  183. $butlabel = $langs->trans("LDAPTestSearch");
  184. $testlabel = 'testsearchgroup';
  185. $key = $conf->global->LDAP_KEY_GROUPS;
  186. $dn = $conf->global->LDAP_GROUP_DN;
  187. $objectclass = $conf->global->LDAP_GROUP_OBJECT_CLASS;
  188. show_ldap_test_button($butlabel, $testlabel, $key, $dn, $objectclass);
  189. }
  190. if (function_exists("ldap_connect")) {
  191. if ($action == 'testgroup') {
  192. // Creation objet
  193. $object = new UserGroup($db);
  194. $object->initAsSpecimen();
  195. // Test synchro
  196. $ldap = new Ldap();
  197. $result = $ldap->connect_bind();
  198. if ($result > 0) {
  199. $info = $object->_load_ldap_info();
  200. $dn = $object->_load_ldap_dn($info);
  201. // Get a gid number for objectclass PosixGroup
  202. if (in_array('posixGroup', $info['objectclass'])) {
  203. $info['gidNumber'] = $ldap->getNextGroupGid('LDAP_KEY_GROUPS');
  204. }
  205. $result1 = $ldap->delete($dn); // To be sure to delete existing records
  206. $result2 = $ldap->add($dn, $info, $user); // Now the test
  207. $result3 = $ldap->delete($dn); // Clean what we did
  208. if ($result2 > 0) {
  209. print img_picto('', 'info').' ';
  210. print '<font class="ok">'.$langs->trans("LDAPSynchroOK").'</font><br>';
  211. } else {
  212. print img_picto('', 'error').' ';
  213. print '<font class="error">'.$langs->trans("LDAPSynchroKOMayBePermissions");
  214. print ': '.$ldap->error;
  215. print '</font><br>';
  216. print $langs->trans("ErrorLDAPMakeManualTest", $conf->ldap->dir_temp).'<br>';
  217. }
  218. print "<br>\n";
  219. print "LDAP input file used for test:<br><br>\n";
  220. print nl2br($ldap->dump_content($dn, $info));
  221. print "\n<br>";
  222. } else {
  223. print img_picto('', 'error').' ';
  224. print '<font class="error">'.$langs->trans("LDAPSynchroKO");
  225. print ': '.$ldap->error;
  226. print '</font><br>';
  227. print $langs->trans("ErrorLDAPMakeManualTest", $conf->ldap->dir_temp).'<br>';
  228. }
  229. }
  230. if ($action == 'testsearchgroup') {
  231. // TODO Mutualize code following with other ldap_xxxx.php pages
  232. // Test synchro
  233. $ldap = new Ldap();
  234. $result = $ldap->connect_bind();
  235. if ($result > 0) {
  236. $required_fields = array(
  237. $conf->global->LDAP_KEY_GROUPS,
  238. // $conf->global->LDAP_GROUP_FIELD_NAME,
  239. $conf->global->LDAP_GROUP_FIELD_DESCRIPTION,
  240. $conf->global->LDAP_GROUP_FIELD_GROUPMEMBERS,
  241. $conf->global->LDAP_GROUP_FIELD_GROUPID
  242. );
  243. // Remove from required_fields all entries not configured in LDAP (empty) and duplicated
  244. $required_fields = array_unique(array_values(array_filter($required_fields, "dol_validElement")));
  245. // Get from LDAP database an array of results
  246. $ldapgroups = $ldap->getRecords('*', $conf->global->LDAP_GROUP_DN, $conf->global->LDAP_KEY_GROUPS, $required_fields, 'group');
  247. //$ldapgroups = $ldap->getRecords('*', $conf->global->LDAP_GROUP_DN, $conf->global->LDAP_KEY_GROUPS, '', 'group');
  248. if (is_array($ldapgroups)) {
  249. $liste = array();
  250. foreach ($ldapgroups as $key => $ldapgroup) {
  251. // Define the label string for this group
  252. $label = '';
  253. foreach ($required_fields as $value) {
  254. if ($value) {
  255. $label .= $value."=".$ldapgroup[$value]." ";
  256. }
  257. }
  258. $liste[$key] = $label;
  259. }
  260. } else {
  261. setEventMessages($ldap->error, $ldap->errors, 'errors');
  262. }
  263. print "<br>\n";
  264. print "LDAP search for group:<br>\n";
  265. print "search: *<br>\n";
  266. print "userDN: ".$conf->global->LDAP_GROUP_DN."<br>\n";
  267. print "useridentifier: ".$conf->global->LDAP_KEY_GROUPS."<br>\n";
  268. print "required_fields: ".implode(',', $required_fields)."<br>\n";
  269. print "=> ".count($liste)." records<br>\n";
  270. print "\n<br>";
  271. } else {
  272. print img_picto('', 'error').' ';
  273. print '<font class="error">'.$langs->trans("LDAPSynchroKO");
  274. print ': '.$ldap->error;
  275. print '</font><br>';
  276. print $langs->trans("ErrorLDAPMakeManualTest", $conf->ldap->dir_temp).'<br>';
  277. }
  278. }
  279. }
  280. // End of page
  281. llxFooter();
  282. $db->close();