ldap_groups.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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@dolibarr.fr>
  6. * Copyright (C) 2006-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \file htdocs/admin/ldap_groups.php
  23. * \ingroup ldap
  24. * \brief Page to setup LDAP synchronization for groups
  25. */
  26. require("../main.inc.php");
  27. require_once(DOL_DOCUMENT_ROOT."/lib/admin.lib.php");
  28. require_once(DOL_DOCUMENT_ROOT."/user/class/user.class.php");
  29. require_once(DOL_DOCUMENT_ROOT."/user/class/usergroup.class.php");
  30. require_once(DOL_DOCUMENT_ROOT."/lib/ldap.class.php");
  31. require_once(DOL_DOCUMENT_ROOT."/lib/ldap.lib.php");
  32. $langs->load("admin");
  33. $langs->load("errors");
  34. if (!$user->admin)
  35. accessforbidden();
  36. /*
  37. * Actions
  38. */
  39. if ($_GET["action"] == 'setvalue' && $user->admin)
  40. {
  41. $error=0;
  42. if (! dolibarr_set_const($db, 'LDAP_GROUP_DN',$_POST["group"],'chaine',0,'',$conf->entity)) $error++;
  43. if (! dolibarr_set_const($db, 'LDAP_GROUP_OBJECT_CLASS',$_POST["objectclass"],'chaine',0,'',$conf->entity)) $error++;
  44. if (! dolibarr_set_const($db, 'LDAP_GROUP_FIELD_FULLNAME',$_POST["fieldfullname"],'chaine',0,'',$conf->entity)) $error++;
  45. //if (! dolibarr_set_const($db, 'LDAP_GROUP_FIELD_NAME',$_POST["fieldname"],'chaine',0,'',$conf->entity)) $error++;
  46. if (! dolibarr_set_const($db, 'LDAP_GROUP_FIELD_DESCRIPTION',$_POST["fielddescription"],'chaine',0,'',$conf->entity)) $error++;
  47. if (! dolibarr_set_const($db, 'LDAP_GROUP_FIELD_GROUPMEMBERS',$_POST["fieldgroupmembers"],'chaine',0,'',$conf->entity)) $error++;
  48. // This one must be after the others
  49. $valkey='';
  50. $key=$_POST["key"];
  51. if ($key) $valkey=$conf->global->$key;
  52. if (! dolibarr_set_const($db, 'LDAP_KEY_GROUPS',$valkey,'chaine',0,'',$conf->entity)) $error++;
  53. if ($error)
  54. {
  55. dol_print_error($db->error());
  56. }
  57. }
  58. /*
  59. * View
  60. */
  61. llxHeader('',$langs->trans("LDAPSetup"),'EN:Module_LDAP_En|FR:Module_LDAP|ES:M&oacute;dulo_LDAP');
  62. print_fiche_titre($langs->trans("LDAPSetup"),'','setup');
  63. $head = ldap_prepare_head();
  64. // Test si fonction LDAP actives
  65. if (! function_exists("ldap_connect"))
  66. {
  67. $mesg=$langs->trans("LDAPFunctionsNotAvailableOnPHP");
  68. }
  69. if ($mesg) print '<div class="error">'.$mesg.'</div>';
  70. dol_fiche_head($head, 'groups', $langs->trans("LDAPSetup"));
  71. print $langs->trans("LDAPDescGroups").'<br>';
  72. print '<br>';
  73. print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?action=setvalue">';
  74. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  75. $html=new Form($db);
  76. print '<table class="noborder" width="100%">';
  77. $var=true;
  78. print '<tr class="liste_titre">';
  79. print '<td colspan="4">'.$langs->trans("LDAPSynchronizeGroups").'</td>';
  80. print "</tr>\n";
  81. // DN pour les groupes
  82. $var=!$var;
  83. print '<tr '.$bc[$var].'><td width="25%"><span class="fieldrequired">'.$langs->trans("LDAPGroupDn").'</span></td><td>';
  84. print '<input size="48" type="text" name="group" value="'.$conf->global->LDAP_GROUP_DN.'">';
  85. print '</td><td>'.$langs->trans("LDAPGroupDnExample").'</td>';
  86. print '<td>&nbsp;</td>';
  87. print '</tr>';
  88. // List of object class used to define attributes in structure
  89. $var=!$var;
  90. print '<tr '.$bc[$var].'><td width="25%"><span class="fieldrequired">'.$langs->trans("LDAPGroupObjectClassList").'</span></td><td>';
  91. print '<input size="48" type="text" name="objectclass" value="'.$conf->global->LDAP_GROUP_OBJECT_CLASS.'">';
  92. print '</td><td>'.$langs->trans("LDAPGroupObjectClassListExample").'</td>';
  93. print '<td>&nbsp;</td>';
  94. print '</tr>';
  95. print '</table>';
  96. print '<br>';
  97. print '<table class="noborder" width="100%">';
  98. $var=true;
  99. print '<tr class="liste_titre">';
  100. print '<td width="25%">'.$langs->trans("LDAPDolibarrMapping").'</td>';
  101. print '<td colspan="2">'.$langs->trans("LDAPLdapMapping").'</td>';
  102. print '<td align="right">'.$langs->trans("LDAPNamingAttribute").'</td>';
  103. print "</tr>\n";
  104. // Filtre
  105. // Common name
  106. $var=!$var;
  107. print '<tr '.$bc[$var].'><td>'.$langs->trans("LDAPFieldName").'</td><td>';
  108. print '<input size="25" type="text" name="fieldfullname" value="'.$conf->global->LDAP_GROUP_FIELD_FULLNAME.'">';
  109. print '</td><td>'.$langs->trans("LDAPFieldCommonNameExample").'</td>';
  110. 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="true"':'')."></td>";
  111. print '</tr>';
  112. // Name
  113. /*$var=!$var;
  114. print '<tr '.$bc[$var].'><td>'.$langs->trans("LDAPFieldName").'</td><td>';
  115. print '<input size="25" type="text" name="fieldname" value="'.$conf->global->LDAP_GROUP_FIELD_NAME.'">';
  116. print '</td><td>'.$langs->trans("LDAPFieldNameExample").'</td>';
  117. 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="true"':'')."></td>";
  118. print '</tr>';
  119. */
  120. // Description
  121. $var=!$var;
  122. print '<tr '.$bc[$var].'><td>'.$langs->trans("LDAPFieldDescription").'</td><td>';
  123. print '<input size="25" type="text" name="fielddescription" value="'.$conf->global->LDAP_GROUP_FIELD_DESCRIPTION.'">';
  124. print '</td><td>'.$langs->trans("LDAPFieldDescriptionExample").'</td>';
  125. 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="true"':'')."></td>";
  126. print '</tr>';
  127. // User group
  128. $var=!$var;
  129. print '<tr '.$bc[$var].'><td>'.$langs->trans("LDAPFieldGroupMembers").'</td><td>';
  130. print '<input size="25" type="text" name="fieldgroupmembers" value="'.$conf->global->LDAP_GROUP_FIELD_GROUPMEMBERS.'">';
  131. print '</td><td>'.$langs->trans("LDAPFieldGroupMembersExample").'</td>';
  132. 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="true"':'')."></td>";
  133. print '</tr>';
  134. $var=!$var;
  135. print '<tr '.$bc[$var].'><td colspan="4" align="center"><input type="submit" class="button" value="'.$langs->trans("Modify").'"></td></tr>';
  136. print '</table>';
  137. print '</form>';
  138. print '</div>';
  139. print info_admin($langs->trans("LDAPDescValues"));
  140. /*
  141. * Test de la connexion
  142. */
  143. if ($conf->global->LDAP_SYNCHRO_ACTIVE == 'dolibarr2ldap')
  144. {
  145. $butlabel=$langs->trans("LDAPTestSynchroGroup");
  146. $testlabel='testgroup';
  147. $key=$conf->global->LDAP_KEY_GROUPS;
  148. $dn=$conf->global->LDAP_GROUP_DN;
  149. $objectclass=$conf->global->LDAP_GROUP_OBJECT_CLASS;
  150. show_ldap_test_button($butlabel,$testlabel,$key,$dn,$objectclass);
  151. }
  152. if (function_exists("ldap_connect"))
  153. {
  154. if ($_GET["action"] == 'testgroup')
  155. {
  156. // Creation objet
  157. $object=new UserGroup($db);
  158. $object->initAsSpecimen();
  159. // Test synchro
  160. $ldap=new Ldap();
  161. $result=$ldap->connect_bind();
  162. if ($result > 0)
  163. {
  164. $info=$object->_load_ldap_info();
  165. $dn=$object->_load_ldap_dn($info);
  166. $result1=$ldap->delete($dn); // To be sure to delete existing records
  167. $result2=$ldap->add($dn,$info,$user); // Now the test
  168. $result3=$ldap->delete($dn); // Clean what we did
  169. if ($result2 > 0)
  170. {
  171. print img_picto('','info').' ';
  172. print '<font class="ok">'.$langs->trans("LDAPSynchroOK").'</font><br>';
  173. }
  174. else
  175. {
  176. print img_picto('','error').' ';
  177. print '<font class="error">'.$langs->trans("LDAPSynchroKOMayBePermissions");
  178. print ': '.$ldap->error;
  179. print '</font><br>';
  180. print $langs->trans("ErrorLDAPMakeManualTest",$conf->ldap->dir_temp).'<br>';
  181. }
  182. print "<br>\n";
  183. print "LDAP input file used for test:<br><br>\n";
  184. print nl2br($ldap->dump_content($dn,$info));
  185. print "\n<br>";
  186. }
  187. else
  188. {
  189. print img_picto('','error').' ';
  190. print '<font class="error">'.$langs->trans("LDAPSynchroKO");
  191. print ': '.$ldap->error;
  192. print '</font><br>';
  193. print $langs->trans("ErrorLDAPMakeManualTest",$conf->ldap->dir_temp).'<br>';
  194. }
  195. }
  196. }
  197. $db->close();
  198. llxFooter();
  199. ?>