perms.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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', '0'); // Force use of CSRF protection with tokens even for GET
  29. }
  30. // Load Dolibarr environment
  31. require '../main.inc.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. $updatedmodulename = GETPOST('updatedmodulename', 'alpha');
  43. $contextpage = GETPOST('contextpage', 'aZ') ?GETPOST('contextpage', 'aZ') : 'userperms'; // To manage different context of search
  44. if (!isset($id) || empty($id)) {
  45. accessforbidden();
  46. }
  47. // Define if user can read permissions
  48. $canreaduser = ($user->admin || $user->hasRight("user", "user", "read"));
  49. // Define if user can modify other users and permissions
  50. $caneditperms = ($user->admin || $user->hasRight("user", "user", "write"));
  51. // Advanced permissions
  52. if (!empty($conf->global->MAIN_USE_ADVANCED_PERMS)) {
  53. $canreaduser = ($user->admin || ($user->hasRight("user", "user", "read") && $user->hasRight("user", "user_advance", "readperms")));
  54. $caneditselfperms = ($user->id == $id && $user->hasRight("user", "self_advance", "writeperms"));
  55. $caneditperms = (($caneditperms || $caneditselfperms) ? 1 : 0);
  56. }
  57. // Security check
  58. $socid = 0;
  59. if (isset($user->socid) && $user->socid > 0) {
  60. $socid = $user->socid;
  61. }
  62. $feature2 = (($socid && $user->hasRight("user", "self", "write")) ? '' : 'user');
  63. // A user can always read its own card if not advanced perms enabled, or if he has advanced perms, except for admin
  64. if ($user->id == $id && (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !$user->hasRight("user", "self_advance", "readperms") && empty($user->admin))) {
  65. accessforbidden();
  66. }
  67. $result = restrictedArea($user, 'user', $id, 'user&user', $feature2);
  68. if ($user->id <> $id && !$canreaduser) {
  69. accessforbidden();
  70. }
  71. $object = new User($db);
  72. $object->fetch($id, '', '', 1);
  73. $object->getrights();
  74. $entity = $conf->entity;
  75. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  76. $hookmanager->initHooks(array('usercard', 'userperms', 'globalcard'));
  77. /*
  78. * Actions
  79. */
  80. $parameters = array('socid'=>$socid);
  81. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  82. if ($reshook < 0) {
  83. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  84. }
  85. if (empty($reshook)) {
  86. if ($action == 'addrights' && $caneditperms && $confirm == 'yes') {
  87. $edituser = new User($db);
  88. $edituser->fetch($object->id);
  89. $result = $edituser->addrights($rights, $module, '', $entity);
  90. if ($result < 0) {
  91. setEventMessages($edituser->error, $edituser->errors, 'errors');
  92. }
  93. // If we are changing our own permissions, we reload permissions and menu
  94. if ($object->id == $user->id) {
  95. $user->clearrights();
  96. $user->getrights();
  97. $menumanager->loadMenu();
  98. }
  99. $object->clearrights();
  100. $object->getrights();
  101. }
  102. if ($action == 'delrights' && $caneditperms && $confirm == 'yes') {
  103. $edituser = new User($db);
  104. $edituser->fetch($object->id);
  105. $result = $edituser->delrights($rights, $module, '', $entity);
  106. if ($result < 0) {
  107. setEventMessages($edituser->error, $edituser->errors, 'errors');
  108. }
  109. // If we are changing our own permissions, we reload permissions and menu
  110. if ($object->id == $user->id) {
  111. $user->clearrights();
  112. $user->getrights();
  113. $menumanager->loadMenu();
  114. }
  115. $object->clearrights();
  116. $object->getrights();
  117. }
  118. }
  119. /*
  120. * View
  121. */
  122. $form = new Form($db);
  123. $person_name = !empty($object->firstname) ? $object->lastname.", ".$object->firstname : $object->lastname;
  124. $title = $person_name." - ".$langs->trans('Permissions');
  125. $help_url = '';
  126. llxHeader('', $title, $help_url);
  127. $head = user_prepare_head($object);
  128. $title = $langs->trans("User");
  129. print dol_get_fiche_head($head, 'rights', $title, -1, 'user');
  130. $db->begin();
  131. // Search all modules with permission and reload permissions def.
  132. $modules = array();
  133. $modulesdir = dolGetModulesDirs();
  134. foreach ($modulesdir as $dir) {
  135. $handle = @opendir(dol_osencode($dir));
  136. if (is_resource($handle)) {
  137. while (($file = readdir($handle)) !== false) {
  138. if (is_readable($dir.$file) && substr($file, 0, 3) == 'mod' && substr($file, dol_strlen($file) - 10) == '.class.php') {
  139. $modName = substr($file, 0, dol_strlen($file) - 10);
  140. if ($modName) {
  141. include_once $dir.$file;
  142. $objMod = new $modName($db);
  143. // Load all lang files of module
  144. if (isset($objMod->langfiles) && is_array($objMod->langfiles)) {
  145. foreach ($objMod->langfiles as $domain) {
  146. $langs->load($domain);
  147. }
  148. }
  149. // Load all permissions
  150. if ($objMod->rights_class) {
  151. $ret = $objMod->insert_permissions(0, $entity);
  152. $modules[$objMod->rights_class] = $objMod;
  153. //print "modules[".$objMod->rights_class."]=$objMod;";
  154. }
  155. }
  156. }
  157. }
  158. }
  159. }
  160. $db->commit();
  161. // Read permissions of user
  162. $permsuser = array();
  163. $sql = "SELECT DISTINCT ur.fk_id";
  164. $sql .= " FROM ".MAIN_DB_PREFIX."user_rights as ur";
  165. $sql .= " WHERE ur.entity = ".((int) $entity);
  166. $sql .= " AND ur.fk_user = ".((int) $object->id);
  167. dol_syslog("get user perms", LOG_DEBUG);
  168. $result = $db->query($sql);
  169. if ($result) {
  170. $num = $db->num_rows($result);
  171. $i = 0;
  172. while ($i < $num) {
  173. $obj = $db->fetch_object($result);
  174. array_push($permsuser, $obj->fk_id);
  175. $i++;
  176. }
  177. $db->free($result);
  178. } else {
  179. dol_print_error($db);
  180. }
  181. // Read the permissions of a user inherited by its groups
  182. $permsgroupbyentity = array();
  183. $sql = "SELECT DISTINCT gr.fk_id, gu.entity"; // fk_id are permission id and entity is entity of the group
  184. $sql .= " FROM ".MAIN_DB_PREFIX."usergroup_rights as gr,";
  185. $sql .= " ".MAIN_DB_PREFIX."usergroup_user as gu"; // all groups of a user
  186. $sql .= " WHERE 1 = 1";
  187. // A very strange business rules. Must be same than into user->getrights() user/perms.php and user/group/perms.php
  188. if (!empty($conf->global->MULTICOMPANY_BACKWARD_COMPATIBILITY)) {
  189. if (isModEnabled('multicompany') && !empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE)) {
  190. $sql .= " AND gu.entity IN (0,".$conf->entity.")";
  191. } else {
  192. //$sql .= " AND r.entity = ".((int) $conf->entity);
  193. }
  194. } else {
  195. $sql .= " AND gr.entity = ".((int) $conf->entity); // Only groups created in current entity
  196. // The entity on the table usergroup_user should be useless and should never be used because it is alreay into gr and r.
  197. // but when using MULTICOMPANY_TRANSVERSE_MODE, we may insert record that make rubbish result due to duplicate record of
  198. // other entities, so we are forced to add a filter here
  199. $sql .= " AND gu.entity IN (0,".$conf->entity.")";
  200. //$sql .= " AND r.entity = ".((int) $conf->entity); // Only permission of modules enabled in current entity
  201. }
  202. // End of strange business rule
  203. $sql .= " AND gr.fk_usergroup = gu.fk_usergroup";
  204. $sql .= " AND gu.fk_user = ".((int) $object->id);
  205. dol_syslog("get user perms", LOG_DEBUG);
  206. $result = $db->query($sql);
  207. if ($result) {
  208. $num = $db->num_rows($result);
  209. $i = 0;
  210. while ($i < $num) {
  211. $obj = $db->fetch_object($result);
  212. if (!isset($permsgroupbyentity[$obj->entity])) {
  213. $permsgroupbyentity[$obj->entity] = array();
  214. }
  215. array_push($permsgroupbyentity[$obj->entity], $obj->fk_id);
  216. $i++;
  217. }
  218. $db->free($result);
  219. } else {
  220. dol_print_error($db);
  221. }
  222. /*
  223. * Part to add/remove permissions
  224. */
  225. $linkback = '';
  226. if ($user->hasRight("user", "user", "read") || $user->admin) {
  227. $linkback = '<a href="'.DOL_URL_ROOT.'/user/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  228. }
  229. $morehtmlref = '<a href="'.DOL_URL_ROOT.'/user/vcard.php?id='.$object->id.'" class="refid">';
  230. $morehtmlref .= img_picto($langs->trans("Download").' '.$langs->trans("VCard"), 'vcard.png', 'class="valignmiddle marginleftonly paddingrightonly"');
  231. $morehtmlref .= '</a>';
  232. dol_banner_tab($object, 'id', $linkback, $user->hasRight("user", "user", "read") || $user->admin, 'rowid', 'ref', $morehtmlref);
  233. print '<div class="fichecenter">';
  234. print '<div class="underbanner clearboth"></div>';
  235. print '<table class="border centpercent tableforfield">';
  236. // Login
  237. print '<tr><td id="anchorforperms" class="titlefield">'.$langs->trans("Login").'</td>';
  238. if (!empty($object->ldap_sid) && $object->statut == 0) {
  239. print '<td class="error">';
  240. print $langs->trans("LoginAccountDisableInDolibarr");
  241. print '</td>';
  242. } else {
  243. print '<td>';
  244. $addadmin = '';
  245. if (property_exists($object, 'admin')) {
  246. if (isModEnabled('multicompany') && !empty($object->admin) && empty($object->entity)) {
  247. $addadmin .= img_picto($langs->trans("SuperAdministratorDesc"), "redstar", 'class="paddingleft"');
  248. } elseif (!empty($object->admin)) {
  249. $addadmin .= img_picto($langs->trans("AdministratorDesc"), "star", 'class="paddingleft"');
  250. }
  251. }
  252. print showValueWithClipboardCPButton($object->login).$addadmin;
  253. print '</td>';
  254. }
  255. print '</tr>'."\n";
  256. // Type
  257. print '<tr><td>';
  258. $text = $langs->trans("Type");
  259. print $form->textwithpicto($text, $langs->trans("InternalExternalDesc"));
  260. print '</td><td>';
  261. $type = $langs->trans("Internal");
  262. if ($object->socid > 0) {
  263. $type = $langs->trans("External");
  264. }
  265. print '<span class="badgeneutral">';
  266. print $type;
  267. if ($object->ldap_sid) {
  268. print ' ('.$langs->trans("DomainUser").')';
  269. }
  270. print '</span>';
  271. print '</td></tr>'."\n";
  272. print '</table>';
  273. print '</div>';
  274. print '<br>';
  275. if ($user->admin) {
  276. print info_admin($langs->trans("WarningOnlyPermissionOfActivatedModules"));
  277. }
  278. // If edited user is an extern user, we show warning for external users
  279. if (!empty($object->socid)) {
  280. print info_admin(showModulesExludedForExternal($modules))."\n";
  281. }
  282. $parameters = array('permsgroupbyentity'=>$permsgroupbyentity);
  283. $reshook = $hookmanager->executeHooks('insertExtraHeader', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  284. if ($reshook < 0) {
  285. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  286. }
  287. $listofexpandedmodules = array();
  288. print "\n";
  289. print '<div class="div-table-responsive-no-min">';
  290. print '<table class="noborder centpercent">';
  291. print '<tr class="liste_titre">';
  292. print '<td>'.$langs->trans("Module").'</td>';
  293. if ($caneditperms) {
  294. print '<td class="center nowrap">';
  295. print '<a class="reposition commonlink addexpandedmodulesinparamlist" 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>";
  296. print ' / ';
  297. print '<a class="reposition commonlink addexpandedmodulesinparamlist" 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>";
  298. print '</td>';
  299. } else {
  300. print '<td></td>';
  301. }
  302. print '<td></td>';
  303. print '<td></td>';
  304. print '<td class="right nowrap">';
  305. print '<a class="showallperms" title="'.dol_escape_htmltag($langs->trans("ShowAllPerms")).'" alt="'.dol_escape_htmltag($langs->trans("ShowAllPerms")).'" href="#">'.img_picto('', 'folder-open', 'class="paddingright"').'<span class="hideonsmartphone">'.$langs->trans("ExpandAll").'</span></a>';
  306. print ' | ';
  307. print '<a class="hideallperms" title="'.dol_escape_htmltag($langs->trans("HideAllPerms")).'" alt="'.dol_escape_htmltag($langs->trans("HideAllPerms")).'" href="#">'.img_picto('', 'folder', 'class="paddingright"').'<span class="hideonsmartphone">'.$langs->trans("UndoExpandAll").'</span></a>';
  308. print '</td>';
  309. print '</tr>'."\n";
  310. // Fix bad value for module_position in table
  311. // ------------------------------------------
  312. $sql = "SELECT r.id, r.libelle as label, r.module, r.perms, r.subperms, r.module_position, r.bydefault";
  313. $sql .= " FROM ".MAIN_DB_PREFIX."rights_def as r";
  314. $sql .= " WHERE r.libelle NOT LIKE 'tou%'"; // On ignore droits "tous"
  315. $sql .= " AND r.entity = ".((int) $entity);
  316. $sql .= " ORDER BY r.family_position, r.module_position, r.module, r.id";
  317. $result = $db->query($sql);
  318. if ($result) {
  319. $num = $db->num_rows($result);
  320. $i = 0;
  321. $oldmod = '';
  322. while ($i < $num) {
  323. $obj = $db->fetch_object($result);
  324. // If line is for a module that does not exist anymore (absent of includes/module), we ignore it
  325. if (!isset($obj->module) || empty($modules[$obj->module])) {
  326. $i++;
  327. continue;
  328. }
  329. // Special cases
  330. if (isModEnabled("reception")) {
  331. // The 2 permissions in fournisseur modules are replaced by permissions into reception module
  332. if ($obj->module == 'fournisseur' && $obj->perms == 'commande' && $obj->subperms == 'receptionner') {
  333. $i++;
  334. continue;
  335. }
  336. if ($obj->module == 'fournisseur' && $obj->perms == 'commande_advance' && $obj->subperms == 'check') {
  337. $i++;
  338. continue;
  339. }
  340. }
  341. $objMod = $modules[$obj->module];
  342. // Save field module_position in database if value is wrong
  343. if (empty($obj->module_position) || (is_object($objMod) && $objMod->isCoreOrExternalModule() == 'external' && $obj->module_position < 100000)) {
  344. if (is_object($modules[$obj->module]) && ($modules[$obj->module]->module_position > 0)) {
  345. // TODO Define familyposition
  346. //$familyposition = $modules[$obj->module]->family_position;
  347. $familyposition = 0;
  348. $newmoduleposition = $modules[$obj->module]->module_position;
  349. // Correct $newmoduleposition position for external modules
  350. $objMod = $modules[$obj->module];
  351. if (is_object($objMod) && $objMod->isCoreOrExternalModule() == 'external' && $newmoduleposition < 100000) {
  352. $newmoduleposition += 100000;
  353. }
  354. $sqlupdate = 'UPDATE '.MAIN_DB_PREFIX."rights_def SET module_position = ".((int) $newmoduleposition).",";
  355. $sqlupdate .= " family_position = ".((int) $familyposition);
  356. $sqlupdate .= " WHERE module_position = ".((int) $obj->module_position)." AND module = '".$db->escape($obj->module)."'";
  357. $db->query($sqlupdate);
  358. }
  359. }
  360. }
  361. }
  362. //print "xx".$conf->global->MAIN_USE_ADVANCED_PERMS;
  363. $sql = "SELECT r.id, r.libelle as label, r.module, r.perms, r.subperms, r.module_position, r.bydefault";
  364. $sql .= " FROM ".MAIN_DB_PREFIX."rights_def as r";
  365. $sql .= " WHERE r.libelle NOT LIKE 'tou%'"; // On ignore droits "tous"
  366. $sql .= " AND r.entity = ".((int) $entity);
  367. if (empty($conf->global->MAIN_USE_ADVANCED_PERMS)) {
  368. $sql .= " AND r.perms NOT LIKE '%_advance'"; // Hide advanced perms if option is not enabled
  369. }
  370. $sql .= " ORDER BY r.family_position, r.module_position, r.module, r.id";
  371. $result = $db->query($sql);
  372. if ($result) {
  373. $num = $db->num_rows($result);
  374. $i = 0; $j = 0;
  375. $oldmod = '';
  376. $cookietohidegroup = (empty($_COOKIE["DOLUSER_PERMS_HIDE_GRP"]) ? '' : preg_replace('/^,/', '', $_COOKIE["DOLUSER_PERMS_HIDE_GRP"]));
  377. $cookietohidegrouparray = explode(',', $cookietohidegroup);
  378. //var_dump($cookietohidegrouparray);
  379. while ($i < $num) {
  380. $obj = $db->fetch_object($result);
  381. // If line is for a module that does not exist anymore (absent of includes/module), we ignore it
  382. if (empty($modules[$obj->module])) {
  383. $i++;
  384. continue;
  385. }
  386. // Special cases
  387. if (isModEnabled("reception")) {
  388. // The 2 permission in fournisseur modules has been replaced by permissions into reception module
  389. if ($obj->module == 'fournisseur' && $obj->perms == 'commande' && $obj->subperms == 'receptionner') {
  390. $i++;
  391. continue;
  392. }
  393. if ($obj->module == 'fournisseur' && $obj->perms == 'commande_advance' && $obj->subperms == 'check') {
  394. $i++;
  395. continue;
  396. }
  397. }
  398. $objMod = $modules[$obj->module];
  399. // Save field module_position in database if value is wrong
  400. /*
  401. if (empty($obj->module_position) || (is_object($objMod) && $objMod->isCoreOrExternalModule() == 'external' && $obj->module_position < 100000)) {
  402. if (is_object($modules[$obj->module]) && ($modules[$obj->module]->module_position > 0)) {
  403. // TODO Define familyposition
  404. //$familyposition = $modules[$obj->module]->family_position;
  405. $familyposition = 0;
  406. $newmoduleposition = $modules[$obj->module]->module_position;
  407. // Correct $newmoduleposition position for external modules
  408. $objMod = $modules[$obj->module];
  409. if (is_object($objMod) && $objMod->isCoreOrExternalModule() == 'external' && $newmoduleposition < 100000) {
  410. $newmoduleposition += 100000;
  411. }
  412. $sqlupdate = 'UPDATE '.MAIN_DB_PREFIX."rights_def SET module_position = ".((int) $newmoduleposition).",";
  413. $sqlupdate .= " family_position = ".((int) $familyposition);
  414. $sqlupdate .= " WHERE module_position = ".((int) $obj->module_position)." AND module = '".$db->escape($obj->module)."'";
  415. $db->query($sqlupdate);
  416. }
  417. }
  418. */
  419. if (GETPOSTISSET('forbreakperms_'.$obj->module)) {
  420. $ishidden = GETPOST('forbreakperms_'.$obj->module, 'int');
  421. } elseif (in_array($j, $cookietohidegrouparray)) { // If j is among list of hidden group
  422. $ishidden = 1;
  423. } else {
  424. $ishidden = 0;
  425. }
  426. $isexpanded = ! $ishidden;
  427. //var_dump("isexpanded=".$isexpanded);
  428. $permsgroupbyentitypluszero = array();
  429. if (!empty($permsgroupbyentity[0])) {
  430. $permsgroupbyentitypluszero = array_merge($permsgroupbyentitypluszero, $permsgroupbyentity[0]);
  431. }
  432. if (!empty($permsgroupbyentity[$entity])) {
  433. $permsgroupbyentitypluszero = array_merge($permsgroupbyentitypluszero, $permsgroupbyentity[$entity]);
  434. }
  435. //var_dump($permsgroupbyentitypluszero);
  436. // Break found, it's a new module to catch
  437. if (isset($obj->module) && ($oldmod <> $obj->module)) {
  438. $oldmod = $obj->module;
  439. $j++;
  440. if (GETPOSTISSET('forbreakperms_'.$obj->module)) {
  441. $ishidden = GETPOST('forbreakperms_'.$obj->module, 'int');
  442. } elseif (in_array($j, $cookietohidegrouparray)) { // If j is among list of hidden group
  443. $ishidden = 1;
  444. } else {
  445. $ishidden = 0;
  446. }
  447. $isexpanded = ! $ishidden;
  448. //var_dump('$obj->module='.$obj->module.' isexpanded='.$isexpanded);
  449. // Break detected, we get objMod
  450. $objMod = $modules[$obj->module];
  451. $picto = ($objMod->picto ? $objMod->picto : 'generic');
  452. // Show break line
  453. print '<tr class="oddeven trforbreakperms" data-hide-perms="'.$obj->module.'" data-j="'.$j.'">';
  454. // Picto and label of module
  455. print '<td class="maxwidthonsmartphone tdoverflowonsmartphone tdforbreakperms" data-hide-perms="'.$obj->module.'">';
  456. print '<input type="hidden" name="forbreakperms_'.$obj->module.'" id="idforbreakperms_'.$obj->module.'" css="cssforfieldishiden" data-j="'.$j.'" value="'.($isexpanded ? '0' : "1").'">';
  457. print img_object('', $picto, 'class="pictoobjectwidth paddingright"').' '.$objMod->getName();
  458. print '<a name="'.$objMod->getName().'"></a>';
  459. print '</td>';
  460. // Permission and tick (2 columns)
  461. if (($caneditperms && empty($objMod->rights_admin_allowed)) || empty($object->admin)) {
  462. if ($caneditperms) {
  463. print '<td class="center wraponsmartphone">';
  464. print '<span class="permtohide_'.$obj->module.'" '.(!$isexpanded ? ' style="display:none"' : '').'>';
  465. print '<a class="reposition alink addexpandedmodulesinparamlist" 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='.$obj->module.'&confirm=yes&updatedmodulename='.$obj->module.'">'.$langs->trans("All")."</a>";
  466. print ' / ';
  467. print '<a class="reposition alink addexpandedmodulesinparamlist" 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='.$obj->module.'&confirm=yes&updatedmodulename='.$obj->module.'">'.$langs->trans("None")."</a>";
  468. print '</span>';
  469. print '</td>';
  470. print '<td class="tdforbreakperms" data-hide-perms="'.$obj->module.'">';
  471. print '</td>';
  472. } else {
  473. print '<td class="tdforbreakperms" data-hide-perms="'.$obj->module.'">&nbsp;</td>';
  474. print '<td class="tdforbreakperms" data-hide-perms="'.$obj->module.'">&nbsp;</td>';
  475. }
  476. } else {
  477. if ($caneditperms) {
  478. print '<td class="center wraponsmartphone">';
  479. /*print '<a class="reposition alink" 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='.$obj->module.'&confirm=yes&updatedmodulename='.$obj->module.'">'.$langs->trans("All")."</a>";
  480. print ' / ';
  481. print '<a class="reposition alink" 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='.$obj->module.'&confirm=yes&updatedmodulename='.$obj->module.'">'.$langs->trans("None")."</a>";
  482. */
  483. print '</td>';
  484. print '<td class="tdforbreakperms" data-hide-perms="'.$obj->module.'">';
  485. print '</td>';
  486. } else {
  487. print '<td class="right tdforbreakperms" data-hide-perms="'.$obj->module.'"></td>';
  488. print '<td class="tdforbreakperms" data-hide-perms="'.$obj->module.'">&nbsp;</td>';
  489. }
  490. }
  491. // Description of permission (2 columns)
  492. print '<td class="tdforbreakperms" data-hide-perms="'.$obj->module.'">&nbsp;</td>';
  493. print '<td class="maxwidthonsmartphone right tdforbreakperms" data-hide-perms="'.$obj->module.'">';
  494. print '<div class="switchfolderperms folderperms_'.$obj->module.'"'.($isexpanded ? ' style="display:none;"' : '').'>';
  495. print img_picto('', 'folder', 'class="marginright"');
  496. print '</div>';
  497. print '<div class="switchfolderperms folderopenperms_'.$obj->module.'"'.(!$isexpanded ? ' style="display:none;"' : '').'>';
  498. print img_picto('', 'folder-open', 'class="marginright"');
  499. print '</div>';
  500. print '</td>'; //Add picto + / - when open en closed
  501. print '</tr>'."\n";
  502. }
  503. print '<!-- '.$obj->module.'->'.$obj->perms.($obj->subperms ? '->'.$obj->subperms : '').' -->'."\n";
  504. print '<tr class="oddeven trtohide_'.$obj->module.'"'.(!$isexpanded ? ' style="display:none"' : '').'>';
  505. // Picto and label of module
  506. print '<td class="maxwidthonsmartphone tdoverflowonsmartphone">';
  507. print '</td>';
  508. // Permission and tick (2 columns)
  509. if (!empty($object->admin) && !empty($objMod->rights_admin_allowed)) { // Permission granted because admin
  510. if ($caneditperms) {
  511. print '<td class="center">'.img_picto($langs->trans("Administrator"), 'star').'</td>';
  512. } else {
  513. print '<td>&nbsp;</td>';
  514. }
  515. print '<td class="center nowrap">';
  516. if (!$caneditperms) {
  517. print img_picto($langs->trans("Active"), 'switch_on', '', false, 0, 0, '', 'opacitymedium');
  518. //print img_picto($langs->trans("Active"), 'tick');
  519. }
  520. print '</td>';
  521. } elseif (in_array($obj->id, $permsuser)) { // Permission granted by user
  522. if ($caneditperms) {
  523. print '<td class="center">';
  524. print '<a class="reposition addexpandedmodulesinparamlist" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=delrights&token='.newToken().'&entity='.$entity.'&rights='.$obj->id.'&confirm=yes&updatedmodulename='.$obj->module.'">';
  525. //print img_edit_remove($langs->trans("Remove"));
  526. print img_picto($langs->trans("Remove"), 'switch_on');
  527. print '</a></td>';
  528. } else {
  529. print '<td>&nbsp;</td>';
  530. }
  531. print '<td class="center nowrap">';
  532. if (!$caneditperms) {
  533. print img_picto($langs->trans("Active"), 'switch_on', '', false, 0, 0, '', 'opacitymedium');
  534. //print img_picto($langs->trans("Active"), 'tick');
  535. }
  536. print '</td>';
  537. } elseif (isset($permsgroupbyentitypluszero) && is_array($permsgroupbyentitypluszero)) {
  538. if (in_array($obj->id, $permsgroupbyentitypluszero)) { // Permission granted by group
  539. if ($caneditperms) {
  540. print '<td class="center">';
  541. print $form->textwithtooltip($langs->trans("Inherited"), $langs->trans("PermissionInheritedFromAGroup"));
  542. print '</td>';
  543. } else {
  544. print '<td>&nbsp;</td>';
  545. }
  546. print '<td class="center nowrap">';
  547. print img_picto($langs->trans("Active"), 'switch_on', '', false, 0, 0, '', 'opacitymedium');
  548. //print img_picto($langs->trans("Active"), 'tick');
  549. print '</td>';
  550. } else {
  551. // Do not own permission
  552. if ($caneditperms) {
  553. print '<td class="center">';
  554. print '<a class="reposition addexpandedmodulesinparamlist" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=addrights&entity='.$entity.'&rights='.$obj->id.'&confirm=yes&token='.newToken().'&updatedmodulename='.$obj->module.'">';
  555. //print img_edit_add($langs->trans("Add"));
  556. print img_picto($langs->trans("Add"), 'switch_off');
  557. print '</a></td>';
  558. } else {
  559. print '<td>';
  560. print '&nbsp;';
  561. print '</td>';
  562. }
  563. print '<td>&nbsp;</td>';
  564. }
  565. } else {
  566. // Do not own permission
  567. if ($caneditperms) {
  568. print '<td class="center">';
  569. print '<a class="reposition addexpandedmodulesinparamlist" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=addrights&entity='.$entity.'&rights='.$obj->id.'&confirm=yes&token='.newToken().'&updatedmodulename='.$obj->module.'">';
  570. //print img_edit_add($langs->trans("Add"));
  571. print img_picto($langs->trans("Add"), 'switch_off');
  572. print '</a></td>';
  573. } else {
  574. print '<td>&nbsp;</td>';
  575. }
  576. print '<td class="center">';
  577. if (!$caneditperms) {
  578. print img_picto($langs->trans("Disabled"), 'switch_off', '', false, 0, 0, '', 'opacitymedium');
  579. }
  580. //print '&nbsp;';
  581. print '</td>';
  582. }
  583. // Description of permission (2 columns)
  584. $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)));
  585. if (!$user->admin) {
  586. print '<td colspan="2">';
  587. } else {
  588. print '<td>';
  589. }
  590. print $permlabel;
  591. if (!empty($conf->global->MAIN_USE_ADVANCED_PERMS)) {
  592. if (preg_match('/_advance$/', $obj->perms)) {
  593. print ' <span class="opacitymedium">('.$langs->trans("AdvancedModeOnly").')</span>';
  594. }
  595. }
  596. print '</td>';
  597. // Permission id
  598. if ($user->admin) {
  599. print '<td class="right">';
  600. $htmltext = $langs->trans("ID").': '.$obj->id;
  601. $htmltext .= '<br>'.$langs->trans("Permission").': user->rights->'.$obj->module.'->'.$obj->perms.($obj->subperms ? '->'.$obj->subperms : '');
  602. print $form->textwithpicto('', $htmltext);
  603. //print '<span class="opacitymedium">'.$obj->id.'</span>';
  604. print '</td>';
  605. }
  606. print '</tr>'."\n";
  607. $i++;
  608. }
  609. } else {
  610. dol_print_error($db);
  611. }
  612. print '</table>';
  613. print '</div>';
  614. print '<script>';
  615. print '$(".tdforbreakperms:not(.alink)").on("click", function(){
  616. console.log("Click on tdforbreakperms");
  617. moduletohide = $(this).data("hide-perms");
  618. j = $(this).data("j");
  619. if ($("#idforbreakperms_"+moduletohide).val() == 1) {
  620. console.log("idforbreakperms_"+moduletohide+" has value hidden=1");
  621. $(".trtohide_"+moduletohide).show();
  622. $(".permtoshow_"+moduletohide).hide();
  623. $(".permtohide_"+moduletohide).show();
  624. $(".folderperms_"+moduletohide).hide();
  625. $(".folderopenperms_"+moduletohide).show();
  626. $("#idforbreakperms_"+moduletohide).val("0");
  627. } else {
  628. console.log("idforbreakperms_"+moduletohide+" has value hidden=0");
  629. $(".trtohide_"+moduletohide).hide();
  630. $(".folderopenperms_"+moduletohide).hide();
  631. $(".folderperms_"+moduletohide).show();
  632. $(".permtoshow_"+moduletohide).show();
  633. $(".permtohide_"+moduletohide).hide();
  634. $("#idforbreakperms_"+moduletohide).val("1");
  635. }
  636. // Now rebuild the value for cookie
  637. var hideuserperm="";
  638. $(".trforbreakperms").each(function(index) {
  639. //console.log( index + ": " + $( this ).data("j") + " " + $( this ).data("hide-perms") + " " + $("input[data-j="+(index+1)+"]").val());
  640. if ($("input[data-j="+(index+1)+"]").val() == 1) {
  641. hideuserperm=hideuserperm+","+(index+1);
  642. }
  643. });
  644. // set cookie by js
  645. date = new Date(); date.setTime(date.getTime()+(30*86400000));
  646. if (hideuserperm) {
  647. console.log("set cookie DOLUSER_PERMS_HIDE_GRP="+hideuserperm);
  648. document.cookie = "DOLUSER_PERMS_HIDE_GRP=" + hideuserperm + "; expires=" + date.toGMTString() + "; path=/ ";
  649. } else {
  650. console.log("delete cookie DOLUSER_PERMS_HIDE_GRP");
  651. document.cookie = "DOLUSER_PERMS_HIDE_GRP=; expires=Thu, 01-Jan-70 00:00:01 GMT; path=/ ";
  652. }
  653. });';
  654. print "\n";
  655. // Button expand / collapse all
  656. print '$(".showallperms").on("click", function(){
  657. console.log("Click on showallperms");
  658. console.log("delete cookie DOLUSER_PERMS_HIDE_GRP from showallperms click");
  659. document.cookie = "DOLUSER_PERMS_HIDE_GRP=; expires=Thu, 01-Jan-70 00:00:01 GMT; path=/ ";
  660. $(".tdforbreakperms").each( function(){
  661. moduletohide = $(this).data("hide-perms");
  662. //console.log(moduletohide);
  663. if ($("#idforbreakperms_"+moduletohide).val() != 0) {
  664. $(this).trigger("click"); // emulate the click, so the cooki will be resaved
  665. }
  666. })
  667. });
  668. $(".hideallperms").on("click", function(){
  669. console.log("Click on hideallperms");
  670. $(".tdforbreakperms").each( function(){
  671. moduletohide = $(this).data("hide-perms");
  672. //console.log(moduletohide);
  673. if ($("#idforbreakperms_"+moduletohide).val() != 1) {
  674. $(this).trigger("click"); // emulate the click, so the cooki will be resaved
  675. }
  676. })
  677. });';
  678. print "\n";
  679. print '</script>';
  680. print '<style>';
  681. print '.switchfolderperms{
  682. cursor: pointer;
  683. }';
  684. print '</style>';
  685. $parameters = array();
  686. $reshook = $hookmanager->executeHooks('insertExtraFooter', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  687. if ($reshook < 0) {
  688. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  689. }
  690. print dol_get_fiche_end();
  691. // End of page
  692. llxFooter();
  693. $db->close();