perms.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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-2015 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@capnetworks.com>
  7. * Copyright (C) 2012 Juanjo Menent <jmenent@2byte.es>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/user/perms.php
  24. * \brief Onglet user et permissions de la fiche utilisateur
  25. */
  26. require '../main.inc.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/usergroups.lib.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  30. $langs->load("users");
  31. $langs->load("admin");
  32. $id=GETPOST('id', 'int');
  33. $action=GETPOST('action', 'alpha');
  34. $confirm=GETPOST('confirm', 'alpha');
  35. $module=GETPOST('module', 'alpha');
  36. $rights=GETPOST('rights', 'int');
  37. if (! isset($id) || empty($id)) accessforbidden();
  38. // Defini si peux lire les permissions
  39. $canreaduser=($user->admin || $user->rights->user->user->lire);
  40. // Defini si peux modifier les autres utilisateurs et leurs permisssions
  41. $caneditperms=($user->admin || $user->rights->user->user->creer);
  42. // Advanced permissions
  43. if (! empty($conf->global->MAIN_USE_ADVANCED_PERMS))
  44. {
  45. $canreaduser=($user->admin || ($user->rights->user->user->lire && $user->rights->user->user_advance->readperms));
  46. $caneditselfperms=($user->id == $id && $user->rights->user->self_advance->writeperms);
  47. $caneditperms = (($caneditperms || $caneditselfperms) ? 1 : 0);
  48. }
  49. // Security check
  50. $socid=0;
  51. if (isset($user->societe_id) && $user->societe_id > 0) $socid = $user->societe_id;
  52. $feature2 = (($socid && $user->rights->user->self->creer)?'':'user');
  53. if ($user->id == $id && (empty($conf->global->MAIN_USE_ADVANCED_PERMS) || $user->rights->user->self_advance->readperms)) // A user can always read its own card if not advanced perms enabled, or if he has advanced perms
  54. {
  55. $feature2='';
  56. $canreaduser=1;
  57. }
  58. $result = restrictedArea($user, 'user', $id, 'user&user', $feature2);
  59. if ($user->id <> $id && ! $canreaduser) accessforbidden();
  60. $object = new User($db);
  61. $object->fetch($id, '', '', 1);
  62. $object->getrights();
  63. $entity=$conf->entity;
  64. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  65. $contextpage=array('usercard','userperms','globalcard');
  66. $hookmanager->initHooks($contextpage);
  67. /**
  68. * Actions
  69. */
  70. $parameters=array('id'=>$socid);
  71. $reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
  72. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  73. if (empty($reshook)) {
  74. if ($action == 'addrights' && $caneditperms) {
  75. $edituser = new User($db);
  76. $edituser->fetch($object->id);
  77. $edituser->addrights($rights, $module, '', $entity);
  78. // Si on a touche a ses propres droits, on recharge
  79. if ($object->id == $user->id) {
  80. $user->clearrights();
  81. $user->getrights();
  82. $menumanager->loadMenu();
  83. }
  84. }
  85. if ($action == 'delrights' && $caneditperms) {
  86. $edituser = new User($db);
  87. $edituser->fetch($object->id);
  88. $edituser->delrights($rights, $module, '', $entity);
  89. // Si on a touche a ses propres droits, on recharge
  90. if ($object->id == $user->id) {
  91. $user->clearrights();
  92. $user->getrights();
  93. $menumanager->loadMenu();
  94. }
  95. }
  96. }
  97. /**
  98. * View
  99. */
  100. llxHeader('',$langs->trans("Permissions"));
  101. $form=new Form($db);
  102. $head = user_prepare_head($object);
  103. $title = $langs->trans("User");
  104. dol_fiche_head($head, 'rights', $title, -1, 'user');
  105. $db->begin();
  106. // Search all modules with permission and reload permissions def.
  107. $modules = array();
  108. $modulesdir = dolGetModulesDirs();
  109. foreach($modulesdir as $dir)
  110. {
  111. $handle=@opendir(dol_osencode($dir));
  112. if (is_resource($handle))
  113. {
  114. while (($file = readdir($handle))!==false)
  115. {
  116. if (is_readable($dir.$file) && substr($file, 0, 3) == 'mod' && substr($file, dol_strlen($file) - 10) == '.class.php')
  117. {
  118. $modName = substr($file, 0, dol_strlen($file) - 10);
  119. if ($modName)
  120. {
  121. include_once $dir.$file;
  122. $objMod = new $modName($db);
  123. // Load all lang files of module
  124. if (isset($objMod->langfiles) && is_array($objMod->langfiles))
  125. {
  126. foreach($objMod->langfiles as $domain)
  127. {
  128. $langs->load($domain);
  129. }
  130. }
  131. // Load all permissions
  132. if ($objMod->rights_class)
  133. {
  134. $ret=$objMod->insert_permissions(0, $entity);
  135. $modules[$objMod->rights_class]=$objMod;
  136. //print "modules[".$objMod->rights_class."]=$objMod;";
  137. }
  138. }
  139. }
  140. }
  141. }
  142. }
  143. $db->commit();
  144. // Lecture des droits utilisateurs
  145. $permsuser = array();
  146. $sql = "SELECT DISTINCT ur.fk_id";
  147. $sql.= " FROM ".MAIN_DB_PREFIX."user_rights as ur";
  148. $sql.= " WHERE ur.entity = ".$entity;
  149. $sql.= " AND ur.fk_user = ".$object->id;
  150. dol_syslog("get user perms", LOG_DEBUG);
  151. $result=$db->query($sql);
  152. if ($result)
  153. {
  154. $num = $db->num_rows($result);
  155. $i = 0;
  156. while ($i < $num)
  157. {
  158. $obj = $db->fetch_object($result);
  159. array_push($permsuser,$obj->fk_id);
  160. $i++;
  161. }
  162. $db->free($result);
  163. }
  164. else
  165. {
  166. dol_print_error($db);
  167. }
  168. // Lecture des droits groupes
  169. $permsgroupbyentity = array();
  170. $sql = "SELECT DISTINCT gr.fk_id, gu.entity";
  171. $sql.= " FROM ".MAIN_DB_PREFIX."usergroup_rights as gr,";
  172. $sql.= " ".MAIN_DB_PREFIX."usergroup_user as gu";
  173. $sql.= " WHERE gr.entity = ".$entity;
  174. $sql.= " AND gr.fk_usergroup = gu.fk_usergroup";
  175. $sql.= " AND gu.fk_user = ".$object->id;
  176. dol_syslog("get user perms", LOG_DEBUG);
  177. $result=$db->query($sql);
  178. if ($result)
  179. {
  180. $num = $db->num_rows($result);
  181. $i = 0;
  182. while ($i < $num)
  183. {
  184. $obj = $db->fetch_object($result);
  185. if (! isset($permsgroupbyentity[$obj->entity]))
  186. $permsgroupbyentity[$obj->entity] = array();
  187. array_push($permsgroupbyentity[$obj->entity], $obj->fk_id);
  188. $i++;
  189. }
  190. $db->free($result);
  191. }
  192. else
  193. {
  194. dol_print_error($db);
  195. }
  196. /*
  197. * Ecran ajout/suppression permission
  198. */
  199. $linkback = '';
  200. if ($user->rights->user->user->lire || $user->admin) {
  201. $linkback = '<a href="'.DOL_URL_ROOT.'/user/index.php">'.$langs->trans("BackToList").'</a>';
  202. }
  203. dol_banner_tab($object,'id',$linkback,$user->rights->user->user->lire || $user->admin);
  204. //print '<div class="underbanner clearboth"></div>';
  205. if ($user->admin) print info_admin($langs->trans("WarningOnlyPermissionOfActivatedModules"));
  206. // Show warning about external users
  207. if (empty($user->societe_id)) print info_admin(showModulesExludedForExternal($modules))."\n";
  208. $parameters=array('permsgroupbyentity'=>$permsgroupbyentity);
  209. $reshook=$hookmanager->executeHooks('insertExtraHeader',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
  210. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  211. print "\n";
  212. print '<table width="100%" class="noborder">';
  213. print '<tr class="liste_titre">';
  214. print '<td>'.$langs->trans("Module").'</td>';
  215. if ($caneditperms) print '<td>&nbsp</td>';
  216. print '<td align="center" width="24">&nbsp;</td>';
  217. print '<td>'.$langs->trans("Permissions").'</td>';
  218. print '</tr>'."\n";
  219. //print "xx".$conf->global->MAIN_USE_ADVANCED_PERMS;
  220. $sql = "SELECT r.id, r.libelle, r.module";
  221. $sql.= " FROM ".MAIN_DB_PREFIX."rights_def as r";
  222. $sql.= " WHERE r.libelle NOT LIKE 'tou%'"; // On ignore droits "tous"
  223. $sql.= " AND r.entity = " . $entity;
  224. if (empty($conf->global->MAIN_USE_ADVANCED_PERMS)) $sql.= " AND r.perms NOT LIKE '%_advance'"; // Hide advanced perms if option is disable
  225. $sql.= " ORDER BY r.module, r.id";
  226. $result=$db->query($sql);
  227. if ($result)
  228. {
  229. $num = $db->num_rows($result);
  230. $i = 0;
  231. $oldmod='';
  232. while ($i < $num)
  233. {
  234. $obj = $db->fetch_object($result);
  235. // Si la ligne correspond a un module qui n'existe plus (absent de includes/module), on l'ignore
  236. if (empty($modules[$obj->module]))
  237. {
  238. $i++;
  239. continue;
  240. }
  241. if (isset($obj->module) && ($oldmod <> $obj->module))
  242. {
  243. $oldmod = $obj->module;
  244. // Break detected, we get objMod
  245. $objMod=$modules[$obj->module];
  246. $picto=($objMod->picto?$objMod->picto:'generic');
  247. // Show break line
  248. print '<tr class="oddeven trforbreak">';
  249. print '<td class="maxwidthonsmartphone tdoverflowonsmartphone">'.img_object('',$picto,'class="pictoobjectwidth"').' '.$objMod->getName();
  250. print '<a name="'.$objMod->getName().'"></a></td>';
  251. print '<td align="center" class="nowrap">';
  252. if ($caneditperms && empty($objMod->rights_admin_allowed) || empty($object->admin))
  253. {
  254. print '<a class="reposition" title="'.dol_escape_htmltag($langs->trans("All")).'" alt="'.dol_escape_htmltag($langs->trans("All")).'" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=addrights&amp;entity='.$entity.'&amp;module='.$obj->module.'">'.$langs->trans("All")."</a>";
  255. print '/';
  256. print '<a class="reposition" title="'.dol_escape_htmltag($langs->trans("None")).'" alt="'.dol_escape_htmltag($langs->trans("None")).'" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=delrights&amp;entity='.$entity.'&amp;module='.$obj->module.'">'.$langs->trans("None")."</a>";
  257. }
  258. print '</td>';
  259. print '<td colspan="2">&nbsp;</td>';
  260. print '</tr>'."\n";
  261. }
  262. print '<tr class="oddeven">';
  263. // Picto and label of permission
  264. print '<td class="maxwidthonsmartphone tdoverflowonsmartphone">'.img_object('',$picto,'class="pictoobjectwidth"').' '.$objMod->getName().'</td>';
  265. // Permission and tick
  266. if (! empty($object->admin) && ! empty($objMod->rights_admin_allowed)) // Permission granted because admin
  267. {
  268. if ($caneditperms)
  269. {
  270. print '<td align="center">'.img_picto($langs->trans("Administrator"),'star').'</td>';
  271. }
  272. print '<td align="center" class="nowrap">';
  273. print img_picto($langs->trans("Active"),'tick');
  274. print '</td>';
  275. }
  276. else if (in_array($obj->id, $permsuser)) // Permission granted by user
  277. {
  278. if ($caneditperms)
  279. {
  280. print '<td align="center"><a class="reposition" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=delrights&amp;entity='.$entity.'&amp;rights='.$obj->id.'">'.img_edit_remove($langs->trans("Remove")).'</a></td>';
  281. }
  282. print '<td align="center" class="nowrap">';
  283. print img_picto($langs->trans("Active"),'tick');
  284. print '</td>';
  285. }
  286. else if (is_array($permsgroupbyentity[$entity]))
  287. {
  288. if (in_array($obj->id, $permsgroupbyentity[$entity])) // Permission granted by group
  289. {
  290. if ($caneditperms)
  291. {
  292. print '<td align="center">';
  293. print $form->textwithtooltip($langs->trans("Inherited"),$langs->trans("PermissionInheritedFromAGroup"));
  294. print '</td>';
  295. }
  296. print '<td align="center" class="nowrap">';
  297. print img_picto($langs->trans("Active"),'tick');
  298. print '</td>';
  299. }
  300. else
  301. {
  302. // Do not own permission
  303. if ($caneditperms)
  304. {
  305. print '<td align="center"><a class="reposition" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=addrights&amp;entity='.$entity.'&amp;rights='.$obj->id.'">'.img_edit_add($langs->trans("Add")).'</a></td>';
  306. }
  307. print '<td>&nbsp</td>';
  308. }
  309. }
  310. else
  311. {
  312. // Do not own permission
  313. if ($caneditperms)
  314. {
  315. print '<td align="center"><a class="reposition" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=addrights&amp;entity='.$entity.'&amp;rights='.$obj->id.'">'.img_edit_add($langs->trans("Add")).'</a></td>';
  316. }
  317. print '<td>&nbsp</td>';
  318. }
  319. $permlabel=($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->libelle)));
  320. print '<td class="maxwidthonsmartphone">'.$permlabel.'</td>';
  321. print '</tr>'."\n";
  322. $i++;
  323. }
  324. }
  325. else dol_print_error($db);
  326. print '</table>';
  327. $parameters=array();
  328. $reshook=$hookmanager->executeHooks('insertExtraFooter',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
  329. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  330. dol_fiche_end();
  331. llxFooter();
  332. $db->close();