perms.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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) 2012 Juanjo Menent <jmenent@2byte.es>
  8. * Copyright (C) 2020 Tobias Sekan <tobias.sekan@startmail.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  22. */
  23. /**
  24. * \file htdocs/user/perms.php
  25. * \brief Page to set permission of a user record
  26. */
  27. if (!defined('CSRFCHECK_WITH_TOKEN')) {
  28. define('CSRFCHECK_WITH_TOKEN', '1'); // Force use of CSRF protection with tokens even for GET
  29. }
  30. require '../main.inc.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') : 'userperms'; // To manage different context of search
  42. if (!isset($id) || empty($id)) {
  43. accessforbidden();
  44. }
  45. // Define if user can read permissions
  46. $canreaduser = ($user->admin || $user->rights->user->user->lire);
  47. // Define if user can modify other users and permissions
  48. $caneditperms = ($user->admin || $user->rights->user->user->creer);
  49. // Advanced permissions
  50. if (!empty($conf->global->MAIN_USE_ADVANCED_PERMS)) {
  51. $canreaduser = ($user->admin || ($user->rights->user->user->lire && $user->rights->user->user_advance->readperms));
  52. $caneditselfperms = ($user->id == $id && $user->rights->user->self_advance->writeperms);
  53. $caneditperms = (($caneditperms || $caneditselfperms) ? 1 : 0);
  54. }
  55. // Security check
  56. $socid = 0;
  57. if (isset($user->socid) && $user->socid > 0) {
  58. $socid = $user->socid;
  59. }
  60. $feature2 = (($socid && $user->rights->user->self->creer) ? '' : 'user');
  61. // A user can always read its own card if not advanced perms enabled, or if he has advanced perms, except for admin
  62. if ($user->id == $id && (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty($user->rights->user->self_advance->readperms) && empty($user->admin))) {
  63. accessforbidden();
  64. }
  65. $result = restrictedArea($user, 'user', $id, 'user&user', $feature2);
  66. if ($user->id <> $id && !$canreaduser) {
  67. accessforbidden();
  68. }
  69. $object = new User($db);
  70. $object->fetch($id, '', '', 1);
  71. $object->getrights();
  72. $entity = $conf->entity;
  73. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  74. $hookmanager->initHooks(array('usercard', 'userperms', 'globalcard'));
  75. /*
  76. * Actions
  77. */
  78. $parameters = array('socid'=>$socid);
  79. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  80. if ($reshook < 0) {
  81. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  82. }
  83. if (empty($reshook)) {
  84. if ($action == 'addrights' && $caneditperms && $confirm == 'yes') {
  85. $edituser = new User($db);
  86. $edituser->fetch($object->id);
  87. $result = $edituser->addrights($rights, $module, '', $entity);
  88. if ($result < 0) {
  89. setEventMessages($edituser->error, $edituser->errors, 'errors');
  90. }
  91. // If we are changing our own permissions, we reload permissions and menu
  92. if ($object->id == $user->id) {
  93. $user->clearrights();
  94. $user->getrights();
  95. $menumanager->loadMenu();
  96. }
  97. $object->clearrights();
  98. $object->getrights();
  99. }
  100. if ($action == 'delrights' && $caneditperms && $confirm == 'yes') {
  101. $edituser = new User($db);
  102. $edituser->fetch($object->id);
  103. $result = $edituser->delrights($rights, $module, '', $entity);
  104. if ($result < 0) {
  105. setEventMessages($edituser->error, $edituser->errors, 'errors');
  106. }
  107. // If we are changing our own permissions, we reload permissions and menu
  108. if ($object->id == $user->id) {
  109. $user->clearrights();
  110. $user->getrights();
  111. $menumanager->loadMenu();
  112. }
  113. $object->clearrights();
  114. $object->getrights();
  115. }
  116. }
  117. /*
  118. * View
  119. */
  120. $form = new Form($db);
  121. llxHeader('', $langs->trans("Permissions"));
  122. $head = user_prepare_head($object);
  123. $title = $langs->trans("User");
  124. print dol_get_fiche_head($head, 'rights', $title, -1, 'user');
  125. $db->begin();
  126. // Search all modules with permission and reload permissions def.
  127. $modules = array();
  128. $modulesdir = dolGetModulesDirs();
  129. foreach ($modulesdir as $dir) {
  130. $handle = @opendir(dol_osencode($dir));
  131. if (is_resource($handle)) {
  132. while (($file = readdir($handle)) !== false) {
  133. if (is_readable($dir.$file) && substr($file, 0, 3) == 'mod' && substr($file, dol_strlen($file) - 10) == '.class.php') {
  134. $modName = substr($file, 0, dol_strlen($file) - 10);
  135. if ($modName) {
  136. include_once $dir.$file;
  137. $objMod = new $modName($db);
  138. // Load all lang files of module
  139. if (isset($objMod->langfiles) && is_array($objMod->langfiles)) {
  140. foreach ($objMod->langfiles as $domain) {
  141. $langs->load($domain);
  142. }
  143. }
  144. // Load all permissions
  145. if ($objMod->rights_class) {
  146. $ret = $objMod->insert_permissions(0, $entity);
  147. $modules[$objMod->rights_class] = $objMod;
  148. //print "modules[".$objMod->rights_class."]=$objMod;";
  149. }
  150. }
  151. }
  152. }
  153. }
  154. }
  155. $db->commit();
  156. // Read permissions of user
  157. $permsuser = array();
  158. $sql = "SELECT DISTINCT ur.fk_id";
  159. $sql .= " FROM ".MAIN_DB_PREFIX."user_rights as ur";
  160. $sql .= " WHERE ur.entity = ".((int) $entity);
  161. $sql .= " AND ur.fk_user = ".((int) $object->id);
  162. dol_syslog("get user perms", LOG_DEBUG);
  163. $result = $db->query($sql);
  164. if ($result) {
  165. $num = $db->num_rows($result);
  166. $i = 0;
  167. while ($i < $num) {
  168. $obj = $db->fetch_object($result);
  169. array_push($permsuser, $obj->fk_id);
  170. $i++;
  171. }
  172. $db->free($result);
  173. } else {
  174. dol_print_error($db);
  175. }
  176. // Lecture des droits groupes
  177. $permsgroupbyentity = array();
  178. $sql = "SELECT DISTINCT gr.fk_id, gu.entity";
  179. $sql .= " FROM ".MAIN_DB_PREFIX."usergroup_rights as gr,";
  180. $sql .= " ".MAIN_DB_PREFIX."usergroup_user as gu";
  181. $sql .= " WHERE gr.entity = ".((int) $entity);
  182. $sql .= " AND gr.fk_usergroup = gu.fk_usergroup";
  183. $sql .= " AND gu.fk_user = ".((int) $object->id);
  184. dol_syslog("get user perms", LOG_DEBUG);
  185. $result = $db->query($sql);
  186. if ($result) {
  187. $num = $db->num_rows($result);
  188. $i = 0;
  189. while ($i < $num) {
  190. $obj = $db->fetch_object($result);
  191. if (!isset($permsgroupbyentity[$obj->entity])) {
  192. $permsgroupbyentity[$obj->entity] = array();
  193. }
  194. array_push($permsgroupbyentity[$obj->entity], $obj->fk_id);
  195. $i++;
  196. }
  197. $db->free($result);
  198. } else {
  199. dol_print_error($db);
  200. }
  201. /*
  202. * Part to add/remove permissions
  203. */
  204. $linkback = '';
  205. if ($user->rights->user->user->lire || $user->admin) {
  206. $linkback = '<a href="'.DOL_URL_ROOT.'/user/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  207. }
  208. dol_banner_tab($object, 'id', $linkback, $user->rights->user->user->lire || $user->admin);
  209. print '<div class="underbanner clearboth"></div>';
  210. if ($user->admin) {
  211. print info_admin($langs->trans("WarningOnlyPermissionOfActivatedModules"));
  212. }
  213. // If edited user is an extern user, we show warning for external users
  214. if (! empty($object->socid)) {
  215. print info_admin(showModulesExludedForExternal($modules))."\n";
  216. }
  217. $parameters = array('permsgroupbyentity'=>$permsgroupbyentity);
  218. $reshook = $hookmanager->executeHooks('insertExtraHeader', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  219. if ($reshook < 0) {
  220. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  221. }
  222. print "\n";
  223. print '<div class="div-table-responsive-no-min">';
  224. print '<table class="noborder centpercent">';
  225. print '<tr class="liste_titre">';
  226. print '<td>'.$langs->trans("Module").'</td>';
  227. if (($caneditperms && empty($objMod->rights_admin_allowed)) || empty($object->admin)) {
  228. if ($caneditperms) {
  229. print '<td class="center nowrap">';
  230. 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.'&amp;action=addrights&amp;entity='.$entity.'&amp;module=allmodules&amp;confirm=yes&amp;token='.newToken().'">'.$langs->trans("All")."</a>";
  231. print ' / ';
  232. 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.'&amp;action=delrights&amp;entity='.$entity.'&amp;module=allmodules&amp;confirm=yes&amp;token='.newToken().'">'.$langs->trans("None")."</a>";
  233. print '</td>';
  234. }
  235. print '<td class="center" width="24">&nbsp;</td>';
  236. }
  237. print '<td>'.$langs->trans("Permissions").'</td>';
  238. if ($user->admin) {
  239. print '<td class="right"></td>';
  240. }
  241. print '</tr>'."\n";
  242. //print "xx".$conf->global->MAIN_USE_ADVANCED_PERMS;
  243. $sql = "SELECT r.id, r.libelle as label, r.module, r.perms, r.subperms, r.module_position, r.bydefault";
  244. $sql .= " FROM ".MAIN_DB_PREFIX."rights_def as r";
  245. $sql .= " WHERE r.libelle NOT LIKE 'tou%'"; // On ignore droits "tous"
  246. $sql .= " AND r.entity = ".((int) $entity);
  247. if (empty($conf->global->MAIN_USE_ADVANCED_PERMS)) {
  248. $sql .= " AND r.perms NOT LIKE '%_advance'"; // Hide advanced perms if option is not enabled
  249. }
  250. $sql .= " ORDER BY r.family_position, r.module_position, r.module, r.id";
  251. $result = $db->query($sql);
  252. if ($result) {
  253. $num = $db->num_rows($result);
  254. $i = 0;
  255. $oldmod = '';
  256. while ($i < $num) {
  257. $obj = $db->fetch_object($result);
  258. // If line is for a module that does not exist anymore (absent of includes/module), we ignore it
  259. if (empty($modules[$obj->module])) {
  260. $i++;
  261. continue;
  262. }
  263. $objMod = $modules[$obj->module];
  264. // Save field module_position in database if value is wrong
  265. if (empty($obj->module_position) || (is_object($objMod) && $objMod->isCoreOrExternalModule() == 'external' && $obj->module_position < 100000)) {
  266. if (is_object($modules[$obj->module]) && ($modules[$obj->module]->module_position > 0)) {
  267. // TODO Define familyposition
  268. //$familyposition = $modules[$obj->module]->family_position;
  269. $familyposition = 0;
  270. $newmoduleposition = $modules[$obj->module]->module_position;
  271. // Correct $newmoduleposition position for external modules
  272. $objMod = $modules[$obj->module];
  273. if (is_object($objMod) && $objMod->isCoreOrExternalModule() == 'external' && $newmoduleposition < 100000) {
  274. $newmoduleposition += 100000;
  275. }
  276. $sqlupdate = 'UPDATE '.MAIN_DB_PREFIX."rights_def SET module_position = ".((int) $newmoduleposition).",";
  277. $sqlupdate .= " family_position = ".((int) $familyposition);
  278. $sqlupdate .= " WHERE module_position = ".((int) $obj->module_position)." AND module = '".$db->escape($obj->module)."'";
  279. $db->query($sqlupdate);
  280. }
  281. }
  282. // Break found, it's a new module to catch
  283. if (isset($obj->module) && ($oldmod <> $obj->module)) {
  284. $oldmod = $obj->module;
  285. // Break detected, we get objMod
  286. $objMod = $modules[$obj->module];
  287. $picto = ($objMod->picto ? $objMod->picto : 'generic');
  288. // Show break line
  289. print '<tr class="oddeven trforbreak">';
  290. print '<td class="maxwidthonsmartphone tdoverflowonsmartphone">';
  291. print img_object('', $picto, 'class="pictoobjectwidth paddingright"').' '.$objMod->getName();
  292. print '<a name="'.$objMod->getName().'"></a>';
  293. print '</td>';
  294. if (($caneditperms && empty($objMod->rights_admin_allowed)) || empty($object->admin)) {
  295. if ($caneditperms) {
  296. print '<td class="center nowrap">';
  297. print '<a class="reposition" title="'.dol_escape_htmltag($langs->trans("All")).'" alt="'.dol_escape_htmltag($langs->trans("All")).'" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=addrights&amp;entity='.$entity.'&amp;module='.$obj->module.'&amp;confirm=yes&amp;token='.newToken().'">'.$langs->trans("All")."</a>";
  298. print ' / ';
  299. print '<a class="reposition" title="'.dol_escape_htmltag($langs->trans("None")).'" alt="'.dol_escape_htmltag($langs->trans("None")).'" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=delrights&amp;entity='.$entity.'&amp;module='.$obj->module.'&amp;confirm=yes&amp;token='.newToken().'">'.$langs->trans("None")."</a>";
  300. print '</td>';
  301. }
  302. print '<td>&nbsp;</td>';
  303. } else {
  304. if ($caneditperms) {
  305. print '<td>&nbsp;</td>';
  306. }
  307. print '<td>&nbsp;</td>';
  308. }
  309. print '<td>&nbsp;</td>';
  310. // Permission id
  311. if ($user->admin) {
  312. print '<td class="right"></td>';
  313. }
  314. print '</tr>'."\n";
  315. }
  316. print '<!-- '.$obj->module.'->'.$obj->perms.($obj->subperms ? '->'.$obj->subperms : '').' -->'."\n";
  317. print '<tr class="oddeven">';
  318. // Picto and label of module
  319. print '<td class="maxwidthonsmartphone tdoverflowonsmartphone">';
  320. //print img_object('', $picto, 'class="inline-block pictoobjectwidth"').' '.$objMod->getName();
  321. print '</td>';
  322. // Permission and tick
  323. if (!empty($object->admin) && !empty($objMod->rights_admin_allowed)) { // Permission granted because admin
  324. if ($caneditperms) {
  325. print '<td class="center">'.img_picto($langs->trans("Administrator"), 'star').'</td>';
  326. }
  327. print '<td class="center nowrap">';
  328. print img_picto($langs->trans("Active"), 'tick');
  329. print '</td>';
  330. } elseif (in_array($obj->id, $permsuser)) { // Permission granted by user
  331. if ($caneditperms) {
  332. print '<td class="center"><a class="reposition" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=delrights&amp;entity='.$entity.'&amp;rights='.$obj->id.'&amp;confirm=yes&amp;token='.newToken().'">';
  333. //print img_edit_remove($langs->trans("Remove"));
  334. print img_picto($langs->trans("Remove"), 'switch_on');
  335. print '</a></td>';
  336. }
  337. print '<td class="center nowrap">';
  338. print img_picto($langs->trans("Active"), 'tick');
  339. print '</td>';
  340. } elseif (isset($permsgroupbyentity[$entity]) && is_array($permsgroupbyentity[$entity])) {
  341. if (in_array($obj->id, $permsgroupbyentity[$entity])) { // Permission granted by group
  342. if ($caneditperms) {
  343. print '<td class="center">';
  344. print $form->textwithtooltip($langs->trans("Inherited"), $langs->trans("PermissionInheritedFromAGroup"));
  345. print '</td>';
  346. }
  347. print '<td class="center nowrap">';
  348. print img_picto($langs->trans("Active"), 'tick');
  349. print '</td>';
  350. } else {
  351. // Do not own permission
  352. if ($caneditperms) {
  353. print '<td class="center"><a class="reposition" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=addrights&amp;entity='.$entity.'&amp;rights='.$obj->id.'&amp;confirm=yes&amp;token='.newToken().'">';
  354. //print img_edit_add($langs->trans("Add"));
  355. print img_picto($langs->trans("Add"), 'switch_off');
  356. print '</a></td>';
  357. }
  358. print '<td>&nbsp;</td>';
  359. }
  360. } else {
  361. // Do not own permission
  362. if ($caneditperms) {
  363. print '<td class="center"><a class="reposition" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=addrights&amp;entity='.$entity.'&amp;rights='.$obj->id.'&amp;confirm=yes&amp;token='.newToken().'">';
  364. //print img_edit_add($langs->trans("Add"));
  365. print img_picto($langs->trans("Add"), 'switch_off');
  366. print '</a></td>';
  367. }
  368. print '<td>&nbsp;</td>';
  369. }
  370. // Description of permission
  371. $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)));
  372. print '<td>';
  373. print $permlabel;
  374. if (!empty($conf->global->MAIN_USE_ADVANCED_PERMS)) {
  375. if (preg_match('/_advance$/', $obj->perms)) {
  376. print ' <span class="opacitymedium">('.$langs->trans("AdvancedModeOnly").')</span>';
  377. }
  378. }
  379. print '</td>';
  380. // Permission id
  381. if ($user->admin) {
  382. print '<td class="right">';
  383. $htmltext = $langs->trans("ID").': '.$obj->id;
  384. $htmltext .= '<br>'.$langs->trans("Permission").': user->rights->'.$obj->module.'->'.$obj->perms.($obj->subperms ? '->'.$obj->subperms : '');
  385. print $form->textwithpicto('', $htmltext);
  386. //print '<span class="opacitymedium">'.$obj->id.'</span>';
  387. print '</td>';
  388. }
  389. print '</tr>'."\n";
  390. $i++;
  391. }
  392. } else {
  393. dol_print_error($db);
  394. }
  395. print '</table>';
  396. print '</div>';
  397. $parameters = array();
  398. $reshook = $hookmanager->executeHooks('insertExtraFooter', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  399. if ($reshook < 0) {
  400. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  401. }
  402. print dol_get_fiche_end();
  403. // End of page
  404. llxFooter();
  405. $db->close();