userrightsprofile.class.inc.php 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  1. <?php
  2. /**
  3. * UserRightsProfile
  4. * User management Module, basing the right on profiles and a matrix (similar to UserRightsMatrix, but profiles and other decorations have been added)
  5. *
  6. * @package iTopORM
  7. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  8. * @author Denis Flaven <denisflave@free.fr>
  9. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  10. * @link www.itop.com
  11. * @since 1.0
  12. * @version 1.1.1.1 $
  13. */
  14. define('ADMIN_PROFILE_ID', 1);
  15. class UserRightsBaseClass extends cmdbAbstractObject
  16. {
  17. }
  18. class URP_Users extends UserRightsBaseClass
  19. {
  20. public static function Init()
  21. {
  22. $aParams = array
  23. (
  24. "category" => "addon/userrights",
  25. "name" => "user",
  26. "description" => "users and credentials",
  27. "key_type" => "autoincrement",
  28. "key_label" => "",
  29. "name_attcode" => "login",
  30. "state_attcode" => "",
  31. "reconc_keys" => array(),
  32. "db_table" => "priv_urp_users",
  33. "db_key_field" => "id",
  34. "db_finalclass_field" => "",
  35. "display_template" => "",
  36. );
  37. MetaModel::Init_Params($aParams);
  38. //MetaModel::Init_InheritAttributes();
  39. MetaModel::Init_AddAttribute(new AttributeExternalKey("userid", array("targetclass"=>"bizPerson", "label"=>"Contact (person)", "description"=>"Personal details from the business data", "allowed_values"=>null, "sql"=>"userid", "is_null_allowed"=>true, "depends_on"=>array())));
  40. MetaModel::Init_AddAttribute(new AttributeExternalField("last_name", array("label"=>"Last name", "description"=>"Name of the corresponding contact", "allowed_values"=>null, "extkey_attcode"=> 'userid', "target_attcode"=>"name")));
  41. MetaModel::Init_AddAttribute(new AttributeExternalField("first_name", array("label"=>"First name", "description"=>"First name of the corresponding contact", "allowed_values"=>null, "extkey_attcode"=> 'userid', "target_attcode"=>"first_name")));
  42. MetaModel::Init_AddAttribute(new AttributeExternalField("email", array("label"=>"Email", "description"=>"Email of the corresponding contact", "allowed_values"=>null, "extkey_attcode"=> 'userid', "target_attcode"=>"email")));
  43. MetaModel::Init_AddAttribute(new AttributeString("login", array("label"=>"Login", "description"=>"user identification string", "allowed_values"=>null, "sql"=>"login", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  44. MetaModel::Init_AddAttribute(new AttributePassword("password", array("label"=>"Password", "description"=>"user authentication string", "allowed_values"=>null, "sql"=>"pwd", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  45. MetaModel::Init_AddAttribute(new AttributeLinkedSetIndirect("profiles", array("label"=>"Profiles", "description"=>"roles, granting rights for that person", "linked_class"=>"URP_UserProfile", "ext_key_to_me"=>"userid", "ext_key_to_remote"=>"profileid", "allowed_values"=>null, "count_min"=>1, "count_max"=>0, "depends_on"=>array())));
  46. //MetaModel::Init_InheritFilters();
  47. MetaModel::Init_AddFilterFromAttribute("userid");
  48. MetaModel::Init_AddFilterFromAttribute("login");
  49. MetaModel::Init_AddFilterFromAttribute("password");
  50. // Display lists
  51. MetaModel::Init_SetZListItems('details', array('userid', 'first_name', 'email', 'login')); // Attributes to be displayed for the complete details
  52. MetaModel::Init_SetZListItems('list', array('first_name', 'last_name', 'login')); // Attributes to be displayed for a list
  53. // Search criteria
  54. MetaModel::Init_SetZListItems('standard_search', array('login', 'userid')); // Criteria of the std search form
  55. MetaModel::Init_SetZListItems('advanced_search', array('login', 'userid')); // Criteria of the advanced search form
  56. }
  57. }
  58. class URP_Profiles extends UserRightsBaseClass
  59. {
  60. public static function Init()
  61. {
  62. $aParams = array
  63. (
  64. "category" => "addon/userrights",
  65. "name" => "profile",
  66. "description" => "usage profiles",
  67. "key_type" => "autoincrement",
  68. "key_label" => "",
  69. "name_attcode" => "name",
  70. "state_attcode" => "",
  71. "reconc_keys" => array(),
  72. "db_table" => "priv_urp_profiles",
  73. "db_key_field" => "id",
  74. "db_finalclass_field" => "",
  75. "display_template" => "",
  76. );
  77. MetaModel::Init_Params($aParams);
  78. //MetaModel::Init_InheritAttributes();
  79. MetaModel::Init_AddAttribute(new AttributeString("name", array("label"=>"Name", "description"=>"label", "allowed_values"=>null, "sql"=>"name", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  80. MetaModel::Init_AddAttribute(new AttributeString("description", array("label"=>"Description", "description"=>"one line description", "allowed_values"=>null, "sql"=>"description", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  81. MetaModel::Init_AddAttribute(new AttributeLinkedSetIndirect("users", array("label"=>"Users", "description"=>"persons having this role", "linked_class"=>"URP_UserProfile", "ext_key_to_me"=>"profileid", "ext_key_to_remote"=>"userid", "allowed_values"=>null, "count_min"=>1, "count_max"=>0, "depends_on"=>array())));
  82. //MetaModel::Init_InheritFilters();
  83. MetaModel::Init_AddFilterFromAttribute("name");
  84. MetaModel::Init_AddFilterFromAttribute("description");
  85. // Display lists
  86. MetaModel::Init_SetZListItems('details', array('name', 'description')); // Attributes to be displayed for the complete details
  87. MetaModel::Init_SetZListItems('list', array('name', 'description')); // Attributes to be displayed for a list
  88. // Search criteria
  89. MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  90. MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  91. }
  92. function GetGrantAsHtml($oUserRights, $sClass, $sAction)
  93. {
  94. $oGrant = $oUserRights->GetClassActionGrant($this->GetKey(), $sClass, $sAction);
  95. if (is_object($oGrant) && ($oGrant->Get('permission') == 'yes'))
  96. {
  97. return '<span style="background-color: #ddffdd;">yes</span>';
  98. }
  99. else
  100. {
  101. return '<span style="background-color: #ffdddd;">no</span>';
  102. }
  103. }
  104. function DoShowGrantSumary($oPage)
  105. {
  106. if ($this->GetName() == "Administrator")
  107. {
  108. // Looks dirty, but ok that's THE ONE
  109. $oPage->p('Has Read/Write access to any object in the database.');
  110. return;
  111. }
  112. // Note: for sure, we assume that the instance is derived from UserRightsProfile
  113. $oUserRights = UserRights::GetModuleInstance();
  114. $aDisplayData = array();
  115. foreach (MetaModel::GetClasses('bizmodel') as $sClass)
  116. {
  117. $aStimuli = array();
  118. foreach (array_keys(MetaModel::EnumStimuli($sClass)) as $sStimulusCode)
  119. {
  120. $oGrant = $oUserRights->GetClassStimulusGrant($this->GetKey(), $sClass, $sStimulusCode);
  121. if (is_object($oGrant) && ($oGrant->Get('permission') == 'yes'))
  122. {
  123. $aStimuli[] = $sStimulusCode;
  124. }
  125. }
  126. $sStimuli = implode(', ', $aStimuli);
  127. $aDisplayData[] = array(
  128. 'class' => MetaModel::GetName($sClass),
  129. 'read' => $this->GetGrantAsHtml($oUserRights, $sClass, 'Read'),
  130. 'bulkread' => $this->GetGrantAsHtml($oUserRights, $sClass, 'Bulk Read'),
  131. 'write' => $this->GetGrantAsHtml($oUserRights, $sClass, 'Modify'),
  132. 'bulkwrite' => $this->GetGrantAsHtml($oUserRights, $sClass, 'Bulk Modify'),
  133. 'stimuli' => $sStimuli,
  134. );
  135. }
  136. $aDisplayConfig = array();
  137. $aDisplayConfig['class'] = array('label' => 'Class', 'description' => '');
  138. $aDisplayConfig['read'] = array('label' => 'Read', 'description' => '');
  139. $aDisplayConfig['bulkread'] = array('label' => 'Bulk read', 'description' => 'List objects or export massively');
  140. $aDisplayConfig['write'] = array('label' => 'Write', 'description' => 'Create and edit (modify)');
  141. $aDisplayConfig['bulkwrite'] = array('label' => 'Bulk write', 'description' => 'Massively create/edit (CSV import)');
  142. $aDisplayConfig['stimuli'] = array('label' => 'Stimuli', 'description' => 'Allowed (compound) actions');
  143. $oPage->table($aDisplayConfig, $aDisplayData);
  144. }
  145. function DisplayBareRelations(web_page $oPage)
  146. {
  147. parent::DisplayBareRelations($oPage);
  148. $oPage->SetCurrentTabContainer('Related Objects');
  149. $oPage->SetCurrentTab('Grants matrix');
  150. $this->DoShowGrantSumary($oPage);
  151. }
  152. }
  153. class URP_Dimensions extends UserRightsBaseClass
  154. {
  155. public static function Init()
  156. {
  157. $aParams = array
  158. (
  159. "category" => "addon/userrights",
  160. "name" => "dimension",
  161. "description" => "application dimension (defining silos)",
  162. "key_type" => "autoincrement",
  163. "key_label" => "",
  164. "name_attcode" => "name",
  165. "state_attcode" => "",
  166. "reconc_keys" => array(),
  167. "db_table" => "priv_urp_dimensions",
  168. "db_key_field" => "id",
  169. "db_finalclass_field" => "",
  170. "display_template" => "../business/templates/default.html",
  171. );
  172. MetaModel::Init_Params($aParams);
  173. //MetaModel::Init_InheritAttributes();
  174. MetaModel::Init_AddAttribute(new AttributeString("name", array("label"=>"Name", "description"=>"label", "allowed_values"=>null, "sql"=>"name", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  175. MetaModel::Init_AddAttribute(new AttributeString("description", array("label"=>"Description", "description"=>"one line description", "allowed_values"=>null, "sql"=>"description", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  176. MetaModel::Init_AddAttribute(new AttributeString("type", array("label"=>"Type", "description"=>"class name or data type (projection unit)", "allowed_values"=>new ValueSetEnumClasses('bizmodel', 'String,Integer'), "sql"=>"type", "default_value"=>'String', "is_null_allowed"=>false, "depends_on"=>array())));
  177. //MetaModel::Init_InheritFilters();
  178. MetaModel::Init_AddFilterFromAttribute("name");
  179. MetaModel::Init_AddFilterFromAttribute("description");
  180. MetaModel::Init_AddFilterFromAttribute("type");
  181. // Display lists
  182. MetaModel::Init_SetZListItems('details', array('name', 'description', 'type')); // Attributes to be displayed for the complete details
  183. MetaModel::Init_SetZListItems('list', array('name', 'description')); // Attributes to be displayed for a list
  184. // Search criteria
  185. MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  186. MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  187. }
  188. public function CheckProjectionSpec($oProjectionSpec, $sProjectedClass)
  189. {
  190. $sExpression = $oProjectionSpec->Get('value');
  191. $sAttribute = $oProjectionSpec->Get('attribute');
  192. // Shortcut: "any value" or "no value" means no projection
  193. if (empty($sExpression)) return;
  194. if ($sExpression == '<any>') return;
  195. // 1st - compute the data type for the dimension
  196. //
  197. $sType = $this->Get('type');
  198. if (MetaModel::IsValidClass($sType))
  199. {
  200. $sExpectedType = $sType;
  201. }
  202. else
  203. {
  204. $sExpectedType = '_scalar_';
  205. }
  206. // 2nd - compute the data type for the projection
  207. //
  208. $sTargetClass = '';
  209. if (($sExpression == '<this>') || ($sExpression == '<user>'))
  210. {
  211. $sTargetClass = $sProjectedClass;
  212. }
  213. elseif ($sExpression == '<any>')
  214. {
  215. $sTargetClass = '';
  216. }
  217. else
  218. {
  219. // Evaluate wether it is a constant or not
  220. try
  221. {
  222. $oObjectSearch = DBObjectSearch::FromOQL($sExpression);
  223. $sTargetClass = $oObjectSearch->GetClass();
  224. }
  225. catch (OqlException $e)
  226. {
  227. }
  228. }
  229. if (empty($sTargetClass))
  230. {
  231. $sFoundType = '_void_';
  232. }
  233. else
  234. {
  235. if (empty($sAttribute))
  236. {
  237. $sFoundType = $sTargetClass;
  238. }
  239. else
  240. {
  241. if (!MetaModel::IsValidAttCode($sTargetClass, $sAttribute))
  242. {
  243. throw new CoreException('Unkown attribute code in projection specification', array('found' => $sAttribute, 'expecting' => MetaModel::GetAttributesList($sTargetClass), 'class' => $sTargetClass, 'projection' => $oProjectionSpec));
  244. }
  245. $oAttDef = MetaModel::GetAttributeDef($sTargetClass, $sAttribute);
  246. if ($oAttDef->IsExternalKey())
  247. {
  248. $sFoundType = $oAttDef->GetTargetClass();
  249. }
  250. else
  251. {
  252. $sFoundType = '_scalar_';
  253. }
  254. }
  255. }
  256. // Compare the dimension type and projection type
  257. if (($sFoundType != '_void_') && ($sFoundType != $sExpectedType))
  258. {
  259. throw new CoreException('Wrong type in projection specification', array('found' => $sFoundType, 'expecting' => $sExpectedType, 'expression' => $sExpression, 'attribute' => $sAttribute, 'projection' => $oProjectionSpec));
  260. }
  261. }
  262. }
  263. class URP_UserProfile extends UserRightsBaseClass
  264. {
  265. public static function Init()
  266. {
  267. $aParams = array
  268. (
  269. "category" => "addon/userrights",
  270. "name" => "User to profile",
  271. "description" => "user profiles",
  272. "key_type" => "autoincrement",
  273. "key_label" => "",
  274. "name_attcode" => "userid",
  275. "state_attcode" => "",
  276. "reconc_keys" => array(),
  277. "db_table" => "priv_urp_userprofile",
  278. "db_key_field" => "id",
  279. "db_finalclass_field" => "",
  280. "display_template" => "../business/templates/default.html",
  281. );
  282. MetaModel::Init_Params($aParams);
  283. //MetaModel::Init_InheritAttributes();
  284. MetaModel::Init_AddAttribute(new AttributeExternalKey("userid", array("targetclass"=>"URP_Users", "jointype"=> "", "label"=>"User", "description"=>"user account", "allowed_values"=>null, "sql"=>"userid", "is_null_allowed"=>false, "depends_on"=>array())));
  285. MetaModel::Init_AddAttribute(new AttributeExternalField("userlogin", array("label"=>"Login", "description"=>"User's login", "allowed_values"=>null, "extkey_attcode"=> 'userid', "target_attcode"=>"login")));
  286. MetaModel::Init_AddAttribute(new AttributeExternalKey("profileid", array("targetclass"=>"URP_Profiles", "jointype"=> "", "label"=>"Profile", "description"=>"usage profile", "allowed_values"=>null, "sql"=>"profileid", "is_null_allowed"=>false, "depends_on"=>array())));
  287. MetaModel::Init_AddAttribute(new AttributeExternalField("profile", array("label"=>"Profile", "description"=>"Profile name", "allowed_values"=>null, "extkey_attcode"=> 'profileid', "target_attcode"=>"name")));
  288. MetaModel::Init_AddAttribute(new AttributeString("reason", array("label"=>"Reason", "description"=>"explain why this person may have this role", "allowed_values"=>null, "sql"=>"description", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  289. //MetaModel::Init_InheritFilters();
  290. MetaModel::Init_AddFilterFromAttribute("userid");
  291. MetaModel::Init_AddFilterFromAttribute("profileid");
  292. // Display lists
  293. MetaModel::Init_SetZListItems('details', array('userid', 'profileid', 'reason')); // Attributes to be displayed for the complete details
  294. MetaModel::Init_SetZListItems('list', array('userid', 'profileid', 'reason')); // Attributes to be displayed for a list
  295. // Search criteria
  296. MetaModel::Init_SetZListItems('standard_search', array('userid', 'profileid')); // Criteria of the std search form
  297. MetaModel::Init_SetZListItems('advanced_search', array('userid', 'profileid')); // Criteria of the advanced search form
  298. }
  299. }
  300. class URP_ProfileProjection extends UserRightsBaseClass
  301. {
  302. public static function Init()
  303. {
  304. $aParams = array
  305. (
  306. "category" => "addon/userrights",
  307. "name" => "profile_projection",
  308. "description" => "profile projections",
  309. "key_type" => "autoincrement",
  310. "key_label" => "",
  311. "name_attcode" => "profileid",
  312. "state_attcode" => "",
  313. "reconc_keys" => array(),
  314. "db_table" => "priv_urp_profileprojection",
  315. "db_key_field" => "id",
  316. "db_finalclass_field" => "",
  317. "display_template" => "../business/templates/default.html",
  318. );
  319. MetaModel::Init_Params($aParams);
  320. //MetaModel::Init_InheritAttributes();
  321. MetaModel::Init_AddAttribute(new AttributeExternalKey("dimensionid", array("targetclass"=>"URP_Dimensions", "jointype"=> "", "label"=>"Dimension", "description"=>"application dimension", "allowed_values"=>null, "sql"=>"dimensionid", "is_null_allowed"=>false, "depends_on"=>array())));
  322. MetaModel::Init_AddAttribute(new AttributeExternalField("dimension", array("label"=>"Dimension", "description"=>"application dimension", "allowed_values"=>null, "extkey_attcode"=> 'dimensionid', "target_attcode"=>"name")));
  323. MetaModel::Init_AddAttribute(new AttributeExternalKey("profileid", array("targetclass"=>"URP_Profiles", "jointype"=> "", "label"=>"Profile", "description"=>"usage profile", "allowed_values"=>null, "sql"=>"profileid", "is_null_allowed"=>false, "depends_on"=>array())));
  324. MetaModel::Init_AddAttribute(new AttributeExternalField("profile", array("label"=>"Profile", "description"=>"Profile name", "allowed_values"=>null, "extkey_attcode"=> 'profileid', "target_attcode"=>"name")));
  325. MetaModel::Init_AddAttribute(new AttributeString("value", array("label"=>"Value expression", "description"=>"OQL expression (using \$user) | constant | <any> | <user>+attribute code", "allowed_values"=>null, "sql"=>"value", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  326. MetaModel::Init_AddAttribute(new AttributeString("attribute", array("label"=>"Attribute", "description"=>"Target attribute code (optional)", "allowed_values"=>null, "sql"=>"attribute", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  327. //MetaModel::Init_InheritFilters();
  328. MetaModel::Init_AddFilterFromAttribute("dimensionid");
  329. MetaModel::Init_AddFilterFromAttribute("profileid");
  330. // Display lists
  331. MetaModel::Init_SetZListItems('details', array('dimensionid', 'profileid', 'value', 'attribute')); // Attributes to be displayed for the complete details
  332. MetaModel::Init_SetZListItems('list', array('dimensionid', 'profileid', 'value', 'attribute')); // Attributes to be displayed for a list
  333. // Search criteria
  334. MetaModel::Init_SetZListItems('standard_search', array('dimensionid', 'profileid')); // Criteria of the std search form
  335. MetaModel::Init_SetZListItems('advanced_search', array('dimensionid', 'profileid')); // Criteria of the advanced search form
  336. }
  337. public function ProjectUser(URP_Users $oUser)
  338. {
  339. $sExpr = $this->Get('value');
  340. if ($sExpr == '<user>')
  341. {
  342. $sColumn = $this->Get('attribute');
  343. if (empty($sColumn))
  344. {
  345. $aRes = array($oUser->GetKey());
  346. }
  347. else
  348. {
  349. $aRes = array($oUser->Get($sColumn));
  350. }
  351. }
  352. elseif ($sExpr == '<any>')
  353. {
  354. $aRes = null;
  355. }
  356. elseif (strtolower(substr($sExpr, 0, 6)) == 'select')
  357. {
  358. $sColumn = $this->Get('attribute');
  359. // SELECT...
  360. $oValueSetDef = new ValueSetObjects($sExpr, $sColumn);
  361. $aValues = $oValueSetDef->GetValues(array('user' => $oUser), '');
  362. $aRes = array_keys($aValues);
  363. }
  364. else
  365. {
  366. // Constant value(s)
  367. $aRes = explode(';', trim($sExpr));
  368. }
  369. return $aRes;
  370. }
  371. }
  372. class URP_ClassProjection extends UserRightsBaseClass
  373. {
  374. public static function Init()
  375. {
  376. $aParams = array
  377. (
  378. "category" => "addon/userrights",
  379. "name" => "class_projection",
  380. "description" => "class projections",
  381. "key_type" => "autoincrement",
  382. "key_label" => "",
  383. "name_attcode" => "dimensionid",
  384. "state_attcode" => "",
  385. "reconc_keys" => array(),
  386. "db_table" => "priv_urp_classprojection",
  387. "db_key_field" => "id",
  388. "db_finalclass_field" => "",
  389. "display_template" => "../business/templates/default.html",
  390. );
  391. MetaModel::Init_Params($aParams);
  392. //MetaModel::Init_InheritAttributes();
  393. MetaModel::Init_AddAttribute(new AttributeExternalKey("dimensionid", array("targetclass"=>"URP_Dimensions", "jointype"=> "", "label"=>"Dimension", "description"=>"application dimension", "allowed_values"=>null, "sql"=>"dimensionid", "is_null_allowed"=>false, "depends_on"=>array())));
  394. MetaModel::Init_AddAttribute(new AttributeExternalField("dimension", array("label"=>"Dimension", "description"=>"application dimension", "allowed_values"=>null, "extkey_attcode"=> 'dimensionid', "target_attcode"=>"name")));
  395. MetaModel::Init_AddAttribute(new AttributeString("class", array("label"=>"Class", "description"=>"Target class", "allowed_values"=>null, "sql"=>"class", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  396. MetaModel::Init_AddAttribute(new AttributeString("value", array("label"=>"Value expression", "description"=>"OQL expression (using \$this) | constant | <any> | <this>+attribute code", "allowed_values"=>null, "sql"=>"value", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  397. MetaModel::Init_AddAttribute(new AttributeString("attribute", array("label"=>"Attribute", "description"=>"Target attribute code (optional)", "allowed_values"=>null, "sql"=>"attribute", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  398. //MetaModel::Init_InheritFilters();
  399. MetaModel::Init_AddFilterFromAttribute("dimensionid");
  400. // #@# verifier
  401. MetaModel::Init_AddFilterFromAttribute("class");
  402. // Display lists
  403. MetaModel::Init_SetZListItems('details', array('dimensionid', 'class', 'value', 'attribute')); // Attributes to be displayed for the complete details
  404. MetaModel::Init_SetZListItems('list', array('dimensionid', 'class', 'value', 'attribute')); // Attributes to be displayed for a list
  405. // Search criteria
  406. MetaModel::Init_SetZListItems('standard_search', array('dimensionid', 'class')); // Criteria of the std search form
  407. MetaModel::Init_SetZListItems('advanced_search', array('dimensionid', 'class')); // Criteria of the advanced search form
  408. }
  409. public function ProjectObject($oObject)
  410. {
  411. $sExpr = $this->Get('value');
  412. if ($sExpr == '<this>')
  413. {
  414. $sColumn = $this->Get('attribute');
  415. if (empty($sColumn))
  416. {
  417. $aRes = array($oObject->GetKey());
  418. }
  419. else
  420. {
  421. $aRes = array($oObject->Get($sColumn));
  422. }
  423. }
  424. elseif ($sExpr == '<any>')
  425. {
  426. $aRes = null;
  427. }
  428. elseif (strtolower(substr($sExpr, 0, 6)) == 'select')
  429. {
  430. $sColumn = $this->Get('attribute');
  431. // SELECT...
  432. $oValueSetDef = new ValueSetObjects($sExpr, $sColumn);
  433. $aValues = $oValueSetDef->GetValues(array('this' => $oObject), '');
  434. $aRes = array_keys($aValues);
  435. }
  436. elseif ($sExpr == '<any>')
  437. {
  438. $aRes = null;
  439. }
  440. else
  441. {
  442. // Constant value(s)
  443. $aRes = explode(';', trim($sExpr));
  444. }
  445. return $aRes;
  446. }
  447. }
  448. class URP_ActionGrant extends UserRightsBaseClass
  449. {
  450. public static function Init()
  451. {
  452. $aParams = array
  453. (
  454. "category" => "addon/userrights",
  455. "name" => "action_permission",
  456. "description" => "permissions on classes",
  457. "key_type" => "autoincrement",
  458. "key_label" => "",
  459. "name_attcode" => "profileid",
  460. "state_attcode" => "",
  461. "reconc_keys" => array(),
  462. "db_table" => "priv_urp_grant_actions",
  463. "db_key_field" => "id",
  464. "db_finalclass_field" => "",
  465. "display_template" => "../business/templates/default.html",
  466. );
  467. MetaModel::Init_Params($aParams);
  468. //MetaModel::Init_InheritAttributes();
  469. // Common to all grant classes (could be factorized by class inheritence, but this has to be benchmarked)
  470. MetaModel::Init_AddAttribute(new AttributeExternalKey("profileid", array("targetclass"=>"URP_Profiles", "jointype"=> "", "label"=>"Profile", "description"=>"usage profile", "allowed_values"=>null, "sql"=>"profileid", "is_null_allowed"=>false, "depends_on"=>array())));
  471. MetaModel::Init_AddAttribute(new AttributeExternalField("profile", array("label"=>"Profile", "description"=>"usage profile", "allowed_values"=>null, "extkey_attcode"=> 'profileid', "target_attcode"=>"name")));
  472. MetaModel::Init_AddAttribute(new AttributeString("class", array("label"=>"Class", "description"=>"class name", "allowed_values"=>null, "sql"=>"class", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  473. MetaModel::Init_AddAttribute(new AttributeEnum("permission", array("label"=>"Permission", "description"=>"allowed or not allowed?", "allowed_values"=>new ValueSetEnum('yes,no'), "sql"=>"permission", "default_value"=>"yes", "is_null_allowed"=>false, "depends_on"=>array())));
  474. MetaModel::Init_AddAttribute(new AttributeString("action", array("label"=>"Action", "description"=>"operations to perform on the given class", "allowed_values"=>null, "sql"=>"action", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  475. //MetaModel::Init_InheritFilters();
  476. // Common to all grant classes (could be factorized by class inheritence, but this has to be benchmarked)
  477. MetaModel::Init_AddFilterFromAttribute("profileid");
  478. MetaModel::Init_AddFilterFromAttribute("profile");
  479. MetaModel::Init_AddFilterFromAttribute("class");
  480. MetaModel::Init_AddFilterFromAttribute("permission");
  481. MetaModel::Init_AddFilterFromAttribute("action");
  482. // Display lists
  483. MetaModel::Init_SetZListItems('details', array('profileid', 'class', 'permission', 'action')); // Attributes to be displayed for the complete details
  484. MetaModel::Init_SetZListItems('list', array('profileid', 'class', 'permission', 'action')); // Attributes to be displayed for a list
  485. // Search criteria
  486. MetaModel::Init_SetZListItems('standard_search', array('profileid', 'class', 'permission', 'action')); // Criteria of the std search form
  487. MetaModel::Init_SetZListItems('advanced_search', array('profileid', 'class', 'permission', 'action')); // Criteria of the advanced search form
  488. }
  489. }
  490. class URP_StimulusGrant extends UserRightsBaseClass
  491. {
  492. public static function Init()
  493. {
  494. $aParams = array
  495. (
  496. "category" => "addon/userrights",
  497. "name" => "stimulus_permission",
  498. "description" => "permissions on stimilus in the life cycle of the object",
  499. "key_type" => "autoincrement",
  500. "key_label" => "",
  501. "name_attcode" => "profileid",
  502. "state_attcode" => "",
  503. "reconc_keys" => array(),
  504. "db_table" => "priv_urp_grant_stimulus",
  505. "db_key_field" => "id",
  506. "db_finalclass_field" => "",
  507. "display_template" => "../business/templates/default.html",
  508. );
  509. MetaModel::Init_Params($aParams);
  510. //MetaModel::Init_InheritAttributes();
  511. // Common to all grant classes (could be factorized by class inheritence, but this has to be benchmarked)
  512. MetaModel::Init_AddAttribute(new AttributeExternalKey("profileid", array("targetclass"=>"URP_Profiles", "jointype"=> "", "label"=>"Profile", "description"=>"usage profile", "allowed_values"=>null, "sql"=>"profileid", "is_null_allowed"=>false, "depends_on"=>array())));
  513. MetaModel::Init_AddAttribute(new AttributeExternalField("profile", array("label"=>"Profile", "description"=>"usage profile", "allowed_values"=>null, "extkey_attcode"=> 'profileid', "target_attcode"=>"name")));
  514. MetaModel::Init_AddAttribute(new AttributeString("class", array("label"=>"Class", "description"=>"class name", "allowed_values"=>null, "sql"=>"class", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  515. MetaModel::Init_AddAttribute(new AttributeEnum("permission", array("label"=>"Permission", "description"=>"allowed or not allowed?", "allowed_values"=>new ValueSetEnum('yes,no'), "sql"=>"permission", "default_value"=>"yes", "is_null_allowed"=>false, "depends_on"=>array())));
  516. MetaModel::Init_AddAttribute(new AttributeString("stimulus", array("label"=>"Stimulus", "description"=>"stimulus code", "allowed_values"=>null, "sql"=>"action", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  517. //MetaModel::Init_InheritFilters();
  518. // Common to all grant classes (could be factorized by class inheritence, but this has to be benchmarked)
  519. MetaModel::Init_AddFilterFromAttribute("profileid");
  520. MetaModel::Init_AddFilterFromAttribute("profile");
  521. MetaModel::Init_AddFilterFromAttribute("class");
  522. MetaModel::Init_AddFilterFromAttribute("permission");
  523. MetaModel::Init_AddFilterFromAttribute("stimulus");
  524. // Display lists
  525. MetaModel::Init_SetZListItems('details', array('profileid', 'class', 'permission', 'stimulus')); // Attributes to be displayed for the complete details
  526. MetaModel::Init_SetZListItems('list', array('profileid', 'class', 'permission', 'stimulus')); // Attributes to be displayed for a list
  527. // Search criteria
  528. MetaModel::Init_SetZListItems('standard_search', array('profileid', 'class', 'permission', 'stimulus')); // Criteria of the std search form
  529. MetaModel::Init_SetZListItems('advanced_search', array('profileid', 'class', 'permission', 'stimulus')); // Criteria of the advanced search form
  530. }
  531. }
  532. class URP_AttributeGrant extends UserRightsBaseClass
  533. {
  534. public static function Init()
  535. {
  536. $aParams = array
  537. (
  538. "category" => "addon/userrights",
  539. "name" => "attribute_permission",
  540. "description" => "permissions at the attributes level",
  541. "key_type" => "autoincrement",
  542. "key_label" => "",
  543. "name_attcode" => "actiongrantid",
  544. "state_attcode" => "",
  545. "reconc_keys" => array(),
  546. "db_table" => "priv_urp_grant_attributes",
  547. "db_key_field" => "id",
  548. "db_finalclass_field" => "",
  549. "display_template" => "../business/templates/default.html",
  550. );
  551. MetaModel::Init_Params($aParams);
  552. //MetaModel::Init_InheritAttributes();
  553. MetaModel::Init_AddAttribute(new AttributeExternalKey("actiongrantid", array("targetclass"=>"URP_ActionGrant", "jointype"=> "", "label"=>"Action grant", "description"=>"action grant", "allowed_values"=>null, "sql"=>"actiongrantid", "is_null_allowed"=>false, "depends_on"=>array())));
  554. MetaModel::Init_AddAttribute(new AttributeString("attcode", array("label"=>"Attribute", "description"=>"attribute code", "allowed_values"=>null, "sql"=>"attcode", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  555. //MetaModel::Init_InheritFilters();
  556. MetaModel::Init_AddFilterFromAttribute("actiongrantid");
  557. MetaModel::Init_AddFilterFromAttribute("attcode");
  558. // Display lists
  559. MetaModel::Init_SetZListItems('details', array('actiongrantid', 'attcode')); // Attributes to be displayed for the complete details
  560. MetaModel::Init_SetZListItems('list', array('actiongrantid', 'attcode')); // Attributes to be displayed for a list
  561. // Search criteria
  562. MetaModel::Init_SetZListItems('standard_search', array('actiongrantid', 'attcode')); // Criteria of the std search form
  563. MetaModel::Init_SetZListItems('advanced_search', array('actiongrantid', 'attcode')); // Criteria of the advanced search form
  564. }
  565. }
  566. class UserRightsProfile extends UserRightsAddOnAPI
  567. {
  568. static public $m_aActionCodes = array(
  569. UR_ACTION_READ => 'read',
  570. UR_ACTION_MODIFY => 'modify',
  571. UR_ACTION_DELETE => 'delete',
  572. UR_ACTION_BULK_READ => 'bulk read',
  573. UR_ACTION_BULK_MODIFY => 'bulk modify',
  574. UR_ACTION_BULK_DELETE => 'bulk delete',
  575. );
  576. // Installation: create the very first user
  577. public function CreateAdministrator($sAdminUser, $sAdminPwd)
  578. {
  579. $oOrg = new bizOrganization();
  580. $oOrg->Set('name', 'My Company/Department');
  581. $oOrg->Set('code', 'SOMECODE');
  582. $oOrg->Set('status', 'implementation');
  583. //$oOrg->Set('parent_id', xxx);
  584. $iOrgId = $oOrg->DBInsertNoReload();
  585. // Location : optional
  586. //$oLocation = new bizLocation();
  587. //$oLocation->Set('name', 'MyOffice');
  588. //$oLocation->Set('status', 'implementation');
  589. //$oLocation->Set('org_id', $iOrgId);
  590. //$oLocation->Set('severity', 'high');
  591. //$oLocation->Set('address', 'my building in my city');
  592. //$oLocation->Set('country', 'my country');
  593. //$oLocation->Set('parent_location_id', xxx);
  594. //$iLocationId = $oLocation->DBInsertNoReload();
  595. $oContact = new bizPerson();
  596. $oContact->Set('name', 'My last name');
  597. $oContact->Set('first_name', 'My first name');
  598. $oContact->Set('status', 'available');
  599. $oContact->Set('org_id', $iOrgId);
  600. $oContact->Set('email', 'my.email@foo.org');
  601. $oContact->Set('phone', '');
  602. //$oContact->Set('location_id', $iLocationId);
  603. $oContact->Set('employee_number', '');
  604. $iContactId = $oContact->DBInsertNoReload();
  605. $oUser = new URP_Users();
  606. $oUser->Set('login', $sAdminUser);
  607. $oUser->Set('password', $sAdminPwd);
  608. $oUser->Set('userid', $iContactId);
  609. $iUserId = $oUser->DBInsertNoReload();
  610. // Add this user to the very specific 'admin' profile
  611. $oUserProfile = new URP_UserProfile();
  612. $oUserProfile->Set('userid', $iUserId);
  613. $oUserProfile->Set('profileid', ADMIN_PROFILE_ID);
  614. $oUserProfile->Set('reason', 'By definition, the administrator must have the administrator profile');
  615. $oUserProfile->DBInsertNoReload();
  616. return true;
  617. }
  618. public function IsAdministrator($iUserId)
  619. {
  620. if (in_array($iUserId, $this->m_aAdmins))
  621. {
  622. return true;
  623. }
  624. else
  625. {
  626. return false;
  627. }
  628. }
  629. public function Setup()
  630. {
  631. SetupITILProfiles::DoCreateProfiles();
  632. return true;
  633. }
  634. public function Init()
  635. {
  636. MetaModel::RegisterPlugin('userrights', 'ACbyProfile', array($this, 'CacheData'));
  637. }
  638. protected $m_aUsers = array(); // id -> object
  639. protected $m_aDimensions = array(); // id -> object
  640. protected $m_aClassProj = array(); // class,dimensionid -> object
  641. protected $m_aProfiles = array(); // id -> object
  642. protected $m_aUserProfiles = array(); // userid,profileid -> object
  643. protected $m_aProPro = array(); // profileid,dimensionid -> object
  644. protected $m_aAdmins = array(); // id of users being linked to the profile #ADMIN_PROFILE_ID
  645. protected $m_aClassActionGrants = array(); // profile, class, action -> permission
  646. protected $m_aClassStimulusGrants = array(); // profile, class, stimulus -> permission
  647. protected $m_aObjectActionGrants = array(); // userid, class, id, action -> permission, list of attributes
  648. public function CacheData()
  649. {
  650. // Could be loaded in a shared memory (?)
  651. $oUserSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_Users"));
  652. while ($oUser = $oUserSet->Fetch())
  653. {
  654. $this->m_aUsers[$oUser->GetKey()] = $oUser;
  655. }
  656. $oDimensionSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_Dimensions"));
  657. while ($oDimension = $oDimensionSet->Fetch())
  658. {
  659. $this->m_aDimensions[$oDimension->GetKey()] = $oDimension;
  660. }
  661. $oClassProjSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_ClassProjection"));
  662. while ($oClassProj = $oClassProjSet->Fetch())
  663. {
  664. $this->m_aClassProjs[$oClassProj->Get('class')][$oClassProj->Get('dimensionid')] = $oClassProj;
  665. }
  666. $oProfileSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_Profiles"));
  667. while ($oProfile = $oProfileSet->Fetch())
  668. {
  669. $this->m_aProfiles[$oProfile->GetKey()] = $oProfile;
  670. }
  671. $oUserProfileSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_UserProfile"));
  672. while ($oUserProfile = $oUserProfileSet->Fetch())
  673. {
  674. $this->m_aUserProfiles[$oUserProfile->Get('userid')][$oUserProfile->Get('profileid')] = $oUserProfile;
  675. if ($oUserProfile->Get('profileid') == ADMIN_PROFILE_ID)
  676. {
  677. $this->m_aAdmins[] = $oUserProfile->Get('userid');
  678. }
  679. }
  680. $oProProSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_ProfileProjection"));
  681. while ($oProPro = $oProProSet->Fetch())
  682. {
  683. $this->m_aProPros[$oProPro->Get('profileid')][$oProPro->Get('dimensionid')] = $oProPro;
  684. }
  685. /*
  686. echo "<pre>\n";
  687. print_r($this->m_aUsers);
  688. print_r($this->m_aDimensions);
  689. print_r($this->m_aClassProjs);
  690. print_r($this->m_aProfiles);
  691. print_r($this->m_aUserProfiles);
  692. print_r($this->m_aProPros);
  693. echo "</pre>\n";
  694. exit;
  695. */
  696. return true;
  697. }
  698. public function CheckCredentials($sUserName, $sPassword)
  699. {
  700. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_Users WHERE login = :login"), array(), array('login' => $sUserName));
  701. if ($oSet->Count() < 1)
  702. {
  703. // todo: throw an exception?
  704. return false;
  705. }
  706. $oUser = $oSet->Fetch();
  707. if ($oUser->Get('password') == $sPassword)
  708. {
  709. return $oUser->GetKey();
  710. }
  711. // todo: throw an exception?
  712. return false;
  713. }
  714. public function GetUserId($sUserName)
  715. {
  716. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_Users WHERE login = :login"), array(), array('login' => $sUserName));
  717. if ($oSet->Count() < 1)
  718. {
  719. // todo: throw an exception?
  720. return false;
  721. }
  722. $oUser = $oSet->Fetch();
  723. return $oUser->GetKey();
  724. }
  725. public function GetFilter($sUserName, $sClass)
  726. {
  727. $oNullFilter = new DBObjectSearch($sClass);
  728. return $oNullFilter;
  729. }
  730. // This verb has been made public to allow the development of an accurate feedback for the current configuration
  731. public function GetClassActionGrant($iProfile, $sClass, $sAction)
  732. {
  733. if (isset($this->m_aClassActionGrants[$iProfile][$sClass][$sAction]))
  734. {
  735. return $this->m_aClassActionGrants[$iProfile][$sClass][$sAction];
  736. }
  737. // Get the permission for this profile/class/action
  738. $oSearch = DBObjectSearch::FromOQL("SELECT URP_ActionGrant WHERE class = :class AND action = :action AND profileid = :profile AND permission = 'yes'");
  739. $oSet = new DBObjectSet($oSearch, array(), array('class'=>$sClass, 'action'=>$sAction, 'profile'=>$iProfile));
  740. if ($oSet->Count() >= 1)
  741. {
  742. $oGrantRecord = $oSet->Fetch();
  743. }
  744. else
  745. {
  746. $sParentClass = MetaModel::GetParentPersistentClass($sClass);
  747. if (empty($sParentClass))
  748. {
  749. $oGrantRecord = null;
  750. }
  751. else
  752. {
  753. $oGrantRecord = $this->GetClassActionGrant($iProfile, $sParentClass, $sAction);
  754. }
  755. }
  756. $this->m_aClassActionGrants[$iProfile][$sClass][$sAction] = $oGrantRecord;
  757. return $oGrantRecord;
  758. }
  759. protected function GetObjectActionGrant($oUser, $sClass, $iActionCode, $oObject)
  760. {
  761. // load and cache permissions for the current user on the given object
  762. //
  763. $aTest = @$this->m_aObjectActionGrants[$oUser->GetKey()][$sClass][$oObject->GetKey][$iActionCode];
  764. if (is_array($aTest)) return $aTest;
  765. $sAction = self::$m_aActionCodes[$iActionCode];
  766. $iInstancePermission = UR_ALLOWED_NO;
  767. $aAttributes = array();
  768. foreach($this->GetMatchingProfiles($oUser, $oObject) as $iProfile)
  769. {
  770. $oGrantRecord = $this->GetClassActionGrant($iProfile, $sClass, $sAction);
  771. if (is_null($oGrantRecord))
  772. {
  773. continue; // loop to the next profile
  774. }
  775. else
  776. {
  777. $iInstancePermission = UR_ALLOWED_YES;
  778. // update the list of attributes with those allowed for this profile
  779. //
  780. $oSearch = DBObjectSearch::FromOQL("SELECT URP_AttributeGrant WHERE actiongrantid = :actiongrantid");
  781. $oSet = new DBObjectSet($oSearch, array(), array('actiongrantid' => $oGrantRecord->GetKey()));
  782. $aProfileAttributes = $oSet->GetColumnAsArray('attcode', false);
  783. if (count($aProfileAttributes) == 0)
  784. {
  785. $aAllAttributes = array_keys(MetaModel::ListAttributeDefs($sClass));
  786. $aAttributes = array_merge($aAttributes, $aAllAttributes);
  787. }
  788. else
  789. {
  790. $aAttributes = array_merge($aAttributes, $aProfileAttributes);
  791. }
  792. }
  793. }
  794. $aRes = array(
  795. 'permission' => $iInstancePermission,
  796. 'attributes' => $aAttributes,
  797. );
  798. $this->m_aObjectActionGrants[$oUser->GetKey()][$sClass][$oObject->GetKey()][$iActionCode] = $aRes;
  799. return $aRes;
  800. }
  801. public function IsActionAllowed($iUserId, $sClass, $iActionCode, dbObjectSet $oInstances)
  802. {
  803. if ($this->IsAdministrator($iUserId)) return true;
  804. $oUser = $this->m_aUsers[$iUserId];
  805. $oInstances->Rewind();
  806. while($oObject = $oInstances->Fetch())
  807. {
  808. $aObjectPermissions = $this->GetObjectActionGrant($oUser, $sClass, $iActionCode, $oObject);
  809. $iInstancePermission = $aObjectPermissions['permission'];
  810. if (isset($iGlobalPermission))
  811. {
  812. if ($iInstancePermission != $iGlobalPermission)
  813. {
  814. $iGlobalPermission = UR_ALLOWED_DEPENDS;
  815. break;
  816. }
  817. }
  818. else
  819. {
  820. $iGlobalPermission = $iInstancePermission;
  821. }
  822. }
  823. $oInstances->Rewind();
  824. if (isset($iGlobalPermission))
  825. {
  826. return $iGlobalPermission;
  827. }
  828. else
  829. {
  830. return UR_ALLOWED_NO;
  831. }
  832. }
  833. public function IsActionAllowedOnAttribute($iUserId, $sClass, $sAttCode, $iActionCode, dbObjectSet $oInstances)
  834. {
  835. if ($this->IsAdministrator($iUserId)) return true;
  836. $oUser = $this->m_aUsers[$iUserId];
  837. $oInstances->Rewind();
  838. while($oObject = $oInstances->Fetch())
  839. {
  840. $aObjectPermissions = $this->GetObjectActionGrant($oUser, $sClass, $iActionCode, $oObject);
  841. $aAttributes = $aObjectPermissions['attributes'];
  842. if (in_array($sAttCode, $aAttributes))
  843. {
  844. $iInstancePermission = $aObjectPermissions['permission'];
  845. }
  846. else
  847. {
  848. $iInstancePermission = UR_ALLOWED_NO;
  849. }
  850. if (isset($iGlobalPermission))
  851. {
  852. if ($iInstancePermission != $iGlobalPermission)
  853. {
  854. $iGlobalPermission = UR_ALLOWED_DEPENDS;
  855. }
  856. }
  857. else
  858. {
  859. $iGlobalPermission = $iInstancePermission;
  860. }
  861. }
  862. $oInstances->Rewind();
  863. if (isset($iGlobalPermission))
  864. {
  865. return $iGlobalPermission;
  866. }
  867. else
  868. {
  869. return UR_ALLOWED_NO;
  870. }
  871. }
  872. // This verb has been made public to allow the development of an accurate feedback for the current configuration
  873. public function GetClassStimulusGrant($iProfile, $sClass, $sStimulusCode)
  874. {
  875. if (isset($this->m_aClassStimulusGrants[$iProfile][$sClass][$sStimulusCode]))
  876. {
  877. return $this->m_aClassStimulusGrants[$iProfile][$sClass][$sStimulusCode];
  878. }
  879. // Get the permission for this profile/class/stimulus
  880. $oSearch = DBObjectSearch::FromOQL("SELECT URP_StimulusGrant WHERE class = :class AND stimulus = :stimulus AND profileid = :profile AND permission = 'yes'");
  881. $oSet = new DBObjectSet($oSearch, array(), array('class'=>$sClass, 'stimulus'=>$sStimulusCode, 'profile'=>$iProfile));
  882. if ($oSet->Count() >= 1)
  883. {
  884. $oGrantRecord = $oSet->Fetch();
  885. }
  886. else
  887. {
  888. $oGrantRecord = null;
  889. }
  890. $this->m_aClassStimulusGrants[$iProfile][$sClass][$sStimulusCode] = $oGrantRecord;
  891. return $oGrantRecord;
  892. }
  893. public function IsStimulusAllowed($iUserId, $sClass, $sStimulusCode, dbObjectSet $oInstances)
  894. {
  895. if ($this->IsAdministrator($iUserId)) return true;
  896. $oUser = $this->m_aUsers[$iUserId];
  897. // Note: this code is VERY close to the code of IsActionAllowed()
  898. $oInstances->Rewind();
  899. while($oObject = $oInstances->Fetch())
  900. {
  901. $iInstancePermission = UR_ALLOWED_NO;
  902. foreach($this->GetMatchingProfiles($oUser, $oObject) as $iProfile)
  903. {
  904. // Get the permission for this profile/class/stimulus
  905. $oSearch = DBObjectSearch::FromOQL("SELECT URP_StimulusGrant WHERE class = :class AND stimulus = :stimulus AND profileid = :profile AND permission = 'yes'");
  906. $oSet = new DBObjectSet($oSearch, array(), array('class'=>$sClass, 'stimulus'=>$sStimulusCode, 'profile'=>$iProfile));
  907. if ($oSet->Count() < 1)
  908. {
  909. return UR_ALLOWED_NO;
  910. }
  911. // no need to fetch the record, we've requested the records having permission = 'yes'
  912. $iInstancePermission = UR_ALLOWED_YES;
  913. }
  914. if (isset($iGlobalPermission))
  915. {
  916. if ($iInstancePermission != $iGlobalPermission)
  917. {
  918. $iGlobalPermission = UR_ALLOWED_DEPENDS;
  919. }
  920. }
  921. else
  922. {
  923. $iGlobalPermission = $iInstancePermission;
  924. }
  925. }
  926. $oInstances->Rewind();
  927. if (isset($iGlobalPermission))
  928. {
  929. return $iGlobalPermission;
  930. }
  931. else
  932. {
  933. return UR_ALLOWED_NO;
  934. }
  935. }
  936. protected function GetMatchingProfilesByDim($oUser, $oObject, $oDimension)
  937. {
  938. //
  939. // List profiles for which the user projection overlaps the object projection in the given dimension
  940. //
  941. $iUser = $oUser->GetKey();
  942. $sClass = get_class($oObject);
  943. $iPKey = $oObject->GetKey();
  944. $iDimension = $oDimension->GetKey();
  945. $aObjectProjection = $this->m_aClassProjs[$sClass][$iDimension]->ProjectObject($oObject);
  946. $aRes = array();
  947. if (array_key_exists($iUser, $this->m_aUserProfiles) > 0)
  948. {
  949. foreach ($this->m_aUserProfiles[$iUser] as $iProfile => $oProfile)
  950. {
  951. if (is_null($aObjectProjection))
  952. {
  953. $aRes[] = $iProfile;
  954. }
  955. else
  956. {
  957. // user projection to be cached on a given page !
  958. $aUserProjection = $this->m_aProPros[$iProfile][$iDimension]->ProjectUser($oUser);
  959. if (is_null($aUserProjection))
  960. {
  961. $aRes[] = $iProfile;
  962. }
  963. else
  964. {
  965. $aMatchingValues = array_intersect($aObjectProjection, $aUserProjection);
  966. if (count($aMatchingValues) > 0)
  967. {
  968. $aRes[] = $iProfile;
  969. }
  970. }
  971. }
  972. }
  973. }
  974. return $aRes;
  975. }
  976. protected $m_aMatchingProfiles = array(); // cache of the matching profiles for a given user/object
  977. protected function GetMatchingProfiles($oUser, $oObject)
  978. {
  979. $iUser = $oUser->GetKey();
  980. $sClass = get_class($oObject);
  981. $iObject = $oObject->GetKey();
  982. //
  983. // List profiles for which the user projection overlaps the object projection in each and every dimension
  984. // Caches the result
  985. //
  986. $aTest = @$this->m_aMatchingProfiles[$iUser][$sClass][$iObject];
  987. if (is_array($aTest))
  988. {
  989. return $aTest;
  990. }
  991. $aProfileRes = array();
  992. foreach ($this->m_aDimensions as $iDimension => $oDimension)
  993. {
  994. foreach ($this->GetMatchingProfilesByDim($oUser, $oObject, $oDimension) as $iProfile)
  995. {
  996. @$aProfileRes[$iProfile] += 1;
  997. }
  998. }
  999. $aRes = array();
  1000. $iDimCount = count($this->m_aDimensions);
  1001. foreach ($aProfileRes as $iProfile => $iMatches)
  1002. {
  1003. if ($iMatches == $iDimCount)
  1004. {
  1005. $aRes[] = $iProfile;
  1006. }
  1007. }
  1008. $this->m_aMatchingProfiles[$iUser][$sClass][$iObject] = $aRes;
  1009. return $aRes;
  1010. }
  1011. }
  1012. //
  1013. // Here is the code that will create some profiles into our user management model, given an ITIL representation of the user profiles
  1014. //
  1015. class SetupITILProfiles
  1016. {
  1017. /*
  1018. Later, maybe ?
  1019. protected static $m_aDimensions = array(
  1020. 'organization' => array(
  1021. 'description' => '',
  1022. 'type' => 'bizOrganization',
  1023. ),
  1024. 'site' => array(
  1025. 'description' => '',
  1026. 'type' => '',
  1027. ),
  1028. );
  1029. */
  1030. protected static $m_aActions = array(
  1031. UR_ACTION_READ => 'Read',
  1032. UR_ACTION_MODIFY => 'Modify',
  1033. UR_ACTION_DELETE => 'Delete',
  1034. UR_ACTION_BULK_READ => 'Bulk Read',
  1035. UR_ACTION_BULK_MODIFY => 'Bulk Modify',
  1036. UR_ACTION_BULK_DELETE => 'Bulk Delete',
  1037. );
  1038. // It is possible to specify the same class in several modules
  1039. //
  1040. protected static $m_aModules = array(
  1041. 'General' => array(
  1042. 'bizOrganization',
  1043. ),
  1044. 'Documentation' => array(
  1045. 'bizDocVersion',
  1046. 'lnkDocumentRealObject',
  1047. 'lnkDocumentContract',
  1048. 'lnkDocumentError',
  1049. ),
  1050. 'Configuration' => array(
  1051. 'logRealObject',
  1052. 'lnkContactRealObject',
  1053. 'lnkInterfaces',
  1054. 'ClientServerLinks',
  1055. 'lnkInfraGrouping',
  1056. ),
  1057. 'Incident' => array(
  1058. 'bizIncidentTicket',
  1059. 'lnkRelatedTicket',
  1060. 'lnkInfraTicket',
  1061. 'lnkContactTicket',
  1062. ),
  1063. 'Problem' => array(
  1064. 'bizKnownError',
  1065. 'lnkInfraError',
  1066. 'lnkDocumentError',
  1067. ),
  1068. 'Change' => array(
  1069. 'bizChangeTicket',
  1070. 'lnkInfraChangeTicket',
  1071. 'lnkContactChange',
  1072. ),
  1073. 'Service' => array(
  1074. 'bizContract',
  1075. 'lnkInfraContract',
  1076. 'lnkContactContract',
  1077. 'lnkDocumentContract',
  1078. ),
  1079. 'Call' => array(
  1080. 'bizServiceCall',
  1081. 'lnkCallTicket',
  1082. 'lnkInfraCall',
  1083. ),
  1084. );
  1085. protected static $m_aProfiles = array(
  1086. 'Configuration Manager' => array(
  1087. 'description' => 'Person in charge of the documentation of the managed CIs',
  1088. 'write_modules' => 'Documentation,Configuration',
  1089. 'stimuli' => array(
  1090. 'bizServer' => 'any',
  1091. //'bizServer' => 'ev_store,ev_ship,ev_plug,ev_configuration_finished,ev_val_failed,ev_mtp,ev_start_change,ev_end_change,ev_decomission,ev_obsolete,ev_recycle',
  1092. 'bizContract' => 'none',
  1093. 'bizIncidentTicket' => 'none',
  1094. 'bizChangeTicket' => 'none',
  1095. ),
  1096. ),
  1097. /* 'Requestor pb granularite actions (create/delete)' => array(
  1098. 'description' => 'Person notifying an incident',
  1099. 'write_modules' => 'Incident',
  1100. 'stimuli' => array(
  1101. 'bizServer' => 'none',
  1102. 'bizContract' => 'none',
  1103. 'bizIncidentTicket' => 'none',
  1104. 'bizChangeTicket' => 'none',
  1105. ),
  1106. ),
  1107. */
  1108. 'Service Desk Agent' => array(
  1109. 'description' => 'Person in charge of creating incident reports',
  1110. 'write_modules' => 'Documentation,Incident,Call',
  1111. 'stimuli' => array(
  1112. 'bizServer' => 'none',
  1113. 'bizContract' => 'none',
  1114. 'bizIncidentTicket' => 'ev_assign',
  1115. 'bizChangeTicket' => 'none',
  1116. 'bizServiceCall' => 'any',
  1117. ),
  1118. ),
  1119. 'Support Agent' => array(
  1120. 'description' => 'Person analyzing and solving the current incidents or problems',
  1121. 'write_modules' => 'Documentation,Incident,Problem',
  1122. 'stimuli' => array(
  1123. 'bizIncidentTicket' => 'any',
  1124. //'bizIncidentTicket' => 'ev_assign,ev_reassign,ev_start_working,ev_close',
  1125. ),
  1126. ),
  1127. 'Change Implementor' => array(
  1128. 'description' => 'Person executing the changes',
  1129. 'write_modules' => 'Documentation,Configuration,Change',
  1130. 'stimuli' => array(
  1131. 'bizServer' => 'none',
  1132. 'bizContract' => 'none',
  1133. 'bizIncidentTicket' => 'none',
  1134. 'bizChangeTicket' => 'ev_plan,ev_replan,ev_implement,ev_monitor',
  1135. ),
  1136. ),
  1137. 'Change Supervisor' => array(
  1138. 'description' => 'Person responsible for the overall change execution',
  1139. 'write_modules' => 'Documentation,Change',
  1140. 'stimuli' => array(
  1141. 'bizServer' => 'none',
  1142. 'bizContract' => 'none',
  1143. 'bizIncidentTicket' => 'none',
  1144. 'bizChangeTicket' => 'ev_validate,ev_reject,ev_reopen,ev_finish',
  1145. ),
  1146. ),
  1147. 'Change Approver' => array(
  1148. 'description' => 'Person who could be impacted by some changes',
  1149. 'write_modules' => 'Documentation,Change',
  1150. 'stimuli' => array(
  1151. 'bizServer' => 'none',
  1152. 'bizContract' => 'none',
  1153. 'bizIncidentTicket' => 'none',
  1154. 'bizChangeTicket' => 'ev_approve,ev_notapprove',
  1155. ),
  1156. ),
  1157. 'Service Manager' => array(
  1158. 'description' => 'Person responsible for the service delivered to the [internal] customer',
  1159. 'write_modules' => 'Documentation,Service',
  1160. 'stimuli' => array(
  1161. 'bizServer' => 'none',
  1162. 'bizContract' => 'any',
  1163. //'bizContract' => 'ev_freeze_version,ev_sign,ev_begin,ev_notice,ev_terminate,ev_elapsed',
  1164. 'bizIncidentTicket' => 'none',
  1165. 'bizChangeTicket' => 'none',
  1166. ),
  1167. ),
  1168. );
  1169. protected static function DoCreateProfileProjection($iProfile, $iDimension)
  1170. {
  1171. $oNewObj = MetaModel::NewObject("URP_ProfileProjection");
  1172. $oNewObj->Set('profileid', $iProfile);
  1173. $oNewObj->Set('dimensionid', $iDimension);
  1174. $oNewObj->Set('value', '<any>');
  1175. $oNewObj->Set('attribute', '');
  1176. $iId = $oNewObj->DBInsertNoReload();
  1177. return $iId;
  1178. }
  1179. protected static function DoCreateActionGrant($iProfile, $iAction, $sClass)
  1180. {
  1181. $oNewObj = MetaModel::NewObject("URP_ActionGrant");
  1182. $oNewObj->Set('profileid', $iProfile);
  1183. $oNewObj->Set('permission', true);
  1184. $oNewObj->Set('class', $sClass);
  1185. $oNewObj->Set('action', self::$m_aActions[$iAction]);
  1186. $iId = $oNewObj->DBInsertNoReload();
  1187. return $iId;
  1188. }
  1189. protected static function DoCreateStimulusGrant($iProfile, $sStimulusCode, $sClass)
  1190. {
  1191. $oNewObj = MetaModel::NewObject("URP_StimulusGrant");
  1192. $oNewObj->Set('profileid', $iProfile);
  1193. $oNewObj->Set('permission', true);
  1194. $oNewObj->Set('class', $sClass);
  1195. $oNewObj->Set('stimulus', $sStimulusCode);
  1196. $iId = $oNewObj->DBInsertNoReload();
  1197. return $iId;
  1198. }
  1199. protected static function DoCreateOneProfile($sName, $aProfileData)
  1200. {
  1201. $sDescription = $aProfileData['description'];
  1202. $aWriteModules = explode(',', $aProfileData['write_modules']);
  1203. $aStimuli = $aProfileData['stimuli'];
  1204. $oNewObj = MetaModel::NewObject("URP_Profiles");
  1205. $oNewObj->Set('name', $sName);
  1206. $oNewObj->Set('description', $sDescription);
  1207. $iProfile = $oNewObj->DBInsertNoReload();
  1208. // Project in every dimension
  1209. //
  1210. $oDimensionSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_Dimensions"));
  1211. while ($oDimension = $oDimensionSet->Fetch())
  1212. {
  1213. $iDimension = $oDimension->GetKey();
  1214. self::DoCreateProfileProjection($iProfile, $iDimension);
  1215. }
  1216. // Grant read rights for everything
  1217. //
  1218. foreach (MetaModel::GetClasses('bizmodel') as $sClass)
  1219. {
  1220. self::DoCreateActionGrant($iProfile, UR_ACTION_READ, $sClass);
  1221. self::DoCreateActionGrant($iProfile, UR_ACTION_BULK_READ, $sClass);
  1222. }
  1223. // Grant write for given modules
  1224. // Start by compiling the information, because some modules may overlap
  1225. $aWriteableClasses = array();
  1226. foreach ($aWriteModules as $sModule)
  1227. {
  1228. // $oPage->p('Granting write access for the module"'.$sModule.'" - '.count(self::$m_aModules[$sModule]).' classes');
  1229. foreach (self::$m_aModules[$sModule] as $sClass)
  1230. {
  1231. $aWriteableClasses[] = $sClass;
  1232. }
  1233. }
  1234. foreach ($aWriteableClasses as $sClass)
  1235. {
  1236. self::DoCreateActionGrant($iProfile, UR_ACTION_MODIFY, $sClass);
  1237. self::DoCreateActionGrant($iProfile, UR_ACTION_DELETE, $sClass);
  1238. self::DoCreateActionGrant($iProfile, UR_ACTION_BULK_MODIFY, $sClass);
  1239. self::DoCreateActionGrant($iProfile, UR_ACTION_BULK_DELETE, $sClass);
  1240. }
  1241. // Grant stimuli for given classes
  1242. foreach ($aStimuli as $sClass => $sAllowedStimuli)
  1243. {
  1244. if ($sAllowedStimuli == 'any')
  1245. {
  1246. $aAllowedStimuli = array_keys(MetaModel::EnumStimuli($sClass));
  1247. }
  1248. elseif ($sAllowedStimuli == 'none')
  1249. {
  1250. $aAllowedStimuli = array();
  1251. }
  1252. else
  1253. {
  1254. $aAllowedStimuli = explode(',', $sAllowedStimuli);
  1255. }
  1256. foreach ($aAllowedStimuli as $sStimulusCode)
  1257. {
  1258. self::DoCreateStimulusGrant($iProfile, $sStimulusCode, $sClass);
  1259. }
  1260. }
  1261. }
  1262. public static function DoCreateProfiles()
  1263. {
  1264. foreach(self::$m_aProfiles as $sName => $aProfileData)
  1265. {
  1266. self::DoCreateOneProfile($sName, $aProfileData);
  1267. }
  1268. }
  1269. }
  1270. UserRights::SelectModule('UserRightsProfile');
  1271. ?>