userrightsprofile.class.inc.php 50 KB

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