userrightsprofile.class.inc.php 51 KB

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