perms.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. /**
  23. * \file htdocs/user/group/perms.php
  24. * \brief Page to set permissions of a user group record
  25. */
  26. if (!defined('CSRFCHECK_WITH_TOKEN')) {
  27. define('CSRFCHECK_WITH_TOKEN', '1'); // Force use of CSRF protection with tokens even for GET
  28. }
  29. // Load Dolibarr environment
  30. require '../../main.inc.php';
  31. require_once DOL_DOCUMENT_ROOT.'/user/class/usergroup.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/lib/usergroups.lib.php';
  33. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  34. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  35. // Load translation files required by page
  36. $langs->loadLangs(array('users', 'admin'));
  37. $id = GETPOST('id', 'int');
  38. $action = GETPOST('action', 'aZ09');
  39. $confirm = GETPOST('confirm', 'alpha');
  40. $module = GETPOST('module', 'alpha');
  41. $rights = GETPOST('rights', 'int');
  42. $contextpage = GETPOST('contextpage', 'aZ') ?GETPOST('contextpage', 'aZ') : 'groupperms'; // To manage different context of search
  43. if (!isset($id) || empty($id)) {
  44. accessforbidden();
  45. }
  46. // Define if user can read permissions
  47. $canreadperms = ($user->admin || $user->hasRight("user", "user", "read"));
  48. // Define if user can modify group permissions
  49. $caneditperms = ($user->admin || $user->hasRight("user", "user", "write"));
  50. // Advanced permissions
  51. $advancedpermsactive = false;
  52. if (!empty($conf->global->MAIN_USE_ADVANCED_PERMS)) {
  53. $advancedpermsactive = true;
  54. $canreadperms = ($user->admin || ($user->hasRight("user", "group_advance", "read") && $user->hasRight("user", "group_advance", "readperms")));
  55. $caneditperms = ($user->admin || $user->hasRight("user", "group_advance", "write"));
  56. }
  57. // Security check
  58. //$result = restrictedArea($user, 'user', $id, 'usergroup', '');
  59. if (!$canreadperms) {
  60. accessforbidden();
  61. }
  62. $object = new Usergroup($db);
  63. $object->fetch($id);
  64. $object->getrights();
  65. $entity = $conf->entity;
  66. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  67. $hookmanager->initHooks(array('groupperms', 'globalcard'));
  68. /**
  69. * Actions
  70. */
  71. $parameters = array();
  72. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  73. if ($reshook < 0) {
  74. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  75. }
  76. if (empty($reshook)) {
  77. if ($action == 'addrights' && $caneditperms) {
  78. $editgroup = new Usergroup($db);
  79. $result = $editgroup->fetch($object->id);
  80. if ($result > 0) {
  81. $result = $editgroup->addrights($rights, $module, '', $entity);
  82. if ($result < 0) {
  83. setEventMessages($editgroup->error, $editgroup->errors, 'errors');
  84. }
  85. } else {
  86. dol_print_error($db);
  87. }
  88. $user->clearrights();
  89. $user->getrights();
  90. }
  91. if ($action == 'delrights' && $caneditperms) {
  92. $editgroup = new Usergroup($db);
  93. $result = $editgroup->fetch($id);
  94. if ($result > 0) {
  95. $result = $editgroup->delrights($rights, $module, '', $entity);
  96. if ($result < 0) {
  97. setEventMessages($editgroup->error, $editgroup->errors, 'errors');
  98. }
  99. } else {
  100. dol_print_error($db);
  101. }
  102. $user->clearrights();
  103. $user->getrights();
  104. }
  105. }
  106. /*
  107. * View
  108. */
  109. $form = new Form($db);
  110. $title = $object->name." - ".$langs->trans('Permissions');
  111. $help_url = '';
  112. llxHeader('', $title, $help_url);
  113. if ($object->id > 0) {
  114. $head = group_prepare_head($object);
  115. $title = $langs->trans("Group");
  116. print dol_get_fiche_head($head, 'rights', $title, -1, 'group');
  117. // Charge les modules soumis a permissions
  118. $modules = array();
  119. $modulesdir = dolGetModulesDirs();
  120. $db->begin();
  121. foreach ($modulesdir as $dir) {
  122. $handle = @opendir(dol_osencode($dir));
  123. if (is_resource($handle)) {
  124. while (($file = readdir($handle)) !== false) {
  125. if (is_readable($dir.$file) && substr($file, 0, 3) == 'mod' && substr($file, dol_strlen($file) - 10) == '.class.php') {
  126. $modName = substr($file, 0, dol_strlen($file) - 10);
  127. if ($modName) {
  128. include_once $dir.$file;
  129. $objMod = new $modName($db);
  130. // Load all lang files of module
  131. if (isset($objMod->langfiles) && is_array($objMod->langfiles)) {
  132. foreach ($objMod->langfiles as $domain) {
  133. $langs->load($domain);
  134. }
  135. }
  136. // Load all permissions
  137. if ($objMod->rights_class) {
  138. $ret = $objMod->insert_permissions(0, $entity);
  139. $modules[$objMod->rights_class] = $objMod;
  140. }
  141. }
  142. }
  143. }
  144. }
  145. }
  146. $db->commit();
  147. // Read permissions of group
  148. $permsgroupbyentity = array();
  149. $sql = "SELECT DISTINCT r.id, r.libelle, r.module, gr.entity";
  150. $sql .= " FROM ".MAIN_DB_PREFIX."rights_def as r,";
  151. $sql .= " ".MAIN_DB_PREFIX."usergroup_rights as gr";
  152. $sql .= " WHERE gr.fk_id = r.id";
  153. $sql .= " AND gr.entity = ".((int) $entity);
  154. $sql .= " AND gr.fk_usergroup = ".((int) $object->id);
  155. dol_syslog("get user perms", LOG_DEBUG);
  156. $result = $db->query($sql);
  157. if ($result) {
  158. $num = $db->num_rows($result);
  159. $i = 0;
  160. while ($i < $num) {
  161. $obj = $db->fetch_object($result);
  162. if (!isset($permsgroupbyentity[$obj->entity])) {
  163. $permsgroupbyentity[$obj->entity] = array();
  164. }
  165. array_push($permsgroupbyentity[$obj->entity], $obj->id);
  166. $i++;
  167. }
  168. $db->free($result);
  169. } else {
  170. dol_print_error($db);
  171. }
  172. /*
  173. * Part to add/remove permissions
  174. */
  175. $linkback = '<a href="'.DOL_URL_ROOT.'/user/group/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  176. dol_banner_tab($object, 'id', $linkback, $user->hasRight("user", "user", "read") || $user->admin);
  177. print '<div class="fichecenter">';
  178. print '<div class="underbanner clearboth"></div>';
  179. print '<table class="border centpercent tableforfield">';
  180. // Name (already in dol_banner, we keep it to have the GlobalGroup picto, but we should move it in dol_banner)
  181. if (!empty($conf->mutlicompany->enabled)) {
  182. print '<tr><td class="titlefield">'.$langs->trans("Name").'</td>';
  183. print '<td colspan="2">'.$object->name.'';
  184. if (!$object->entity) {
  185. print img_picto($langs->trans("GlobalGroup"), 'redstar');
  186. }
  187. print "</td></tr>\n";
  188. }
  189. // Note
  190. print '<tr><td class="titlefield tdtop">'.$langs->trans("Description").'</td>';
  191. print '<td class="valeur sensiblehtmlcontent">';
  192. print dol_string_onlythesehtmltags(dol_htmlentitiesbr($object->note));
  193. print '</td>';
  194. print "</tr>\n";
  195. print '</table><br>';
  196. if ($user->admin) {
  197. print info_admin($langs->trans("WarningOnlyPermissionOfActivatedModules"));
  198. }
  199. $parameters = array();
  200. $reshook = $hookmanager->executeHooks('insertExtraHeader', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  201. if ($reshook < 0) {
  202. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  203. }
  204. print "\n";
  205. print '<div class="div-table-responsive-no-min">';
  206. print '<table class="noborder centpercent">';
  207. print '<tr class="liste_titre">';
  208. print '<td>'.$langs->trans("Module").'</td>';
  209. if ($caneditperms) {
  210. print '<td class="center nowrap">';
  211. 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>";
  212. print '/';
  213. 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>";
  214. print '</td>';
  215. }
  216. print '<td class="center" width="24">&nbsp;</td>';
  217. print '<td>'.$langs->trans("Permissions").'</td>';
  218. if ($user->admin) {
  219. print '<td class="right"></td>';
  220. }
  221. print '</tr>'."\n";
  222. $sql = "SELECT r.id, r.libelle as label, r.module, r.perms, r.subperms, r.module_position, r.bydefault";
  223. $sql .= " FROM ".MAIN_DB_PREFIX."rights_def as r";
  224. $sql .= " WHERE r.libelle NOT LIKE 'tou%'"; // On ignore droits "tous"
  225. $sql .= " AND r.entity = ".((int) $entity);
  226. if (empty($conf->global->MAIN_USE_ADVANCED_PERMS)) {
  227. $sql .= " AND r.perms NOT LIKE '%_advance'"; // Hide advanced perms if option is disable
  228. }
  229. $sql .= " ORDER BY r.family_position, r.module_position, r.module, r.id";
  230. $result = $db->query($sql);
  231. if ($result) {
  232. $num = $db->num_rows($result);
  233. $i = 0;
  234. $oldmod = '';
  235. while ($i < $num) {
  236. $obj = $db->fetch_object($result);
  237. // If line is for a module that does not exist anymore (absent of includes/module), we ignore it
  238. if (empty($modules[$obj->module])) {
  239. $i++;
  240. continue;
  241. }
  242. $objMod = $modules[$obj->module];
  243. // Break found, it's a new module to catch
  244. if (isset($obj->module) && ($oldmod <> $obj->module)) {
  245. $oldmod = $obj->module;
  246. // Break detected, we get objMod
  247. $objMod = $modules[$obj->module];
  248. $picto = ($objMod->picto ? $objMod->picto : 'generic');
  249. // Show break line
  250. print '<tr class="oddeven trforbreak">';
  251. print '<td class="maxwidthonsmartphone tdoverflowonsmartphone">';
  252. print img_object('', $picto, 'class="pictoobjectwidth paddingright"').' '.$objMod->getName();
  253. print '<a name="'.$objMod->getName().'"></a>';
  254. print '</td>';
  255. if ($caneditperms) {
  256. print '<td class="center nowrap">';
  257. 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>";
  258. print '/';
  259. 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>";
  260. print '</td>';
  261. } else {
  262. print '<td>&nbsp;</td>';
  263. }
  264. print '<td>&nbsp;</td>';
  265. print '<td>&nbsp;</td>';
  266. // Permission id
  267. if ($user->admin) {
  268. print '<td class="right"></td>';
  269. }
  270. print '</tr>'."\n";
  271. }
  272. print '<!-- '.$obj->module.'->'.$obj->perms.($obj->subperms ? '->'.$obj->subperms : '').' -->'."\n";
  273. print '<tr class="oddeven">';
  274. // Picto and label of module
  275. print '<td class="maxwidthonsmartphone tdoverflowonsmartphone">';
  276. //print img_object('', $picto, 'class="inline-block pictoobjectwidth"').' '.$objMod->getName();
  277. print '</td>';
  278. if (!empty($permsgroupbyentity[$entity]) && is_array($permsgroupbyentity[$entity])) {
  279. if (in_array($obj->id, $permsgroupbyentity[$entity])) {
  280. // Own permission by group
  281. if ($caneditperms) {
  282. 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">';
  283. //print img_edit_remove($langs->trans("Remove"));
  284. print img_picto($langs->trans("Remove"), 'switch_on');
  285. print '</a></td>';
  286. }
  287. print '<td class="center nowrap">';
  288. print img_picto($langs->trans("Active"), 'tick');
  289. print '</td>';
  290. } else {
  291. // Do not own permission
  292. if ($caneditperms) {
  293. 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">';
  294. //print img_edit_add($langs->trans("Add"));
  295. print img_picto($langs->trans("Add"), 'switch_off');
  296. print '</a></td>';
  297. }
  298. print '<td>&nbsp;</td>';
  299. }
  300. } else {
  301. // Do not own permission
  302. if ($caneditperms) {
  303. 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().'">';
  304. //print img_edit_add($langs->trans("Add"));
  305. print img_picto($langs->trans("Add"), 'switch_off');
  306. print '</a></td>';
  307. }
  308. print '<td>&nbsp;</td>';
  309. }
  310. // Description of permission
  311. $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)));
  312. print '<td>';
  313. print $permlabel;
  314. if (!empty($conf->global->MAIN_USE_ADVANCED_PERMS)) {
  315. if (preg_match('/_advance$/', $obj->perms)) {
  316. print ' <span class="opacitymedium">('.$langs->trans("AdvancedModeOnly").')</span>';
  317. }
  318. }
  319. print '</td>';
  320. // Permission id
  321. if ($user->admin) {
  322. print '<td class="right">';
  323. $htmltext = $langs->trans("ID").': '.$obj->id;
  324. $htmltext .= '<br>'.$langs->trans("Permission").': user->rights->'.$obj->module.'->'.$obj->perms.($obj->subperms ? '->'.$obj->subperms : '');
  325. print $form->textwithpicto('', $htmltext);
  326. //print '<span class="opacitymedium">'.$obj->id.'</span>';
  327. print '</td>';
  328. }
  329. print '</tr>'."\n";
  330. $i++;
  331. }
  332. }
  333. print '</table>';
  334. print '</div>';
  335. print '</div>';
  336. $parameters = array();
  337. $reshook = $hookmanager->executeHooks('insertExtraFooter', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  338. if ($reshook < 0) {
  339. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  340. }
  341. print dol_get_fiche_end();
  342. }
  343. // End of page
  344. llxFooter();
  345. $db->close();