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