userrightsprofile.class.inc.php 52 KB

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