dolibarr  16.0.5
ldap_groups.php
Go to the documentation of this file.
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 
29 require '../main.inc.php';
30 require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
31 require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
32 require_once DOL_DOCUMENT_ROOT.'/user/class/usergroup.class.php';
33 require_once DOL_DOCUMENT_ROOT.'/core/class/ldap.class.php';
34 require_once DOL_DOCUMENT_ROOT.'/core/lib/ldap.lib.php';
35 
36 // Load translation files required by the page
37 $langs->loadLangs(array("admin", "errors"));
38 
39 if (!$user->admin) {
41 }
42 
43 $action = GETPOST('action', 'aZ09');
44 
45 
46 /*
47  * Actions
48  */
49 
50 if ($action == 'setvalue' && $user->admin) {
51  $error = 0;
52  $db->begin();
53 
54  if (!dolibarr_set_const($db, 'LDAP_GROUP_DN', GETPOST("group", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) {
55  $error++;
56  }
57  if (!dolibarr_set_const($db, 'LDAP_GROUP_OBJECT_CLASS', GETPOST("objectclass", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) {
58  $error++;
59  }
60  if (!dolibarr_set_const($db, 'LDAP_GROUP_FILTER', GETPOST("filter"), 'chaine', 0, '', $conf->entity)) {
61  $error++;
62  }
63  if (!dolibarr_set_const($db, 'LDAP_GROUP_FIELD_FULLNAME', GETPOST("fieldfullname", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) {
64  $error++;
65  }
66  //if (! dolibarr_set_const($db, 'LDAP_GROUP_FIELD_NAME',GETPOST("fieldname", 'alphanohtml'),'chaine',0,'',$conf->entity)) $error++;
67  if (!dolibarr_set_const($db, 'LDAP_GROUP_FIELD_DESCRIPTION', GETPOST("fielddescription", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) {
68  $error++;
69  }
70  if (!dolibarr_set_const($db, 'LDAP_GROUP_FIELD_GROUPMEMBERS', GETPOST("fieldgroupmembers", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) {
71  $error++;
72  }
73  if (!dolibarr_set_const($db, 'LDAP_GROUP_FIELD_GROUPID', GETPOST("fieldgroupid", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) {
74  $error++;
75  }
76 
77  // This one must be after the others
78  $valkey = '';
79  $key = GETPOST("key");
80  if ($key) {
81  $valkey = $conf->global->$key;
82  }
83  if (!dolibarr_set_const($db, 'LDAP_KEY_GROUPS', $valkey, 'chaine', 0, '', $conf->entity)) {
84  $error++;
85  }
86 
87  if (!$error) {
88  $db->commit();
89  setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
90  } else {
91  $db->rollback();
92  dol_print_error($db);
93  }
94 }
95 
96 
97 
98 /*
99  * View
100  */
101 
102 $form = new Form($db);
103 
104 llxHeader('', $langs->trans("LDAPSetup"), 'EN:Module_LDAP_En|FR:Module_LDAP|ES:M&oacute;dulo_LDAP');
105 $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
106 
107 print load_fiche_titre($langs->trans("LDAPSetup"), $linkback, 'title_setup');
108 
109 $head = ldap_prepare_head();
110 
111 // Test si fonction LDAP actives
112 if (!function_exists("ldap_connect")) {
113  setEventMessages($langs->trans("LDAPFunctionsNotAvailableOnPHP"), null, 'errors');
114 }
115 
116 print dol_get_fiche_head($head, 'groups', $langs->trans("LDAPSetup"), -1);
117 
118 
119 print '<span class="opacitymedium">'.$langs->trans("LDAPDescGroups").'</span><br>';
120 print '<br>';
121 
122 
123 print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?action=setvalue&token='.newToken().'">';
124 print '<input type="hidden" name="token" value="'.newToken().'">';
125 
126 print '<table class="noborder centpercent">';
127 
128 print '<tr class="liste_titre">';
129 print '<td colspan="4">'.$langs->trans("LDAPSynchronizeGroups").'</td>';
130 print "</tr>\n";
131 
132 // DN pour les groupes
133 print '<!-- LDAP_GROUP_DN -->';
134 print '<tr class="oddeven"><td><span class="fieldrequired">'.$langs->trans("LDAPGroupDn").'</span></td><td>';
135 print '<input size="48" type="text" name="group" value="'.$conf->global->LDAP_GROUP_DN.'">';
136 print '</td><td>'.$langs->trans("LDAPGroupDnExample").'</td>';
137 print '<td>&nbsp;</td>';
138 print '</tr>';
139 
140 // List of object class used to define attributes in structure
141 print '<!-- LDAP_GROUP_OBJECT_CLASS -->';
142 print '<tr class="oddeven"><td><span class="fieldrequired">'.$langs->trans("LDAPGroupObjectClassList").'</span></td><td>';
143 print '<input size="48" type="text" name="objectclass" value="'.$conf->global->LDAP_GROUP_OBJECT_CLASS.'">';
144 print '</td><td>'.$langs->trans("LDAPGroupObjectClassListExample").'</td>';
145 print '<td>&nbsp;</td>';
146 print '</tr>';
147 
148 // Filter, used to filter search
149 print '<!-- LDAP_GROUP_FILTER -->';
150 print '<tr class="oddeven"><td>'.$langs->trans("LDAPFilterConnection").'</td><td>';
151 print '<input size="48" type="text" name="filter" value="'.$conf->global->LDAP_GROUP_FILTER.'">';
152 print '</td><td>'.$langs->trans("LDAPGroupFilterExample").'</td>';
153 print '<td></td>';
154 print '</tr>';
155 
156 print '</table>';
157 
158 print '<br>';
159 
160 print '<table class="noborder centpercent">';
161 
162 print '<tr class="liste_titre">';
163 print '<td>'.$langs->trans("LDAPDolibarrMapping").'</td>';
164 print '<td colspan="2">'.$langs->trans("LDAPLdapMapping").'</td>';
165 print '<td class="right">'.$langs->trans("LDAPNamingAttribute").'</td>';
166 print "</tr>\n";
167 
168 // Filtre
169 
170 // Common name
171 print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldName").'</td><td>';
172 print '<input size="25" type="text" name="fieldfullname" value="'.$conf->global->LDAP_GROUP_FIELD_FULLNAME.'">';
173 print '</td><td>'.$langs->trans("LDAPFieldCommonNameExample").'</td>';
174 print '<td class="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>";
175 print '</tr>';
176 
177 // Name
178 /*
179 print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldName").'</td><td>';
180 print '<input size="25" type="text" name="fieldname" value="'.$conf->global->LDAP_GROUP_FIELD_NAME.'">';
181 print '</td><td>'.$langs->trans("LDAPFieldNameExample").'</td>';
182 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>";
183 print '</tr>';
184 */
185 
186 // Description
187 print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldDescription").'</td><td>';
188 print '<input size="25" type="text" name="fielddescription" value="'.$conf->global->LDAP_GROUP_FIELD_DESCRIPTION.'">';
189 print '</td><td>'.$langs->trans("LDAPFieldDescriptionExample").'</td>';
190 print '<td class="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>";
191 print '</tr>';
192 
193 // User group
194 print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldGroupMembers").'</td><td>';
195 print '<input size="25" type="text" name="fieldgroupmembers" value="'.$conf->global->LDAP_GROUP_FIELD_GROUPMEMBERS.'">';
196 print '</td><td>'.$langs->trans("LDAPFieldGroupMembersExample").'</td>';
197 print '<td class="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>";
198 print '</tr>';
199 
200 // Group id
201 print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldGroupid").'</td><td>';
202 print '<input size="25" type="text" name="fieldgroupid" value="'.$conf->global->LDAP_GROUP_FIELD_GROUPID.'">';
203 print '</td><td>'.$langs->trans("LDAPFieldGroupidExample").'</td>';
204 print '<td class="right">&nbsp;</td>';
205 print '</tr>';
206 
207 print '</table>';
208 
209 print info_admin($langs->trans("LDAPDescValues"));
210 
211 print dol_get_fiche_end();
212 
213 print $form->buttonsSaveCancel("Modify", '');
214 
215 print '</form>';
216 
217 
218 /*
219  * Test de la connexion
220  */
221 if (getDolGlobalInt('LDAP_SYNCHRO_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
222  $butlabel = $langs->trans("LDAPTestSynchroGroup");
223  $testlabel = 'testgroup';
224  $key = $conf->global->LDAP_KEY_GROUPS;
225  $dn = $conf->global->LDAP_GROUP_DN;
226  $objectclass = $conf->global->LDAP_GROUP_OBJECT_CLASS;
227 
228  show_ldap_test_button($butlabel, $testlabel, $key, $dn, $objectclass);
229 } elseif (getDolGlobalInt('LDAP_SYNCHRO_ACTIVE') === Ldap::SYNCHRO_LDAP_TO_DOLIBARR) {
230  $butlabel = $langs->trans("LDAPTestSearch");
231  $testlabel = 'testsearchgroup';
232  $key = $conf->global->LDAP_KEY_GROUPS;
233  $dn = $conf->global->LDAP_GROUP_DN;
234  $objectclass = $conf->global->LDAP_GROUP_OBJECT_CLASS;
235  show_ldap_test_button($butlabel, $testlabel, $key, $dn, $objectclass);
236 }
237 
238 if (function_exists("ldap_connect")) {
239  if ($action == 'testgroup') {
240  // Creation objet
241  $object = new UserGroup($db);
242  $object->initAsSpecimen();
243 
244  // Test synchro
245  $ldap = new Ldap();
246  $result = $ldap->connect_bind();
247 
248  if ($result > 0) {
249  $info = $object->_load_ldap_info();
250  $dn = $object->_load_ldap_dn($info);
251 
252  // Get a gid number for objectclass PosixGroup
253  if (in_array('posixGroup', $info['objectclass'])) {
254  $info['gidNumber'] = $ldap->getNextGroupGid('LDAP_KEY_GROUPS');
255  }
256 
257  $result1 = $ldap->delete($dn); // To be sure to delete existing records
258  $result2 = $ldap->add($dn, $info, $user); // Now the test
259  $result3 = $ldap->delete($dn); // Clean what we did
260 
261  if ($result2 > 0) {
262  print img_picto('', 'info').' ';
263  print '<span class="ok">'.$langs->trans("LDAPSynchroOK").'</span><br>';
264  } else {
265  print img_picto('', 'error').' ';
266  print '<span class="error">'.$langs->trans("LDAPSynchroKOMayBePermissions");
267  print ': '.$ldap->error;
268  print '</span><br>';
269  print $langs->trans("ErrorLDAPMakeManualTest", $conf->ldap->dir_temp).'<br>';
270  }
271 
272  print "<br>\n";
273  print "LDAP input file used for test:<br><br>\n";
274  print nl2br($ldap->dump_content($dn, $info));
275  print "\n<br>";
276  } else {
277  print img_picto('', 'error').' ';
278  print '<span class="error">'.$langs->trans("LDAPSynchroKO");
279  print ': '.$ldap->error;
280  print '</span><br>';
281  print $langs->trans("ErrorLDAPMakeManualTest", $conf->ldap->dir_temp).'<br>';
282  }
283  }
284 
285  if ($action == 'testsearchgroup') {
286  // TODO Mutualize code following with other ldap_xxxx.php pages
287 
288  // Test synchro
289  $ldap = new Ldap();
290  $result = $ldap->connect_bind();
291 
292  if ($result > 0) {
293  $required_fields = array(
294  $conf->global->LDAP_KEY_GROUPS,
295  // $conf->global->LDAP_GROUP_FIELD_NAME,
296  $conf->global->LDAP_GROUP_FIELD_DESCRIPTION,
297  $conf->global->LDAP_GROUP_FIELD_GROUPMEMBERS,
298  $conf->global->LDAP_GROUP_FIELD_GROUPID
299  );
300 
301  // Remove from required_fields all entries not configured in LDAP (empty) and duplicated
302  $required_fields = array_unique(array_values(array_filter($required_fields, "dol_validElement")));
303 
304  // Get from LDAP database an array of results
305  $ldapgroups = $ldap->getRecords('*', $conf->global->LDAP_GROUP_DN, $conf->global->LDAP_KEY_GROUPS, $required_fields, 'group');
306  //$ldapgroups = $ldap->getRecords('*', $conf->global->LDAP_GROUP_DN, $conf->global->LDAP_KEY_GROUPS, '', 'group');
307 
308  if (is_array($ldapgroups)) {
309  $liste = array();
310  foreach ($ldapgroups as $key => $ldapgroup) {
311  // Define the label string for this group
312  $label = '';
313  foreach ($required_fields as $value) {
314  if ($value) {
315  $label .= $value."=".$ldapgroup[$value]." ";
316  }
317  }
318  $liste[$key] = $label;
319  }
320  } else {
321  setEventMessages($ldap->error, $ldap->errors, 'errors');
322  }
323 
324  print "<br>\n";
325  print "LDAP search for group:<br>\n";
326  print "search: *<br>\n";
327  print "userDN: ".$conf->global->LDAP_GROUP_DN."<br>\n";
328  print "useridentifier: ".$conf->global->LDAP_KEY_GROUPS."<br>\n";
329  print "required_fields: ".implode(',', $required_fields)."<br>\n";
330  print "=> ".count($liste)." records<br>\n";
331  print "\n<br>";
332  } else {
333  print img_picto('', 'error').' ';
334  print '<span class="error">'.$langs->trans("LDAPSynchroKO");
335  print ': '.$ldap->error;
336  print '</span><br>';
337  print $langs->trans("ErrorLDAPMakeManualTest", $conf->ldap->dir_temp).'<br>';
338  }
339  }
340 }
341 
342 // End of page
343 llxFooter();
344 $db->close();
Ldap
Class to manage LDAP features.
Definition: ldap.class.php:34
llxFooter
llxFooter()
Empty footer.
Definition: wrapper.php:73
load_fiche_titre
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
Definition: functions.lib.php:5204
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
UserGroup
Class to manage user groups.
Definition: usergroup.class.php:39
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
Ldap\SYNCHRO_DOLIBARR_TO_LDAP
const SYNCHRO_DOLIBARR_TO_LDAP
Dolibarr to Ldap synchronization.
Definition: ldap.class.php:133
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:3880
dol_get_fiche_head
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='')
Show tabs of a record.
Definition: functions.lib.php:1822
info_admin
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin='1', $morecss='hideonsmartphone', $textfordropdown='')
Show information for admin users or standard users.
Definition: functions.lib.php:4800
newToken
newToken()
Return the value of token currently saved into session with name 'newtoken'.
Definition: functions.lib.php:10878
dol_get_fiche_end
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition: functions.lib.php:2018
dolibarr_set_const
dolibarr_set_const($db, $name, $value, $type='chaine', $visible=0, $note='', $entity=1)
Insert a parameter (key,value) into database (delete old key then insert it again).
Definition: admin.lib.php:627
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
Ldap\SYNCHRO_LDAP_TO_DOLIBARR
const SYNCHRO_LDAP_TO_DOLIBARR
Ldap to Dolibarr synchronization.
Definition: ldap.class.php:138
ldap_prepare_head
ldap_prepare_head()
Initialize the array of tabs for customer invoice.
Definition: ldap.lib.php:31
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137
accessforbidden
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Definition: security.lib.php:933
getDolGlobalInt
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
Definition: functions.lib.php:93
llxHeader
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOCSRFCHECK')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:59
show_ldap_test_button
show_ldap_test_button($butlabel, $testlabel, $key, $dn, $objectclass)
Show button test LDAP synchro.
Definition: ldap.lib.php:102