perms.php 14 KB

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