perms.php 14 KB

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