perms.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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. // 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 (getDolGlobalString('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 && (getDolGlobalString('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 edited 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 gr.entity = ".((int) $entity);
  187. $sql .= " AND gu.entity =".((int) $entity);
  188. $sql .= " AND gr.fk_usergroup = gu.fk_usergroup";
  189. $sql .= " AND gu.fk_user = ".((int) $object->id);
  190. dol_syslog("get user perms", LOG_DEBUG);
  191. $result = $db->query($sql);
  192. if ($result) {
  193. $num = $db->num_rows($result);
  194. $i = 0;
  195. while ($i < $num) {
  196. $obj = $db->fetch_object($result);
  197. if (!isset($permsgroupbyentity[$obj->entity])) {
  198. $permsgroupbyentity[$obj->entity] = array();
  199. }
  200. array_push($permsgroupbyentity[$obj->entity], $obj->fk_id);
  201. $i++;
  202. }
  203. $db->free($result);
  204. } else {
  205. dol_print_error($db);
  206. }
  207. /*
  208. * Part to add/remove permissions
  209. */
  210. $linkback = '';
  211. if ($user->hasRight("user", "user", "read") || $user->admin) {
  212. $linkback = '<a href="'.DOL_URL_ROOT.'/user/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  213. }
  214. $morehtmlref = '<a href="'.DOL_URL_ROOT.'/user/vcard.php?id='.$object->id.'&output=file&file='.urlencode(dol_sanitizeFileName($object->getFullName($langs).'.vcf')).'" class="refid" rel="noopener">';
  215. $morehtmlref .= img_picto($langs->trans("Download").' '.$langs->trans("VCard"), 'vcard.png', 'class="valignmiddle marginleftonly paddingrightonly"');
  216. $morehtmlref .= '</a>';
  217. $urltovirtualcard = '/user/virtualcard.php?id='.((int) $object->id);
  218. $morehtmlref .= dolButtonToOpenUrlInDialogPopup('publicvirtualcard', $langs->trans("PublicVirtualCardUrl").' - '.$object->getFullName($langs), img_picto($langs->trans("PublicVirtualCardUrl"), 'card', 'class="valignmiddle marginleftonly paddingrightonly"'), $urltovirtualcard, '', 'nohover');
  219. dol_banner_tab($object, 'id', $linkback, $user->hasRight("user", "user", "read") || $user->admin, 'rowid', 'ref', $morehtmlref);
  220. print '<div class="fichecenter">';
  221. print '<div class="underbanner clearboth"></div>';
  222. print '<table class="border centpercent tableforfield">';
  223. // Login
  224. print '<tr><td id="anchorforperms" class="titlefield">'.$langs->trans("Login").'</td>';
  225. if (!empty($object->ldap_sid) && $object->statut == 0) {
  226. print '<td class="error">';
  227. print $langs->trans("LoginAccountDisableInDolibarr");
  228. print '</td>';
  229. } else {
  230. print '<td>';
  231. $addadmin = '';
  232. if (property_exists($object, 'admin')) {
  233. if (isModEnabled('multicompany') && !empty($object->admin) && empty($object->entity)) {
  234. $addadmin .= img_picto($langs->trans("SuperAdministratorDesc"), "redstar", 'class="paddingleft"');
  235. } elseif (!empty($object->admin)) {
  236. $addadmin .= img_picto($langs->trans("AdministratorDesc"), "star", 'class="paddingleft"');
  237. }
  238. }
  239. print showValueWithClipboardCPButton($object->login).$addadmin;
  240. print '</td>';
  241. }
  242. print '</tr>'."\n";
  243. // Type
  244. print '<tr><td>';
  245. $text = $langs->trans("Type");
  246. print $form->textwithpicto($text, $langs->trans("InternalExternalDesc"));
  247. print '</td><td>';
  248. $type = $langs->trans("Internal");
  249. if ($object->socid > 0) {
  250. $type = $langs->trans("External");
  251. }
  252. print '<span class="badgeneutral">';
  253. print $type;
  254. if ($object->ldap_sid) {
  255. print ' ('.$langs->trans("DomainUser").')';
  256. }
  257. print '</span>';
  258. print '</td></tr>'."\n";
  259. print '</table>';
  260. print '</div>';
  261. print '<br>';
  262. if ($user->admin) {
  263. print info_admin($langs->trans("WarningOnlyPermissionOfActivatedModules"));
  264. }
  265. // If edited user is an extern user, we show warning for external users
  266. if (!empty($object->socid)) {
  267. print info_admin(showModulesExludedForExternal($modules))."\n";
  268. }
  269. $parameters = array('permsgroupbyentity'=>$permsgroupbyentity);
  270. $reshook = $hookmanager->executeHooks('insertExtraHeader', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  271. if ($reshook < 0) {
  272. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  273. }
  274. $listofexpandedmodules = array();
  275. print "\n";
  276. print '<div class="div-table-responsive-no-min">';
  277. print '<table class="noborder centpercent">';
  278. print '<tr class="liste_titre">';
  279. print '<td>'.$langs->trans("Module").'</td>';
  280. if ($caneditperms) {
  281. print '<td class="center nowrap">';
  282. 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>";
  283. print ' / ';
  284. 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>";
  285. print '</td>';
  286. } else {
  287. print '<td></td>';
  288. }
  289. print '<td></td>';
  290. //print '<td></td>';
  291. print '<td class="right nowrap" colspan="2">';
  292. 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>';
  293. print ' | ';
  294. 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>';
  295. print '</td>';
  296. print '</tr>'."\n";
  297. // Fix bad value for module_position in table
  298. // ------------------------------------------
  299. $sql = "SELECT r.id, r.libelle as label, r.module, r.perms, r.subperms, r.module_position, r.bydefault";
  300. $sql .= " FROM ".MAIN_DB_PREFIX."rights_def as r";
  301. $sql .= " WHERE r.libelle NOT LIKE 'tou%'"; // On ignore droits "tous"
  302. $sql .= " AND r.entity = ".((int) $entity);
  303. $sql .= " ORDER BY r.family_position, r.module_position, r.module, r.id";
  304. $result = $db->query($sql);
  305. if ($result) {
  306. $num = $db->num_rows($result);
  307. $i = 0;
  308. $oldmod = '';
  309. while ($i < $num) {
  310. $obj = $db->fetch_object($result);
  311. // If line is for a module that does not exist anymore (absent of includes/module), we ignore it
  312. if (!isset($obj->module) || empty($modules[$obj->module])) {
  313. $i++;
  314. continue;
  315. }
  316. // Special cases
  317. if (isModEnabled("reception")) {
  318. // The 2 permissions in fournisseur modules are replaced by permissions into reception module
  319. if ($obj->module == 'fournisseur' && $obj->perms == 'commande' && $obj->subperms == 'receptionner') {
  320. $i++;
  321. continue;
  322. }
  323. if ($obj->module == 'fournisseur' && $obj->perms == 'commande_advance' && $obj->subperms == 'check') {
  324. $i++;
  325. continue;
  326. }
  327. }
  328. $objMod = $modules[$obj->module];
  329. // Save field module_position in database if value is wrong
  330. if (empty($obj->module_position) || (is_object($objMod) && $objMod->isCoreOrExternalModule() == 'external' && $obj->module_position < 100000)) {
  331. if (is_object($modules[$obj->module]) && ($modules[$obj->module]->module_position > 0)) {
  332. // TODO Define familyposition
  333. //$familyposition = $modules[$obj->module]->family_position;
  334. $familyposition = 0;
  335. $newmoduleposition = $modules[$obj->module]->module_position;
  336. // Correct $newmoduleposition position for external modules
  337. $objMod = $modules[$obj->module];
  338. if (is_object($objMod) && $objMod->isCoreOrExternalModule() == 'external' && $newmoduleposition < 100000) {
  339. $newmoduleposition += 100000;
  340. }
  341. $sqlupdate = 'UPDATE '.MAIN_DB_PREFIX."rights_def SET module_position = ".((int) $newmoduleposition).",";
  342. $sqlupdate .= " family_position = ".((int) $familyposition);
  343. $sqlupdate .= " WHERE module_position = ".((int) $obj->module_position)." AND module = '".$db->escape($obj->module)."'";
  344. $db->query($sqlupdate);
  345. }
  346. }
  347. }
  348. }
  349. //print "xx".$conf->global->MAIN_USE_ADVANCED_PERMS;
  350. $sql = "SELECT r.id, r.libelle as label, r.module, r.perms, r.subperms, r.module_position, r.bydefault";
  351. $sql .= " FROM ".MAIN_DB_PREFIX."rights_def as r";
  352. $sql .= " WHERE r.libelle NOT LIKE 'tou%'"; // On ignore droits "tous"
  353. $sql .= " AND r.entity = ".((int) $entity);
  354. if (!getDolGlobalString('MAIN_USE_ADVANCED_PERMS')) {
  355. $sql .= " AND r.perms NOT LIKE '%_advance'"; // Hide advanced perms if option is not enabled
  356. }
  357. $sql .= " ORDER BY r.family_position, r.module_position, r.module, r.id";
  358. $result = $db->query($sql);
  359. if ($result) {
  360. $num = $db->num_rows($result);
  361. $i = 0; $j = 0;
  362. $oldmod = '';
  363. $cookietohidegroup = (empty($_COOKIE["DOLUSER_PERMS_HIDE_GRP"]) ? '' : preg_replace('/^,/', '', $_COOKIE["DOLUSER_PERMS_HIDE_GRP"]));
  364. $cookietohidegrouparray = explode(',', $cookietohidegroup);
  365. //var_dump($cookietohidegrouparray);
  366. while ($i < $num) {
  367. $obj = $db->fetch_object($result);
  368. // If line is for a module that does not exist anymore (absent of includes/module), we ignore it
  369. if (empty($modules[$obj->module])) {
  370. $i++;
  371. continue;
  372. }
  373. // Special cases
  374. if (isModEnabled("reception")) {
  375. // The 2 permission in fournisseur modules has been replaced by permissions into reception module
  376. if ($obj->module == 'fournisseur' && $obj->perms == 'commande' && $obj->subperms == 'receptionner') {
  377. $i++;
  378. continue;
  379. }
  380. if ($obj->module == 'fournisseur' && $obj->perms == 'commande_advance' && $obj->subperms == 'check') {
  381. $i++;
  382. continue;
  383. }
  384. }
  385. $objMod = $modules[$obj->module];
  386. // Save field module_position in database if value is wrong
  387. /*
  388. if (empty($obj->module_position) || (is_object($objMod) && $objMod->isCoreOrExternalModule() == 'external' && $obj->module_position < 100000)) {
  389. if (is_object($modules[$obj->module]) && ($modules[$obj->module]->module_position > 0)) {
  390. // TODO Define familyposition
  391. //$familyposition = $modules[$obj->module]->family_position;
  392. $familyposition = 0;
  393. $newmoduleposition = $modules[$obj->module]->module_position;
  394. // Correct $newmoduleposition position for external modules
  395. $objMod = $modules[$obj->module];
  396. if (is_object($objMod) && $objMod->isCoreOrExternalModule() == 'external' && $newmoduleposition < 100000) {
  397. $newmoduleposition += 100000;
  398. }
  399. $sqlupdate = 'UPDATE '.MAIN_DB_PREFIX."rights_def SET module_position = ".((int) $newmoduleposition).",";
  400. $sqlupdate .= " family_position = ".((int) $familyposition);
  401. $sqlupdate .= " WHERE module_position = ".((int) $obj->module_position)." AND module = '".$db->escape($obj->module)."'";
  402. $db->query($sqlupdate);
  403. }
  404. }
  405. */
  406. if (GETPOSTISSET('forbreakperms_'.$obj->module)) {
  407. $ishidden = GETPOST('forbreakperms_'.$obj->module, 'int');
  408. } elseif (in_array($j, $cookietohidegrouparray)) { // If j is among list of hidden group
  409. $ishidden = 1;
  410. } else {
  411. $ishidden = 0;
  412. }
  413. $isexpanded = ! $ishidden;
  414. //var_dump("isexpanded=".$isexpanded);
  415. $permsgroupbyentitypluszero = array();
  416. if (!empty($permsgroupbyentity[0])) {
  417. $permsgroupbyentitypluszero = array_merge($permsgroupbyentitypluszero, $permsgroupbyentity[0]);
  418. }
  419. if (!empty($permsgroupbyentity[$entity])) {
  420. $permsgroupbyentitypluszero = array_merge($permsgroupbyentitypluszero, $permsgroupbyentity[$entity]);
  421. }
  422. //var_dump($permsgroupbyentitypluszero);
  423. // Break found, it's a new module to catch
  424. if (isset($obj->module) && ($oldmod <> $obj->module)) {
  425. $oldmod = $obj->module;
  426. $j++;
  427. if (GETPOSTISSET('forbreakperms_'.$obj->module)) {
  428. $ishidden = GETPOST('forbreakperms_'.$obj->module, 'int');
  429. } elseif (in_array($j, $cookietohidegrouparray)) { // If j is among list of hidden group
  430. $ishidden = 1;
  431. } else {
  432. $ishidden = 0;
  433. }
  434. $isexpanded = ! $ishidden;
  435. //var_dump('$obj->module='.$obj->module.' isexpanded='.$isexpanded);
  436. // Break detected, we get objMod
  437. $objMod = $modules[$obj->module];
  438. $picto = ($objMod->picto ? $objMod->picto : 'generic');
  439. // Show break line
  440. print '<tr class="oddeven trforbreakperms" data-hide-perms="'.$obj->module.'" data-j="'.$j.'">';
  441. // Picto and label of module
  442. print '<td class="maxwidthonsmartphone tdoverflowmax150 tdforbreakperms" data-hide-perms="'.dol_escape_htmltag($obj->module).'" title="'.dol_escape_htmltag($objMod->getName()).'">';
  443. print '<input type="hidden" name="forbreakperms_'.$obj->module.'" id="idforbreakperms_'.$obj->module.'" css="cssforfieldishiden" data-j="'.$j.'" value="'.($isexpanded ? '0' : "1").'">';
  444. print img_object('', $picto, 'class="pictoobjectwidth paddingright"').' '.$objMod->getName();
  445. print '<a name="'.$objMod->getName().'"></a>';
  446. print '</td>';
  447. // Permission and tick (2 columns)
  448. if (($caneditperms && empty($objMod->rights_admin_allowed)) || empty($object->admin)) {
  449. if ($caneditperms) {
  450. print '<td class="center wraponsmartphone">';
  451. print '<span class="permtohide_'.$obj->module.'" '.(!$isexpanded ? ' style="display:none"' : '').'>';
  452. 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>";
  453. print ' / ';
  454. 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>";
  455. print '</span>';
  456. print '</td>';
  457. print '<td class="tdforbreakperms" data-hide-perms="'.dol_escape_htmltag($obj->module).'">';
  458. print '</td>';
  459. } else {
  460. print '<td class="tdforbreakperms" data-hide-perms="'.dol_escape_htmltag($obj->module).'">&nbsp;</td>';
  461. print '<td class="tdforbreakperms" data-hide-perms="'.dol_escape_htmltag($obj->module).'">&nbsp;</td>';
  462. }
  463. } else {
  464. if ($caneditperms) {
  465. print '<td class="center wraponsmartphone">';
  466. /*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>";
  467. print ' / ';
  468. 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>";
  469. */
  470. print '</td>';
  471. print '<td class="tdforbreakperms" data-hide-perms="'.dol_escape_htmltag($obj->module).'">';
  472. print '</td>';
  473. } else {
  474. print '<td class="right tdforbreakperms" data-hide-perms="'.dol_escape_htmltag($obj->module).'"></td>';
  475. print '<td class="tdforbreakperms" data-hide-perms="'.dol_escape_htmltag($obj->module).'">&nbsp;</td>';
  476. }
  477. }
  478. // Description of permission (2 columns)
  479. print '<td class="tdforbreakperms" data-hide-perms="'.dol_escape_htmltag($obj->module).'">&nbsp;</td>';
  480. print '<td class="maxwidthonsmartphone right tdforbreakperms" data-hide-perms="'.dol_escape_htmltag($obj->module).'">';
  481. print '<div class="switchfolderperms folderperms_'.$obj->module.'"'.($isexpanded ? ' style="display:none;"' : '').'>';
  482. print img_picto('', 'folder', 'class="marginright"');
  483. print '</div>';
  484. print '<div class="switchfolderperms folderopenperms_'.$obj->module.'"'.(!$isexpanded ? ' style="display:none;"' : '').'>';
  485. print img_picto('', 'folder-open', 'class="marginright"');
  486. print '</div>';
  487. print '</td>'; //Add picto + / - when open en closed
  488. print '</tr>'."\n";
  489. }
  490. print '<!-- '.$obj->module.'->'.$obj->perms.($obj->subperms ? '->'.$obj->subperms : '').' -->'."\n";
  491. print '<tr class="oddeven trtohide_'.$obj->module.'"'.(!$isexpanded ? ' style="display:none"' : '').'>';
  492. // Picto and label of module
  493. print '<td class="maxwidthonsmartphone tdoverflowonsmartphone">';
  494. print '</td>';
  495. // Permission and tick (2 columns)
  496. if (!empty($object->admin) && !empty($objMod->rights_admin_allowed)) { // Permission granted because admin
  497. if ($caneditperms) {
  498. print '<td class="center">'.img_picto($langs->trans("AdministratorDesc"), 'star').'</td>';
  499. } else {
  500. print '<td>&nbsp;</td>';
  501. }
  502. print '<td class="center nowrap">';
  503. if (!$caneditperms) {
  504. print img_picto($langs->trans("Active"), 'switch_on', '', false, 0, 0, '', 'opacitymedium');
  505. //print img_picto($langs->trans("Active"), 'tick');
  506. }
  507. print '</td>';
  508. } elseif (in_array($obj->id, $permsuser)) { // Permission granted by user
  509. if ($caneditperms) {
  510. print '<td class="center">';
  511. 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.'">';
  512. //print img_edit_remove($langs->trans("Remove"));
  513. print img_picto($langs->trans("Remove"), 'switch_on');
  514. print '</a></td>';
  515. } else {
  516. print '<td>&nbsp;</td>';
  517. }
  518. print '<td class="center nowrap">';
  519. if (!$caneditperms) {
  520. print img_picto($langs->trans("Active"), 'switch_on', '', false, 0, 0, '', 'opacitymedium');
  521. //print img_picto($langs->trans("Active"), 'tick');
  522. }
  523. print '</td>';
  524. } elseif (isset($permsgroupbyentitypluszero) && is_array($permsgroupbyentitypluszero)) {
  525. if (in_array($obj->id, $permsgroupbyentitypluszero)) { // Permission granted by group
  526. print '<td class="center nowrap">';
  527. print img_picto($langs->trans("Active"), 'switch_on', '', false, 0, 0, '', 'opacitymedium');
  528. //print img_picto($langs->trans("Active"), 'tick');
  529. print '</td>';
  530. if ($caneditperms) {
  531. print '<td class="">';
  532. print $form->textwithtooltip($langs->trans("Inherited"), $langs->trans("PermissionInheritedFromAGroup"));
  533. print '</td>';
  534. } else {
  535. print '<td>c&nbsp;</td>';
  536. }
  537. } else {
  538. // Do not own permission
  539. if ($caneditperms) {
  540. print '<td class="center">';
  541. 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.'">';
  542. //print img_edit_add($langs->trans("Add"));
  543. print img_picto($langs->trans("Add"), 'switch_off');
  544. print '</a></td>';
  545. } else {
  546. print '<td>';
  547. print '&nbsp;';
  548. print '</td>';
  549. }
  550. print '<td>&nbsp;</td>';
  551. }
  552. } else {
  553. // Do not own permission
  554. if ($caneditperms) {
  555. print '<td class="center">';
  556. 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.'">';
  557. //print img_edit_add($langs->trans("Add"));
  558. print img_picto($langs->trans("Add"), 'switch_off');
  559. print '</a></td>';
  560. } else {
  561. print '<td>aa';
  562. print img_picto($langs->trans("Disabled"), 'switch_off', '', false, 0, 0, '', 'opacitymedium');
  563. print '</td>';
  564. }
  565. print '<td class="center">';
  566. print '</td>';
  567. }
  568. // Description of permission (2 columns)
  569. $permlabel = (getDolGlobalString('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)));
  570. if (!$user->admin) {
  571. print '<td colspan="2">';
  572. } else {
  573. print '<td>';
  574. }
  575. print $permlabel;
  576. if (getDolGlobalString('MAIN_USE_ADVANCED_PERMS')) {
  577. if (preg_match('/_advance$/', $obj->perms)) {
  578. print ' <span class="opacitymedium">('.$langs->trans("AdvancedModeOnly").')</span>';
  579. }
  580. }
  581. // Special warning case for the permission "Allow to modify other users password"
  582. if ($obj->module == 'user' && $obj->perms == 'user' && $obj->subperms == 'password') {
  583. if ((!empty($object->admin) && !empty($objMod->rights_admin_allowed)) ||
  584. in_array($obj->id, $permsuser) /* if edited user owns this permissions */ ||
  585. (isset($permsgroupbyentitypluszero) && is_array($permsgroupbyentitypluszero) && in_array($obj->id, $permsgroupbyentitypluszero))) {
  586. print ' '.img_warning($langs->trans("AllowPasswordResetBySendingANewPassByEmail"));
  587. }
  588. }
  589. // Special warning case for the permission "Create/modify other users, groups and permissions"
  590. if ($obj->module == 'user' && $obj->perms == 'user' && ($obj->subperms == 'creer' || $obj->subperms == 'create')) {
  591. if ((!empty($object->admin) && !empty($objMod->rights_admin_allowed)) ||
  592. in_array($obj->id, $permsuser) /* if edited user owns this permissions */ ||
  593. (isset($permsgroupbyentitypluszero) && is_array($permsgroupbyentitypluszero) && in_array($obj->id, $permsgroupbyentitypluszero))) {
  594. print ' '.img_warning($langs->trans("AllowAnyPrivileges"));
  595. }
  596. }
  597. print '</td>';
  598. // Permission id
  599. if ($user->admin) {
  600. print '<td class="right">';
  601. $htmltext = $langs->trans("ID").': '.$obj->id;
  602. $htmltext .= '<br>'.$langs->trans("Permission").': user->rights->'.$obj->module.'->'.$obj->perms.($obj->subperms ? '->'.$obj->subperms : '');
  603. print $form->textwithpicto('', $htmltext);
  604. //print '<span class="opacitymedium">'.$obj->id.'</span>';
  605. print '</td>';
  606. }
  607. print '</tr>'."\n";
  608. $i++;
  609. }
  610. } else {
  611. dol_print_error($db);
  612. }
  613. print '</table>';
  614. print '</div>';
  615. print '<script>';
  616. print '$(".tdforbreakperms:not(.alink)").on("click", function(){
  617. console.log("Click on tdforbreakperms");
  618. moduletohide = $(this).data("hide-perms");
  619. j = $(this).data("j");
  620. if ($("#idforbreakperms_"+moduletohide).val() == 1) {
  621. console.log("idforbreakperms_"+moduletohide+" has value hidden=1");
  622. $(".trtohide_"+moduletohide).show();
  623. $(".permtoshow_"+moduletohide).hide();
  624. $(".permtohide_"+moduletohide).show();
  625. $(".folderperms_"+moduletohide).hide();
  626. $(".folderopenperms_"+moduletohide).show();
  627. $("#idforbreakperms_"+moduletohide).val("0");
  628. } else {
  629. console.log("idforbreakperms_"+moduletohide+" has value hidden=0");
  630. $(".trtohide_"+moduletohide).hide();
  631. $(".folderopenperms_"+moduletohide).hide();
  632. $(".folderperms_"+moduletohide).show();
  633. $(".permtoshow_"+moduletohide).show();
  634. $(".permtohide_"+moduletohide).hide();
  635. $("#idforbreakperms_"+moduletohide).val("1");
  636. }
  637. // Now rebuild the value for cookie
  638. var hideuserperm="";
  639. $(".trforbreakperms").each(function(index) {
  640. //console.log( index + ": " + $( this ).data("j") + " " + $( this ).data("hide-perms") + " " + $("input[data-j="+(index+1)+"]").val());
  641. if ($("input[data-j="+(index+1)+"]").val() == 1) {
  642. hideuserperm=hideuserperm+","+(index+1);
  643. }
  644. });
  645. // set cookie by js
  646. date = new Date(); date.setTime(date.getTime()+(30*86400000));
  647. if (hideuserperm) {
  648. console.log("set cookie DOLUSER_PERMS_HIDE_GRP="+hideuserperm);
  649. document.cookie = "DOLUSER_PERMS_HIDE_GRP=" + hideuserperm + "; expires=" + date.toGMTString() + "; path=/ ";
  650. } else {
  651. console.log("delete cookie DOLUSER_PERMS_HIDE_GRP");
  652. document.cookie = "DOLUSER_PERMS_HIDE_GRP=; expires=Thu, 01-Jan-70 00:00:01 GMT; path=/ ";
  653. }
  654. });';
  655. print "\n";
  656. // Button expand / collapse all
  657. print '$(".showallperms").on("click", function(){
  658. console.log("Click on showallperms");
  659. console.log("delete cookie DOLUSER_PERMS_HIDE_GRP from showallperms click");
  660. document.cookie = "DOLUSER_PERMS_HIDE_GRP=; expires=Thu, 01-Jan-70 00:00:01 GMT; path=/ ";
  661. $(".tdforbreakperms").each( function(){
  662. moduletohide = $(this).data("hide-perms");
  663. //console.log(moduletohide);
  664. if ($("#idforbreakperms_"+moduletohide).val() != 0) {
  665. $(this).trigger("click"); // emulate the click, so the cooki will be resaved
  666. }
  667. })
  668. });
  669. $(".hideallperms").on("click", function(){
  670. console.log("Click on hideallperms");
  671. $(".tdforbreakperms").each( function(){
  672. moduletohide = $(this).data("hide-perms");
  673. //console.log(moduletohide);
  674. if ($("#idforbreakperms_"+moduletohide).val() != 1) {
  675. $(this).trigger("click"); // emulate the click, so the cooki will be resaved
  676. }
  677. })
  678. });';
  679. print "\n";
  680. print '</script>';
  681. print '<style>';
  682. print '.switchfolderperms{
  683. cursor: pointer;
  684. }';
  685. print '</style>';
  686. $parameters = array();
  687. $reshook = $hookmanager->executeHooks('insertExtraFooter', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  688. if ($reshook < 0) {
  689. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  690. }
  691. print dol_get_fiche_end();
  692. // End of page
  693. llxFooter();
  694. $db->close();