userrightsprofile.class.inc.php 60 KB

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