userrightsprofile.class.inc.php 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663
  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. $oOrg = new bizOrganization();
  666. $oOrg->Set('name', 'My Company/Department');
  667. $oOrg->Set('code', 'SOMECODE');
  668. $oOrg->Set('status', 'implementation');
  669. //$oOrg->Set('parent_id', xxx);
  670. $iOrgId = $oOrg->DBInsertNoReload();
  671. // Location : optional
  672. //$oLocation = new bizLocation();
  673. //$oLocation->Set('name', 'MyOffice');
  674. //$oLocation->Set('status', 'implementation');
  675. //$oLocation->Set('org_id', $iOrgId);
  676. //$oLocation->Set('severity', 'high');
  677. //$oLocation->Set('address', 'my building in my city');
  678. //$oLocation->Set('country', 'my country');
  679. //$oLocation->Set('parent_location_id', xxx);
  680. //$iLocationId = $oLocation->DBInsertNoReload();
  681. $oContact = new bizPerson();
  682. $oContact->Set('name', 'My last name');
  683. $oContact->Set('first_name', 'My first name');
  684. $oContact->Set('status', 'available');
  685. $oContact->Set('org_id', $iOrgId);
  686. $oContact->Set('email', 'my.email@foo.org');
  687. $oContact->Set('phone', '');
  688. //$oContact->Set('location_id', $iLocationId);
  689. $oContact->Set('employee_number', '');
  690. $iContactId = $oContact->DBInsertNoReload();
  691. $oUser = new URP_Users();
  692. $oUser->Set('login', $sAdminUser);
  693. $oUser->Set('password', $sAdminPwd);
  694. $oUser->Set('userid', $iContactId);
  695. $iUserId = $oUser->DBInsertNoReload();
  696. // Add this user to the very specific 'admin' profile
  697. $oUserProfile = new URP_UserProfile();
  698. $oUserProfile->Set('userid', $iUserId);
  699. $oUserProfile->Set('profileid', ADMIN_PROFILE_ID);
  700. $oUserProfile->Set('reason', 'By definition, the administrator must have the administrator profile');
  701. $oUserProfile->DBInsertNoReload();
  702. return true;
  703. }
  704. public function IsAdministrator($iUserId)
  705. {
  706. if (in_array($iUserId, $this->m_aAdmins))
  707. {
  708. return true;
  709. }
  710. else
  711. {
  712. return false;
  713. }
  714. }
  715. public function Setup()
  716. {
  717. SetupITILProfiles::DoCreateDimensions();
  718. SetupITILProfiles::DoCreateProfiles();
  719. return true;
  720. }
  721. public function Init()
  722. {
  723. MetaModel::RegisterPlugin('userrights', 'ACbyProfile', array($this, 'CacheData'));
  724. }
  725. protected $m_aUsers = array(); // id -> object
  726. protected $m_aDimensions = array(); // id -> object
  727. protected $m_aClassProj = array(); // class,dimensionid -> object
  728. protected $m_aProfiles = array(); // id -> object
  729. protected $m_aUserProfiles = array(); // userid,profileid -> object
  730. protected $m_aProPro = array(); // profileid,dimensionid -> object
  731. protected $m_aLogin2UserId = array(); // login -> id
  732. protected $m_aAdmins = array(); // id of users being linked to the well-known admin profile
  733. protected $m_aClassActionGrants = array(); // profile, class, action -> permission
  734. protected $m_aClassStimulusGrants = array(); // profile, class, stimulus -> permission
  735. protected $m_aObjectActionGrants = array(); // userid, class, id, action -> permission, list of attributes
  736. public function CacheData()
  737. {
  738. // Could be loaded in a shared memory (?)
  739. $oUserSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_Users"));
  740. while ($oUser = $oUserSet->Fetch())
  741. {
  742. $this->m_aUsers[$oUser->GetKey()] = $oUser;
  743. $this->m_aLogin2UserId[$oUser->Get('login')] = $oUser->GetKey();
  744. }
  745. $oDimensionSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_Dimensions"));
  746. while ($oDimension = $oDimensionSet->Fetch())
  747. {
  748. $this->m_aDimensions[$oDimension->GetKey()] = $oDimension;
  749. }
  750. $oClassProjSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_ClassProjection"));
  751. while ($oClassProj = $oClassProjSet->Fetch())
  752. {
  753. $this->m_aClassProjs[$oClassProj->Get('class')][$oClassProj->Get('dimensionid')] = $oClassProj;
  754. }
  755. $oProfileSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_Profiles"));
  756. while ($oProfile = $oProfileSet->Fetch())
  757. {
  758. $this->m_aProfiles[$oProfile->GetKey()] = $oProfile;
  759. }
  760. $oUserProfileSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_UserProfile"));
  761. while ($oUserProfile = $oUserProfileSet->Fetch())
  762. {
  763. $this->m_aUserProfiles[$oUserProfile->Get('userid')][$oUserProfile->Get('profileid')] = $oUserProfile;
  764. if ($oUserProfile->Get('profileid') == ADMIN_PROFILE_ID)
  765. {
  766. $this->m_aAdmins[] = $oUserProfile->Get('userid');
  767. }
  768. }
  769. $oProProSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_ProfileProjection"));
  770. while ($oProPro = $oProProSet->Fetch())
  771. {
  772. $this->m_aProPros[$oProPro->Get('profileid')][$oProPro->Get('dimensionid')] = $oProPro;
  773. }
  774. /*
  775. echo "<pre>\n";
  776. print_r($this->m_aUsers);
  777. print_r($this->m_aDimensions);
  778. print_r($this->m_aClassProjs);
  779. print_r($this->m_aProfiles);
  780. print_r($this->m_aUserProfiles);
  781. print_r($this->m_aProPros);
  782. echo "</pre>\n";
  783. exit;
  784. */
  785. return true;
  786. }
  787. public function CheckCredentials($sUserName, $sPassword)
  788. {
  789. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_Users WHERE login = :login"), array(), array('login' => $sUserName));
  790. if ($oSet->Count() < 1)
  791. {
  792. // todo: throw an exception?
  793. return false;
  794. }
  795. $oUser = $oSet->Fetch();
  796. if ($oUser->Get('password') == $sPassword)
  797. {
  798. return $oUser->GetKey();
  799. }
  800. // todo: throw an exception?
  801. return false;
  802. }
  803. public function GetUserId($sUserName)
  804. {
  805. if (array_key_exists($sUserName, $this->m_aLogin2UserId))
  806. {
  807. // This happens really when the list of users is being loaded into the cache!!!
  808. $iUserId = $this->m_aLogin2UserId[$sUserName];
  809. return $iUserId;
  810. }
  811. return null;
  812. }
  813. public function GetContactId($sUserName)
  814. {
  815. if (array_key_exists($sUserName, $this->m_aLogin2UserId))
  816. {
  817. // This happens really when the list of users is being loaded into the cache!!!
  818. $iUserId = $this->m_aLogin2UserId[$sUserName];
  819. $oUser = $this->m_aUsers[$iUserId];
  820. return $oUser->Get('userid');
  821. }
  822. return null;
  823. }
  824. public function GetFilter($sUserName, $sClass)
  825. {
  826. $oNullFilter = new DBObjectSearch($sClass);
  827. return $oNullFilter;
  828. }
  829. // This verb has been made public to allow the development of an accurate feedback for the current configuration
  830. public function GetClassActionGrant($iProfile, $sClass, $sAction)
  831. {
  832. if (isset($this->m_aClassActionGrants[$iProfile][$sClass][$sAction]))
  833. {
  834. return $this->m_aClassActionGrants[$iProfile][$sClass][$sAction];
  835. }
  836. // Get the permission for this profile/class/action
  837. $oSearch = DBObjectSearch::FromOQL("SELECT URP_ActionGrant WHERE class = :class AND action = :action AND profileid = :profile AND permission = 'yes'");
  838. $oSet = new DBObjectSet($oSearch, array(), array('class'=>$sClass, 'action'=>$sAction, 'profile'=>$iProfile));
  839. if ($oSet->Count() >= 1)
  840. {
  841. $oGrantRecord = $oSet->Fetch();
  842. }
  843. else
  844. {
  845. $sParentClass = MetaModel::GetParentPersistentClass($sClass);
  846. if (empty($sParentClass))
  847. {
  848. $oGrantRecord = null;
  849. }
  850. else
  851. {
  852. $oGrantRecord = $this->GetClassActionGrant($iProfile, $sParentClass, $sAction);
  853. }
  854. }
  855. $this->m_aClassActionGrants[$iProfile][$sClass][$sAction] = $oGrantRecord;
  856. return $oGrantRecord;
  857. }
  858. protected function GetObjectActionGrant($oUser, $sClass, $iActionCode, /*DBObject*/ $oObject = null)
  859. {
  860. if(is_null($oObject))
  861. {
  862. $iObjectRef = -999;
  863. }
  864. else
  865. {
  866. $iObjectRef = $oObject->GetKey();
  867. }
  868. // load and cache permissions for the current user on the given object
  869. //
  870. $aTest = @$this->m_aObjectActionGrants[$oUser->GetKey()][$sClass][$iObjectRef][$iActionCode];
  871. if (is_array($aTest)) return $aTest;
  872. $sAction = self::$m_aActionCodes[$iActionCode];
  873. $iInstancePermission = UR_ALLOWED_NO;
  874. $aAttributes = array();
  875. foreach($this->GetMatchingProfiles($oUser, $sClass, $oObject) as $iProfile)
  876. {
  877. $oGrantRecord = $this->GetClassActionGrant($iProfile, $sClass, $sAction);
  878. if (is_null($oGrantRecord))
  879. {
  880. continue; // loop to the next profile
  881. }
  882. else
  883. {
  884. $iInstancePermission = UR_ALLOWED_YES;
  885. // update the list of attributes with those allowed for this profile
  886. //
  887. $oSearch = DBObjectSearch::FromOQL("SELECT URP_AttributeGrant WHERE actiongrantid = :actiongrantid");
  888. $oSet = new DBObjectSet($oSearch, array(), array('actiongrantid' => $oGrantRecord->GetKey()));
  889. $aProfileAttributes = $oSet->GetColumnAsArray('attcode', false);
  890. if (count($aProfileAttributes) == 0)
  891. {
  892. $aAllAttributes = array_keys(MetaModel::ListAttributeDefs($sClass));
  893. $aAttributes = array_merge($aAttributes, $aAllAttributes);
  894. }
  895. else
  896. {
  897. $aAttributes = array_merge($aAttributes, $aProfileAttributes);
  898. }
  899. }
  900. }
  901. $aRes = array(
  902. 'permission' => $iInstancePermission,
  903. 'attributes' => $aAttributes,
  904. );
  905. $this->m_aObjectActionGrants[$oUser->GetKey()][$sClass][$iObjectRef][$iActionCode] = $aRes;
  906. return $aRes;
  907. }
  908. public function IsActionAllowed($iUserId, $sClass, $iActionCode, $oInstanceSet = null)
  909. {
  910. $oUser = $this->m_aUsers[$iUserId];
  911. if (is_null($oInstanceSet))
  912. {
  913. $aObjectPermissions = $this->GetObjectActionGrant($oUser, $sClass, $iActionCode);
  914. return $aObjectPermissions['permission'];
  915. }
  916. $oInstanceSet->Rewind();
  917. while($oObject = $oInstanceSet->Fetch())
  918. {
  919. $aObjectPermissions = $this->GetObjectActionGrant($oUser, $sClass, $iActionCode, $oObject);
  920. $iInstancePermission = $aObjectPermissions['permission'];
  921. if (isset($iGlobalPermission))
  922. {
  923. if ($iInstancePermission != $iGlobalPermission)
  924. {
  925. $iGlobalPermission = UR_ALLOWED_DEPENDS;
  926. break;
  927. }
  928. }
  929. else
  930. {
  931. $iGlobalPermission = $iInstancePermission;
  932. }
  933. }
  934. $oInstanceSet->Rewind();
  935. if (isset($iGlobalPermission))
  936. {
  937. return $iGlobalPermission;
  938. }
  939. else
  940. {
  941. return UR_ALLOWED_NO;
  942. }
  943. }
  944. public function IsActionAllowedOnAttribute($iUserId, $sClass, $sAttCode, $iActionCode, $oInstanceSet = null)
  945. {
  946. $oUser = $this->m_aUsers[$iUserId];
  947. if (is_null($oInstanceSet))
  948. {
  949. $aObjectPermissions = $this->GetObjectActionGrant($oUser, $sClass, $iActionCode);
  950. $aAttributes = $aObjectPermissions['attributes'];
  951. if (in_array($sAttCode, $aAttributes))
  952. {
  953. return $aObjectPermissions['permission'];
  954. }
  955. else
  956. {
  957. return UR_ALLOWED_NO;
  958. }
  959. }
  960. $oInstanceSet->Rewind();
  961. while($oObject = $oInstanceSet->Fetch())
  962. {
  963. $aObjectPermissions = $this->GetObjectActionGrant($oUser, $sClass, $iActionCode, $oObject);
  964. $aAttributes = $aObjectPermissions['attributes'];
  965. if (in_array($sAttCode, $aAttributes))
  966. {
  967. $iInstancePermission = $aObjectPermissions['permission'];
  968. }
  969. else
  970. {
  971. $iInstancePermission = UR_ALLOWED_NO;
  972. }
  973. if (isset($iGlobalPermission))
  974. {
  975. if ($iInstancePermission != $iGlobalPermission)
  976. {
  977. $iGlobalPermission = UR_ALLOWED_DEPENDS;
  978. }
  979. }
  980. else
  981. {
  982. $iGlobalPermission = $iInstancePermission;
  983. }
  984. }
  985. $oInstanceSet->Rewind();
  986. if (isset($iGlobalPermission))
  987. {
  988. return $iGlobalPermission;
  989. }
  990. else
  991. {
  992. return UR_ALLOWED_NO;
  993. }
  994. }
  995. // This verb has been made public to allow the development of an accurate feedback for the current configuration
  996. public function GetClassStimulusGrant($iProfile, $sClass, $sStimulusCode)
  997. {
  998. if (isset($this->m_aClassStimulusGrants[$iProfile][$sClass][$sStimulusCode]))
  999. {
  1000. return $this->m_aClassStimulusGrants[$iProfile][$sClass][$sStimulusCode];
  1001. }
  1002. // Get the permission for this profile/class/stimulus
  1003. $oSearch = DBObjectSearch::FromOQL("SELECT URP_StimulusGrant WHERE class = :class AND stimulus = :stimulus AND profileid = :profile AND permission = 'yes'");
  1004. $oSet = new DBObjectSet($oSearch, array(), array('class'=>$sClass, 'stimulus'=>$sStimulusCode, 'profile'=>$iProfile));
  1005. if ($oSet->Count() >= 1)
  1006. {
  1007. $oGrantRecord = $oSet->Fetch();
  1008. }
  1009. else
  1010. {
  1011. $oGrantRecord = null;
  1012. }
  1013. $this->m_aClassStimulusGrants[$iProfile][$sClass][$sStimulusCode] = $oGrantRecord;
  1014. return $oGrantRecord;
  1015. }
  1016. public function IsStimulusAllowed($iUserId, $sClass, $sStimulusCode, $oInstanceSet = null)
  1017. {
  1018. $oUser = $this->m_aUsers[$iUserId];
  1019. // Note: this code is VERY close to the code of IsActionAllowed()
  1020. if (is_null($oInstanceSet))
  1021. {
  1022. $iInstancePermission = UR_ALLOWED_NO;
  1023. foreach($this->GetMatchingProfiles($oUser, $sClass) as $iProfile)
  1024. {
  1025. $oGrantRecord = $this->GetClassStimulusGrant($iProfile, $sClass, $sStimulusCode);
  1026. if (!is_null($oGrantRecord))
  1027. {
  1028. // no need to fetch the record, we've requested the records having permission = 'yes'
  1029. $iInstancePermission = UR_ALLOWED_YES;
  1030. }
  1031. }
  1032. return $iInstancePermission;
  1033. }
  1034. $oInstanceSet->Rewind();
  1035. while($oObject = $oInstanceSet->Fetch())
  1036. {
  1037. $iInstancePermission = UR_ALLOWED_NO;
  1038. foreach($this->GetMatchingProfiles($oUser, $sClass, $oObject) as $iProfile)
  1039. {
  1040. $oGrantRecord = $this->GetClassStimulusGrant($iProfile, $sClass, $sStimulusCode);
  1041. if (!is_null($oGrantRecord))
  1042. {
  1043. // no need to fetch the record, we've requested the records having permission = 'yes'
  1044. $iInstancePermission = UR_ALLOWED_YES;
  1045. }
  1046. }
  1047. if (isset($iGlobalPermission))
  1048. {
  1049. if ($iInstancePermission != $iGlobalPermission)
  1050. {
  1051. $iGlobalPermission = UR_ALLOWED_DEPENDS;
  1052. }
  1053. }
  1054. else
  1055. {
  1056. $iGlobalPermission = $iInstancePermission;
  1057. }
  1058. }
  1059. $oInstanceSet->Rewind();
  1060. if (isset($iGlobalPermission))
  1061. {
  1062. return $iGlobalPermission;
  1063. }
  1064. else
  1065. {
  1066. return UR_ALLOWED_NO;
  1067. }
  1068. }
  1069. protected function GetMatchingProfilesByDim($oUser, $oObject, $oDimension)
  1070. {
  1071. //
  1072. // List profiles for which the user projection overlaps the object projection in the given dimension
  1073. //
  1074. $iUser = $oUser->GetKey();
  1075. $sClass = get_class($oObject);
  1076. $iPKey = $oObject->GetKey();
  1077. $iDimension = $oDimension->GetKey();
  1078. $aObjectProjection = $this->m_aClassProjs[$sClass][$iDimension]->ProjectObject($oObject);
  1079. $aRes = array();
  1080. if (array_key_exists($iUser, $this->m_aUserProfiles))
  1081. {
  1082. foreach ($this->m_aUserProfiles[$iUser] as $iProfile => $oProfile)
  1083. {
  1084. if (is_null($aObjectProjection))
  1085. {
  1086. $aRes[] = $iProfile;
  1087. }
  1088. else
  1089. {
  1090. // user projection to be cached on a given page !
  1091. $aUserProjection = $this->m_aProPros[$iProfile][$iDimension]->ProjectUser($oUser);
  1092. if (is_null($aUserProjection))
  1093. {
  1094. $aRes[] = $iProfile;
  1095. }
  1096. else
  1097. {
  1098. $aMatchingValues = array_intersect($aObjectProjection, $aUserProjection);
  1099. if (count($aMatchingValues) > 0)
  1100. {
  1101. $aRes[] = $iProfile;
  1102. }
  1103. }
  1104. }
  1105. }
  1106. }
  1107. return $aRes;
  1108. }
  1109. protected $m_aMatchingProfiles = array(); // cache of the matching profiles for a given user/object
  1110. protected function GetMatchingProfiles($oUser, $sClass, /*DBObject*/ $oObject = null)
  1111. {
  1112. $iUser = $oUser->GetKey();
  1113. if(is_null($oObject))
  1114. {
  1115. $iObjectRef = -999;
  1116. }
  1117. else
  1118. {
  1119. $iObjectRef = $oObject->GetKey();
  1120. }
  1121. //
  1122. // List profiles for which the user projection overlaps the object projection in each and every dimension
  1123. // Caches the result
  1124. //
  1125. $aTest = @$this->m_aMatchingProfiles[$iUser][$sClass][$iObjectRef];
  1126. if (is_array($aTest))
  1127. {
  1128. return $aTest;
  1129. }
  1130. if (is_null($oObject))
  1131. {
  1132. if (array_key_exists($iUser, $this->m_aUserProfiles))
  1133. {
  1134. $aRes = array_keys($this->m_aUserProfiles[$iUser]);
  1135. }
  1136. else
  1137. {
  1138. // no profile has been defined for this user
  1139. $aRes = array();
  1140. }
  1141. }
  1142. else
  1143. {
  1144. $aProfileRes = array();
  1145. foreach ($this->m_aDimensions as $iDimension => $oDimension)
  1146. {
  1147. foreach ($this->GetMatchingProfilesByDim($oUser, $oObject, $oDimension) as $iProfile)
  1148. {
  1149. @$aProfileRes[$iProfile] += 1;
  1150. }
  1151. }
  1152. $aRes = array();
  1153. $iDimCount = count($this->m_aDimensions);
  1154. foreach ($aProfileRes as $iProfile => $iMatches)
  1155. {
  1156. if ($iMatches == $iDimCount)
  1157. {
  1158. $aRes[] = $iProfile;
  1159. }
  1160. }
  1161. }
  1162. // store into the cache
  1163. $this->m_aMatchingProfiles[$iUser][$sClass][$iObjectRef] = $aRes;
  1164. return $aRes;
  1165. }
  1166. public function FlushPrivileges()
  1167. {
  1168. $this->CacheData();
  1169. }
  1170. }
  1171. //
  1172. // Here is the code that will create some profiles into our user management model, given an ITIL representation of the user profiles
  1173. //
  1174. class SetupITILProfiles
  1175. {
  1176. /*
  1177. Later, maybe ?
  1178. protected static $m_aDimensions = array(
  1179. 'organization' => array(
  1180. 'description' => '',
  1181. 'type' => 'bizOrganization',
  1182. ),
  1183. 'site' => array(
  1184. 'description' => '',
  1185. 'type' => '',
  1186. ),
  1187. );
  1188. */
  1189. protected static $m_aDimensions = array(
  1190. 'organization' => array(
  1191. 'description' => '',
  1192. 'type' => 'bizOrganization',
  1193. ),
  1194. );
  1195. protected static $m_aActions = array(
  1196. UR_ACTION_READ => 'Read',
  1197. UR_ACTION_MODIFY => 'Modify',
  1198. UR_ACTION_DELETE => 'Delete',
  1199. UR_ACTION_BULK_READ => 'Bulk Read',
  1200. UR_ACTION_BULK_MODIFY => 'Bulk Modify',
  1201. UR_ACTION_BULK_DELETE => 'Bulk Delete',
  1202. );
  1203. // It is possible to specify the same class in several modules
  1204. //
  1205. protected static $m_aModules = array(
  1206. 'General' => array(
  1207. 'bizOrganization',
  1208. ),
  1209. 'Documentation' => array(
  1210. 'bizDocVersion',
  1211. 'lnkDocumentRealObject',
  1212. 'lnkDocumentContract',
  1213. 'lnkDocumentError',
  1214. ),
  1215. 'Configuration' => array(
  1216. 'logRealObject',
  1217. 'lnkContactRealObject',
  1218. 'lnkInterfaces',
  1219. 'ClientServerLinks',
  1220. 'lnkInfraGrouping',
  1221. ),
  1222. 'Incident' => array(
  1223. 'bizIncidentTicket',
  1224. 'lnkRelatedTicket',
  1225. 'lnkInfraTicket',
  1226. 'lnkContactTicket',
  1227. ),
  1228. 'Problem' => array(
  1229. 'bizKnownError',
  1230. 'lnkInfraError',
  1231. 'lnkDocumentError',
  1232. ),
  1233. 'Change' => array(
  1234. 'bizChangeTicket',
  1235. 'lnkInfraChangeTicket',
  1236. 'lnkContactChange',
  1237. ),
  1238. 'Service' => array(
  1239. 'bizContract',
  1240. 'lnkInfraContract',
  1241. 'lnkContactContract',
  1242. 'lnkDocumentContract',
  1243. ),
  1244. 'Call' => array(
  1245. 'bizServiceCall',
  1246. 'lnkCallTicket',
  1247. 'lnkInfraCall',
  1248. ),
  1249. );
  1250. protected static $m_aProfiles = array(
  1251. 'Configuration Manager' => array(
  1252. 'description' => 'Person in charge of the documentation of the managed CIs',
  1253. 'write_modules' => 'Documentation,Configuration',
  1254. 'stimuli' => array(
  1255. 'bizServer' => 'any',
  1256. //'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',
  1257. 'bizContract' => 'none',
  1258. 'bizIncidentTicket' => 'none',
  1259. 'bizChangeTicket' => 'none',
  1260. ),
  1261. ),
  1262. /* 'Requestor pb granularite actions (create/delete)' => array(
  1263. 'description' => 'Person notifying an incident',
  1264. 'write_modules' => 'Incident',
  1265. 'stimuli' => array(
  1266. 'bizServer' => 'none',
  1267. 'bizContract' => 'none',
  1268. 'bizIncidentTicket' => 'none',
  1269. 'bizChangeTicket' => 'none',
  1270. ),
  1271. ),
  1272. */
  1273. 'Service Desk Agent' => array(
  1274. 'description' => 'Person in charge of creating incident reports',
  1275. 'write_modules' => 'Documentation,Incident,Call',
  1276. 'stimuli' => array(
  1277. 'bizServer' => 'none',
  1278. 'bizContract' => 'none',
  1279. 'bizIncidentTicket' => 'ev_assign',
  1280. 'bizChangeTicket' => 'none',
  1281. 'bizServiceCall' => 'any',
  1282. ),
  1283. ),
  1284. 'Support Agent' => array(
  1285. 'description' => 'Person analyzing and solving the current incidents or problems',
  1286. 'write_modules' => 'Documentation,Incident,Problem',
  1287. 'stimuli' => array(
  1288. 'bizIncidentTicket' => 'any',
  1289. //'bizIncidentTicket' => 'ev_assign,ev_reassign,ev_start_working,ev_close',
  1290. ),
  1291. ),
  1292. 'Change Implementor' => array(
  1293. 'description' => 'Person executing the changes',
  1294. 'write_modules' => 'Documentation,Configuration,Change',
  1295. 'stimuli' => array(
  1296. 'bizServer' => 'none',
  1297. 'bizContract' => 'none',
  1298. 'bizIncidentTicket' => 'none',
  1299. 'bizChangeTicket' => 'ev_plan,ev_replan,ev_implement,ev_monitor',
  1300. ),
  1301. ),
  1302. 'Change Supervisor' => array(
  1303. 'description' => 'Person responsible for the overall change execution',
  1304. 'write_modules' => 'Documentation,Change',
  1305. 'stimuli' => array(
  1306. 'bizServer' => 'none',
  1307. 'bizContract' => 'none',
  1308. 'bizIncidentTicket' => 'none',
  1309. 'bizChangeTicket' => 'ev_validate,ev_reject,ev_reopen,ev_finish',
  1310. ),
  1311. ),
  1312. 'Change Approver' => array(
  1313. 'description' => 'Person who could be impacted by some changes',
  1314. 'write_modules' => 'Documentation,Change',
  1315. 'stimuli' => array(
  1316. 'bizServer' => 'none',
  1317. 'bizContract' => 'none',
  1318. 'bizIncidentTicket' => 'none',
  1319. 'bizChangeTicket' => 'ev_approve,ev_notapprove',
  1320. ),
  1321. ),
  1322. 'Service Manager' => array(
  1323. 'description' => 'Person responsible for the service delivered to the [internal] customer',
  1324. 'write_modules' => 'Documentation,Service',
  1325. 'stimuli' => array(
  1326. 'bizServer' => 'none',
  1327. 'bizContract' => 'any',
  1328. //'bizContract' => 'ev_freeze_version,ev_sign,ev_begin,ev_notice,ev_terminate,ev_elapsed',
  1329. 'bizIncidentTicket' => 'none',
  1330. 'bizChangeTicket' => 'none',
  1331. ),
  1332. ),
  1333. );
  1334. protected static function DoCreateClassProjection($iDimension, $sClass)
  1335. {
  1336. $oNewObj = MetaModel::NewObject("URP_ClassProjection");
  1337. $oNewObj->Set('dimensionid', $iDimension);
  1338. $oNewObj->Set('class', $sClass);
  1339. $oNewObj->Set('attribute', '');
  1340. $iId = $oNewObj->DBInsertNoReload();
  1341. return $iId;
  1342. }
  1343. protected static function DoCreateDimension($sName, $aDimensionData)
  1344. {
  1345. $oNewObj = MetaModel::NewObject("URP_Dimensions");
  1346. $oNewObj->Set('name', $sName);
  1347. $oNewObj->Set('description', $aDimensionData['description']);
  1348. $oNewObj->Set('type', $aDimensionData['type']);
  1349. $iId = $oNewObj->DBInsertNoReload();
  1350. return $iId;
  1351. }
  1352. protected static function DoCreateProfileProjection($iProfile, $iDimension)
  1353. {
  1354. $oNewObj = MetaModel::NewObject("URP_ProfileProjection");
  1355. $oNewObj->Set('profileid', $iProfile);
  1356. $oNewObj->Set('dimensionid', $iDimension);
  1357. $oNewObj->Set('value', '<any>');
  1358. $oNewObj->Set('attribute', '');
  1359. $iId = $oNewObj->DBInsertNoReload();
  1360. return $iId;
  1361. }
  1362. protected static function DoCreateActionGrant($iProfile, $iAction, $sClass, $bPermission = true)
  1363. {
  1364. $oNewObj = MetaModel::NewObject("URP_ActionGrant");
  1365. $oNewObj->Set('profileid', $iProfile);
  1366. $oNewObj->Set('permission', $bPermission);
  1367. $oNewObj->Set('class', $sClass);
  1368. $oNewObj->Set('action', self::$m_aActions[$iAction]);
  1369. $iId = $oNewObj->DBInsertNoReload();
  1370. return $iId;
  1371. }
  1372. protected static function DoCreateStimulusGrant($iProfile, $sStimulusCode, $sClass)
  1373. {
  1374. $oNewObj = MetaModel::NewObject("URP_StimulusGrant");
  1375. $oNewObj->Set('profileid', $iProfile);
  1376. $oNewObj->Set('permission', true);
  1377. $oNewObj->Set('class', $sClass);
  1378. $oNewObj->Set('stimulus', $sStimulusCode);
  1379. $iId = $oNewObj->DBInsertNoReload();
  1380. return $iId;
  1381. }
  1382. protected static function DoCreateAdminProfile()
  1383. {
  1384. $oNewObj = MetaModel::NewObject("URP_Profiles");
  1385. $oNewObj->Set('name', 'Administrator');
  1386. $oNewObj->Set('description', 'Has the rights on everything (bypassing any control)');
  1387. $iNewId = $oNewObj->DBInsertNoReload();
  1388. if ($iNewId != ADMIN_PROFILE_ID)
  1389. {
  1390. throw new CoreException('Admin profile could not be created with its standard id', array('requested'=>ADMIN_PROFILE_ID, 'obtained'=>$iNewId));
  1391. }
  1392. }
  1393. protected static function DoCreateOneProfile($sName, $aProfileData)
  1394. {
  1395. $sDescription = $aProfileData['description'];
  1396. $aWriteModules = explode(',', $aProfileData['write_modules']);
  1397. $aStimuli = $aProfileData['stimuli'];
  1398. $oNewObj = MetaModel::NewObject("URP_Profiles");
  1399. $oNewObj->Set('name', $sName);
  1400. $oNewObj->Set('description', $sDescription);
  1401. $iProfile = $oNewObj->DBInsertNoReload();
  1402. // Project in every dimension
  1403. //
  1404. $oDimensionSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_Dimensions"));
  1405. while ($oDimension = $oDimensionSet->Fetch())
  1406. {
  1407. $iDimension = $oDimension->GetKey();
  1408. self::DoCreateProfileProjection($iProfile, $iDimension);
  1409. }
  1410. // Grant read rights for everything
  1411. //
  1412. foreach (MetaModel::GetClasses('bizmodel') as $sClass)
  1413. {
  1414. self::DoCreateActionGrant($iProfile, UR_ACTION_READ, $sClass);
  1415. self::DoCreateActionGrant($iProfile, UR_ACTION_BULK_READ, $sClass);
  1416. }
  1417. // Grant write for given modules
  1418. // Start by compiling the information, because some modules may overlap
  1419. $aWriteableClasses = array();
  1420. foreach ($aWriteModules as $sModule)
  1421. {
  1422. // $oPage->p('Granting write access for the module"'.$sModule.'" - '.count(self::$m_aModules[$sModule]).' classes');
  1423. foreach (self::$m_aModules[$sModule] as $sClass)
  1424. {
  1425. $aWriteableClasses[] = $sClass;
  1426. }
  1427. }
  1428. foreach ($aWriteableClasses as $sClass)
  1429. {
  1430. self::DoCreateActionGrant($iProfile, UR_ACTION_MODIFY, $sClass);
  1431. self::DoCreateActionGrant($iProfile, UR_ACTION_DELETE, $sClass);
  1432. self::DoCreateActionGrant($iProfile, UR_ACTION_BULK_MODIFY, $sClass);
  1433. self::DoCreateActionGrant($iProfile, UR_ACTION_BULK_DELETE, $sClass);
  1434. }
  1435. // Grant stimuli for given classes
  1436. foreach ($aStimuli as $sClass => $sAllowedStimuli)
  1437. {
  1438. if ($sAllowedStimuli == 'any')
  1439. {
  1440. $aAllowedStimuli = array_keys(MetaModel::EnumStimuli($sClass));
  1441. }
  1442. elseif ($sAllowedStimuli == 'none')
  1443. {
  1444. $aAllowedStimuli = array();
  1445. }
  1446. else
  1447. {
  1448. $aAllowedStimuli = explode(',', $sAllowedStimuli);
  1449. }
  1450. foreach ($aAllowedStimuli as $sStimulusCode)
  1451. {
  1452. self::DoCreateStimulusGrant($iProfile, $sStimulusCode, $sClass);
  1453. }
  1454. }
  1455. }
  1456. public static function DoCreateDimensions()
  1457. {
  1458. $aClass = MetaModel::GetClasses();
  1459. foreach(self::$m_aDimensions as $sName => $aDimensionData)
  1460. {
  1461. $iDimension = self::DoCreateDimension($sName, $aDimensionData);
  1462. foreach($aClass as $sClass)
  1463. {
  1464. self::DoCreateClassProjection($iDimension, $sClass);
  1465. }
  1466. }
  1467. }
  1468. public static function DoCreateProfiles()
  1469. {
  1470. self::DoCreateAdminProfile();
  1471. foreach(self::$m_aProfiles as $sName => $aProfileData)
  1472. {
  1473. self::DoCreateOneProfile($sName, $aProfileData);
  1474. }
  1475. }
  1476. }
  1477. UserRights::SelectModule('UserRightsProfile');
  1478. ?>