dolibarr 21.0.0-beta
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 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$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
117
118print load_fiche_titre($langs->trans("LDAPSetup"), $linkback, 'title_setup');
119
120$head = ldap_prepare_head();
121
122// Test if the LDAP functionality is available
123if (!function_exists("ldap_connect")) {
124 setEventMessages($langs->trans("LDAPFunctionsNotAvailableOnPHP"), null, 'errors');
125}
126
127print dol_get_fiche_head($head, 'groups', '', -1);
128
129
130print '<span class="opacitymedium">'.$langs->trans("LDAPDescGroups").'</span><br>';
131print '<br>';
132
133
134print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?action=setvalue&token='.newToken().'">';
135print '<input type="hidden" name="token" value="'.newToken().'">';
136
137print '<table class="noborder centpercent">';
138
139print '<tr class="liste_titre">';
140print '<td colspan="4">'.$langs->trans("LDAPSynchronizeGroups").'</td>';
141print "</tr>\n";
142
143// DN (Domain Name) for the groups
144print '<!-- LDAP_GROUP_DN -->';
145print '<tr class="oddeven"><td><span class="fieldrequired">'.$langs->trans("LDAPGroupDn").'</span></td><td>';
146print '<input size="48" type="text" name="group" value="'.getDolGlobalString('LDAP_GROUP_DN').'">';
147print '</td><td>'.$langs->trans("LDAPGroupDnExample").'</td>';
148print '<td>&nbsp;</td>';
149print '</tr>';
150
151// List of object class used to define attributes in structure
152print '<!-- LDAP_GROUP_OBJECT_CLASS -->';
153print '<tr class="oddeven"><td><span class="fieldrequired">'.$langs->trans("LDAPGroupObjectClassList").'</span></td><td>';
154print '<input size="48" type="text" name="objectclass" value="'.getDolGlobalString('LDAP_GROUP_OBJECT_CLASS').'">';
155print '</td><td>'.$langs->trans("LDAPGroupObjectClassListExample").'</td>';
156print '<td>&nbsp;</td>';
157print '</tr>';
158
159// Filter, used to filter search
160print '<!-- LDAP_GROUP_FILTER -->';
161print '<tr class="oddeven"><td>'.$langs->trans("LDAPFilterConnection").'</td><td>';
162print '<input size="48" type="text" name="filter" value="'.getDolGlobalString('LDAP_GROUP_FILTER').'">';
163print '</td><td>'.$langs->trans("LDAPGroupFilterExample").'</td>';
164print '<td></td>';
165print '</tr>';
166
167print '</table>';
168
169print '<br>';
170
171print '<table class="noborder centpercent">';
172
173print '<tr class="liste_titre">';
174print '<td>'.$langs->trans("LDAPDolibarrMapping").'</td>';
175print '<td colspan="2">'.$langs->trans("LDAPLdapMapping").'</td>';
176print '<td class="right">'.$langs->trans("LDAPNamingAttribute").'</td>';
177print "</tr>\n";
178
179// Filtre
180
181// Common name
182print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldName").'</td><td>';
183print '<input size="25" type="text" name="fieldfullname" value="'.getDolGlobalString('LDAP_GROUP_FIELD_FULLNAME').'">';
184print '</td><td>'.$langs->trans("LDAPFieldCommonNameExample").'</td>';
185print '<td class="right"><input type="radio" name="key" value="LDAP_GROUP_FIELD_FULLNAME"'.((getDolGlobalString('LDAP_KEY_GROUPS') == getDolGlobalString('LDAP_GROUP_FIELD_FULLNAME')) ? ' checked' : '')."></td>";
186print '</tr>';
187
188// Name
189/*
190print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldName").'</td><td>';
191print '<input size="25" type="text" name="fieldname" value="'.$conf->global->LDAP_GROUP_FIELD_NAME.'">';
192print '</td><td>'.$langs->trans("LDAPFieldNameExample").'</td>';
193print '<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>";
194print '</tr>';
195*/
196
197// Description
198print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldDescription").'</td><td>';
199print '<input size="25" type="text" name="fielddescription" value="'.getDolGlobalString('LDAP_GROUP_FIELD_DESCRIPTION').'">';
200print '</td><td>'.$langs->trans("LDAPFieldDescriptionExample").'</td>';
201print '<td class="right"><input type="radio" name="key" value="LDAP_GROUP_FIELD_DESCRIPTION"'.((getDolGlobalString('LDAP_KEY_GROUPS') == getDolGlobalString('LDAP_GROUP_FIELD_DESCRIPTION')) ? ' checked' : '')."></td>";
202print '</tr>';
203
204// User group
205print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldGroupMembers").'</td><td>';
206print '<input size="25" type="text" name="fieldgroupmembers" value="'.getDolGlobalString('LDAP_GROUP_FIELD_GROUPMEMBERS').'">';
207print '</td><td>'.$langs->trans("LDAPFieldGroupMembersExample").'</td>';
208print '<td class="right"><input type="radio" name="key" value="LDAP_GROUP_FIELD_GROUPMEMBERS"'.((getDolGlobalString('LDAP_KEY_GROUPS') == getDolGlobalString('LDAP_GROUP_FIELD_GROUPMEMBERS')) ? ' checked' : '')."></td>";
209print '</tr>';
210
211// Group id
212print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldGroupid").'</td><td>';
213print '<input size="25" type="text" name="fieldgroupid" value="'.getDolGlobalString('LDAP_GROUP_FIELD_GROUPID').'">';
214print '</td><td>'.$langs->trans("LDAPFieldGroupidExample").'</td>';
215print '<td class="right">&nbsp;</td>';
216print '</tr>';
217
218print '</table>';
219
220print info_admin($langs->trans("LDAPDescValues"));
221
222print dol_get_fiche_end();
223
224print $form->buttonsSaveCancel("Modify", '');
225
226print '</form>';
227
228
229/*
230 * Test the connection
231 */
232if (getDolGlobalInt('LDAP_SYNCHRO_ACTIVE') === Ldap::SYNCHRO_DOLIBARR_TO_LDAP) {
233 $butlabel = $langs->trans("LDAPTestSynchroGroup");
234 $testlabel = 'testgroup';
235 $key = getDolGlobalString('LDAP_KEY_GROUPS');
236 $dn = getDolGlobalString('LDAP_GROUP_DN');
237 $objectclass = getDolGlobalString('LDAP_GROUP_OBJECT_CLASS');
238
239 show_ldap_test_button($butlabel, $testlabel, $key, $dn, $objectclass);
240} elseif (getDolGlobalInt('LDAP_SYNCHRO_ACTIVE') === Ldap::SYNCHRO_LDAP_TO_DOLIBARR) {
241 $butlabel = $langs->trans("LDAPTestSearch");
242 $testlabel = 'testsearchgroup';
243 $key = getDolGlobalString('LDAP_KEY_GROUPS');
244 $dn = getDolGlobalString('LDAP_GROUP_DN');
245 $objectclass = getDolGlobalString('LDAP_GROUP_OBJECT_CLASS');
246 show_ldap_test_button($butlabel, $testlabel, $key, $dn, $objectclass);
247}
248
249if (function_exists("ldap_connect")) {
250 if ($action == 'testgroup') {
251 // Create object
252 $object = new UserGroup($db);
253 $object->initAsSpecimen();
254
255 // Test synchro
256 $ldap = new Ldap();
257 $result = $ldap->connectBind();
258
259 if ($result > 0) {
260 $info = $object->_load_ldap_info();
261 $dn = $object->_load_ldap_dn($info);
262
263 // Get a gid number for objectclass PosixGroup
264 if (in_array('posixGroup', $info['objectclass'])) {
265 $info['gidNumber'] = $ldap->getNextGroupGid('LDAP_KEY_GROUPS');
266 }
267
268 $result1 = $ldap->delete($dn); // To be sure to delete existing records
269 $result2 = $ldap->add($dn, $info, $user); // Now the test
270 $result3 = $ldap->delete($dn); // Clean what we did
271
272 if ($result2 > 0) {
273 print img_picto('', 'info').' ';
274 print '<span class="ok">'.$langs->trans("LDAPSynchroOK").'</span><br>';
275 } else {
276 print img_picto('', 'error').' ';
277 print '<span class="error">'.$langs->trans("LDAPSynchroKOMayBePermissions");
278 print ': '.$ldap->error;
279 print '</span><br>';
280 print $langs->trans("ErrorLDAPMakeManualTest", $conf->ldap->dir_temp).'<br>';
281 }
282
283 print "<br>\n";
284 print "LDAP input file used for test:<br><br>\n";
285 print nl2br($ldap->dumpContent($dn, $info));
286 print "\n<br>";
287 } else {
288 print img_picto('', 'error').' ';
289 print '<span class="error">'.$langs->trans("LDAPSynchroKO");
290 print ': '.$ldap->error;
291 print '</span><br>';
292 print $langs->trans("ErrorLDAPMakeManualTest", $conf->ldap->dir_temp).'<br>';
293 }
294 }
295
296 if ($action == 'testsearchgroup') {
297 // TODO Mutualize code below with other ldap_xxxx.php pages
298
299 // Test synchro
300 $ldap = new Ldap();
301 $result = $ldap->connectBind();
302
303 if ($result > 0) {
304 $required_fields = array(
305 getDolGlobalString('LDAP_KEY_GROUPS'),
306 // getDolGlobalString('LDAP_GROUP_FIELD_NAME'),
307 getDolGlobalString('LDAP_GROUP_FIELD_DESCRIPTION'),
308 getDolGlobalString('LDAP_GROUP_FIELD_GROUPMEMBERS'),
309 getDolGlobalString('LDAP_GROUP_FIELD_GROUPID')
310 );
311
312 // Remove from required_fields all entries not configured in LDAP (empty) and duplicated
313 $required_fields = array_unique(array_values(array_filter($required_fields, "dol_validElement")));
314
315 // Get from LDAP database an array of results
316 $ldapgroups = $ldap->getRecords('*', getDolGlobalString('LDAP_GROUP_DN'), getDolGlobalString('LDAP_KEY_GROUPS'), $required_fields, 'group');
317 //$ldapgroups = $ldap->getRecords('*', $conf->global->LDAP_GROUP_DN, $conf->global->LDAP_KEY_GROUPS, '', 'group');
318
319 $liste = array();
320 if (is_array($ldapgroups)) {
321 foreach ($ldapgroups as $key => $ldapgroup) {
322 // Define the label string for this group
323 $label = '';
324 foreach ($required_fields as $value) {
325 if ($value) {
326 $label .= $value."=".$ldapgroup[$value]." ";
327 }
328 }
329 $liste[$key] = $label;
330 }
331 } else {
332 setEventMessages($ldap->error, $ldap->errors, 'errors');
333 }
334
335 print "<br>\n";
336 print "LDAP search for group:<br>\n";
337 print "search: *<br>\n";
338 print "userDN: ".getDolGlobalString('LDAP_GROUP_DN')."<br>\n";
339 print "useridentifier: ".getDolGlobalString('LDAP_KEY_GROUPS')."<br>\n";
340 print "required_fields: ".implode(',', $required_fields)."<br>\n";
341 print "=> ".count($liste)." records<br>\n";
342 print "\n<br>";
343 } else {
344 print img_picto('', 'error').' ';
345 print '<span class="error">'.$langs->trans("LDAPSynchroKO");
346 print ': '.$ldap->error;
347 print '</span><br>';
348 print $langs->trans("ErrorLDAPMakeManualTest", $conf->ldap->dir_temp).'<br>';
349 }
350 }
351}
352
353// End of page
354llxFooter();
355$db->close();
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
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).
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:71
Class to manage generation of HTML components Only common components must be here.
Class to manage LDAP features.
Class to manage user groups.
llxFooter()
Footer empty.
Definition document.php:107
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
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)
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)
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...
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
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.