usermanagement_userstatus.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Specific to the addon 'user management by profile'
  18. * Was developed for testing purposes only
  19. *
  20. * @author Erwan Taloc <erwan.taloc@combodo.com>
  21. * @author Romain Quetiez <romain.quetiez@combodo.com>
  22. * @author Denis Flaven <denis.flaven@combodo.com>
  23. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  24. */
  25. require_once('../application/application.inc.php');
  26. require_once('../application/itopwebpage.class.inc.php');
  27. require_once('../application/startup.inc.php');
  28. function ComputeObjectProjections($oPage, $oObject)
  29. {
  30. // Load the classes for a further usage
  31. //
  32. $aClasses = MetaModel::GetClasses();
  33. // Load the dimensions for a further usage
  34. //
  35. $aDimensions = array();
  36. $oDimensionSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_Dimensions"));
  37. while ($oDimension = $oDimensionSet->Fetch())
  38. {
  39. $aDimensions[$oDimension->GetKey()] = $oDimension;
  40. }
  41. // Load the class projections for a further usage
  42. //
  43. $aClassProj = array();
  44. $oClassProjSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_ClassProjection"));
  45. while ($oClassProj = $oClassProjSet->Fetch())
  46. {
  47. $aClassProjs[$oClassProj->Get('class')][$oClassProj->Get('dimensionid')] = $oClassProj;
  48. }
  49. // Setup display structure
  50. //
  51. $aDisplayConfig = array();
  52. foreach ($aDimensions as $iDimension => $oDimension)
  53. {
  54. $aDisplayConfig['dim'.$oDimension->GetKey()] = array('label' => $oDimension->GetName(), 'description' => $oDimension->Get('description'));
  55. }
  56. // Load objects
  57. //
  58. $aDisplayData = array();
  59. $sClass = get_class($oObject);
  60. $aObjectProj = array();
  61. foreach ($aDimensions as $iDimension => $oDimension)
  62. {
  63. // #@# to be moved, may be time consuming
  64. $oDimension->CheckProjectionSpec($aClassProjs[$sClass][$iDimension], $sClass);
  65. $aValues = $aClassProjs[$sClass][$iDimension]->ProjectObject($oObject);
  66. if (is_null($aValues))
  67. {
  68. $sValues = htmlentities(Dict::S('UI:UserManagement:AnyObject'));
  69. }
  70. else
  71. {
  72. $sValues = implode(', ', $aValues);
  73. }
  74. $oObjectProj['dim'.$oDimension->GetKey()] = $sValues;
  75. }
  76. $aDisplayData[] = $oObjectProj;
  77. $oPage->table($aDisplayConfig, $aDisplayData);
  78. }
  79. function ComputeUserProjections($oPage, $oUser)
  80. {
  81. // Load the profiles for a further usage
  82. //
  83. $aProfiles = array();
  84. $oProfileSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_Profiles"));
  85. while ($oProfile = $oProfileSet->Fetch())
  86. {
  87. $aProfiles[$oProfile->GetKey()] = $oProfile;
  88. }
  89. // Load the dimensions for a further usage
  90. //
  91. $aDimensions = array();
  92. $oDimensionSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_Dimensions"));
  93. while ($oDimension = $oDimensionSet->Fetch())
  94. {
  95. $aDimensions[$oDimension->GetKey()] = $oDimension;
  96. }
  97. // Load the profile projections for a further usage
  98. //
  99. $aProPro = array();
  100. $oProProSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_ProfileProjection"));
  101. while ($oProPro = $oProProSet->Fetch())
  102. {
  103. $aProPros[$oProPro->Get('profileid')][$oProPro->Get('dimensionid')] = $oProPro;
  104. }
  105. // Setup display structure
  106. //
  107. $aDisplayConfig = array();
  108. $aDisplayConfig['profile'] = array('label' => Dict::S('UI:UserManagement:Profile'), 'description' => Dict::S('UI:UserManagement:Profile+'));
  109. foreach ($aDimensions as $iDimension => $oDimension)
  110. {
  111. $aDisplayConfig['dim'.$oDimension->GetKey()] = array('label' => $oDimension->GetName(), 'description' => $oDimension->Get('description'));
  112. }
  113. // Create a record per profile
  114. //
  115. $aDisplayData = array();
  116. $oUserProfileSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_UserProfile WHERE userid = :user->id"), array(), array('user' => $oUser));
  117. while ($oUserProfile = $oUserProfileSet->Fetch())
  118. {
  119. $iProfile = $oUserProfile->Get('profileid');
  120. $oProfile = $aProfiles[$iProfile];
  121. $aUserProfileProj = array();
  122. $aUserProfileProj['profile'] = $oProfile->GetName();
  123. foreach ($aDimensions as $iDimension => $oDimension)
  124. {
  125. // #@# to be moved, may be time consuming
  126. $oDimension->CheckProjectionSpec($aProPros[$iProfile][$iDimension], get_class($oUser));
  127. $aValues = $aProPros[$iProfile][$iDimension]->ProjectUser($oUser);
  128. if (is_null($aValues))
  129. {
  130. $sValues = htmlentities(Dict::S('UI:UserManagement:AnyObject'));
  131. }
  132. else
  133. {
  134. $sValues = implode(', ', $aValues);
  135. }
  136. $aUserProfileProj['dim'.$oDimension->GetKey()] = $sValues;
  137. }
  138. $aDisplayData[] = $aUserProfileProj;
  139. }
  140. $oPage->table($aDisplayConfig, $aDisplayData);
  141. }
  142. function ComputeUserRights($oPage, $oUser, $oObject)
  143. {
  144. // Set the stage
  145. //
  146. $iUser = $oUser->GetKey();
  147. $sClass = get_class($oObject);
  148. $iPKey = $oObject->GetKey();
  149. $oInstances = DBObjectSet::FromArray($sClass, array($oObject));
  150. $aPermissions = array(
  151. UR_ALLOWED_NO => '<span style="background-color: #ffdddd;">UR_ALLOWED_NO</span>',
  152. UR_ALLOWED_YES => '<span style="background-color: #ddffdd;">UR_ALLOWED_YES</span>',
  153. UR_ALLOWED_DEPENDS => '<span style="">UR_ALLOWED_DEPENDS</span>',
  154. );
  155. $aActions = array(
  156. UR_ACTION_READ => Dict::S('UI:UserManagement:Action:Read'),
  157. UR_ACTION_MODIFY => Dict::S('UI:UserManagement:Action:Modify'),
  158. UR_ACTION_DELETE => Dict::S('UI:UserManagement:Action:Delete'),
  159. UR_ACTION_BULK_READ => Dict::S('UI:UserManagement:Action:BulkRead'),
  160. UR_ACTION_BULK_MODIFY => Dict::S('UI:UserManagement:Action:BulkModify'),
  161. UR_ACTION_BULK_DELETE => Dict::S('UI:UserManagement:Action:BulkDelete'),
  162. );
  163. $aAttributeActions = array(
  164. UR_ACTION_READ => Dict::S('UI:UserManagement:Action:Read'),
  165. UR_ACTION_MODIFY => Dict::S('UI:UserManagement:Action:Modify'),
  166. UR_ACTION_BULK_READ => Dict::S('UI:UserManagement:Action:BulkRead'),
  167. UR_ACTION_BULK_MODIFY => Dict::S('UI:UserManagement:Action:BulkModify'),
  168. );
  169. // Determine allowed actions for the object
  170. //
  171. $aDisplayData = array();
  172. foreach($aActions as $iActionCode => $sActionDesc)
  173. {
  174. $iPermission = UserRights::IsActionAllowed($sClass, $iActionCode, $oInstances, $iUser);
  175. $aDisplayData[] = array(
  176. 'action' => $sActionDesc,
  177. 'permission' => $aPermissions[$iPermission],
  178. );
  179. }
  180. $aDisplayConfig = array();
  181. $aDisplayConfig['action'] = array('label' => Dict::S('UI:UserManagement:Action'), 'description' => Dict::S('UI:UserManagement:Action+'));
  182. $aDisplayConfig['permission'] = array('label' => Dict::S('UI:UserManagement:Permission'), 'description' => Dict::S('UI:UserManagement:Permission+'));
  183. $oPage->p('<h3>'.Dict::S('UI:UserManagement:Actions').'</h3>');
  184. $oPage->table($aDisplayConfig, $aDisplayData);
  185. // Determine allowed actions for the object
  186. //
  187. $aDisplayData = array();
  188. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  189. {
  190. if (!$oAttDef->IsDirectField()) continue;
  191. foreach($aAttributeActions as $iActionCode => $sActionDesc)
  192. {
  193. $iPermission = UserRights::IsActionAllowedOnAttribute($sClass, $sAttCode, $iActionCode, $oInstances, $iUser);
  194. $aDisplayData[] = array(
  195. 'attribute' => $sAttCode,
  196. 'action' => $sActionDesc,
  197. 'permission' => $aPermissions[$iPermission],
  198. );
  199. }
  200. }
  201. $oPage->p('<h3>'.Dict::S('UI:UserManagement:Attributes').'</h3>');
  202. if (count($aDisplayData) > 0)
  203. {
  204. $aDisplayConfig = array();
  205. $aDisplayConfig['attribute'] = array('label' => 'Attribute', 'description' => '');
  206. $aDisplayConfig['action'] = array('label' => 'Action', 'description' => '');
  207. $aDisplayConfig['permission'] = array('label' => 'Permission', 'description' => '');
  208. $oPage->table($aDisplayConfig, $aDisplayData);
  209. }
  210. else
  211. {
  212. $oPage->p('<em>none</em>');
  213. }
  214. // Determine allowed stimuli
  215. //
  216. $aDisplayData = array();
  217. foreach(MetaModel::EnumStimuli($sClass) as $sStimulusCode => $oStimulus)
  218. {
  219. $iPermission = UserRights::IsStimulusAllowed($sClass, $sStimulusCode, $oInstances, $iUser);
  220. $aDisplayData[] = array(
  221. 'stimulus' => $sStimulusCode,
  222. 'permission' => $aPermissions[$iPermission],
  223. );
  224. }
  225. $oPage->p('<h3>Stimuli</h3>');
  226. if (count($aDisplayData) > 0)
  227. {
  228. $aDisplayConfig = array();
  229. $aDisplayConfig['stimulus'] = array('label' => 'Stimulus', 'description' => '');
  230. $aDisplayConfig['permission'] = array('label' => 'Permission', 'description' => '');
  231. $oPage->table($aDisplayConfig, $aDisplayData);
  232. }
  233. else
  234. {
  235. $oPage->p('<em>none</em>');
  236. }
  237. }
  238. require_once('../application/loginwebpage.class.inc.php');
  239. LoginWebPage::DoLogin(); // Check user rights and prompt if needed
  240. // Display the menu on the left
  241. $oContext = new UserContext();
  242. $oAppContext = new ApplicationContext();
  243. $iActiveNodeId = utils::ReadParam('menu', -1);
  244. $currentOrganization = utils::ReadParam('org_id', 1);
  245. $iUser = utils::ReadParam('user_id', -1);
  246. $sObjectClass = utils::ReadParam('object_class', '');
  247. $iObjectId = utils::ReadParam('object_id', 0);
  248. $oPage = new iTopWebPage("iTop user management - user status", $currentOrganization);
  249. $oPage->no_cache();
  250. if ($iUser == -1)
  251. {
  252. $oPage->p('Missing parameter "user_id" - current user is '.UserRights::GetUserId());
  253. }
  254. else
  255. {
  256. $oPage->p('<h2>How are the user rights computed?</h2>');
  257. $oPage->p('<h3>1st, find the profiles that apply</h3>');
  258. $oPage->p('<p>Project the current object in every existing dimension</p>');
  259. $oPage->p('<p>Project the observed profile in every existing dimension (might depend on the user)</p>');
  260. $oPage->p('<p>If an overlap is found in any dimension, then the profile applies</p>');
  261. $oPage->p('<h3>2nd, interpret the profiles</h3>');
  262. $oPage->p('<p>Note: granting rights for specific attributes is not fully implemented. It is still not taking into account the inheritance of rights AND the UI will not take that information into account!</p>');
  263. $oPage->p('<p>Actions: looks into URP_ActionGrant for a permission (yes or no) and goes up into the class hierarchy until an answer is found, defaults to <em>no</em></p>');
  264. $oPage->p('<p>Stimuli: looks into URP_StimulusGrant for a permission (yes or no), defaults to <em>no</em></p>');
  265. $oPage->p('<h3>3rd, keep the most permissive one</h3>');
  266. $oPage->p('<p>If one profile says YES, then the answer is YES</p>');
  267. $oUser = MetaModel::GetObject('URP_Users', $iUser);
  268. $oPage->p('<h2>Projections for user '.$oUser->GetName().'</h2>');
  269. ComputeUserProjections($oPage, $oUser);
  270. if (strlen($sObjectClass) != 0)
  271. {
  272. $oObject = MetaModel::GetObject($sObjectClass, $iObjectId);
  273. $oPage->p('<h2>Projections for object '.$oObject->GetName().'</h2>');
  274. ComputeObjectProjections($oPage, $oObject);
  275. $oPage->p('<h2>Resulting rights</h2>');
  276. ComputeUserRights($oPage, $oUser, $oObject);
  277. }
  278. }
  279. $oPage->output();
  280. ?>