perms.php 9.2 KB

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