ldap_groups.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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@capnetworks.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 <http://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. $langs->load("admin");
  34. $langs->load("errors");
  35. if (!$user->admin)
  36. accessforbidden();
  37. $action = GETPOST('action','aZ09');
  38. /*
  39. * Actions
  40. */
  41. if ($action == 'setvalue' && $user->admin)
  42. {
  43. $error=0;
  44. $db->begin();
  45. if (! dolibarr_set_const($db, 'LDAP_GROUP_DN',GETPOST("group"),'chaine',0,'',$conf->entity)) $error++;
  46. if (! dolibarr_set_const($db, 'LDAP_GROUP_OBJECT_CLASS',GETPOST("objectclass"),'chaine',0,'',$conf->entity)) $error++;
  47. if (! dolibarr_set_const($db, 'LDAP_GROUP_FIELD_FULLNAME',GETPOST("fieldfullname"),'chaine',0,'',$conf->entity)) $error++;
  48. //if (! dolibarr_set_const($db, 'LDAP_GROUP_FIELD_NAME',$_POST["fieldname"],'chaine',0,'',$conf->entity)) $error++;
  49. if (! dolibarr_set_const($db, 'LDAP_GROUP_FIELD_DESCRIPTION',GETPOST("fielddescription"),'chaine',0,'',$conf->entity)) $error++;
  50. if (! dolibarr_set_const($db, 'LDAP_GROUP_FIELD_GROUPMEMBERS',GETPOST("fieldgroupmembers"),'chaine',0,'',$conf->entity)) $error++;
  51. // This one must be after the others
  52. $valkey='';
  53. $key=GETPOST("key");
  54. if ($key) $valkey=$conf->global->$key;
  55. if (! dolibarr_set_const($db, 'LDAP_KEY_GROUPS',$valkey,'chaine',0,'',$conf->entity)) $error++;
  56. if (! $error)
  57. {
  58. $db->commit();
  59. setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
  60. }
  61. else
  62. {
  63. $db->rollback();
  64. dol_print_error($db);
  65. }
  66. }
  67. /*
  68. * View
  69. */
  70. llxHeader('',$langs->trans("LDAPSetup"),'EN:Module_LDAP_En|FR:Module_LDAP|ES:M&oacute;dulo_LDAP');
  71. $linkback='<a href="'.DOL_URL_ROOT.'/admin/modules.php">'.$langs->trans("BackToModuleList").'</a>';
  72. print load_fiche_titre($langs->trans("LDAPSetup"),$linkback,'title_setup');
  73. $head = ldap_prepare_head();
  74. // Test si fonction LDAP actives
  75. if (! function_exists("ldap_connect"))
  76. {
  77. setEventMessages($langs->trans("LDAPFunctionsNotAvailableOnPHP"), null, 'errors');
  78. }
  79. dol_fiche_head($head, 'groups', $langs->trans("LDAPSetup"));
  80. print $langs->trans("LDAPDescGroups").'<br>';
  81. print '<br>';
  82. print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?action=setvalue">';
  83. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  84. $form=new Form($db);
  85. print '<table class="noborder" width="100%">';
  86. $var=true;
  87. print '<tr class="liste_titre">';
  88. print '<td colspan="4">'.$langs->trans("LDAPSynchronizeGroups").'</td>';
  89. print "</tr>\n";
  90. // DN pour les groupes
  91. print '<tr class="oddeven"><td width="25%"><span class="fieldrequired">'.$langs->trans("LDAPGroupDn").'</span></td><td>';
  92. print '<input size="48" type="text" name="group" value="'.$conf->global->LDAP_GROUP_DN.'">';
  93. print '</td><td>'.$langs->trans("LDAPGroupDnExample").'</td>';
  94. print '<td>&nbsp;</td>';
  95. print '</tr>';
  96. // List of object class used to define attributes in structure
  97. print '<tr class="oddeven"><td width="25%"><span class="fieldrequired">'.$langs->trans("LDAPGroupObjectClassList").'</span></td><td>';
  98. print '<input size="48" type="text" name="objectclass" value="'.$conf->global->LDAP_GROUP_OBJECT_CLASS.'">';
  99. print '</td><td>'.$langs->trans("LDAPGroupObjectClassListExample").'</td>';
  100. print '<td>&nbsp;</td>';
  101. print '</tr>';
  102. print '</table>';
  103. print '<br>';
  104. print '<table class="noborder" width="100%">';
  105. $var=true;
  106. print '<tr class="liste_titre">';
  107. print '<td width="25%">'.$langs->trans("LDAPDolibarrMapping").'</td>';
  108. print '<td colspan="2">'.$langs->trans("LDAPLdapMapping").'</td>';
  109. print '<td align="right">'.$langs->trans("LDAPNamingAttribute").'</td>';
  110. print "</tr>\n";
  111. // Filtre
  112. // Common name
  113. print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldName").'</td><td>';
  114. print '<input size="25" type="text" name="fieldfullname" value="'.$conf->global->LDAP_GROUP_FIELD_FULLNAME.'">';
  115. print '</td><td>'.$langs->trans("LDAPFieldCommonNameExample").'</td>';
  116. print '<td align="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>";
  117. print '</tr>';
  118. // Name
  119. /*
  120. print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldName").'</td><td>';
  121. print '<input size="25" type="text" name="fieldname" value="'.$conf->global->LDAP_GROUP_FIELD_NAME.'">';
  122. print '</td><td>'.$langs->trans("LDAPFieldNameExample").'</td>';
  123. print '<td align="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>";
  124. print '</tr>';
  125. */
  126. // Description
  127. print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldDescription").'</td><td>';
  128. print '<input size="25" type="text" name="fielddescription" value="'.$conf->global->LDAP_GROUP_FIELD_DESCRIPTION.'">';
  129. print '</td><td>'.$langs->trans("LDAPFieldDescriptionExample").'</td>';
  130. print '<td align="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>";
  131. print '</tr>';
  132. // User group
  133. print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldGroupMembers").'</td><td>';
  134. print '<input size="25" type="text" name="fieldgroupmembers" value="'.$conf->global->LDAP_GROUP_FIELD_GROUPMEMBERS.'">';
  135. print '</td><td>'.$langs->trans("LDAPFieldGroupMembersExample").'</td>';
  136. print '<td align="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>";
  137. print '</tr>';
  138. print '</table>';
  139. print info_admin($langs->trans("LDAPDescValues"));
  140. dol_fiche_end();
  141. print '<div class="center"><input type="submit" class="button" value="'.$langs->trans("Modify").'"></div>';
  142. print '</form>';
  143. /*
  144. * Test de la connexion
  145. */
  146. if ($conf->global->LDAP_SYNCHRO_ACTIVE == 'dolibarr2ldap')
  147. {
  148. $butlabel=$langs->trans("LDAPTestSynchroGroup");
  149. $testlabel='testgroup';
  150. $key=$conf->global->LDAP_KEY_GROUPS;
  151. $dn=$conf->global->LDAP_GROUP_DN;
  152. $objectclass=$conf->global->LDAP_GROUP_OBJECT_CLASS;
  153. show_ldap_test_button($butlabel,$testlabel,$key,$dn,$objectclass);
  154. }
  155. if (function_exists("ldap_connect"))
  156. {
  157. if ($_GET["action"] == 'testgroup')
  158. {
  159. // Creation objet
  160. $object=new UserGroup($db);
  161. $object->initAsSpecimen();
  162. // Test synchro
  163. $ldap=new Ldap();
  164. $result=$ldap->connect_bind();
  165. if ($result > 0)
  166. {
  167. $info=$object->_load_ldap_info();
  168. $dn=$object->_load_ldap_dn($info);
  169. // Get a gid number for objectclass PosixGroup
  170. if(in_array('posixGroup',$info['objectclass']))
  171. $info['gidNumber'] = $ldap->getNextGroupGid();
  172. $result1=$ldap->delete($dn); // To be sure to delete existing records
  173. $result2=$ldap->add($dn,$info,$user); // Now the test
  174. $result3=$ldap->delete($dn); // Clean what we did
  175. if ($result2 > 0)
  176. {
  177. print img_picto('','info').' ';
  178. print '<font class="ok">'.$langs->trans("LDAPSynchroOK").'</font><br>';
  179. }
  180. else
  181. {
  182. print img_picto('','error').' ';
  183. print '<font class="error">'.$langs->trans("LDAPSynchroKOMayBePermissions");
  184. print ': '.$ldap->error;
  185. print '</font><br>';
  186. print $langs->trans("ErrorLDAPMakeManualTest",$conf->ldap->dir_temp).'<br>';
  187. }
  188. print "<br>\n";
  189. print "LDAP input file used for test:<br><br>\n";
  190. print nl2br($ldap->dump_content($dn,$info));
  191. print "\n<br>";
  192. }
  193. else
  194. {
  195. print img_picto('','error').' ';
  196. print '<font class="error">'.$langs->trans("LDAPSynchroKO");
  197. print ': '.$ldap->error;
  198. print '</font><br>';
  199. print $langs->trans("ErrorLDAPMakeManualTest",$conf->ldap->dir_temp).'<br>';
  200. }
  201. }
  202. }
  203. llxFooter();
  204. $db->close();