perms.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2009 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2013 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2011 Herve Prot <herve.prot@symeos.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/admin/perms.php
  22. * \ingroup core
  23. * \brief Page to setup default permissions of a new user
  24. */
  25. // Load Dolibarr environment
  26. require '../main.inc.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  29. // Load translation files required by the page
  30. $langs->loadLangs(array('admin', 'users', 'other'));
  31. $action = GETPOST('action', 'aZ09');
  32. if (!$user->admin) {
  33. accessforbidden();
  34. }
  35. $entity = $conf->entity;
  36. /*
  37. * Actions
  38. */
  39. if ($action == 'add') {
  40. $sql = "UPDATE ".MAIN_DB_PREFIX."rights_def SET bydefault=1";
  41. $sql .= " WHERE id = ".GETPOST("pid", 'int');
  42. $sql .= " AND entity = ".$conf->entity;
  43. $db->query($sql);
  44. }
  45. if ($action == 'remove') {
  46. $sql = "UPDATE ".MAIN_DB_PREFIX."rights_def SET bydefault=0";
  47. $sql .= " WHERE id = ".GETPOST('pid', 'int');
  48. $sql .= " AND entity = ".$conf->entity;
  49. $db->query($sql);
  50. }
  51. /*
  52. * View
  53. */
  54. $wikihelp = 'EN:Setup_Security|FR:Paramétrage_Sécurité|ES:Configuración_Seguridad';
  55. llxHeader('', $langs->trans("DefaultRights"), $wikihelp);
  56. print load_fiche_titre($langs->trans("SecuritySetup"), '', 'title_setup');
  57. print '<span class="opacitymedium">'.$langs->trans("DefaultRightsDesc")." ".$langs->trans("OnlyActiveElementsAreShown")."</span><br><br>\n";
  58. $db->begin();
  59. // Search all modules with permission and reload permissions def.
  60. $modules = array();
  61. $modulesdir = dolGetModulesDirs();
  62. foreach ($modulesdir as $dir) {
  63. $handle = @opendir(dol_osencode($dir));
  64. if (is_resource($handle)) {
  65. while (($file = readdir($handle)) !== false) {
  66. if (is_readable($dir.$file) && substr($file, 0, 3) == 'mod' && substr($file, dol_strlen($file) - 10) == '.class.php') {
  67. $modName = substr($file, 0, dol_strlen($file) - 10);
  68. if ($modName) {
  69. include_once $dir.$file;
  70. $objMod = new $modName($db);
  71. // Load all lang files of module
  72. if (isset($objMod->langfiles) && is_array($objMod->langfiles)) {
  73. foreach ($objMod->langfiles as $domain) {
  74. $langs->load($domain);
  75. }
  76. }
  77. // Load all permissions
  78. if ($objMod->rights_class) {
  79. $ret = $objMod->insert_permissions(0, $entity);
  80. $modules[$objMod->rights_class] = $objMod;
  81. //print "modules[".$objMod->rights_class."]=$objMod;";
  82. }
  83. }
  84. }
  85. }
  86. }
  87. }
  88. $db->commit();
  89. $head = security_prepare_head();
  90. print dol_get_fiche_head($head, 'default', '', -1);
  91. // Show warning about external users
  92. print info_admin(showModulesExludedForExternal($modules)).'<br>'."\n";
  93. print "\n";
  94. print '<div class="div-table-responsive-no-min">';
  95. print '<table class="noborder centpercent">';
  96. print '<tr class="liste_titre">';
  97. print '<td>'.$langs->trans("Module").'</td>';
  98. print '<td class="center">'.$langs->trans("Default").'</td>';
  99. print '<td class="center" width="24">&nbsp;</td>';
  100. print '<td>'.$langs->trans("Permissions").'</td>';
  101. if ($user->admin) {
  102. print '<td class="right"></td>';
  103. }
  104. print '</tr>'."\n";
  105. //print "xx".$conf->global->MAIN_USE_ADVANCED_PERMS;
  106. $sql = "SELECT r.id, r.libelle as label, r.module, r.perms, r.subperms, r.module_position, r.bydefault";
  107. $sql .= " FROM ".MAIN_DB_PREFIX."rights_def as r";
  108. $sql .= " WHERE r.libelle NOT LIKE 'tou%'"; // On ignore droits "tous"
  109. $sql .= " AND r.entity = ".((int) $entity);
  110. if (empty($conf->global->MAIN_USE_ADVANCED_PERMS)) {
  111. $sql .= " AND r.perms NOT LIKE '%_advance'"; // Hide advanced perms if option is not enabled
  112. }
  113. $sql .= " ORDER BY r.family_position, r.module_position, r.module, r.id";
  114. $result = $db->query($sql);
  115. if ($result) {
  116. $num = $db->num_rows($result);
  117. $i = 0;
  118. $oldmod = '';
  119. while ($i < $num) {
  120. $obj = $db->fetch_object($result);
  121. // If line is for a module that does not exist anymore (absent of includes/module), we ignore it
  122. if (empty($modules[$obj->module])) {
  123. $i++;
  124. continue;
  125. }
  126. $objMod = $modules[$obj->module];
  127. // Save field module_position in database if value is wrong
  128. if (empty($obj->module_position) || (is_object($objMod) && $objMod->isCoreOrExternalModule() == 'external' && $obj->module_position < 100000)) {
  129. if (is_object($modules[$obj->module]) && ($modules[$obj->module]->module_position > 0)) {
  130. // TODO Define familyposition
  131. //$familyposition = $modules[$obj->module]->family_position;
  132. $familyposition = 0;
  133. $newmoduleposition = $modules[$obj->module]->module_position;
  134. // Correct $newmoduleposition position for external modules
  135. $objMod = $modules[$obj->module];
  136. if (is_object($objMod) && $objMod->isCoreOrExternalModule() == 'external' && $newmoduleposition < 100000) {
  137. $newmoduleposition += 100000;
  138. }
  139. $sqlupdate = 'UPDATE '.MAIN_DB_PREFIX."rights_def SET module_position = ".((int) $newmoduleposition).",";
  140. $sqlupdate .= " family_position = ".((int) $familyposition);
  141. $sqlupdate .= " WHERE module_position = ".((int) $obj->module_position)." AND module = '".$db->escape($obj->module)."'";
  142. $db->query($sqlupdate);
  143. }
  144. }
  145. // Check if permission we found is inside a module definition. If not, we discard it.
  146. $found = false;
  147. foreach ($modules[$obj->module]->rights as $key => $val) {
  148. if ($val[4] == $obj->perms && (empty($val[5]) || $val[5] == $obj->subperms)) {
  149. $found = true;
  150. break;
  151. }
  152. }
  153. if (!$found) {
  154. $i++;
  155. continue;
  156. }
  157. // Break found, it's a new module to catch
  158. if (isset($obj->module) && ($oldmod <> $obj->module)) {
  159. $oldmod = $obj->module;
  160. // Break detected, we get objMod
  161. $objMod = $modules[$obj->module];
  162. $picto = ($objMod->picto ? $objMod->picto : 'generic');
  163. // Show break line
  164. print '<tr class="oddeven trforbreak">';
  165. print '<td class="maxwidthonsmartphone tdoverflowonsmartphone">';
  166. print img_object('', $picto, 'class="pictoobjectwidth paddingright"').' '.$objMod->getName();
  167. print '<a name="'.$objMod->getName().'"></a>';
  168. print '</td>';
  169. print '<td>&nbsp;</td>';
  170. print '<td>&nbsp;</td>';
  171. print '<td>&nbsp;</td>';
  172. // Permission id
  173. if ($user->admin) {
  174. print '<td class="right"></td>';
  175. }
  176. print '</tr>'."\n";
  177. }
  178. print '<!-- '.$obj->module.'->'.$obj->perms.($obj->subperms ? '->'.$obj->subperms : '').' -->'."\n";
  179. print '<tr class="oddeven">';
  180. // Picto and label of module
  181. print '<td class="maxwidthonsmartphone tdoverflowonsmartphone">';
  182. //print img_object('', $picto, 'class="pictoobjectwidth"').' '.$objMod->getName();
  183. print '</td>';
  184. // Tick
  185. if ($obj->bydefault == 1) {
  186. print '<td class="center">';
  187. print '<a class="reposition" href="perms.php?pid='.$obj->id.'&action=remove&token='.newToken().'">';
  188. //print img_edit_remove();
  189. print img_picto('', 'switch_on');
  190. print '</a>';
  191. print '</td>';
  192. print '<td class="center">';
  193. //print img_picto($langs->trans("Active"), 'tick');
  194. print '</td>';
  195. } else {
  196. print '<td class="center">';
  197. print '<a class="reposition" href="perms.php?pid='.$obj->id.'&action=add&token='.newToken().'">';
  198. //print img_edit_add();
  199. print img_picto('', 'switch_off');
  200. print '</a>';
  201. print '</td>';
  202. print '<td class="center">';
  203. print '&nbsp;';
  204. print '</td>';
  205. }
  206. // Permission and tick
  207. $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)));
  208. print '<td>';
  209. print $permlabel;
  210. if (!empty($conf->global->MAIN_USE_ADVANCED_PERMS)) {
  211. if (preg_match('/_advance$/', $obj->perms)) {
  212. print ' <span class="opacitymedium">('.$langs->trans("AdvancedModeOnly").')</span>';
  213. }
  214. }
  215. print '</td>';
  216. // Permission id
  217. if ($user->admin) {
  218. print '<td class="right">';
  219. $htmltext = $langs->trans("ID").': '.$obj->id;
  220. $htmltext .= '<br>'.$langs->trans("Permission").': user->rights->'.$obj->module.'->'.$obj->perms.($obj->subperms ? '->'.$obj->subperms : '');
  221. print $form->textwithpicto('', $htmltext);
  222. //print '<span class="opacitymedium">'.$obj->id.'</span>';
  223. print '</td>';
  224. }
  225. print '</tr>'."\n";
  226. $i++;
  227. }
  228. } else {
  229. dol_print_error($db);
  230. }
  231. print '</table>';
  232. print '</div>';
  233. $parameters = array();
  234. $reshook = $hookmanager->executeHooks('insertExtraFooter', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  235. if ($reshook < 0) {
  236. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  237. }
  238. print dol_get_fiche_end();
  239. // End of page
  240. llxFooter();
  241. $db->close();