dolibarr  16.0.5
perms.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2002-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2002-2003 Jean-Louis Bergamo <jlb@j1b.org>
4  * Copyright (C) 2004-2020 Laurent Destailleur <eldy@users.sourceforge.net>
5  * Copyright (C) 2004 Eric Seigne <eric.seigne@ryxeo.com>
6  * Copyright (C) 2005-2017 Regis Houssin <regis.houssin@inodbox.com>
7  * Copyright (C) 2020 Tobias Sekan <tobias.sekan@startmail.com>
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 
28 if (!defined('CSRFCHECK_WITH_TOKEN')) {
29  define('CSRFCHECK_WITH_TOKEN', '1'); // Force use of CSRF protection with tokens even for GET
30 }
31 
32 require '../../main.inc.php';
33 require_once DOL_DOCUMENT_ROOT.'/user/class/usergroup.class.php';
34 require_once DOL_DOCUMENT_ROOT.'/core/lib/usergroups.lib.php';
35 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
36 require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
37 
38 // Load translation files required by page
39 $langs->loadLangs(array('users', 'admin'));
40 
41 $id = GETPOST('id', 'int');
42 $action = GETPOST('action', 'aZ09');
43 $confirm = GETPOST('confirm', 'alpha');
44 $module = GETPOST('module', 'alpha');
45 $rights = GETPOST('rights', 'int');
46 $contextpage = GETPOST('contextpage', 'aZ') ?GETPOST('contextpage', 'aZ') : 'groupperms'; // To manage different context of search
47 
48 if (!isset($id) || empty($id)) {
50 }
51 
52 // Define if user can read permissions
53 $canreadperms = ($user->admin || $user->rights->user->user->lire);
54 // Define if user can modify group permissions
55 $caneditperms = ($user->admin || $user->rights->user->user->creer);
56 // Advanced permissions
57 $advancedpermsactive = false;
58 if (!empty($conf->global->MAIN_USE_ADVANCED_PERMS)) {
59  $advancedpermsactive = true;
60  $canreadperms = ($user->admin || ($user->rights->user->group_advance->read && $user->rights->user->group_advance->readperms));
61  $caneditperms = ($user->admin || $user->rights->user->group_advance->write);
62 }
63 
64 // Security check
65 //$result = restrictedArea($user, 'user', $id, 'usergroup', '');
66 if (!$canreadperms) {
68 }
69 
70 $object = new Usergroup($db);
71 $object->fetch($id);
72 $object->getrights();
73 
74 $entity = $conf->entity;
75 
76 // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
77 $hookmanager->initHooks(array('groupperms', 'globalcard'));
78 
79 
84 $parameters = array();
85 $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
86 if ($reshook < 0) {
87  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
88 }
89 
90 if (empty($reshook)) {
91  if ($action == 'addrights' && $caneditperms) {
92  $editgroup = new Usergroup($db);
93  $result = $editgroup->fetch($object->id);
94  if ($result > 0) {
95  $result = $editgroup->addrights($rights, $module, '', $entity);
96  if ($result < 0) {
97  setEventMessages($editgroup->error, $editgroup->errors, 'errors');
98  }
99  } else {
100  dol_print_error($db);
101  }
102 
103  $user->clearrights();
104  $user->getrights();
105  }
106 
107  if ($action == 'delrights' && $caneditperms) {
108  $editgroup = new Usergroup($db);
109  $result = $editgroup->fetch($id);
110  if ($result > 0) {
111  $result = $editgroup->delrights($rights, $module, '', $entity);
112  if ($result < 0) {
113  setEventMessages($editgroup->error, $editgroup->errors, 'errors');
114  }
115  } else {
116  dol_print_error($db);
117  }
118 
119  $user->clearrights();
120  $user->getrights();
121  }
122 }
123 
124 
125 /*
126  * View
127  */
128 
129 $form = new Form($db);
130 
131 llxHeader('', $langs->trans("Permissions"));
132 
133 if ($object->id > 0) {
134  $head = group_prepare_head($object);
135  $title = $langs->trans("Group");
136  print dol_get_fiche_head($head, 'rights', $title, -1, 'group');
137 
138  // Charge les modules soumis a permissions
139  $modules = array();
140  $modulesdir = dolGetModulesDirs();
141 
142  $db->begin();
143 
144  foreach ($modulesdir as $dir) {
145  $handle = @opendir(dol_osencode($dir));
146  if (is_resource($handle)) {
147  while (($file = readdir($handle)) !== false) {
148  if (is_readable($dir.$file) && substr($file, 0, 3) == 'mod' && substr($file, dol_strlen($file) - 10) == '.class.php') {
149  $modName = substr($file, 0, dol_strlen($file) - 10);
150 
151  if ($modName) {
152  include_once $dir.$file;
153  $objMod = new $modName($db);
154  // Load all lang files of module
155  if (isset($objMod->langfiles) && is_array($objMod->langfiles)) {
156  foreach ($objMod->langfiles as $domain) {
157  $langs->load($domain);
158  }
159  }
160  // Load all permissions
161  if ($objMod->rights_class) {
162  $ret = $objMod->insert_permissions(0, $entity);
163  $modules[$objMod->rights_class] = $objMod;
164  }
165  }
166  }
167  }
168  }
169  }
170 
171  $db->commit();
172 
173  // Read permissions of group
174  $permsgroupbyentity = array();
175 
176  $sql = "SELECT DISTINCT r.id, r.libelle, r.module, gr.entity";
177  $sql .= " FROM ".MAIN_DB_PREFIX."rights_def as r,";
178  $sql .= " ".MAIN_DB_PREFIX."usergroup_rights as gr";
179  $sql .= " WHERE gr.fk_id = r.id";
180  $sql .= " AND gr.entity = ".((int) $entity);
181  $sql .= " AND gr.fk_usergroup = ".((int) $object->id);
182 
183  dol_syslog("get user perms", LOG_DEBUG);
184  $result = $db->query($sql);
185  if ($result) {
186  $num = $db->num_rows($result);
187  $i = 0;
188  while ($i < $num) {
189  $obj = $db->fetch_object($result);
190  if (!isset($permsgroupbyentity[$obj->entity])) {
191  $permsgroupbyentity[$obj->entity] = array();
192  }
193  array_push($permsgroupbyentity[$obj->entity], $obj->id);
194  $i++;
195  }
196  $db->free($result);
197  } else {
198  dol_print_error($db);
199  }
200 
201  /*
202  * Part to add/remove permissions
203  */
204 
205  $linkback = '<a href="'.DOL_URL_ROOT.'/user/group/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
206 
207  dol_banner_tab($object, 'id', $linkback, $user->rights->user->user->lire || $user->admin);
208 
209  print '<div class="fichecenter">';
210  print '<div class="underbanner clearboth"></div>';
211 
212 
213  print '<table class="border centpercent tableforfield">';
214 
215  // Name (already in dol_banner, we keep it to have the GlobalGroup picto, but we should move it in dol_banner)
216  if (!empty($conf->mutlicompany->enabled)) {
217  print '<tr><td class="titlefield">'.$langs->trans("Name").'</td>';
218  print '<td colspan="2">'.$object->name.'';
219  if (!$object->entity) {
220  print img_picto($langs->trans("GlobalGroup"), 'redstar');
221  }
222  print "</td></tr>\n";
223  }
224 
225  // Note
226  print '<tr><td class="titlefield tdtop">'.$langs->trans("Description").'</td>';
227  print '<td class="valeur sensiblehtmlcontent">';
228  print dol_string_onlythesehtmltags(dol_htmlentitiesbr($object->note));
229  print '</td>';
230  print "</tr>\n";
231 
232  print '</table><br>';
233 
234  if ($user->admin) {
235  print info_admin($langs->trans("WarningOnlyPermissionOfActivatedModules"));
236  }
237 
238  $parameters = array();
239  $reshook = $hookmanager->executeHooks('insertExtraHeader', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
240  if ($reshook < 0) {
241  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
242  }
243 
244  print "\n";
245  print '<div class="div-table-responsive-no-min">';
246  print '<table class="noborder centpercent">';
247  print '<tr class="liste_titre">';
248  print '<td>'.$langs->trans("Module").'</td>';
249  if ($caneditperms) {
250  print '<td class="center nowrap">';
251  print '<a class="reposition commonlink" title="'.dol_escape_htmltag($langs->trans("All")).'" alt="'.dol_escape_htmltag($langs->trans("All")).'" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=addrights&token='.newToken().'&entity='.$entity.'&module=allmodules&confirm=yes">'.$langs->trans("All")."</a>";
252  print '/';
253  print '<a class="reposition commonlink" title="'.dol_escape_htmltag($langs->trans("None")).'" alt="'.dol_escape_htmltag($langs->trans("None")).'" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=delrights&&token='.newToken().'&entity='.$entity.'&module=allmodules&confirm=yes">'.$langs->trans("None")."</a>";
254  print '</td>';
255  }
256  print '<td class="center" width="24">&nbsp;</td>';
257  print '<td>'.$langs->trans("Permissions").'</td>';
258  if ($user->admin) {
259  print '<td class="right"></td>';
260  }
261  print '</tr>'."\n";
262 
263  $sql = "SELECT r.id, r.libelle as label, r.module, r.perms, r.subperms, r.module_position, r.bydefault";
264  $sql .= " FROM ".MAIN_DB_PREFIX."rights_def as r";
265  $sql .= " WHERE r.libelle NOT LIKE 'tou%'"; // On ignore droits "tous"
266  $sql .= " AND r.entity = ".((int) $entity);
267  if (empty($conf->global->MAIN_USE_ADVANCED_PERMS)) {
268  $sql .= " AND r.perms NOT LIKE '%_advance'"; // Hide advanced perms if option is disable
269  }
270  $sql .= " ORDER BY r.family_position, r.module_position, r.module, r.id";
271 
272  $result = $db->query($sql);
273  if ($result) {
274  $num = $db->num_rows($result);
275  $i = 0;
276  $oldmod = '';
277 
278  while ($i < $num) {
279  $obj = $db->fetch_object($result);
280 
281  // If line is for a module that does not exist anymore (absent of includes/module), we ignore it
282  if (empty($modules[$obj->module])) {
283  $i++;
284  continue;
285  }
286 
287  $objMod = $modules[$obj->module];
288 
289  // Break found, it's a new module to catch
290  if (isset($obj->module) && ($oldmod <> $obj->module)) {
291  $oldmod = $obj->module;
292 
293  // Break detected, we get objMod
294  $objMod = $modules[$obj->module];
295  $picto = ($objMod->picto ? $objMod->picto : 'generic');
296 
297  // Show break line
298  print '<tr class="oddeven trforbreak">';
299  print '<td class="maxwidthonsmartphone tdoverflowonsmartphone">';
300  print img_object('', $picto, 'class="pictoobjectwidth paddingright"').' '.$objMod->getName();
301  print '<a name="'.$objMod->getName().'"></a>';
302  print '</td>';
303  if ($caneditperms) {
304  print '<td class="center nowrap">';
305  print '<a class="reposition" title="'.dol_escape_htmltag($langs->trans("All")).'" alt="'.$langs->trans("All").'" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=addrights&entity='.$entity.'&module='.$obj->module.'&token='.newToken().'">'.$langs->trans("All")."</a>";
306  print '/';
307  print '<a class="reposition" title="'.dol_escape_htmltag($langs->trans("None")).'" alt="'.$langs->trans("None").'" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=delrights&entity='.$entity.'&module='.$obj->module.'&token='.newToken().'">'.$langs->trans("None")."</a>";
308  print '</td>';
309  } else {
310  print '<td>&nbsp;</td>';
311  }
312  print '<td>&nbsp;</td>';
313  print '<td>&nbsp;</td>';
314 
315  // Permission id
316  if ($user->admin) {
317  print '<td class="right"></td>';
318  }
319 
320  print '</tr>'."\n";
321  }
322 
323  print '<!-- '.$obj->module.'->'.$obj->perms.($obj->subperms ? '->'.$obj->subperms : '').' -->'."\n";
324  print '<tr class="oddeven">';
325 
326  // Picto and label of module
327  print '<td class="maxwidthonsmartphone tdoverflowonsmartphone">';
328  //print img_object('', $picto, 'class="inline-block pictoobjectwidth"').' '.$objMod->getName();
329  print '</td>';
330 
331  if (!empty($permsgroupbyentity[$entity]) && is_array($permsgroupbyentity[$entity])) {
332  if (in_array($obj->id, $permsgroupbyentity[$entity])) {
333  // Own permission by group
334  if ($caneditperms) {
335  print '<td class="center"><a class="reposition" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=delrights&token='.newToken().'&entity='.$entity.'&rights='.$obj->id.'&confirm=yes">';
336  //print img_edit_remove($langs->trans("Remove"));
337  print img_picto($langs->trans("Remove"), 'switch_on');
338  print '</a></td>';
339  }
340  print '<td class="center nowrap">';
341  print img_picto($langs->trans("Active"), 'tick');
342  print '</td>';
343  } else {
344  // Do not own permission
345  if ($caneditperms) {
346  print '<td class="center"><a class="reposition" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=addrights&token='.newToken().'&entity='.$entity.'&rights='.$obj->id.'&confirm=yes">';
347  //print img_edit_add($langs->trans("Add"));
348  print img_picto($langs->trans("Add"), 'switch_off');
349  print '</a></td>';
350  }
351  print '<td>&nbsp;</td>';
352  }
353  } else {
354  // Do not own permission
355  if ($caneditperms) {
356  print '<td class="center"><a class="reposition" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=addrights&entity='.$entity.'&rights='.$obj->id.'&confirm=yes&token='.newToken().'">';
357  //print img_edit_add($langs->trans("Add"));
358  print img_picto($langs->trans("Add"), 'switch_off');
359  print '</a></td>';
360  }
361  print '<td>&nbsp;</td>';
362  }
363 
364  // Description of permission
365  $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)));
366  print '<td>';
367  print $permlabel;
368  if (!empty($conf->global->MAIN_USE_ADVANCED_PERMS)) {
369  if (preg_match('/_advance$/', $obj->perms)) {
370  print ' <span class="opacitymedium">('.$langs->trans("AdvancedModeOnly").')</span>';
371  }
372  }
373  print '</td>';
374 
375  // Permission id
376  if ($user->admin) {
377  print '<td class="right">';
378  $htmltext = $langs->trans("ID").': '.$obj->id;
379  $htmltext .= '<br>'.$langs->trans("Permission").': user->rights->'.$obj->module.'->'.$obj->perms.($obj->subperms ? '->'.$obj->subperms : '');
380  print $form->textwithpicto('', $htmltext);
381  //print '<span class="opacitymedium">'.$obj->id.'</span>';
382  print '</td>';
383  }
384 
385  print '</tr>'."\n";
386 
387  $i++;
388  }
389  }
390  print '</table>';
391  print '</div>';
392 
393  print '</div>';
394 
395  $parameters = array();
396  $reshook = $hookmanager->executeHooks('insertExtraFooter', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
397  if ($reshook < 0) {
398  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
399  }
400 
401  print dol_get_fiche_end();
402 }
403 
404 // End of page
405 llxFooter();
406 $db->close();
dol_escape_htmltag
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
Definition: functions.lib.php:1468
llxFooter
llxFooter()
Empty footer.
Definition: wrapper.php:73
dol_osencode
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
Definition: functions.lib.php:8499
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
dol_banner_tab
dol_banner_tab($object, $paramid, $morehtml='', $shownav=1, $fieldid='rowid', $fieldref='ref', $morehtmlref='', $moreparam='', $nodbprefix=0, $morehtmlleft='', $morehtmlstatus='', $onlybanner=0, $morehtmlright='')
Show tab footer of a card.
Definition: functions.lib.php:2046
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
group_prepare_head
group_prepare_head($object)
Prepare array with list of tabs.
Definition: usergroups.lib.php:202
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
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
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
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
dol_htmlentitiesbr
dol_htmlentitiesbr($stringtoencode, $nl2brmode=0, $pagecodefrom='UTF-8', $removelasteolbr=1)
This function is called to encode a string into a HTML string but differs from htmlentities because a...
Definition: functions.lib.php:6991
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
dol_string_onlythesehtmltags
dol_string_onlythesehtmltags($stringtoclean, $cleanalsosomestyles=1, $removeclassattribute=1, $cleanalsojavascript=0, $allowiframe=0)
Clean a string to keep only desirable HTML tags.
Definition: functions.lib.php:6760
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