ldap_members_types.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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-2017 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_members_types.php
  24. * \ingroup ldap
  25. * \brief Page to setup LDAP synchronization for members types
  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.'/adherents/class/adherent.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent_type.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_MEMBER_TYPE_DN', GETPOST("membertype"), 'chaine', 0, '', $conf->entity)) {
  47. $error++;
  48. }
  49. if (!dolibarr_set_const($db, 'LDAP_MEMBER_TYPE_OBJECT_CLASS', GETPOST("objectclass"), 'chaine', 0, '', $conf->entity)) {
  50. $error++;
  51. }
  52. if (!dolibarr_set_const($db, 'LDAP_MEMBER_TYPE_FIELD_FULLNAME', GETPOST("fieldfullname"), 'chaine', 0, '', $conf->entity)) {
  53. $error++;
  54. }
  55. if (!dolibarr_set_const($db, 'LDAP_MEMBER_TYPE_FIELD_DESCRIPTION', GETPOST("fielddescription"), 'chaine', 0, '', $conf->entity)) {
  56. $error++;
  57. }
  58. if (!dolibarr_set_const($db, 'LDAP_MEMBER_TYPE_FIELD_GROUPMEMBERS', GETPOST("fieldmembertypemembers"), 'chaine', 0, '', $conf->entity)) {
  59. $error++;
  60. }
  61. // This one must be after the others
  62. $valkey = '';
  63. $key = GETPOST("key");
  64. if ($key) {
  65. $valkey = getDolGlobalString($key);
  66. }
  67. if (!dolibarr_set_const($db, 'LDAP_KEY_MEMBERS_TYPES', $valkey, 'chaine', 0, '', $conf->entity)) {
  68. $error++;
  69. }
  70. if (!$error) {
  71. $db->commit();
  72. setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
  73. } else {
  74. $db->rollback();
  75. dol_print_error($db);
  76. }
  77. }
  78. /*
  79. * View
  80. */
  81. llxHeader('', $langs->trans("LDAPSetup"), 'EN:Module_LDAP_En|FR:Module_LDAP|ES:M&oacute;dulo_LDAP');
  82. $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
  83. print load_fiche_titre($langs->trans("LDAPSetup"), $linkback, 'title_setup');
  84. $head = ldap_prepare_head();
  85. // Test si fonction LDAP actives
  86. if (!function_exists("ldap_connect")) {
  87. setEventMessages($langs->trans("LDAPFunctionsNotAvailableOnPHP"), null, 'errors');
  88. }
  89. print dol_get_fiche_head($head, 'memberstypes', '', -1);
  90. print '<span class="opacitymedium">'.$langs->trans("LDAPDescMembersTypes").'</span><br>';
  91. print '<br>';
  92. print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'?action=setvalue&token='.newToken().'">';
  93. print '<input type="hidden" name="token" value="'.newToken().'">';
  94. $form = new Form($db);
  95. print '<table class="noborder centpercent">';
  96. print '<tr class="liste_titre">';
  97. print '<td colspan="4">'.$langs->trans("LDAPSynchronizeMembersTypes").'</td>';
  98. print "</tr>\n";
  99. // DN pour les types de membres
  100. print '<!-- LDAP_MEMBER_TYPE_DN -->';
  101. print '<tr class="oddeven"><td><span class="fieldrequired">'.$langs->trans("LDAPMemberTypeDn").'</span></td><td>';
  102. print '<input size="48" type="text" name="membertype" value="' . getDolGlobalString('LDAP_MEMBER_TYPE_DN').'">';
  103. print '</td><td>'.$langs->trans("LDAPMemberTypepDnExample").'</td>';
  104. print '<td>&nbsp;</td>';
  105. print '</tr>';
  106. // List of object class used to define attributes in structure
  107. print '<!-- LDAP_MEMBER_TYPE_OBJECT_CLASS -->';
  108. print '<tr class="oddeven"><td><span class="fieldrequired">'.$langs->trans("LDAPMemberTypeObjectClassList").'</span></td><td>';
  109. print '<input size="48" type="text" name="objectclass" value="' . getDolGlobalString('LDAP_MEMBER_TYPE_OBJECT_CLASS').'">';
  110. print '</td><td>'.$langs->trans("LDAPMemberTypeObjectClassListExample").'</td>';
  111. print '<td>&nbsp;</td>';
  112. print '</tr>';
  113. print '</table>';
  114. print '<br>';
  115. print '<table class="noborder centpercent">';
  116. print '<tr class="liste_titre">';
  117. print '<td>'.$langs->trans("LDAPDolibarrMapping").'</td>';
  118. print '<td colspan="2">'.$langs->trans("LDAPLdapMapping").'</td>';
  119. print '<td class="right">'.$langs->trans("LDAPNamingAttribute").'</td>';
  120. print "</tr>\n";
  121. // Filtre
  122. // Common name
  123. print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldName").'</td><td>';
  124. print '<input size="25" type="text" name="fieldfullname" value="' . getDolGlobalString('LDAP_MEMBER_TYPE_FIELD_FULLNAME').'">';
  125. print '</td><td>'.$langs->trans("LDAPFieldCommonNameExample").'</td>';
  126. print '<td class="right"><input type="radio" name="key" value="LDAP_MEMBER_TYPE_FIELD_FULLNAME"'.(($conf->global->LDAP_KEY_MEMBERS_TYPES && $conf->global->LDAP_KEY_MEMBERS_TYPES == $conf->global->LDAP_MEMBER_TYPE_FIELD_FULLNAME) ? ' checked' : '')."></td>";
  127. print '</tr>';
  128. // Description
  129. print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldDescription").'</td><td>';
  130. print '<input size="25" type="text" name="fielddescription" value="' . getDolGlobalString('LDAP_MEMBER_TYPE_FIELD_DESCRIPTION').'">';
  131. print '</td><td>'.$langs->trans("LDAPFieldDescriptionExample").'</td>';
  132. print '<td class="right"><input type="radio" name="key" value="LDAP_MEMBER_TYPE_FIELD_DESCRIPTION"'.(($conf->global->LDAP_KEY_MEMBERS_TYPES && $conf->global->LDAP_KEY_MEMBER_TYPES == $conf->global->LDAP_MEMBER_TYPE_FIELD_DESCRIPTION) ? ' checked' : '')."></td>";
  133. print '</tr>';
  134. // User group
  135. print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldGroupMembers").'</td><td>';
  136. print '<input size="25" type="text" name="fieldmembertypemembers" value="' . getDolGlobalString('LDAP_MEMBER_TYPE_FIELD_GROUPMEMBERS').'">';
  137. print '</td><td>'.$langs->trans("LDAPFieldGroupMembersExample").'</td>';
  138. print '<td class="right"><input type="radio" name="key" value="LDAP_MEMBER_TYPE_FIELD_GROUPMEMBERS"'.(($conf->global->LDAP_KEY_MEMBERS_TYPES && $conf->global->LDAP_KEY_MEMBERS_TYPES == $conf->global->LDAP_MEMBER_TYPE_FIELD_GROUPMEMBERS) ? ' checked' : '')."></td>";
  139. print '</tr>';
  140. print '</table>';
  141. print info_admin($langs->trans("LDAPDescValues"));
  142. print dol_get_fiche_end();
  143. print $form->buttonsSaveCancel("Modify", '');
  144. print '</form>';
  145. /*
  146. * Test de la connexion
  147. */
  148. if (getDolGlobalInt('LDAP_MEMBER_TYPE_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
  149. $butlabel = $langs->trans("LDAPTestSynchroMemberType");
  150. $testlabel = 'testmembertype';
  151. $key = $conf->global->LDAP_KEY_MEMBERS_TYPES;
  152. $dn = $conf->global->LDAP_MEMBER_TYPE_DN;
  153. $objectclass = $conf->global->LDAP_MEMBER_TYPE_OBJECT_CLASS;
  154. show_ldap_test_button($butlabel, $testlabel, $key, $dn, $objectclass);
  155. }
  156. if (function_exists("ldap_connect")) {
  157. if ($_GET["action"] == 'testmembertype') {
  158. // Creation objet
  159. $object = new AdherentType($db);
  160. $object->initAsSpecimen();
  161. // Test synchro
  162. $ldap = new Ldap();
  163. $result = $ldap->connect_bind();
  164. if ($result > 0) {
  165. $info = $object->_load_ldap_info();
  166. $dn = $object->_load_ldap_dn($info);
  167. // Get a gid number for objectclass PosixGroup
  168. if (in_array('posixGroup', $info['objectclass'])) {
  169. $info['gidNumber'] = $ldap->getNextGroupGid('LDAP_KEY_MEMBERS_TYPES');
  170. }
  171. $result1 = $ldap->delete($dn); // To be sure to delete existing records
  172. $result2 = $ldap->add($dn, $info, $user); // Now the test
  173. $result3 = $ldap->delete($dn); // Clean what we did
  174. if ($result2 > 0) {
  175. print img_picto('', 'info').' ';
  176. print '<span class="ok">'.$langs->trans("LDAPSynchroOK").'</span><br>';
  177. } else {
  178. print img_picto('', 'error').' ';
  179. print '<span class="error">'.$langs->trans("LDAPSynchroKOMayBePermissions");
  180. print ': '.$ldap->error;
  181. print '</span><br>';
  182. print $langs->trans("ErrorLDAPMakeManualTest", $conf->ldap->dir_temp).'<br>';
  183. }
  184. print "<br>\n";
  185. print "LDAP input file used for test:<br><br>\n";
  186. print nl2br($ldap->dump_content($dn, $info));
  187. print "\n<br>";
  188. } else {
  189. print img_picto('', 'error').' ';
  190. print '<span class="error">'.$langs->trans("LDAPSynchroKO");
  191. print ': '.$ldap->error;
  192. print '</span><br>';
  193. print $langs->trans("ErrorLDAPMakeManualTest", $conf->ldap->dir_temp).'<br>';
  194. }
  195. }
  196. }
  197. // End of page
  198. llxFooter();
  199. $db->close();