userrightsprofile.class.inc.php 60 KB

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