ldap_groups.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. // Load Dolibarr environment
  28. require '../main.inc.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  30. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/user/class/usergroup.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/class/ldap.class.php';
  33. require_once DOL_DOCUMENT_ROOT.'/core/lib/ldap.lib.php';
  34. // Load translation files required by the page
  35. $langs->loadLangs(array("admin", "errors"));
  36. if (!$user->admin) {
  37. accessforbidden();
  38. }
  39. $action = GETPOST('action', 'aZ09');
  40. /*
  41. * Actions
  42. */
  43. if ($action == 'setvalue' && $user->admin) {
  44. $error = 0;
  45. $db->begin();
  46. if (!dolibarr_set_const($db, 'LDAP_GROUP_DN', GETPOST("group", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) {
  47. $error++;
  48. }
  49. if (!dolibarr_set_const($db, 'LDAP_GROUP_OBJECT_CLASS', GETPOST("objectclass", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) {
  50. $error++;
  51. }
  52. if (!dolibarr_set_const($db, 'LDAP_GROUP_FILTER', GETPOST("filter"), 'chaine', 0, '', $conf->entity)) {
  53. $error++;
  54. }
  55. if (!dolibarr_set_const($db, 'LDAP_GROUP_FIELD_FULLNAME', GETPOST("fieldfullname", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) {
  56. $error++;
  57. }
  58. //if (! dolibarr_set_const($db, 'LDAP_GROUP_FIELD_NAME',GETPOST("fieldname", 'alphanohtml'),'chaine',0,'',$conf->entity)) $error++;
  59. if (!dolibarr_set_const($db, 'LDAP_GROUP_FIELD_DESCRIPTION', GETPOST("fielddescription", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) {
  60. $error++;
  61. }
  62. if (!dolibarr_set_const($db, 'LDAP_GROUP_FIELD_GROUPMEMBERS', GETPOST("fieldgroupmembers", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) {
  63. $error++;
  64. }
  65. if (!dolibarr_set_const($db, 'LDAP_GROUP_FIELD_GROUPID', GETPOST("fieldgroupid", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) {
  66. $error++;
  67. }
  68. // This one must be after the others
  69. $valkey = '';
  70. $key = GETPOST("key");
  71. if ($key) {
  72. $valkey = getDolGlobalString($key);
  73. }
  74. if (!dolibarr_set_const($db, 'LDAP_KEY_GROUPS', $valkey, 'chaine', 0, '', $conf->entity)) {
  75. $error++;
  76. }
  77. if (!$error) {
  78. $db->commit();
  79. setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
  80. } else {
  81. $db->rollback();
  82. dol_print_error($db);
  83. }
  84. }
  85. /*
  86. * View
  87. */
  88. $form = new Form($db);
  89. llxHeader('', $langs->trans("LDAPSetup"), 'EN:Module_LDAP_En|FR:Module_LDAP|ES:M&oacute;dulo_LDAP');
  90. $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
  91. print load_fiche_titre($langs->trans("LDAPSetup"), $linkback, 'title_setup');
  92. $head = ldap_prepare_head();
  93. // Test si fonction LDAP actives
  94. if (!function_exists("ldap_connect")) {
  95. setEventMessages($langs->trans("LDAPFunctionsNotAvailableOnPHP"), null, 'errors');
  96. }
  97. print dol_get_fiche_head($head, 'groups', '', -1);
  98. print '<span class="opacitymedium">'.$langs->trans("LDAPDescGroups").'</span><br>';
  99. print '<br>';
  100. print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?action=setvalue&token='.newToken().'">';
  101. print '<input type="hidden" name="token" value="'.newToken().'">';
  102. print '<table class="noborder centpercent">';
  103. print '<tr class="liste_titre">';
  104. print '<td colspan="4">'.$langs->trans("LDAPSynchronizeGroups").'</td>';
  105. print "</tr>\n";
  106. // DN pour les groupes
  107. print '<!-- LDAP_GROUP_DN -->';
  108. print '<tr class="oddeven"><td><span class="fieldrequired">'.$langs->trans("LDAPGroupDn").'</span></td><td>';
  109. print '<input size="48" type="text" name="group" value="'.getDolGlobalString('LDAP_GROUP_DN').'">';
  110. print '</td><td>'.$langs->trans("LDAPGroupDnExample").'</td>';
  111. print '<td>&nbsp;</td>';
  112. print '</tr>';
  113. // List of object class used to define attributes in structure
  114. print '<!-- LDAP_GROUP_OBJECT_CLASS -->';
  115. print '<tr class="oddeven"><td><span class="fieldrequired">'.$langs->trans("LDAPGroupObjectClassList").'</span></td><td>';
  116. print '<input size="48" type="text" name="objectclass" value="'.getDolGlobalString('LDAP_GROUP_OBJECT_CLASS').'">';
  117. print '</td><td>'.$langs->trans("LDAPGroupObjectClassListExample").'</td>';
  118. print '<td>&nbsp;</td>';
  119. print '</tr>';
  120. // Filter, used to filter search
  121. print '<!-- LDAP_GROUP_FILTER -->';
  122. print '<tr class="oddeven"><td>'.$langs->trans("LDAPFilterConnection").'</td><td>';
  123. print '<input size="48" type="text" name="filter" value="'.getDolGlobalString('LDAP_GROUP_FILTER').'">';
  124. print '</td><td>'.$langs->trans("LDAPGroupFilterExample").'</td>';
  125. print '<td></td>';
  126. print '</tr>';
  127. print '</table>';
  128. print '<br>';
  129. print '<table class="noborder centpercent">';
  130. print '<tr class="liste_titre">';
  131. print '<td>'.$langs->trans("LDAPDolibarrMapping").'</td>';
  132. print '<td colspan="2">'.$langs->trans("LDAPLdapMapping").'</td>';
  133. print '<td class="right">'.$langs->trans("LDAPNamingAttribute").'</td>';
  134. print "</tr>\n";
  135. // Filtre
  136. // Common name
  137. print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldName").'</td><td>';
  138. print '<input size="25" type="text" name="fieldfullname" value="'.getDolGlobalString('LDAP_GROUP_FIELD_FULLNAME').'">';
  139. print '</td><td>'.$langs->trans("LDAPFieldCommonNameExample").'</td>';
  140. print '<td class="right"><input type="radio" name="key" value="LDAP_GROUP_FIELD_FULLNAME"'.((getDolGlobalString('LDAP_KEY_GROUPS') == getDolGlobalString('LDAP_GROUP_FIELD_FULLNAME')) ? ' checked' : '')."></td>";
  141. print '</tr>';
  142. // Name
  143. /*
  144. print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldName").'</td><td>';
  145. print '<input size="25" type="text" name="fieldname" value="'.$conf->global->LDAP_GROUP_FIELD_NAME.'">';
  146. print '</td><td>'.$langs->trans("LDAPFieldNameExample").'</td>';
  147. 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>";
  148. print '</tr>';
  149. */
  150. // Description
  151. print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldDescription").'</td><td>';
  152. print '<input size="25" type="text" name="fielddescription" value="'.getDolGlobalString('LDAP_GROUP_FIELD_DESCRIPTION').'">';
  153. print '</td><td>'.$langs->trans("LDAPFieldDescriptionExample").'</td>';
  154. print '<td class="right"><input type="radio" name="key" value="LDAP_GROUP_FIELD_DESCRIPTION"'.((getDolGlobalString('LDAP_KEY_GROUPS') == getDolGlobalString('LDAP_GROUP_FIELD_DESCRIPTION')) ? ' checked' : '')."></td>";
  155. print '</tr>';
  156. // User group
  157. print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldGroupMembers").'</td><td>';
  158. print '<input size="25" type="text" name="fieldgroupmembers" value="'.getDolGlobalString('LDAP_GROUP_FIELD_GROUPMEMBERS').'">';
  159. print '</td><td>'.$langs->trans("LDAPFieldGroupMembersExample").'</td>';
  160. print '<td class="right"><input type="radio" name="key" value="LDAP_GROUP_FIELD_GROUPMEMBERS"'.((getDolGlobalString('LDAP_KEY_GROUPS') == getDolGlobalString('LDAP_GROUP_FIELD_GROUPMEMBERS')) ? ' checked' : '')."></td>";
  161. print '</tr>';
  162. // Group id
  163. print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldGroupid").'</td><td>';
  164. print '<input size="25" type="text" name="fieldgroupid" value="'.getDolGlobalString('LDAP_GROUP_FIELD_GROUPID').'">';
  165. print '</td><td>'.$langs->trans("LDAPFieldGroupidExample").'</td>';
  166. print '<td class="right">&nbsp;</td>';
  167. print '</tr>';
  168. print '</table>';
  169. print info_admin($langs->trans("LDAPDescValues"));
  170. print dol_get_fiche_end();
  171. print $form->buttonsSaveCancel("Modify", '');
  172. print '</form>';
  173. /*
  174. * Test de la connexion
  175. */
  176. if (getDolGlobalInt('LDAP_SYNCHRO_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
  177. $butlabel = $langs->trans("LDAPTestSynchroGroup");
  178. $testlabel = 'testgroup';
  179. $key = getDolGlobalString('LDAP_KEY_GROUPS');
  180. $dn = getDolGlobalString('LDAP_GROUP_DN');
  181. $objectclass = getDolGlobalString('LDAP_GROUP_OBJECT_CLASS');
  182. show_ldap_test_button($butlabel, $testlabel, $key, $dn, $objectclass);
  183. } elseif (getDolGlobalInt('LDAP_SYNCHRO_ACTIVE') === Ldap::SYNCHRO_LDAP_TO_DOLIBARR) {
  184. $butlabel = $langs->trans("LDAPTestSearch");
  185. $testlabel = 'testsearchgroup';
  186. $key = getDolGlobalString('LDAP_KEY_GROUPS');
  187. $dn = getDolGlobalString('LDAP_GROUP_DN');
  188. $objectclass = getDolGlobalString('LDAP_GROUP_OBJECT_CLASS');
  189. show_ldap_test_button($butlabel, $testlabel, $key, $dn, $objectclass);
  190. }
  191. if (function_exists("ldap_connect")) {
  192. if ($action == 'testgroup') {
  193. // Creation objet
  194. $object = new UserGroup($db);
  195. $object->initAsSpecimen();
  196. // Test synchro
  197. $ldap = new Ldap();
  198. $result = $ldap->connect_bind();
  199. if ($result > 0) {
  200. $info = $object->_load_ldap_info();
  201. $dn = $object->_load_ldap_dn($info);
  202. // Get a gid number for objectclass PosixGroup
  203. if (in_array('posixGroup', $info['objectclass'])) {
  204. $info['gidNumber'] = $ldap->getNextGroupGid('LDAP_KEY_GROUPS');
  205. }
  206. $result1 = $ldap->delete($dn); // To be sure to delete existing records
  207. $result2 = $ldap->add($dn, $info, $user); // Now the test
  208. $result3 = $ldap->delete($dn); // Clean what we did
  209. if ($result2 > 0) {
  210. print img_picto('', 'info').' ';
  211. print '<span class="ok">'.$langs->trans("LDAPSynchroOK").'</span><br>';
  212. } else {
  213. print img_picto('', 'error').' ';
  214. print '<span class="error">'.$langs->trans("LDAPSynchroKOMayBePermissions");
  215. print ': '.$ldap->error;
  216. print '</span><br>';
  217. print $langs->trans("ErrorLDAPMakeManualTest", $conf->ldap->dir_temp).'<br>';
  218. }
  219. print "<br>\n";
  220. print "LDAP input file used for test:<br><br>\n";
  221. print nl2br($ldap->dump_content($dn, $info));
  222. print "\n<br>";
  223. } else {
  224. print img_picto('', 'error').' ';
  225. print '<span class="error">'.$langs->trans("LDAPSynchroKO");
  226. print ': '.$ldap->error;
  227. print '</span><br>';
  228. print $langs->trans("ErrorLDAPMakeManualTest", $conf->ldap->dir_temp).'<br>';
  229. }
  230. }
  231. if ($action == 'testsearchgroup') {
  232. // TODO Mutualize code following with other ldap_xxxx.php pages
  233. // Test synchro
  234. $ldap = new Ldap();
  235. $result = $ldap->connect_bind();
  236. if ($result > 0) {
  237. $required_fields = array(
  238. getDolGlobalString('LDAP_KEY_GROUPS'),
  239. // getDolGlobalString('LDAP_GROUP_FIELD_NAME'),
  240. getDolGlobalString('LDAP_GROUP_FIELD_DESCRIPTION'),
  241. getDolGlobalString('LDAP_GROUP_FIELD_GROUPMEMBERS'),
  242. getDolGlobalString('LDAP_GROUP_FIELD_GROUPID')
  243. );
  244. // Remove from required_fields all entries not configured in LDAP (empty) and duplicated
  245. $required_fields = array_unique(array_values(array_filter($required_fields, "dol_validElement")));
  246. // Get from LDAP database an array of results
  247. $ldapgroups = $ldap->getRecords('*', getDolGlobalString('LDAP_GROUP_DN'), getDolGlobalString('LDAP_KEY_GROUPS'), $required_fields, 'group');
  248. //$ldapgroups = $ldap->getRecords('*', $conf->global->LDAP_GROUP_DN, $conf->global->LDAP_KEY_GROUPS, '', 'group');
  249. if (is_array($ldapgroups)) {
  250. $liste = array();
  251. foreach ($ldapgroups as $key => $ldapgroup) {
  252. // Define the label string for this group
  253. $label = '';
  254. foreach ($required_fields as $value) {
  255. if ($value) {
  256. $label .= $value."=".$ldapgroup[$value]." ";
  257. }
  258. }
  259. $liste[$key] = $label;
  260. }
  261. } else {
  262. setEventMessages($ldap->error, $ldap->errors, 'errors');
  263. }
  264. print "<br>\n";
  265. print "LDAP search for group:<br>\n";
  266. print "search: *<br>\n";
  267. print "userDN: ".getDolGlobalString('LDAP_GROUP_DN')."<br>\n";
  268. print "useridentifier: ".getDolGlobalString('LDAP_KEY_GROUPS')."<br>\n";
  269. print "required_fields: ".implode(',', $required_fields)."<br>\n";
  270. print "=> ".count($liste)." records<br>\n";
  271. print "\n<br>";
  272. } else {
  273. print img_picto('', 'error').' ';
  274. print '<span class="error">'.$langs->trans("LDAPSynchroKO");
  275. print ': '.$ldap->error;
  276. print '</span><br>';
  277. print $langs->trans("ErrorLDAPMakeManualTest", $conf->ldap->dir_temp).'<br>';
  278. }
  279. }
  280. }
  281. // End of page
  282. llxFooter();
  283. $db->close();