ldap_users.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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_users.php
  23. * \ingroup ldap
  24. * \brief Page d'administration/configuration du module Ldap
  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_USER_DN',$_POST["user"],'chaine',0,'',$conf->entity)) $error++;
  43. if (! dolibarr_set_const($db, 'LDAP_USER_OBJECT_CLASS',$_POST["objectclass"],'chaine',0,'',$conf->entity)) $error++;
  44. if (! dolibarr_set_const($db, 'LDAP_FILTER_CONNECTION',$_POST["filterconnection"],'chaine',0,'',$conf->entity)) $error++;
  45. if (! dolibarr_set_const($db, 'LDAP_FIELD_FULLNAME',$_POST["fieldfullname"],'chaine',0,'',$conf->entity)) $error++;
  46. if (! dolibarr_set_const($db, 'LDAP_FIELD_LOGIN',$_POST["fieldlogin"],'chaine',0,'',$conf->entity)) $error++;
  47. if (! dolibarr_set_const($db, 'LDAP_FIELD_LOGIN_SAMBA',$_POST["fieldloginsamba"],'chaine',0,'',$conf->entity)) $error++;
  48. if (! dolibarr_set_const($db, 'LDAP_FIELD_PASSWORD',$_POST["fieldpassword"],'chaine',0,'',$conf->entity)) $error++;
  49. if (! dolibarr_set_const($db, 'LDAP_FIELD_PASSWORD_CRYPTED',$_POST["fieldpasswordcrypted"],'chaine',0,'',$conf->entity)) $error++;
  50. if (! dolibarr_set_const($db, 'LDAP_FIELD_NAME',$_POST["fieldname"],'chaine',0,'',$conf->entity)) $error++;
  51. if (! dolibarr_set_const($db, 'LDAP_FIELD_FIRSTNAME',$_POST["fieldfirstname"],'chaine',0,'',$conf->entity)) $error++;
  52. if (! dolibarr_set_const($db, 'LDAP_FIELD_MAIL',$_POST["fieldmail"],'chaine',0,'',$conf->entity)) $error++;
  53. if (! dolibarr_set_const($db, 'LDAP_FIELD_PHONE',$_POST["fieldphone"],'chaine',0,'',$conf->entity)) $error++;
  54. if (! dolibarr_set_const($db, 'LDAP_FIELD_MOBILE',$_POST["fieldmobile"],'chaine',0,'',$conf->entity)) $error++;
  55. if (! dolibarr_set_const($db, 'LDAP_FIELD_FAX',$_POST["fieldfax"],'chaine',0,'',$conf->entity)) $error++;
  56. if (! dolibarr_set_const($db, 'LDAP_FIELD_DESCRIPTION',$_POST["fielddescription"],'chaine',0,'',$conf->entity)) $error++;
  57. if (! dolibarr_set_const($db, 'LDAP_FIELD_SID',$_POST["fieldsid"],'chaine',0,'',$conf->entity)) $error++;
  58. // This one must be after the others
  59. $valkey='';
  60. $key=$_POST["key"];
  61. if ($key) $valkey=$conf->global->$key;
  62. if (! dolibarr_set_const($db, 'LDAP_KEY_USERS',$valkey,'chaine',0,'',$conf->entity)) $error++;
  63. if ($error)
  64. {
  65. dol_print_error($db->error());
  66. }
  67. }
  68. /*
  69. * Visu
  70. */
  71. llxHeader('',$langs->trans("LDAPSetup"),'EN:Module_LDAP_En|FR:Module_LDAP|ES:M&oacute;dulo_LDAP');
  72. print_fiche_titre($langs->trans("LDAPSetup"),'','setup');
  73. $head = ldap_prepare_head();
  74. // Test si fonction LDAP actives
  75. if (! function_exists("ldap_connect"))
  76. {
  77. $mesg=$langs->trans("LDAPFunctionsNotAvailableOnPHP");
  78. }
  79. if ($mesg) print '<div class="error">'.$mesg.'</div>';
  80. dol_fiche_head($head, 'users', $langs->trans("LDAPSetup"));
  81. print $langs->trans("LDAPDescUsers").'<br>';
  82. print '<br>';
  83. print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?action=setvalue">';
  84. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  85. $html=new Form($db);
  86. print '<table class="noborder" width="100%">';
  87. $var=true;
  88. print '<tr class="liste_titre">';
  89. print '<td colspan="4">'.$langs->trans("LDAPSynchronizeUsers").'</td>';
  90. print "</tr>\n";
  91. // DN Pour les utilisateurs
  92. $var=!$var;
  93. print '<tr '.$bc[$var].'><td width="25%"><span class="fieldrequired">'.$langs->trans("LDAPUserDn").'</span></td><td>';
  94. print '<input size="48" type="text" name="user" value="'.$conf->global->LDAP_USER_DN.'">';
  95. print '</td><td>'.$langs->trans("LDAPUserDnExample").'</td>';
  96. print '<td>&nbsp;</td>';
  97. print '</tr>';
  98. // List of object class used to define attributes in structure
  99. $var=!$var;
  100. print '<tr '.$bc[$var].'><td width="25%"><span class="fieldrequired">'.$langs->trans("LDAPUserObjectClassList").'</span></td><td>';
  101. print '<input size="48" type="text" name="objectclass" value="'.$conf->global->LDAP_USER_OBJECT_CLASS.'">';
  102. print '</td><td>'.$langs->trans("LDAPUserObjectClassListExample").'</td>';
  103. print '<td>&nbsp;</td>';
  104. print '</tr>';
  105. // Filtre
  106. //Utilise pour filtrer la recherche
  107. $var=!$var;
  108. print '<tr '.$bc[$var].'><td>'.$langs->trans("LDAPFilterConnection").'</td><td>';
  109. print '<input size="48" type="text" name="filterconnection" value="'.$conf->global->LDAP_FILTER_CONNECTION.'">';
  110. print '</td><td>'.$langs->trans("LDAPFilterConnectionExample").'</td>';
  111. print '<td></td>';
  112. print '</tr>';
  113. print '</table>';
  114. print '<br>';
  115. print '<table class="noborder" width="100%">';
  116. $var=true;
  117. print '<tr class="liste_titre">';
  118. print '<td width="25%">'.$langs->trans("LDAPDolibarrMapping").'</td>';
  119. print '<td colspan="2">'.$langs->trans("LDAPLdapMapping").'</td>';
  120. print '<td align="right">'.$langs->trans("LDAPNamingAttribute").'</td>';
  121. print "</tr>\n";
  122. // Common name
  123. $var=!$var;
  124. print '<tr '.$bc[$var].'><td>'.$langs->trans("LDAPFieldFullname").'</td><td>';
  125. print '<input size="25" type="text" name="fieldfullname" value="'.$conf->global->LDAP_FIELD_FULLNAME.'">';
  126. print '</td><td>'.$langs->trans("LDAPFieldFullnameExample").'</td>';
  127. print '<td align="right"><input type="radio" name="key" value="LDAP_FIELD_FULLNAME"'.(($conf->global->LDAP_KEY_USERS && $conf->global->LDAP_KEY_USERS==$conf->global->LDAP_FIELD_FULLNAME)?' checked="true"':'')."></td>";
  128. print '</tr>';
  129. // Name
  130. $var=!$var;
  131. print '<tr '.$bc[$var].'><td>'.$langs->trans("LDAPFieldName").'</td><td>';
  132. print '<input size="25" type="text" name="fieldname" value="'.$conf->global->LDAP_FIELD_NAME.'">';
  133. print '</td><td>'.$langs->trans("LDAPFieldNameExample").'</td>';
  134. print '<td align="right"><input type="radio" name="key" value="LDAP_FIELD_NAME"'.(($conf->global->LDAP_KEY_USERS && $conf->global->LDAP_KEY_USERS==$conf->global->LDAP_FIELD_NAME)?' checked="true"':'')."></td>";
  135. print '</tr>';
  136. // Firstname
  137. $var=!$var;
  138. print '<tr '.$bc[$var].'><td>'.$langs->trans("LDAPFieldFirstName").'</td><td>';
  139. print '<input size="25" type="text" name="fieldfirstname" value="'.$conf->global->LDAP_FIELD_FIRSTNAME.'">';
  140. print '</td><td>'.$langs->trans("LDAPFieldFirstNameExample").'</td>';
  141. print '<td align="right"><input type="radio" name="key" value="LDAP_FIELD_FIRSTNAME"'.(($conf->global->LDAP_KEY_USERS && $conf->global->LDAP_KEY_USERS==$conf->global->LDAP_FIELD_FIRSTNAME)?' checked="true"':'')."></td>";
  142. print '</tr>';
  143. // Login unix
  144. $var=!$var;
  145. print '<tr '.$bc[$var].'><td>'.$langs->trans("LDAPFieldLoginUnix").'</td><td>';
  146. print '<input size="25" type="text" name="fieldlogin" value="'.$conf->global->LDAP_FIELD_LOGIN.'">';
  147. print '</td><td>'.$langs->trans("LDAPFieldLoginExample").'</td>';
  148. print '<td align="right"><input type="radio" name="key" value="LDAP_FIELD_LOGIN"'.(($conf->global->LDAP_KEY_USERS && $conf->global->LDAP_KEY_USERS==$conf->global->LDAP_FIELD_LOGIN)?' checked="true"':'')."></td>";
  149. print '</tr>';
  150. // Login samba
  151. $var=!$var;
  152. print '<tr '.$bc[$var].'><td>'.$langs->trans("LDAPFieldLoginSamba").'</td><td>';
  153. print '<input size="25" type="text" name="fieldloginsamba" value="'.$conf->global->LDAP_FIELD_LOGIN_SAMBA.'">';
  154. print '</td><td>'.$langs->trans("LDAPFieldLoginSambaExample").'</td>';
  155. print '<td align="right"><input type="radio" name="key" value="LDAP_FIELD_LOGIN_SAMBA"'.(($conf->global->LDAP_KEY_USERS && $conf->global->LDAP_KEY_USERS==$conf->global->LDAP_FIELD_LOGIN_SAMBA)?' checked="true"':'')."></td>";
  156. print '</tr>';
  157. // Password not crypted
  158. $var=!$var;
  159. print '<tr '.$bc[$var].'><td>'.$langs->trans("LDAPFieldPasswordNotCrypted").'</td><td>';
  160. print '<input size="25" type="text" name="fieldpassword" value="'.$conf->global->LDAP_FIELD_PASSWORD.'">';
  161. print '</td><td>'.$langs->trans("LDAPFieldPasswordExample").'</td>';
  162. print '<td align="right"><input type="radio" name="key" value="LDAP_FIELD_PASSWORD"'.(($conf->global->LDAP_KEY_USERS && $conf->global->LDAP_KEY_USERS==$conf->global->LDAP_FIELD_PASSWORD)?' checked="true"':'')."></td>";
  163. print '</tr>';
  164. // Password crypted
  165. $var=!$var;
  166. print '<tr '.$bc[$var].'><td>'.$langs->trans("LDAPFieldPasswordCrypted").'</td><td>';
  167. print '<input size="25" type="text" name="fieldpasswordcrypted" value="'.$conf->global->LDAP_FIELD_PASSWORD_CRYPTED.'">';
  168. print '</td><td>'.$langs->trans("LDAPFieldPasswordExample").'</td>';
  169. print '<td align="right"><input type="radio" name="key" value="LDAP_FIELD_PASSWORD_CRYPTED"'.(($conf->global->LDAP_KEY_USERS && $conf->global->LDAP_KEY_USERS==$conf->global->LDAP_FIELD_PASSWORD_CRYPTED)?' checked="true"':'')."></td>";
  170. print '</tr>';
  171. // Mail
  172. $var=!$var;
  173. print '<tr '.$bc[$var].'><td>'.$langs->trans("LDAPFieldMail").'</td><td>';
  174. print '<input size="25" type="text" name="fieldmail" value="'.$conf->global->LDAP_FIELD_MAIL.'">';
  175. print '</td><td>'.$langs->trans("LDAPFieldMailExample").'</td>';
  176. print '<td align="right"><input type="radio" name="key" value="LDAP_FIELD_MAIL"'.(($conf->global->LDAP_KEY_USERS && $conf->global->LDAP_KEY_USERS==$conf->global->LDAP_FIELD_MAIL)?' checked="true"':'')."></td>";
  177. print '</tr>';
  178. // Phone
  179. $var=!$var;
  180. print '<tr '.$bc[$var].'><td>'.$langs->trans("LDAPFieldPhone").'</td><td>';
  181. print '<input size="25" type="text" name="fieldphone" value="'.$conf->global->LDAP_FIELD_PHONE.'">';
  182. print '</td><td>'.$langs->trans("LDAPFieldPhoneExample").'</td>';
  183. print '<td align="right"><input type="radio" name="key" value="LDAP_FIELD_PHONE"'.(($conf->global->LDAP_KEY_USERS && $conf->global->LDAP_KEY_USERS==$conf->global->LDAP_FIELD_PHONE)?' checked="true"':'')."></td>";
  184. print '</tr>';
  185. // Mobile
  186. $var=!$var;
  187. print '<tr '.$bc[$var].'><td>'.$langs->trans("LDAPFieldMobile").'</td><td>';
  188. print '<input size="25" type="text" name="fieldmobile" value="'.$conf->global->LDAP_FIELD_MOBILE.'">';
  189. print '</td><td>'.$langs->trans("LDAPFieldMobileExample").'</td>';
  190. print '<td align="right"><input type="radio" name="key" value="LDAP_FIELD_MOBILE"'.(($conf->global->LDAP_KEY_USERS && $conf->global->LDAP_KEY_USERS==$conf->global->LDAP_FIELD_MOBILE)?' checked="true"':'')."></td>";
  191. print '</tr>';
  192. // Fax
  193. $var=!$var;
  194. print '<tr '.$bc[$var].'><td>'.$langs->trans("LDAPFieldFax").'</td><td>';
  195. print '<input size="25" type="text" name="fieldfax" value="'.$conf->global->LDAP_FIELD_FAX.'">';
  196. print '</td><td>'.$langs->trans("LDAPFieldFaxExample").'</td>';
  197. print '<td align="right"><input type="radio" name="key" value="LDAP_FIELD_FAX"'.(($conf->global->LDAP_KEY_USERS && $conf->global->LDAP_KEY_USERS==$conf->global->LDAP_FIELD_FAX)?' checked="true"':'')."></td>";
  198. print '</tr>';
  199. // Description
  200. $var=!$var;
  201. print '<tr '.$bc[$var].'><td>'.$langs->trans("LDAPFieldDescription").'</td><td>';
  202. print '<input size="25" type="text" name="fielddescription" value="'.$conf->global->LDAP_FIELD_DESCRIPTION.'">';
  203. print '</td><td>'.$langs->trans("LDAPFieldDescriptionExample").'</td>';
  204. print '<td align="right"><input type="radio" name="key" value="LDAP_FIELD_DESCRIPTION"'.(($conf->global->LDAP_KEY_USERS && $conf->global->LDAP_KEY_USERS==$conf->global->LDAP_FIELD_DESCRIPTION)?' checked="true"':'')."></td>";
  205. print '</tr>';
  206. // Sid
  207. $var=!$var;
  208. print '<tr '.$bc[$var].'><td>'.$langs->trans("LDAPFieldSid").'</td><td>';
  209. print '<input size="25" type="text" name="fieldsid" value="'.$conf->global->LDAP_FIELD_SID.'">';
  210. print '</td><td>'.$langs->trans("LDAPFieldSidExample").'</td>';
  211. print '<td align="right"><input type="radio" name="key" value="LDAP_FIELD_SID"'.(($conf->global->LDAP_KEY_USERS && $conf->global->LDAP_KEY_USERS==$conf->global->LDAP_FIELD_SID)?' checked="true"':'')."></td>";
  212. print '</tr>';
  213. $var=!$var;
  214. print '<tr '.$bc[$var].'><td colspan="4" align="center"><input type="submit" class="button" value="'.$langs->trans("Modify").'"></td></tr>';
  215. print '</table>';
  216. print '</form>';
  217. print '</div>';
  218. print info_admin($langs->trans("LDAPDescValues"));
  219. /*
  220. * Test de la connexion
  221. */
  222. if ($conf->global->LDAP_SYNCHRO_ACTIVE == 'dolibarr2ldap')
  223. {
  224. $butlabel=$langs->trans("LDAPTestSynchroUser");
  225. $testlabel='testuser';
  226. $key=$conf->global->LDAP_KEY_USERS;
  227. $dn=$conf->global->LDAP_USER_DN;
  228. $objectclass=$conf->global->LDAP_USER_OBJECT_CLASS;
  229. show_ldap_test_button($butlabel,$testlabel,$key,$dn,$objectclass);
  230. }
  231. if (function_exists("ldap_connect"))
  232. {
  233. if ($_GET["action"] == 'testuser')
  234. {
  235. // Creation objet
  236. $object=new User($db);
  237. $object->initAsSpecimen();
  238. // TODO Mutualize code following with other ldap_xxxx.php pages
  239. // Test synchro
  240. $ldap=new Ldap();
  241. $result=$ldap->connect_bind();
  242. if ($result > 0)
  243. {
  244. $info=$object->_load_ldap_info();
  245. $dn=$object->_load_ldap_dn($info);
  246. $result1=$ldap->delete($dn); // To be sure to delete existing records
  247. $result2=$ldap->add($dn,$info,$user); // Now the test
  248. $result3=$ldap->delete($dn); // Clean what we did
  249. if ($result2 > 0)
  250. {
  251. print img_picto('','info').' ';
  252. print '<font class="ok">'.$langs->trans("LDAPSynchroOK").'</font><br>';
  253. }
  254. else
  255. {
  256. print img_picto('','error').' ';
  257. print '<font class="error">'.$langs->trans("LDAPSynchroKOMayBePermissions");
  258. print ': '.$ldap->error;
  259. print '</font><br>';
  260. print $langs->trans("ErrorLDAPMakeManualTest",$conf->ldap->dir_temp).'<br>';
  261. }
  262. print "<br>\n";
  263. print "LDAP input file used for test:<br><br>\n";
  264. print nl2br($ldap->dump_content($dn,$info));
  265. print "\n<br>";
  266. }
  267. else
  268. {
  269. print img_picto('','error').' ';
  270. print '<font class="error">'.$langs->trans("LDAPSynchroKO");
  271. print ': '.$ldap->error;
  272. print '</font><br>';
  273. print $langs->trans("ErrorLDAPMakeManualTest",$conf->ldap->dir_temp).'<br>';
  274. }
  275. }
  276. }
  277. $db->close();
  278. llxFooter();
  279. ?>