userrightsprofile.class.inc.php 59 KB

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