dolibarr 23.0.3
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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
9 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 */
24
31// Load Dolibarr environment
32require '../main.inc.php';
33require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
34require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
35require_once DOL_DOCUMENT_ROOT.'/user/class/usergroup.class.php';
36require_once DOL_DOCUMENT_ROOT.'/core/class/ldap.class.php';
37require_once DOL_DOCUMENT_ROOT.'/core/lib/ldap.lib.php';
38
47// Load translation files required by the page
48$langs->loadLangs(array("admin", "errors"));
49
50if (!$user->admin) {
52}
53
54$action = GETPOST('action', 'aZ09');
55
56
57/*
58 * Actions
59 */
60
61if ($action == 'setvalue' /* && $user->admin */) {
62 $error = 0;
63 $db->begin();
64
65 if (!dolibarr_set_const($db, 'LDAP_GROUP_DN', GETPOST("group", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) {
66 $error++;
67 }
68 if (!dolibarr_set_const($db, 'LDAP_GROUP_OBJECT_CLASS', GETPOST("objectclass", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) {
69 $error++;
70 }
71 if (!dolibarr_set_const($db, 'LDAP_GROUP_FILTER', GETPOST("filter"), 'chaine', 0, '', $conf->entity)) {
72 $error++;
73 }
74 if (!dolibarr_set_const($db, 'LDAP_GROUP_FIELD_FULLNAME', GETPOST("fieldfullname", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) {
75 $error++;
76 }
77 //if (! dolibarr_set_const($db, 'LDAP_GROUP_FIELD_NAME',GETPOST("fieldname", 'alphanohtml'),'chaine',0,'',$conf->entity)) $error++;
78 if (!dolibarr_set_const($db, 'LDAP_GROUP_FIELD_DESCRIPTION', GETPOST("fielddescription", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) {
79 $error++;
80 }
81 if (!dolibarr_set_const($db, 'LDAP_GROUP_FIELD_GROUPMEMBERS', GETPOST("fieldgroupmembers", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) {
82 $error++;
83 }
84 if (!dolibarr_set_const($db, 'LDAP_GROUP_FIELD_GROUPID', GETPOST("fieldgroupid", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) {
85 $error++;
86 }
87
88 // This one must be after the others
89 $valkey = '';
90 $key = GETPOST("key");
91 if ($key) {
92 $valkey = getDolGlobalString($key);
93 }
94 if (!dolibarr_set_const($db, 'LDAP_KEY_GROUPS', $valkey, 'chaine', 0, '', $conf->entity)) {
95 $error++;
96 }
97
98 if (!$error) {
99 $db->commit();
100 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
101 } else {
102 $db->rollback();
103 dol_print_error($db);
104 }
105}
106
107
108
109/*
110 * View
111 */
112
113$form = new Form($db);
114
115llxHeader('', $langs->trans("LDAPSetup"), 'EN:Module_LDAP_En|FR:Module_LDAP|ES:M&oacute;dulo_LDAP', '', 0, 0, '', '', '', 'mod-admin page-ldap_groups');
116
117$linkback = '<a href="'.dolBuildUrl(DOL_URL_ROOT.'/admin/modules.php', ['restore_lastsearch_values' => 1]).'">'.img_picto($langs->trans("BackToModuleList"), 'back', 'class="pictofixedwidth"').'<span class="hideonsmartphone">'.$langs->trans("BackToModuleList").'</span></a>';
118
119print load_fiche_titre($langs->trans("LDAPSetup"), $linkback, 'title_setup');
120
121$head = ldap_prepare_head();
122
123// Test if the LDAP functionality is available
124if (!function_exists("ldap_connect")) {
125 setEventMessages($langs->trans("LDAPFunctionsNotAvailableOnPHP"), null, 'errors');
126}
127
128print dol_get_fiche_head($head, 'groups', '', -1);
129
130
131print '<span class="opacitymedium">'.$langs->trans("LDAPDescGroups").'</span><br>';
132print '<br>';
133
134
135print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?action=setvalue&token='.newToken().'">';
136print '<input type="hidden" name="token" value="'.newToken().'">';
137
138print '<table class="noborder centpercent">';
139
140print '<tr class="liste_titre">';
141print '<td colspan="4">'.$langs->trans("LDAPSynchronizeGroups").'</td>';
142print "</tr>\n";
143
144// DN (Domain Name) for the groups
145print '<!-- LDAP_GROUP_DN -->';
146print '<tr class="oddeven"><td><span class="fieldrequired">'.$langs->trans("LDAPGroupDn").'</span></td><td>';
147print '<input size="48" type="text" name="group" value="'.getDolGlobalString('LDAP_GROUP_DN').'">';
148print '</td><td>'.$langs->trans("LDAPGroupDnExample").'</td>';
149print '<td>&nbsp;</td>';
150print '</tr>';
151
152// List of object class used to define attributes in structure
153print '<!-- LDAP_GROUP_OBJECT_CLASS -->';
154print '<tr class="oddeven"><td><span class="fieldrequired">'.$langs->trans("LDAPGroupObjectClassList").'</span></td><td>';
155print '<input size="48" type="text" name="objectclass" value="'.getDolGlobalString('LDAP_GROUP_OBJECT_CLASS').'">';
156print '</td><td>'.$langs->trans("LDAPGroupObjectClassListExample").'</td>';
157print '<td>&nbsp;</td>';
158print '</tr>';
159
160// Filter, used to filter search
161print '<!-- LDAP_GROUP_FILTER -->';
162print '<tr class="oddeven"><td>'.$langs->trans("LDAPFilterConnection").'</td><td>';
163print '<input size="48" type="text" name="filter" value="'.getDolGlobalString('LDAP_GROUP_FILTER').'">';
164print '</td><td>'.$langs->trans("LDAPGroupFilterExample").'</td>';
165print '<td></td>';
166print '</tr>';
167
168print '</table>';
169
170print '<br>';
171
172print '<table class="noborder centpercent">';
173
174print '<tr class="liste_titre">';
175print '<td>'.$langs->trans("LDAPDolibarrMapping").'</td>';
176print '<td colspan="2">'.$langs->trans("LDAPLdapMapping").'</td>';
177print '<td class="right">'.$langs->trans("LDAPNamingAttribute").'</td>';
178print "</tr>\n";
179
180// Filtre
181
182// Common name
183print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldName").'</td><td>';
184print '<input size="25" type="text" name="fieldfullname" value="'.getDolGlobalString('LDAP_GROUP_FIELD_FULLNAME').'">';
185print '</td><td>'.$langs->trans("LDAPFieldCommonNameExample").'</td>';
186print '<td class="right"><input type="radio" name="key" value="LDAP_GROUP_FIELD_FULLNAME"'.((getDolGlobalString('LDAP_KEY_GROUPS') == getDolGlobalString('LDAP_GROUP_FIELD_FULLNAME')) ? ' checked' : '')."></td>";
187print '</tr>';
188
189// Name
190/*
191print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldName").'</td><td>';
192print '<input size="25" type="text" name="fieldname" value="'.$conf->global->LDAP_GROUP_FIELD_NAME.'">';
193print '</td><td>'.$langs->trans("LDAPFieldNameExample").'</td>';
194print '<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>";
195print '</tr>';
196*/
197
198// Description
199print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldDescription").'</td><td>';
200print '<input size="25" type="text" name="fielddescription" value="'.getDolGlobalString('LDAP_GROUP_FIELD_DESCRIPTION').'">';
201print '</td><td>'.$langs->trans("LDAPFieldDescriptionExample").'</td>';
202print '<td class="right"><input type="radio" name="key" value="LDAP_GROUP_FIELD_DESCRIPTION"'.((getDolGlobalString('LDAP_KEY_GROUPS') == getDolGlobalString('LDAP_GROUP_FIELD_DESCRIPTION')) ? ' checked' : '')."></td>";
203print '</tr>';
204
205// User group
206print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldGroupMembers").'</td><td>';
207print '<input size="25" type="text" name="fieldgroupmembers" value="'.getDolGlobalString('LDAP_GROUP_FIELD_GROUPMEMBERS').'">';
208print '</td><td>'.$langs->trans("LDAPFieldGroupMembersExample").'</td>';
209print '<td class="right"><input type="radio" name="key" value="LDAP_GROUP_FIELD_GROUPMEMBERS"'.((getDolGlobalString('LDAP_KEY_GROUPS') == getDolGlobalString('LDAP_GROUP_FIELD_GROUPMEMBERS')) ? ' checked' : '')."></td>";
210print '</tr>';
211
212// Group id
213print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldGroupid").'</td><td>';
214print '<input size="25" type="text" name="fieldgroupid" value="'.getDolGlobalString('LDAP_GROUP_FIELD_GROUPID').'">';
215print '</td><td>'.$langs->trans("LDAPFieldGroupidExample").'</td>';
216print '<td class="right">&nbsp;</td>';
217print '</tr>';
218
219print '</table>';
220
221print info_admin($langs->trans("LDAPDescValues"));
222
223print dol_get_fiche_end();
224
225print $form->buttonsSaveCancel("Modify", '');
226
227print '</form>';
228
229
230/*
231 * Test the connection
232 */
233if (getDolGlobalInt('LDAP_SYNCHRO_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
234 $butlabel = $langs->trans("LDAPTestSynchroGroup");
235 $testlabel = 'testgroup';
236 $key = getDolGlobalString('LDAP_KEY_GROUPS');
237 $dn = getDolGlobalString('LDAP_GROUP_DN');
238 $objectclass = getDolGlobalString('LDAP_GROUP_OBJECT_CLASS');
239
240 show_ldap_test_button($butlabel, $testlabel, $key, $dn, $objectclass);
241} elseif (getDolGlobalInt('LDAP_SYNCHRO_ACTIVE') === Ldap::SYNCHRO_LDAP_TO_DOLIBARR) {
242 $butlabel = $langs->trans("LDAPTestSearch");
243 $testlabel = 'testsearchgroup';
244 $key = getDolGlobalString('LDAP_KEY_GROUPS');
245 $dn = getDolGlobalString('LDAP_GROUP_DN');
246 $objectclass = getDolGlobalString('LDAP_GROUP_OBJECT_CLASS');
247 show_ldap_test_button($butlabel, $testlabel, $key, $dn, $objectclass);
248}
249
250if (function_exists("ldap_connect")) {
251 if ($action == 'testgroup') {
252 // Create object
253 $object = new UserGroup($db);
254 $object->initAsSpecimen();
255
256 // Test synchro
257 $ldap = new Ldap();
258 $result = $ldap->connectBind();
259
260 if ($result > 0) {
261 $info = $object->_load_ldap_info();
262 $dn = $object->_load_ldap_dn($info);
263
264 // Get a gid number for objectclass PosixGroup
265 if (in_array('posixGroup', $info['objectclass'])) {
266 $info['gidNumber'] = $ldap->getNextGroupGid('LDAP_KEY_GROUPS');
267 }
268
269 $result1 = $ldap->delete($dn); // To be sure to delete existing records
270 $result2 = $ldap->add($dn, $info, $user); // Now the test
271 $result3 = $ldap->delete($dn); // Clean what we did
272
273 if ($result2 > 0) {
274 print img_picto('', 'info').' ';
275 print '<span class="ok">'.$langs->trans("LDAPSynchroOK").'</span><br>';
276 } else {
277 print img_picto('', 'error').' ';
278 print '<span class="error">'.$langs->trans("LDAPSynchroKOMayBePermissions");
279 print ': '.$ldap->error;
280 print '</span><br>';
281 print $langs->trans("ErrorLDAPMakeManualTest", $conf->ldap->dir_temp).'<br>';
282 }
283
284 print "<br>\n";
285 print "LDAP input file used for test:<br><br>\n";
286 print nl2br($ldap->dumpContent($dn, $info));
287 print "\n<br>";
288 } else {
289 print img_picto('', 'error').' ';
290 print '<span class="error">'.$langs->trans("LDAPSynchroKO");
291 print ': '.$ldap->error;
292 print '</span><br>';
293 print $langs->trans("ErrorLDAPMakeManualTest", $conf->ldap->dir_temp).'<br>';
294 }
295 }
296
297 if ($action == 'testsearchgroup') {
298 // TODO Mutualize code below with other ldap_xxxx.php pages
299
300 // Test synchro
301 $ldap = new Ldap();
302 $result = $ldap->connectBind();
303
304 if ($result > 0) {
305 $required_fields = array(
306 getDolGlobalString('LDAP_KEY_GROUPS'),
307 // getDolGlobalString('LDAP_GROUP_FIELD_NAME'),
308 getDolGlobalString('LDAP_GROUP_FIELD_DESCRIPTION'),
309 getDolGlobalString('LDAP_GROUP_FIELD_GROUPMEMBERS'),
310 getDolGlobalString('LDAP_GROUP_FIELD_GROUPID')
311 );
312
313 // Remove from required_fields all entries not configured in LDAP (empty) and duplicated
314 $required_fields = array_unique(array_values(array_filter($required_fields, "dol_validElement")));
315
316 // Get from LDAP database an array of results
317 $ldapgroups = $ldap->getRecords('*', getDolGlobalString('LDAP_GROUP_DN'), getDolGlobalString('LDAP_KEY_GROUPS'), $required_fields, 'group');
318 //$ldapgroups = $ldap->getRecords('*', $conf->global->LDAP_GROUP_DN, $conf->global->LDAP_KEY_GROUPS, '', 'group');
319
320 $liste = array();
321 if (is_array($ldapgroups)) {
322 foreach ($ldapgroups as $key => $ldapgroup) {
323 // Define the label string for this group
324 $label = '';
325 foreach ($required_fields as $value) {
326 if ($value) {
327 $label .= $value."=".$ldapgroup[$value]." ";
328 }
329 }
330 $liste[$key] = $label;
331 }
332 } else {
333 setEventMessages($ldap->error, $ldap->errors, 'errors');
334 }
335
336 print "<br>\n";
337 print "LDAP search for group:<br>\n";
338 print "search: *<br>\n";
339 print "userDN: ".getDolGlobalString('LDAP_GROUP_DN')."<br>\n";
340 print "useridentifier: ".getDolGlobalString('LDAP_KEY_GROUPS')."<br>\n";
341 print "required_fields: ".implode(',', $required_fields)."<br>\n";
342 print "=> ".count($liste)." records<br>\n";
343 print "\n<br>";
344 } else {
345 print img_picto('', 'error').' ';
346 print '<span class="error">'.$langs->trans("LDAPSynchroKO");
347 print ': '.$ldap->error;
348 print '</span><br>';
349 print $langs->trans("ErrorLDAPMakeManualTest", $conf->ldap->dir_temp).'<br>';
350 }
351 }
352}
353
354// End of page
355llxFooter();
356$db->close();
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
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).
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:91
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:73
Class to manage generation of HTML components Only common components must be here.
Class to manage LDAP features.
Class to manage user groups.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0, $morecssdiv='')
Show tabs of a record.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
newToken()
Return the value of token currently saved into session with name 'newtoken'.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin='1', $morecss='hideonsmartphone', $textfordropdown='', $picto='')
Show information in HTML for admin users or standard users.
ldap_prepare_head()
Initialize the array of tabs for customer invoice.
Definition ldap.lib.php:33
show_ldap_test_button($butlabel, $testlabel, $key, $dn, $objectclass)
Show button test LDAP synchro.
Definition ldap.lib.php:104
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.