dolibarr 21.0.0-alpha
perms.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2009 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2005-2013 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2011 Herve Prot <herve.prot@symeos.com>
6 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
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 3 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 <https://www.gnu.org/licenses/>.
20 */
21
28// Load Dolibarr environment
29require '../main.inc.php';
30require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
31require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
32
33// Load translation files required by the page
34$langs->loadLangs(array('admin', 'users', 'other'));
35
36$action = GETPOST('action', 'aZ09');
37
38$entity = $conf->entity;
39
40if (!$user->admin) {
42}
43
44
45/*
46 * Actions
47 */
48
49if ($action == 'add') {
50 $sql = "UPDATE ".MAIN_DB_PREFIX."rights_def SET bydefault=1";
51 $sql .= " WHERE id = ".GETPOSTINT("pid");
52 $sql .= " AND entity = ".$conf->entity;
53 $db->query($sql);
54}
55
56if ($action == 'remove') {
57 $sql = "UPDATE ".MAIN_DB_PREFIX."rights_def SET bydefault=0";
58 $sql .= " WHERE id = ".GETPOSTINT('pid');
59 $sql .= " AND entity = ".$conf->entity;
60 $db->query($sql);
61}
62
63
64/*
65 * View
66 */
67
68$form = new Form($db);
69
70$wikihelp = 'EN:Setup_Security|FR:Paramétrage_Sécurité|ES:Configuración_Seguridad';
71
72llxHeader('', $langs->trans("DefaultRights"), $wikihelp, '', 0, 0, '', '', '', 'mod-admin page-perms');
73
74print load_fiche_titre($langs->trans("SecuritySetup"), '', 'title_setup');
75
76print '<span class="opacitymedium">'.$langs->trans("DefaultRightsDesc")." ".$langs->trans("OnlyActiveElementsAreShown")."</span><br><br>\n";
77
78$db->begin();
79
80// Search all modules with permission and reload permissions def.
81$modules = array();
82$modulesdir = dolGetModulesDirs();
83
84foreach ($modulesdir as $dir) {
85 $handle = @opendir(dol_osencode($dir));
86 if (is_resource($handle)) {
87 while (($file = readdir($handle)) !== false) {
88 if (is_readable($dir.$file) && substr($file, 0, 3) == 'mod' && substr($file, dol_strlen($file) - 10) == '.class.php') {
89 $modName = substr($file, 0, dol_strlen($file) - 10);
90 if ($modName) {
91 include_once $dir.$file;
92 $objMod = new $modName($db);
93
94 '@phan-var-force DolibarrModules $objMod';
95
96 // Load all lang files of module
97 if (isset($objMod->langfiles) && is_array($objMod->langfiles)) {
98 foreach ($objMod->langfiles as $domain) {
99 $langs->load($domain);
100 }
101 }
102 // Load all permissions
103 if ($objMod->rights_class) {
104 $ret = $objMod->insert_permissions(0, $entity);
105 $modules[$objMod->rights_class] = $objMod;
106 //print "modules[".$objMod->rights_class."]=$objMod;";
107 }
108 }
109 }
110 }
111 }
112}
113
114$db->commit();
115'@phan-var-force DolibarrModules[] $modules';
116
117$head = security_prepare_head();
118
119print dol_get_fiche_head($head, 'default', '', -1);
120
121
122// Show warning about external users
123print info_admin(showModulesExludedForExternal($modules)).'<br>'."\n";
124
125print "\n";
126print '<div class="div-table-responsive-no-min">';
127print '<table class="noborder centpercent">';
128
129print '<tr class="liste_titre">';
130print '<td>'.$langs->trans("Module").'</td>';
131print '<td class="center">'.$langs->trans("Default").'</td>';
132print '<td class="center" width="24">&nbsp;</td>';
133print '<td>'.$langs->trans("Permissions").'</td>';
134if ($user->admin) {
135 print '<td class="right"></td>';
136}
137print '</tr>'."\n";
138
139//print "xx".$conf->global->MAIN_USE_ADVANCED_PERMS;
140$sql = "SELECT r.id, r.libelle as label, r.module, r.perms, r.subperms, r.module_position, r.bydefault";
141$sql .= " FROM ".MAIN_DB_PREFIX."rights_def as r";
142$sql .= " WHERE r.libelle NOT LIKE 'tou%'"; // On ignore droits "tous"
143$sql .= " AND r.entity = ".((int) $entity);
144if (!getDolGlobalString('MAIN_USE_ADVANCED_PERMS')) {
145 $sql .= " AND r.perms NOT LIKE '%_advance'"; // Hide advanced perms if option is not enabled
146}
147$sql .= " ORDER BY r.family_position, r.module_position, r.module, r.id";
148
149$result = $db->query($sql);
150if ($result) {
151 $num = $db->num_rows($result);
152 $i = 0;
153 $oldmod = '';
154
155 while ($i < $num) {
156 $obj = $db->fetch_object($result);
157
158 // If line is for a module that does not exist anymore (absent of includes/module), we ignore it
159 if (empty($modules[$obj->module])) {
160 $i++;
161 continue;
162 }
163
164 $objMod = $modules[$obj->module];
165
166 // Save field module_position in database if value is wrong
167 if (empty($obj->module_position) || (is_object($objMod) && $objMod->isCoreOrExternalModule() == 'external' && $obj->module_position < 100000)) {
168 if (is_object($modules[$obj->module]) && ($modules[$obj->module]->module_position > 0)) {
169 // TODO Define familyposition
170 //$familyposition = $modules[$obj->module]->family_position;
171 $familyposition = 0;
172
173 $newmoduleposition = $modules[$obj->module]->module_position;
174
175 // Correct $newmoduleposition position for external modules
176 $objMod = $modules[$obj->module];
177 if (is_object($objMod) && $objMod->isCoreOrExternalModule() == 'external' && $newmoduleposition < 100000) {
178 $newmoduleposition += 100000;
179 }
180
181 $sqlupdate = 'UPDATE '.MAIN_DB_PREFIX."rights_def SET module_position = ".((int) $newmoduleposition).",";
182 $sqlupdate .= " family_position = ".((int) $familyposition);
183 $sqlupdate .= " WHERE module_position = ".((int) $obj->module_position)." AND module = '".$db->escape($obj->module)."'";
184 $db->query($sqlupdate);
185 }
186 }
187
188 // Check if permission we found is inside a module definition. If not, we discard it.
189 $found = false;
190 foreach ($modules[$obj->module]->rights as $key => $val) {
191 if ($val[4] == $obj->perms && (empty($val[5]) || $val[5] == $obj->subperms)) {
192 $found = true;
193 break;
194 }
195 }
196 if (!$found) {
197 $i++;
198 continue;
199 }
200
201 // Break found, it's a new module to catch
202 if (isset($obj->module) && ($oldmod != $obj->module)) {
203 $oldmod = $obj->module;
204
205 // Break detected, we get objMod
206 $objMod = $modules[$obj->module];
207 $picto = ($objMod->picto ? $objMod->picto : 'generic');
208
209 // Show break line
210 print '<tr class="oddeven trforbreak">';
211 print '<td class="maxwidthonsmartphone tdoverflowmax200" title="'.dol_escape_htmltag($objMod->getName()).'">';
212 print img_object('', $picto, 'class="pictoobjectwidth paddingright"').' '.$objMod->getName();
213 print '<a name="'.$objMod->getName().'"></a>';
214 print '</td>';
215 print '<td>&nbsp;</td>';
216 print '<td>&nbsp;</td>';
217 print '<td>&nbsp;</td>';
218 // Permission id
219 if ($user->admin) {
220 print '<td class="right"></td>';
221 }
222 print '</tr>'."\n";
223 }
224
225 print '<!-- '.$obj->module.'->'.$obj->perms.($obj->subperms ? '->'.$obj->subperms : '').' -->'."\n";
226 print '<tr class="oddeven">';
227
228 // Picto and label of module
229 print '<td class="maxwidthonsmartphone tdoverflowmax200">';
230 //print img_object('', $picto, 'class="pictoobjectwidth"').' '.$objMod->getName();
231 print '</td>';
232
233 // Tick
234 if ($obj->bydefault == 1) {
235 print '<td class="center">';
236 print '<a class="reposition" href="perms.php?pid='.$obj->id.'&action=remove&token='.newToken().'">';
237 //print img_edit_remove();
238 print img_picto('', 'switch_on');
239 print '</a>';
240 print '</td>';
241 print '<td class="center">';
242 //print img_picto($langs->trans("Active"), 'tick');
243 print '</td>';
244 } else {
245 print '<td class="center">';
246 print '<a class="reposition" href="perms.php?pid='.$obj->id.'&action=add&token='.newToken().'">';
247 //print img_edit_add();
248 print img_picto('', 'switch_off');
249 print '</a>';
250 print '</td>';
251 print '<td class="center">';
252 print '&nbsp;';
253 print '</td>';
254 }
255
256 // Permission and tick
257 $permlabel = (getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && ($langs->trans("PermissionAdvanced".$obj->id) != "PermissionAdvanced".$obj->id) ? $langs->trans("PermissionAdvanced".$obj->id) : (($langs->trans("Permission".$obj->id) != "Permission".$obj->id) ? $langs->trans("Permission".$obj->id) : $langs->trans($obj->label)));
258 print '<td>';
259 print $permlabel;
260 if ($langs->trans("Permission".$obj->id.'b') != "Permission".$obj->id.'b') {
261 print '<br><span class="opacitymedium">'.$langs->trans("Permission".$obj->id.'b').'</span>';
262 }
263 if ($langs->trans("Permission".$obj->id.'c') != "Permission".$obj->id.'c') {
264 print '<br><span class="opacitymedium">'.$langs->trans("Permission".$obj->id.'c').'</span>';
265 }
266 if (getDolGlobalString('MAIN_USE_ADVANCED_PERMS')) {
267 if (preg_match('/_advance$/', $obj->perms)) {
268 print ' <span class="opacitymedium">('.$langs->trans("AdvancedModeOnly").')</span>';
269 }
270 }
271 print '</td>';
272
273 // Permission id
274 if ($user->admin) {
275 print '<td class="right">';
276 $htmltext = $langs->trans("ID").': '.$obj->id;
277 $htmltext .= '<br>'.$langs->trans("Permission").': user->hasRight(\''.dol_escape_htmltag($obj->module).'\', \''.dol_escape_htmltag($obj->perms).'\''.($obj->subperms ? ', \''.dol_escape_htmltag($obj->subperms).'\'' : '').')';
278 print $form->textwithpicto('', $htmltext);
279 //print '<span class="opacitymedium">'.$obj->id.'</span>';
280 print '</td>';
281 }
282
283 print '</tr>'."\n";
284
285 $i++;
286 }
287} else {
288 dol_print_error($db);
289}
290print '</table>';
291print '</div>';
292
293$parameters = array();
294$reshook = $hookmanager->executeHooks('insertExtraFooter', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
295if ($reshook < 0) {
296 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
297}
298
299print dol_get_fiche_end();
300
301// End of page
302llxFooter();
303$db->close();
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
security_prepare_head()
Prepare array with list of tabs.
showModulesExludedForExternal($modules)
Show array with constants to edit.
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.
llxFooter()
Footer empty.
Definition document.php:107
dolGetModulesDirs($subdir='')
Return list of directories that contain modules.
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
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_osencode($str)
Return a string encoded into OS filesystem encoding.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
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.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.