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