dolibarr  16.0.5
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  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
27 require '../main.inc.php';
28 require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
30 
31 // Load translation files required by the page
32 $langs->loadLangs(array('admin', 'users', 'other'));
33 
34 $action = GETPOST('action', 'aZ09');
35 
36 if (!$user->admin) {
38 }
39 
40 $entity = $conf->entity;
41 
42 
43 /*
44  * Actions
45  */
46 
47 if ($action == 'add') {
48  $sql = "UPDATE ".MAIN_DB_PREFIX."rights_def SET bydefault=1";
49  $sql .= " WHERE id = ".GETPOST("pid", 'int');
50  $sql .= " AND entity = ".$conf->entity;
51  $db->query($sql);
52 }
53 
54 if ($action == 'remove') {
55  $sql = "UPDATE ".MAIN_DB_PREFIX."rights_def SET bydefault=0";
56  $sql .= " WHERE id = ".GETPOST('pid', 'int');
57  $sql .= " AND entity = ".$conf->entity;
58  $db->query($sql);
59 }
60 
61 
62 /*
63  * View
64  */
65 
66 $wikihelp = 'EN:Setup_Security|FR:Paramétrage_Sécurité|ES:Configuración_Seguridad';
67 
68 llxHeader('', $langs->trans("DefaultRights"), $wikihelp);
69 
70 print load_fiche_titre($langs->trans("SecuritySetup"), '', 'title_setup');
71 
72 print '<span class="opacitymedium">'.$langs->trans("DefaultRightsDesc")." ".$langs->trans("OnlyActiveElementsAreShown")."</span><br><br>\n";
73 
74 $db->begin();
75 
76 // Search all modules with permission and reload permissions def.
77 $modules = array();
78 $modulesdir = dolGetModulesDirs();
79 
80 foreach ($modulesdir as $dir) {
81  $handle = @opendir(dol_osencode($dir));
82  if (is_resource($handle)) {
83  while (($file = readdir($handle)) !== false) {
84  if (is_readable($dir.$file) && substr($file, 0, 3) == 'mod' && substr($file, dol_strlen($file) - 10) == '.class.php') {
85  $modName = substr($file, 0, dol_strlen($file) - 10);
86  if ($modName) {
87  include_once $dir.$file;
88  $objMod = new $modName($db);
89 
90  // Load all lang files of module
91  if (isset($objMod->langfiles) && is_array($objMod->langfiles)) {
92  foreach ($objMod->langfiles as $domain) {
93  $langs->load($domain);
94  }
95  }
96  // Load all permissions
97  if ($objMod->rights_class) {
98  $ret = $objMod->insert_permissions(0, $entity);
99  $modules[$objMod->rights_class] = $objMod;
100  //print "modules[".$objMod->rights_class."]=$objMod;";
101  }
102  }
103  }
104  }
105  }
106 }
107 
108 $db->commit();
109 
110 $head = security_prepare_head();
111 
112 print dol_get_fiche_head($head, 'default', '', -1);
113 
114 
115 // Show warning about external users
116 print info_admin(showModulesExludedForExternal($modules)).'<br>'."\n";
117 
118 print "\n";
119 print '<div class="div-table-responsive-no-min">';
120 print '<table class="noborder centpercent">';
121 
122 print '<tr class="liste_titre">';
123 print '<td>'.$langs->trans("Module").'</td>';
124 print '<td class="center">'.$langs->trans("Default").'</td>';
125 print '<td class="center" width="24">&nbsp;</td>';
126 print '<td>'.$langs->trans("Permissions").'</td>';
127 if ($user->admin) {
128  print '<td class="right"></td>';
129 }
130 print '</tr>'."\n";
131 
132 //print "xx".$conf->global->MAIN_USE_ADVANCED_PERMS;
133 $sql = "SELECT r.id, r.libelle as label, r.module, r.perms, r.subperms, r.module_position, r.bydefault";
134 $sql .= " FROM ".MAIN_DB_PREFIX."rights_def as r";
135 $sql .= " WHERE r.libelle NOT LIKE 'tou%'"; // On ignore droits "tous"
136 $sql .= " AND r.entity = ".((int) $entity);
137 if (empty($conf->global->MAIN_USE_ADVANCED_PERMS)) {
138  $sql .= " AND r.perms NOT LIKE '%_advance'"; // Hide advanced perms if option is not enabled
139 }
140 $sql .= " ORDER BY r.family_position, r.module_position, r.module, r.id";
141 
142 $result = $db->query($sql);
143 if ($result) {
144  $num = $db->num_rows($result);
145  $i = 0;
146  $oldmod = '';
147 
148  while ($i < $num) {
149  $obj = $db->fetch_object($result);
150 
151  // If line is for a module that does not exist anymore (absent of includes/module), we ignore it
152  if (empty($modules[$obj->module])) {
153  $i++;
154  continue;
155  }
156 
157  $objMod = $modules[$obj->module];
158 
159  // Save field module_position in database if value is wrong
160  if (empty($obj->module_position) || (is_object($objMod) && $objMod->isCoreOrExternalModule() == 'external' && $obj->module_position < 100000)) {
161  if (is_object($modules[$obj->module]) && ($modules[$obj->module]->module_position > 0)) {
162  // TODO Define familyposition
163  //$familyposition = $modules[$obj->module]->family_position;
164  $familyposition = 0;
165 
166  $newmoduleposition = $modules[$obj->module]->module_position;
167 
168  // Correct $newmoduleposition position for external modules
169  $objMod = $modules[$obj->module];
170  if (is_object($objMod) && $objMod->isCoreOrExternalModule() == 'external' && $newmoduleposition < 100000) {
171  $newmoduleposition += 100000;
172  }
173 
174  $sqlupdate = 'UPDATE '.MAIN_DB_PREFIX."rights_def SET module_position = ".((int) $newmoduleposition).",";
175  $sqlupdate .= " family_position = ".((int) $familyposition);
176  $sqlupdate .= " WHERE module_position = ".((int) $obj->module_position)." AND module = '".$db->escape($obj->module)."'";
177  $db->query($sqlupdate);
178  }
179  }
180 
181  // Check if permission we found is inside a module definition. If not, we discard it.
182  $found = false;
183  foreach ($modules[$obj->module]->rights as $key => $val) {
184  if ($val[4] == $obj->perms && (empty($val[5]) || $val[5] == $obj->subperms)) {
185  $found = true;
186  break;
187  }
188  }
189  if (!$found) {
190  $i++;
191  continue;
192  }
193 
194  // Break found, it's a new module to catch
195  if (isset($obj->module) && ($oldmod <> $obj->module)) {
196  $oldmod = $obj->module;
197 
198  // Break detected, we get objMod
199  $objMod = $modules[$obj->module];
200  $picto = ($objMod->picto ? $objMod->picto : 'generic');
201 
202  // Show break line
203  print '<tr class="oddeven trforbreak">';
204  print '<td class="maxwidthonsmartphone tdoverflowonsmartphone">';
205  print img_object('', $picto, 'class="pictoobjectwidth paddingright"').' '.$objMod->getName();
206  print '<a name="'.$objMod->getName().'"></a>';
207  print '</td>';
208  print '<td>&nbsp;</td>';
209  print '<td>&nbsp;</td>';
210  print '<td>&nbsp;</td>';
211  // Permission id
212  if ($user->admin) {
213  print '<td class="right"></td>';
214  }
215  print '</tr>'."\n";
216  }
217 
218  print '<!-- '.$obj->module.'->'.$obj->perms.($obj->subperms ? '->'.$obj->subperms : '').' -->'."\n";
219  print '<tr class="oddeven">';
220 
221  // Picto and label of module
222  print '<td class="maxwidthonsmartphone tdoverflowonsmartphone">';
223  //print img_object('', $picto, 'class="pictoobjectwidth"').' '.$objMod->getName();
224  print '</td>';
225 
226  // Tick
227  if ($obj->bydefault == 1) {
228  print '<td class="center">';
229  print '<a class="reposition" href="perms.php?pid='.$obj->id.'&action=remove&token='.newToken().'">';
230  //print img_edit_remove();
231  print img_picto('', 'switch_on');
232  print '</a>';
233  print '</td>';
234  print '<td class="center">';
235  //print img_picto($langs->trans("Active"), 'tick');
236  print '</td>';
237  } else {
238  print '<td class="center">';
239  print '<a class="reposition" href="perms.php?pid='.$obj->id.'&action=add&token='.newToken().'">';
240  //print img_edit_add();
241  print img_picto('', 'switch_off');
242  print '</a>';
243  print '</td>';
244  print '<td class="center">';
245  print '&nbsp;';
246  print '</td>';
247  }
248 
249  // Permission and tick
250  $permlabel = (!empty($conf->global->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)));
251  print '<td>';
252  print $permlabel;
253  if (!empty($conf->global->MAIN_USE_ADVANCED_PERMS)) {
254  if (preg_match('/_advance$/', $obj->perms)) {
255  print ' <span class="opacitymedium">('.$langs->trans("AdvancedModeOnly").')</span>';
256  }
257  }
258  print '</td>';
259 
260  // Permission id
261  if ($user->admin) {
262  print '<td class="right">';
263  $htmltext = $langs->trans("ID").': '.$obj->id;
264  $htmltext .= '<br>'.$langs->trans("Permission").': user->rights->'.$obj->module.'->'.$obj->perms.($obj->subperms ? '->'.$obj->subperms : '');
265  print $form->textwithpicto('', $htmltext);
266  //print '<span class="opacitymedium">'.$obj->id.'</span>';
267  print '</td>';
268  }
269 
270  print '</tr>'."\n";
271 
272  $i++;
273  }
274 } else {
275  dol_print_error($db);
276 }
277 print '</table>';
278 print '</div>';
279 
280 $parameters = array();
281 $reshook = $hookmanager->executeHooks('insertExtraFooter', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
282 if ($reshook < 0) {
283  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
284 }
285 
286 print dol_get_fiche_end();
287 
288 // End of page
289 llxFooter();
290 $db->close();
llxFooter
llxFooter()
Empty footer.
Definition: wrapper.php:73
security_prepare_head
security_prepare_head()
Prepare array with list of tabs.
Definition: admin.lib.php:772
dol_osencode
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
Definition: functions.lib.php:8499
load_fiche_titre
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
Definition: functions.lib.php:5204
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:3880
$parameters
$parameters
Actions.
Definition: perms.php:84
$wikihelp
if($actionsave) if(!isset($conf->global->MAIN_AGENDA_EXPORT_PAST_DELAY)) $wikihelp
View.
Definition: agenda_xcal.php:72
dol_get_fiche_head
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='')
Show tabs of a record.
Definition: functions.lib.php:1822
dol_strlen
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Definition: functions.lib.php:3747
info_admin
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin='1', $morecss='hideonsmartphone', $textfordropdown='')
Show information for admin users or standard users.
Definition: functions.lib.php:4800
newToken
newToken()
Return the value of token currently saved into session with name 'newtoken'.
Definition: functions.lib.php:10878
dol_get_fiche_end
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition: functions.lib.php:2018
dolGetModulesDirs
dolGetModulesDirs($subdir='')
Return list of modules directories.
Definition: functions2.lib.php:80
img_object
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Definition: functions.lib.php:4211
showModulesExludedForExternal
showModulesExludedForExternal($modules)
Show array with constants to edit.
Definition: admin.lib.php:1822
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137
accessforbidden
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Definition: security.lib.php:933
llxHeader
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOCSRFCHECK')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:59