usermanagement_userstatus.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. require_once('../application/application.inc.php');
  3. require_once('../application/itopwebpage.class.inc.php');
  4. require_once('../application/startup.inc.php');
  5. function ComputeObjectProjections($oPage, $oObject)
  6. {
  7. // Load the classes for a further usage
  8. //
  9. $aClasses = MetaModel::GetClasses();
  10. // Load the dimensions for a further usage
  11. //
  12. $aDimensions = array();
  13. $oDimensionSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_Dimensions"));
  14. while ($oDimension = $oDimensionSet->Fetch())
  15. {
  16. $aDimensions[$oDimension->GetKey()] = $oDimension;
  17. }
  18. // Load the class projections for a further usage
  19. //
  20. $aClassProj = array();
  21. $oClassProjSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_ClassProjection"));
  22. while ($oClassProj = $oClassProjSet->Fetch())
  23. {
  24. $aClassProjs[$oClassProj->Get('class')][$oClassProj->Get('dimensionid')] = $oClassProj;
  25. }
  26. // Setup display structure
  27. //
  28. $aDisplayConfig = array();
  29. foreach ($aDimensions as $iDimension => $oDimension)
  30. {
  31. $aDisplayConfig['dim'.$oDimension->GetKey()] = array('label' => $oDimension->GetName(), 'description' => $oDimension->Get('description'));
  32. }
  33. // Load objects
  34. //
  35. $aDisplayData = array();
  36. $sClass = get_class($oObject);
  37. $aObjectProj = array();
  38. foreach ($aDimensions as $iDimension => $oDimension)
  39. {
  40. // #@# to be moved, may be time consuming
  41. $oDimension->CheckProjectionSpec($aClassProjs[$sClass][$iDimension], $sClass);
  42. $aValues = $aClassProjs[$sClass][$iDimension]->ProjectObject($oObject);
  43. if (is_null($aValues))
  44. {
  45. $sValues = htmlentities('<any>');
  46. }
  47. else
  48. {
  49. $sValues = implode(', ', $aValues);
  50. }
  51. $oObjectProj['dim'.$oDimension->GetKey()] = $sValues;
  52. }
  53. $aDisplayData[] = $oObjectProj;
  54. $oPage->table($aDisplayConfig, $aDisplayData);
  55. }
  56. function ComputeUserProjections($oPage, $oUser)
  57. {
  58. // Load the profiles for a further usage
  59. //
  60. $aProfiles = array();
  61. $oProfileSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_Profiles"));
  62. while ($oProfile = $oProfileSet->Fetch())
  63. {
  64. $aProfiles[$oProfile->GetKey()] = $oProfile;
  65. }
  66. // Load the dimensions for a further usage
  67. //
  68. $aDimensions = array();
  69. $oDimensionSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_Dimensions"));
  70. while ($oDimension = $oDimensionSet->Fetch())
  71. {
  72. $aDimensions[$oDimension->GetKey()] = $oDimension;
  73. }
  74. // Load the profile projections for a further usage
  75. //
  76. $aProPro = array();
  77. $oProProSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_ProfileProjection"));
  78. while ($oProPro = $oProProSet->Fetch())
  79. {
  80. $aProPros[$oProPro->Get('profileid')][$oProPro->Get('dimensionid')] = $oProPro;
  81. }
  82. // Setup display structure
  83. //
  84. $aDisplayConfig = array();
  85. $aDisplayConfig['profile'] = array('label' => 'Profile', 'description' => 'Profile in which the projection is specified');
  86. foreach ($aDimensions as $iDimension => $oDimension)
  87. {
  88. $aDisplayConfig['dim'.$oDimension->GetKey()] = array('label' => $oDimension->GetName(), 'description' => $oDimension->Get('description'));
  89. }
  90. // Create a record per profile
  91. //
  92. $aDisplayData = array();
  93. $oUserProfileSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_UserProfile WHERE userid = :user->id"), array(), array('user' => $oUser));
  94. while ($oUserProfile = $oUserProfileSet->Fetch())
  95. {
  96. $iProfile = $oUserProfile->Get('profileid');
  97. $oProfile = $aProfiles[$iProfile];
  98. $aUserProfileProj = array();
  99. $aUserProfileProj['profile'] = $oProfile->GetName();
  100. foreach ($aDimensions as $iDimension => $oDimension)
  101. {
  102. // #@# to be moved, may be time consuming
  103. $oDimension->CheckProjectionSpec($aProPros[$iProfile][$iDimension], get_class($oUser));
  104. $aValues = $aProPros[$iProfile][$iDimension]->ProjectUser($oUser);
  105. if (is_null($aValues))
  106. {
  107. $sValues = htmlentities('<any>');
  108. }
  109. else
  110. {
  111. $sValues = implode(', ', $aValues);
  112. }
  113. $aUserProfileProj['dim'.$oDimension->GetKey()] = $sValues;
  114. }
  115. $aDisplayData[] = $aUserProfileProj;
  116. }
  117. $oPage->table($aDisplayConfig, $aDisplayData);
  118. }
  119. function ComputeUserRights($oPage, $oUser, $oObject)
  120. {
  121. // Set the stage
  122. //
  123. $iUser = $oUser->GetKey();
  124. $sClass = get_class($oObject);
  125. $iPKey = $oObject->GetKey();
  126. $oInstances = DBObjectSet::FromArray($sClass, array($oObject));
  127. $aPermissions = array(
  128. UR_ALLOWED_NO => '<span style="background-color: #ffdddd;">UR_ALLOWED_NO</span>',
  129. UR_ALLOWED_YES => '<span style="background-color: #ddffdd;">UR_ALLOWED_YES</span>',
  130. UR_ALLOWED_DEPENDS => '<span style="">UR_ALLOWED_DEPENDS</span>',
  131. );
  132. $aActions = array(
  133. UR_ACTION_READ => 'Read',
  134. UR_ACTION_MODIFY => 'Modify',
  135. UR_ACTION_DELETE => 'Delete',
  136. UR_ACTION_BULK_READ => 'Bulk Read',
  137. UR_ACTION_BULK_MODIFY => 'Bulk Modify',
  138. UR_ACTION_BULK_DELETE => 'Bulk Delete',
  139. );
  140. $aAttributeActions = array(
  141. UR_ACTION_READ => 'Read',
  142. UR_ACTION_MODIFY => 'Modify',
  143. UR_ACTION_BULK_READ => 'Bulk Read',
  144. UR_ACTION_BULK_MODIFY => 'Bulk Modify',
  145. );
  146. // Determine allowed actions for the object
  147. //
  148. $aDisplayData = array();
  149. foreach($aActions as $iActionCode => $sActionDesc)
  150. {
  151. $iPermission = UserRights::IsActionAllowed($sClass, $iActionCode, $oInstances, $iUser);
  152. $aDisplayData[] = array(
  153. 'action' => $sActionDesc,
  154. 'permission' => $aPermissions[$iPermission],
  155. );
  156. }
  157. $aDisplayConfig = array();
  158. $aDisplayConfig['action'] = array('label' => 'Action', 'description' => '');
  159. $aDisplayConfig['permission'] = array('label' => 'Permission', 'description' => '');
  160. $oPage->p('<h3>Actions</h3>');
  161. $oPage->table($aDisplayConfig, $aDisplayData);
  162. // Determine allowed actions for the object
  163. //
  164. $aDisplayData = array();
  165. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  166. {
  167. if (!$oAttDef->IsDirectField()) continue;
  168. foreach($aAttributeActions as $iActionCode => $sActionDesc)
  169. {
  170. $iPermission = UserRights::IsActionAllowedOnAttribute($sClass, $sAttCode, $iActionCode, $oInstances, $iUser);
  171. $aDisplayData[] = array(
  172. 'attribute' => $sAttCode,
  173. 'action' => $sActionDesc,
  174. 'permission' => $aPermissions[$iPermission],
  175. );
  176. }
  177. }
  178. $oPage->p('<h3>Attributes</h3>');
  179. if (count($aDisplayData) > 0)
  180. {
  181. $aDisplayConfig = array();
  182. $aDisplayConfig['attribute'] = array('label' => 'Attribute', 'description' => '');
  183. $aDisplayConfig['action'] = array('label' => 'Action', 'description' => '');
  184. $aDisplayConfig['permission'] = array('label' => 'Permission', 'description' => '');
  185. $oPage->table($aDisplayConfig, $aDisplayData);
  186. }
  187. else
  188. {
  189. $oPage->p('<em>none</em>');
  190. }
  191. // Determine allowed stimuli
  192. //
  193. $aDisplayData = array();
  194. foreach(MetaModel::EnumStimuli($sClass) as $sStimulusCode => $oStimulus)
  195. {
  196. $iPermission = UserRights::IsStimulusAllowed($sClass, $sStimulusCode, $oInstances, $iUser);
  197. $aDisplayData[] = array(
  198. 'stimulus' => $sStimulusCode,
  199. 'permission' => $aPermissions[$iPermission],
  200. );
  201. }
  202. $oPage->p('<h3>Stimuli</h3>');
  203. if (count($aDisplayData) > 0)
  204. {
  205. $aDisplayConfig = array();
  206. $aDisplayConfig['stimulus'] = array('label' => 'Stimulus', '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. }
  215. require_once('../application/loginwebpage.class.inc.php');
  216. LoginWebPage::DoLogin(); // Check user rights and prompt if needed
  217. // Display the menu on the left
  218. $oContext = new UserContext();
  219. $oAppContext = new ApplicationContext();
  220. $iActiveNodeId = utils::ReadParam('menu', -1);
  221. $currentOrganization = utils::ReadParam('org_id', 1);
  222. $iUser = utils::ReadParam('user_id', -1);
  223. $sObjectClass = utils::ReadParam('object_class', '');
  224. $iObjectId = utils::ReadParam('object_id', 0);
  225. $oPage = new iTopWebPage("iTop user management - user status", $currentOrganization);
  226. $oPage->no_cache();
  227. if ($iUser == -1)
  228. {
  229. $oPage->p('Missing parameter "user_id" - current user is '.UserRights::GetUserId());
  230. }
  231. else
  232. {
  233. $oPage->p('<h2>How is it computing the user rights?</h2>');
  234. $oPage->p('<h3>1st, find the profiles that apply</h3>');
  235. $oPage->p('<p>Project the current object in every existing dimension</p>');
  236. $oPage->p('<p>Project the observed profile in every existing dimension (might depend on the user)</p>');
  237. $oPage->p('<p>If an overlap is found in any dimension, then the profile applies</p>');
  238. $oPage->p('<h3>2nd, interpret the profiles</h3>');
  239. $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>');
  240. $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>');
  241. $oPage->p('<p>Stimuli: looks into URP_StimulusGrant for a permission (yes or no), defaults to <em>no</em></p>');
  242. $oPage->p('<h3>3rd, keep the most permissive one</h3>');
  243. $oPage->p('<p>If one profile says YES, then the answer is YES</p>');
  244. $oUser = MetaModel::GetObject('URP_Users', $iUser);
  245. $oPage->p('<h2>Projections for user '.$oUser->GetName().'</h2>');
  246. ComputeUserProjections($oPage, $oUser);
  247. if (strlen($sObjectClass) != 0)
  248. {
  249. $oObject = MetaModel::GetObject($sObjectClass, $iObjectId);
  250. $oPage->p('<h2>Projections for object '.$oObject->GetName().'</h2>');
  251. ComputeObjectProjections($oPage, $oObject);
  252. $oPage->p('<h2>Resulting rights</h2>');
  253. ComputeUserRights($oPage, $oUser, $oObject);
  254. }
  255. }
  256. $oPage->output();
  257. ?>