perms.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. if (! empty($conf->multicompany->enabled))
  65. {
  66. if (! empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE))
  67. $entity=(GETPOST('entity','int') ? GETPOST('entity','int') : $conf->entity);
  68. else
  69. $entity=(! empty($object->entity) ? $object->entity : $conf->entity);
  70. }
  71. // Initialize technical object to manage hooks of thirdparties. Note that conf->hooks_modules contains array array
  72. $hookmanager->initHooks(array('usercard','globalcard'));
  73. /**
  74. * Actions
  75. */
  76. $parameters=array('id'=>$socid);
  77. $reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
  78. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  79. if (empty($reshook)) {
  80. if ($action == 'addrights' && $caneditperms) {
  81. $edituser = new User($db);
  82. $edituser->fetch($object->id);
  83. $edituser->addrights($rights, $module, '', $entity);
  84. // Si on a touche a ses propres droits, on recharge
  85. if ($object->id == $user->id) {
  86. $user->clearrights();
  87. $user->getrights();
  88. $menumanager->loadMenu();
  89. }
  90. }
  91. if ($action == 'delrights' && $caneditperms) {
  92. $edituser = new User($db);
  93. $edituser->fetch($object->id);
  94. $edituser->delrights($rights, $module, '', $entity);
  95. // Si on a touche a ses propres droits, on recharge
  96. if ($object->id == $user->id) {
  97. $user->clearrights();
  98. $user->getrights();
  99. $menumanager->loadMenu();
  100. }
  101. }
  102. }
  103. /**
  104. * View
  105. */
  106. llxHeader('',$langs->trans("Permissions"));
  107. $form=new Form($db);
  108. $head = user_prepare_head($object);
  109. $title = $langs->trans("User");
  110. dol_fiche_head($head, 'rights', $title, -1, 'user');
  111. $db->begin();
  112. // Search all modules with permission and reload permissions def.
  113. $modules = array();
  114. $modulesdir = dolGetModulesDirs();
  115. foreach($modulesdir as $dir)
  116. {
  117. $handle=@opendir(dol_osencode($dir));
  118. if (is_resource($handle))
  119. {
  120. while (($file = readdir($handle))!==false)
  121. {
  122. if (is_readable($dir.$file) && substr($file, 0, 3) == 'mod' && substr($file, dol_strlen($file) - 10) == '.class.php')
  123. {
  124. $modName = substr($file, 0, dol_strlen($file) - 10);
  125. if ($modName)
  126. {
  127. include_once $dir.$file;
  128. $objMod = new $modName($db);
  129. // Load all lang files of module
  130. if (isset($objMod->langfiles) && is_array($objMod->langfiles))
  131. {
  132. foreach($objMod->langfiles as $domain)
  133. {
  134. $langs->load($domain);
  135. }
  136. }
  137. // Load all permissions
  138. if ($objMod->rights_class)
  139. {
  140. $ret=$objMod->insert_permissions(0, $entity);
  141. $modules[$objMod->rights_class]=$objMod;
  142. //print "modules[".$objMod->rights_class."]=$objMod;";
  143. }
  144. }
  145. }
  146. }
  147. }
  148. }
  149. $db->commit();
  150. // Lecture des droits utilisateurs
  151. $permsuser = array();
  152. $sql = "SELECT DISTINCT r.id, r.libelle, r.module";
  153. $sql.= " FROM ".MAIN_DB_PREFIX."rights_def as r,";
  154. $sql.= " ".MAIN_DB_PREFIX."user_rights as ur";
  155. $sql.= " WHERE ur.fk_id = r.id";
  156. $sql.= " AND ur.entity = ".$entity;
  157. $sql.= " AND ur.fk_user = ".$object->id;
  158. dol_syslog("get user perms", LOG_DEBUG);
  159. $result=$db->query($sql);
  160. if ($result)
  161. {
  162. $num = $db->num_rows($result);
  163. $i = 0;
  164. while ($i < $num)
  165. {
  166. $obj = $db->fetch_object($result);
  167. array_push($permsuser,$obj->id);
  168. $i++;
  169. }
  170. $db->free($result);
  171. }
  172. else
  173. {
  174. dol_print_error($db);
  175. }
  176. // Lecture des droits groupes
  177. $permsgroupbyentity = array();
  178. $sql = "SELECT DISTINCT r.id, r.libelle, r.module, gu.entity";
  179. $sql.= " FROM ".MAIN_DB_PREFIX."rights_def as r,";
  180. $sql.= " ".MAIN_DB_PREFIX."usergroup_rights as gr,";
  181. $sql.= " ".MAIN_DB_PREFIX."usergroup_user as gu";
  182. $sql.= " WHERE gr.fk_id = r.id";
  183. $sql.= " AND gr.entity = ".$entity;
  184. $sql.= " AND gr.fk_usergroup = gu.fk_usergroup";
  185. $sql.= " AND gu.fk_user = ".$object->id;
  186. dol_syslog("get user perms", LOG_DEBUG);
  187. $result=$db->query($sql);
  188. if ($result)
  189. {
  190. $num = $db->num_rows($result);
  191. $i = 0;
  192. while ($i < $num)
  193. {
  194. $obj = $db->fetch_object($result);
  195. if (! isset($permsgroupbyentity[$obj->entity]))
  196. $permsgroupbyentity[$obj->entity] = array();
  197. array_push($permsgroupbyentity[$obj->entity], $obj->id);
  198. $i++;
  199. }
  200. $db->free($result);
  201. }
  202. else
  203. {
  204. dol_print_error($db);
  205. }
  206. /*
  207. * Ecran ajout/suppression permission
  208. */
  209. $linkback = '';
  210. if ($user->rights->user->user->lire || $user->admin) {
  211. $linkback = '<a href="'.DOL_URL_ROOT.'/user/index.php">'.$langs->trans("BackToList").'</a>';
  212. }
  213. dol_banner_tab($object,'id',$linkback,$user->rights->user->user->lire || $user->admin);
  214. //print '<div class="underbanner clearboth"></div>';
  215. if ($user->admin) print info_admin($langs->trans("WarningOnlyPermissionOfActivatedModules"));
  216. // Show warning about external users
  217. if (empty($user->societe_id)) print info_admin(showModulesExludedForExternal($modules))."\n";
  218. $parameters=array('permsgroupbyentity'=>$permsgroupbyentity);
  219. $reshook=$hookmanager->executeHooks('insertExtraHeader',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
  220. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  221. print "\n";
  222. print '<table width="100%" class="noborder">';
  223. print '<tr class="liste_titre">';
  224. print '<td>'.$langs->trans("Module").'</td>';
  225. if ($caneditperms) print '<td>&nbsp</td>';
  226. print '<td align="center" width="24">&nbsp;</td>';
  227. print '<td>'.$langs->trans("Permissions").'</td>';
  228. print '</tr>'."\n";
  229. //print "xx".$conf->global->MAIN_USE_ADVANCED_PERMS;
  230. $sql = "SELECT r.id, r.libelle, r.module";
  231. $sql.= " FROM ".MAIN_DB_PREFIX."rights_def as r";
  232. $sql.= " WHERE r.libelle NOT LIKE 'tou%'"; // On ignore droits "tous"
  233. $sql.= " AND r.entity = " . $entity;
  234. if (empty($conf->global->MAIN_USE_ADVANCED_PERMS)) $sql.= " AND r.perms NOT LIKE '%_advance'"; // Hide advanced perms if option is disable
  235. $sql.= " ORDER BY r.module, r.id";
  236. $result=$db->query($sql);
  237. if ($result)
  238. {
  239. $num = $db->num_rows($result);
  240. $i = 0;
  241. $var = True;
  242. $oldmod='';
  243. while ($i < $num)
  244. {
  245. $obj = $db->fetch_object($result);
  246. // Si la ligne correspond a un module qui n'existe plus (absent de includes/module), on l'ignore
  247. if (empty($modules[$obj->module]))
  248. {
  249. $i++;
  250. continue;
  251. }
  252. if (isset($obj->module) && ($oldmod <> $obj->module))
  253. {
  254. $oldmod = $obj->module;
  255. $var = !$var;
  256. // Rupture detectee, on recupere objMod
  257. $objMod=$modules[$obj->module];
  258. $picto=($objMod->picto?$objMod->picto:'generic');
  259. if ($caneditperms && (empty($objMod->rights_admin_allowed) || empty($object->admin)))
  260. {
  261. // On affiche ligne pour modifier droits
  262. print '<tr '. $bc[$var].'>';
  263. print '<td class="maxwidthonsmartphone tdoverflowonsmartphone">'.img_object('',$picto,'class="pictoobjectwidth"').' '.$objMod->getName();
  264. print '<a name="'.$objMod->getName().'"></a></td>';
  265. print '<td align="center" class="nowrap">';
  266. 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>";
  267. print '/';
  268. 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>";
  269. print '</td>';
  270. print '<td colspan="2">&nbsp;</td>';
  271. print '</tr>'."\n";
  272. }
  273. }
  274. print '<tr '. $bc[$var].'>';
  275. // Picto and label of permission
  276. print '<td class="maxwidthonsmartphone tdoverflowonsmartphone">'.img_object('',$picto,'class="pictoobjectwidth"').' '.$objMod->getName().'</td>';
  277. // Permission and tick
  278. if (! empty($object->admin) && ! empty($objMod->rights_admin_allowed)) // Permission own because admin
  279. {
  280. if ($caneditperms)
  281. {
  282. print '<td align="center">'.img_picto($langs->trans("Administrator"),'star').'</td>';
  283. }
  284. print '<td align="center" class="nowrap">';
  285. print img_picto($langs->trans("Active"),'tick');
  286. print '</td>';
  287. }
  288. else if (in_array($obj->id, $permsuser)) // Permission own by user
  289. {
  290. if ($caneditperms)
  291. {
  292. 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>';
  293. }
  294. print '<td align="center" class="nowrap">';
  295. print img_picto($langs->trans("Active"),'tick');
  296. print '</td>';
  297. }
  298. else if (is_array($permsgroupbyentity[$entity]))
  299. {
  300. if (in_array($obj->id, $permsgroupbyentity[$entity])) // Permission own by group
  301. {
  302. if ($caneditperms)
  303. {
  304. print '<td align="center">';
  305. print $form->textwithtooltip($langs->trans("Inherited"),$langs->trans("PermissionInheritedFromAGroup"));
  306. print '</td>';
  307. }
  308. print '<td align="center" class="nowrap">';
  309. print img_picto($langs->trans("Active"),'tick');
  310. print '</td>';
  311. }
  312. else
  313. {
  314. // Do not own permission
  315. if ($caneditperms)
  316. {
  317. 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>';
  318. }
  319. print '<td>&nbsp</td>';
  320. }
  321. }
  322. else
  323. {
  324. // Do not own permission
  325. if ($caneditperms)
  326. {
  327. 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>';
  328. }
  329. print '<td>&nbsp</td>';
  330. }
  331. $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)));
  332. print '<td class="maxwidthonsmartphone">'.$permlabel.'</td>';
  333. print '</tr>'."\n";
  334. $i++;
  335. }
  336. }
  337. else dol_print_error($db);
  338. print '</table>';
  339. $parameters=array();
  340. $reshook=$hookmanager->executeHooks('insertExtraFooter',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
  341. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  342. dol_fiche_end();
  343. llxFooter();
  344. $db->close();