userrightsprofile.class.inc.php 59 KB

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