userrightsprojection.class.inc.php 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594
  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. * UserRightsProjection
  18. * User management Module, basing the right on profiles and a matrix (similar to UserRightsProfile, but enhanced with dimensions and projection of classes and profile over the dimensions)
  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 UserRightsProjection
  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_AllData($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. protected $m_aUserProjections; // cache
  312. public function ProjectUser(User $oUser)
  313. {
  314. if (is_array($this->m_aUserProjections))
  315. {
  316. // Hit!
  317. return $this->m_aUserProjections;
  318. }
  319. $sExpr = $this->Get('value');
  320. if ($sExpr == '<user>')
  321. {
  322. $sColumn = $this->Get('attribute');
  323. if (empty($sColumn))
  324. {
  325. $aRes = array($oUser->GetKey());
  326. }
  327. else
  328. {
  329. $aRes = array($oUser->Get($sColumn));
  330. }
  331. }
  332. elseif (($sExpr == '<any>') || ($sExpr == ''))
  333. {
  334. $aRes = null;
  335. }
  336. elseif (strtolower(substr($sExpr, 0, 6)) == 'select')
  337. {
  338. $sColumn = $this->Get('attribute');
  339. // SELECT...
  340. $oValueSetDef = new ValueSetObjects($sExpr, $sColumn, array(), true /*allow all data*/);
  341. $aRes = $oValueSetDef->GetValues(array('user' => $oUser), '');
  342. }
  343. else
  344. {
  345. // Constant value(s)
  346. $aRes = explode(';', trim($sExpr));
  347. }
  348. $this->m_aUserProjections = $aRes;
  349. return $aRes;
  350. }
  351. }
  352. class URP_ClassProjection extends UserRightsBaseClass
  353. {
  354. public static function Init()
  355. {
  356. $aParams = array
  357. (
  358. "category" => "addon/userrights",
  359. "key_type" => "autoincrement",
  360. "name_attcode" => "dimensionid",
  361. "state_attcode" => "",
  362. "reconc_keys" => array(),
  363. "db_table" => "priv_urp_classprojection",
  364. "db_key_field" => "id",
  365. "db_finalclass_field" => "",
  366. "display_template" => "",
  367. );
  368. MetaModel::Init_Params($aParams);
  369. //MetaModel::Init_InheritAttributes();
  370. 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())));
  371. MetaModel::Init_AddAttribute(new AttributeExternalField("dimension", array("allowed_values"=>null, "extkey_attcode"=> 'dimensionid', "target_attcode"=>"name")));
  372. MetaModel::Init_AddAttribute(new AttributeClass("class", array("class_category"=>"", "more_values"=>"", "sql"=>"class", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  373. MetaModel::Init_AddAttribute(new AttributeString("value", array("allowed_values"=>null, "sql"=>"value", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  374. MetaModel::Init_AddAttribute(new AttributeString("attribute", array("allowed_values"=>null, "sql"=>"attribute", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  375. // Display lists
  376. MetaModel::Init_SetZListItems('details', array('dimensionid', 'class', 'value', 'attribute')); // Attributes to be displayed for the complete details
  377. MetaModel::Init_SetZListItems('list', array('class', 'value', 'attribute')); // Attributes to be displayed for a list
  378. // Search criteria
  379. MetaModel::Init_SetZListItems('standard_search', array('dimensionid', 'class')); // Criteria of the std search form
  380. MetaModel::Init_SetZListItems('advanced_search', array('dimensionid', 'class')); // Criteria of the advanced search form
  381. }
  382. public function ProjectObject($oObject)
  383. {
  384. $sExpr = $this->Get('value');
  385. if ($sExpr == '<this>')
  386. {
  387. $sColumn = $this->Get('attribute');
  388. if (empty($sColumn))
  389. {
  390. $aRes = array($oObject->GetKey());
  391. }
  392. else
  393. {
  394. $aRes = array($oObject->Get($sColumn));
  395. }
  396. }
  397. elseif (($sExpr == '<any>') || ($sExpr == ''))
  398. {
  399. $aRes = null;
  400. }
  401. elseif (strtolower(substr($sExpr, 0, 6)) == 'select')
  402. {
  403. $sColumn = $this->Get('attribute');
  404. // SELECT...
  405. $oValueSetDef = new ValueSetObjects($sExpr, $sColumn, array(), true /*allow all data*/);
  406. $aRes = $oValueSetDef->GetValues(array('this' => $oObject), '');
  407. }
  408. else
  409. {
  410. // Constant value(s)
  411. $aRes = explode(';', trim($sExpr));
  412. }
  413. return $aRes;
  414. }
  415. }
  416. class URP_ActionGrant extends UserRightsBaseClass
  417. {
  418. public static function Init()
  419. {
  420. $aParams = array
  421. (
  422. "category" => "addon/userrights",
  423. "key_type" => "autoincrement",
  424. "name_attcode" => "profileid",
  425. "state_attcode" => "",
  426. "reconc_keys" => array(),
  427. "db_table" => "priv_urp_grant_actions",
  428. "db_key_field" => "id",
  429. "db_finalclass_field" => "",
  430. "display_template" => "",
  431. );
  432. MetaModel::Init_Params($aParams);
  433. //MetaModel::Init_InheritAttributes();
  434. // Common to all grant classes (could be factorized by class inheritence, but this has to be benchmarked)
  435. 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())));
  436. MetaModel::Init_AddAttribute(new AttributeExternalField("profile", array("allowed_values"=>null, "extkey_attcode"=> 'profileid', "target_attcode"=>"name")));
  437. MetaModel::Init_AddAttribute(new AttributeClass("class", array("class_category"=>"", "more_values"=>"", "sql"=>"class", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  438. 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())));
  439. MetaModel::Init_AddAttribute(new AttributeString("action", array("allowed_values"=>null, "sql"=>"action", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  440. // Display lists
  441. MetaModel::Init_SetZListItems('details', array('profileid', 'class', 'permission', 'action')); // Attributes to be displayed for the complete details
  442. MetaModel::Init_SetZListItems('list', array('class', 'permission', 'action')); // Attributes to be displayed for a list
  443. // Search criteria
  444. MetaModel::Init_SetZListItems('standard_search', array('profileid', 'class', 'permission', 'action')); // Criteria of the std search form
  445. MetaModel::Init_SetZListItems('advanced_search', array('profileid', 'class', 'permission', 'action')); // Criteria of the advanced search form
  446. }
  447. }
  448. class URP_StimulusGrant extends UserRightsBaseClass
  449. {
  450. public static function Init()
  451. {
  452. $aParams = array
  453. (
  454. "category" => "addon/userrights",
  455. "key_type" => "autoincrement",
  456. "name_attcode" => "profileid",
  457. "state_attcode" => "",
  458. "reconc_keys" => array(),
  459. "db_table" => "priv_urp_grant_stimulus",
  460. "db_key_field" => "id",
  461. "db_finalclass_field" => "",
  462. "display_template" => "",
  463. );
  464. MetaModel::Init_Params($aParams);
  465. //MetaModel::Init_InheritAttributes();
  466. // Common to all grant classes (could be factorized by class inheritence, but this has to be benchmarked)
  467. 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())));
  468. MetaModel::Init_AddAttribute(new AttributeExternalField("profile", array("allowed_values"=>null, "extkey_attcode"=> 'profileid', "target_attcode"=>"name")));
  469. MetaModel::Init_AddAttribute(new AttributeClass("class", array("class_category"=>"", "more_values"=>"", "sql"=>"class", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  470. 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())));
  471. MetaModel::Init_AddAttribute(new AttributeString("stimulus", array("allowed_values"=>null, "sql"=>"action", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  472. // Display lists
  473. MetaModel::Init_SetZListItems('details', array('profileid', 'class', 'permission', 'stimulus')); // Attributes to be displayed for the complete details
  474. MetaModel::Init_SetZListItems('list', array('class', 'permission', 'stimulus')); // Attributes to be displayed for a list
  475. // Search criteria
  476. MetaModel::Init_SetZListItems('standard_search', array('profileid', 'class', 'permission', 'stimulus')); // Criteria of the std search form
  477. MetaModel::Init_SetZListItems('advanced_search', array('profileid', 'class', 'permission', 'stimulus')); // Criteria of the advanced search form
  478. }
  479. }
  480. class URP_AttributeGrant extends UserRightsBaseClass
  481. {
  482. public static function Init()
  483. {
  484. $aParams = array
  485. (
  486. "category" => "addon/userrights",
  487. "key_type" => "autoincrement",
  488. "name_attcode" => "actiongrantid",
  489. "state_attcode" => "",
  490. "reconc_keys" => array(),
  491. "db_table" => "priv_urp_grant_attributes",
  492. "db_key_field" => "id",
  493. "db_finalclass_field" => "",
  494. "display_template" => "",
  495. );
  496. MetaModel::Init_Params($aParams);
  497. //MetaModel::Init_InheritAttributes();
  498. 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())));
  499. MetaModel::Init_AddAttribute(new AttributeString("attcode", array("allowed_values"=>null, "sql"=>"attcode", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  500. // Display lists
  501. MetaModel::Init_SetZListItems('details', array('actiongrantid', 'attcode')); // Attributes to be displayed for the complete details
  502. MetaModel::Init_SetZListItems('list', array('attcode')); // Attributes to be displayed for a list
  503. // Search criteria
  504. MetaModel::Init_SetZListItems('standard_search', array('actiongrantid', 'attcode')); // Criteria of the std search form
  505. MetaModel::Init_SetZListItems('advanced_search', array('actiongrantid', 'attcode')); // Criteria of the advanced search form
  506. }
  507. }
  508. class UserRightsProjection extends UserRightsAddOnAPI
  509. {
  510. static public $m_aActionCodes = array(
  511. UR_ACTION_READ => 'read',
  512. UR_ACTION_MODIFY => 'modify',
  513. UR_ACTION_DELETE => 'delete',
  514. UR_ACTION_BULK_READ => 'bulk read',
  515. UR_ACTION_BULK_MODIFY => 'bulk modify',
  516. UR_ACTION_BULK_DELETE => 'bulk delete',
  517. );
  518. // Installation: create the very first user
  519. public function CreateAdministrator($sAdminUser, $sAdminPwd, $sLanguage = 'EN US')
  520. {
  521. // Create a change to record the history of the User object
  522. $oChange = MetaModel::NewObject("CMDBChange");
  523. $oChange->Set("date", time());
  524. $oChange->Set("userinfo", "Initialization");
  525. $iChangeId = $oChange->DBInsert();
  526. $oOrg = new Organization();
  527. $oOrg->Set('name', 'My Company/Department');
  528. $oOrg->Set('code', 'SOMECODE');
  529. // $oOrg->Set('status', 'implementation');
  530. //$oOrg->Set('parent_id', xxx);
  531. $iOrgId = $oOrg->DBInsertTrackedNoReload($oChange);
  532. // Location : optional
  533. //$oLocation = new bizLocation();
  534. //$oLocation->Set('name', 'MyOffice');
  535. //$oLocation->Set('status', 'implementation');
  536. //$oLocation->Set('org_id', $iOrgId);
  537. //$oLocation->Set('severity', 'high');
  538. //$oLocation->Set('address', 'my building in my city');
  539. //$oLocation->Set('country', 'my country');
  540. //$oLocation->Set('parent_location_id', xxx);
  541. //$iLocationId = $oLocation->DBInsertNoReload();
  542. $oContact = new Person();
  543. $oContact->Set('name', 'My last name');
  544. $oContact->Set('first_name', 'My first name');
  545. //$oContact->Set('status', 'available');
  546. $oContact->Set('org_id', $iOrgId);
  547. $oContact->Set('email', 'my.email@foo.org');
  548. //$oContact->Set('phone', '');
  549. //$oContact->Set('location_id', $iLocationId);
  550. //$oContact->Set('employee_number', '');
  551. $iContactId = $oContact->DBInsertTrackedNoReload($oChange);
  552. $oUser = new UserLocal();
  553. $oUser->Set('login', $sAdminUser);
  554. $oUser->Set('password', $sAdminPwd);
  555. $oUser->Set('contactid', $iContactId);
  556. $oUser->Set('language', $sLanguage); // Language was chosen during the installation
  557. $iUserId = $oUser->DBInsertTrackedNoReload($oChange);
  558. // Add this user to the very specific 'admin' profile
  559. $oUserProfile = new URP_UserProfile();
  560. $oUserProfile->Set('userid', $iUserId);
  561. $oUserProfile->Set('profileid', ADMIN_PROFILE_ID);
  562. $oUserProfile->Set('reason', 'By definition, the administrator must have the administrator profile');
  563. $oUserProfile->DBInsertTrackedNoReload($oChange);
  564. return true;
  565. }
  566. public function IsAdministrator($oUser)
  567. {
  568. if (in_array($oUser->GetKey(), $this->m_aAdmins))
  569. {
  570. return true;
  571. }
  572. else
  573. {
  574. return false;
  575. }
  576. }
  577. public function Setup()
  578. {
  579. SetupProfiles::ComputeITILProfiles();
  580. //SetupProfiles::ComputeBasicProfiles();
  581. SetupProfiles::DoCreateDimensions();
  582. SetupProfiles::DoCreateProfiles();
  583. return true;
  584. }
  585. public function Init()
  586. {
  587. MetaModel::RegisterPlugin('userrights', 'ACbyProfile', array($this, 'CacheData'));
  588. }
  589. protected $m_aDimensions = array(); // id -> object
  590. protected $m_aClassProj = array(); // class,dimensionid -> object
  591. protected $m_aProfiles = array(); // id -> object
  592. protected $m_aUserProfiles = array(); // userid,profileid -> object
  593. protected $m_aProPro = array(); // profileid,dimensionid -> object
  594. protected $m_aAdmins = array(); // id of users being linked to the well-known admin profile
  595. protected $m_aClassActionGrants = array(); // profile, class, action -> permission
  596. protected $m_aClassStimulusGrants = array(); // profile, class, stimulus -> permission
  597. protected $m_aObjectActionGrants = array(); // userid, class, id, action -> permission, list of attributes
  598. public function CacheData()
  599. {
  600. // Could be loaded in a shared memory (?)
  601. $oDimensionSet = new DBObjectSet(DBObjectSearch::FromOQL_AllData("SELECT URP_Dimensions"));
  602. $this->m_aDimensions = array();
  603. while ($oDimension = $oDimensionSet->Fetch())
  604. {
  605. $this->m_aDimensions[$oDimension->GetKey()] = $oDimension;
  606. }
  607. $oClassProjSet = new DBObjectSet(DBObjectSearch::FromOQL_AllData("SELECT URP_ClassProjection"));
  608. $this->m_aClassProjs = array();
  609. while ($oClassProj = $oClassProjSet->Fetch())
  610. {
  611. $this->m_aClassProjs[$oClassProj->Get('class')][$oClassProj->Get('dimensionid')] = $oClassProj;
  612. }
  613. $oProfileSet = new DBObjectSet(DBObjectSearch::FromOQL_AllData("SELECT URP_Profiles"));
  614. $this->m_aProfiles = array();
  615. while ($oProfile = $oProfileSet->Fetch())
  616. {
  617. $this->m_aProfiles[$oProfile->GetKey()] = $oProfile;
  618. }
  619. $oUserProfileSet = new DBObjectSet(DBObjectSearch::FromOQL_AllData("SELECT URP_UserProfile"));
  620. $this->m_aUserProfiles = array();
  621. $this->m_aAdmins = array();
  622. while ($oUserProfile = $oUserProfileSet->Fetch())
  623. {
  624. $this->m_aUserProfiles[$oUserProfile->Get('userid')][$oUserProfile->Get('profileid')] = $oUserProfile;
  625. if ($oUserProfile->Get('profileid') == ADMIN_PROFILE_ID)
  626. {
  627. $this->m_aAdmins[] = $oUserProfile->Get('userid');
  628. }
  629. }
  630. $oProProSet = new DBObjectSet(DBObjectSearch::FromOQL_AllData("SELECT URP_ProfileProjection"));
  631. $this->m_aProPros = array();
  632. while ($oProPro = $oProProSet->Fetch())
  633. {
  634. $this->m_aProPros[$oProPro->Get('profileid')][$oProPro->Get('dimensionid')] = $oProPro;
  635. }
  636. /*
  637. echo "<pre>\n";
  638. print_r($this->m_aDimensions);
  639. print_r($this->m_aClassProjs);
  640. print_r($this->m_aProfiles);
  641. print_r($this->m_aUserProfiles);
  642. print_r($this->m_aProPros);
  643. echo "</pre>\n";
  644. exit;
  645. */
  646. return true;
  647. }
  648. public function GetSelectFilter($oUser, $sClass)
  649. {
  650. $aConditions = array();
  651. foreach ($this->m_aDimensions as $iDimension => $oDimension)
  652. {
  653. $oClassProj = @$this->m_aClassProjs[$sClass][$iDimension];
  654. if (is_null($oClassProj))
  655. {
  656. // Authorize any for this dimension, then no additional criteria is required
  657. continue;
  658. }
  659. // 1 - Get class projection info
  660. //
  661. $oExpression = null;
  662. $sExpr = $oClassProj->Get('value');
  663. if ($sExpr == '<this>')
  664. {
  665. $sColumn = $oClassProj->Get('attribute');
  666. if (empty($sColumn))
  667. {
  668. $oExpression = new FieldExpression('id', $sClass);
  669. }
  670. else
  671. {
  672. $oExpression = new FieldExpression($sColumn, $sClass);
  673. }
  674. }
  675. elseif (($sExpr == '<any>') || ($sExpr == ''))
  676. {
  677. // Authorize any for this dimension, then no additional criteria is required
  678. continue;
  679. }
  680. elseif (strtolower(substr($sExpr, 0, 6)) == 'select')
  681. {
  682. throw new CoreException('Sorry, projections by the mean of OQL are not supported currently, please specify an attribute instead', array('class' => $sClass, 'expression' => $sExpr));
  683. }
  684. else
  685. {
  686. // Constant value(s)
  687. // unsupported
  688. throw new CoreException('Sorry, constant projections are not supported currently, please specify an attribute instead', array('class' => $sClass, 'expression' => $sExpr));
  689. // $aRes = explode(';', trim($sExpr));
  690. }
  691. // 2 - Get profile projection info and use it if needed
  692. //
  693. $aProjections = self::GetReadableProjectionsByDim($oUser, $sClass, $oDimension);
  694. if (is_null($aProjections))
  695. {
  696. // Authorize any for this dimension, then no additional criteria is required
  697. continue;
  698. }
  699. elseif (count($aProjections) == 0)
  700. {
  701. // Authorize none, then exit as quickly as possible
  702. return false;
  703. }
  704. else
  705. {
  706. // Authorize the given set of values
  707. $oListExpr = ListExpression::FromScalars($aProjections);
  708. $oCondition = new BinaryExpression($oExpression, 'IN', $oListExpr);
  709. $aConditions[] = $oCondition;
  710. }
  711. }
  712. if (count($aConditions) == 0)
  713. {
  714. // allow all
  715. return true;
  716. }
  717. else
  718. {
  719. $oFilter = new DBObjectSearch($sClass);
  720. foreach($aConditions as $oCondition)
  721. {
  722. $oFilter->AddConditionExpression($oCondition);
  723. }
  724. //return true;
  725. return $oFilter;
  726. }
  727. }
  728. // This verb has been made public to allow the development of an accurate feedback for the current configuration
  729. public function GetClassActionGrant($iProfile, $sClass, $sAction)
  730. {
  731. if (isset($this->m_aClassActionGrants[$iProfile][$sClass][$sAction]))
  732. {
  733. return $this->m_aClassActionGrants[$iProfile][$sClass][$sAction];
  734. }
  735. // Get the permission for this profile/class/action
  736. $oSearch = DBObjectSearch::FromOQL_AllData("SELECT URP_ActionGrant WHERE class = :class AND action = :action AND profileid = :profile AND permission = 'yes'");
  737. $oSet = new DBObjectSet($oSearch, array(), array('class'=>$sClass, 'action'=>$sAction, 'profile'=>$iProfile));
  738. if ($oSet->Count() >= 1)
  739. {
  740. $oGrantRecord = $oSet->Fetch();
  741. }
  742. else
  743. {
  744. $sParentClass = MetaModel::GetParentPersistentClass($sClass);
  745. if (empty($sParentClass))
  746. {
  747. $oGrantRecord = null;
  748. }
  749. else
  750. {
  751. $oGrantRecord = $this->GetClassActionGrant($iProfile, $sParentClass, $sAction);
  752. }
  753. }
  754. $this->m_aClassActionGrants[$iProfile][$sClass][$sAction] = $oGrantRecord;
  755. return $oGrantRecord;
  756. }
  757. protected function GetObjectActionGrant($oUser, $sClass, $iActionCode, /*DBObject*/ $oObject = null)
  758. {
  759. if(is_null($oObject))
  760. {
  761. $iObjectRef = -999;
  762. }
  763. else
  764. {
  765. $iObjectRef = $oObject->GetKey();
  766. }
  767. // load and cache permissions for the current user on the given object
  768. //
  769. $aTest = @$this->m_aObjectActionGrants[$oUser->GetKey()][$sClass][$iObjectRef][$iActionCode];
  770. if (is_array($aTest)) return $aTest;
  771. $sAction = self::$m_aActionCodes[$iActionCode];
  772. $iInstancePermission = UR_ALLOWED_NO;
  773. $aAttributes = array();
  774. foreach($this->GetMatchingProfiles($oUser, $sClass, $oObject) as $iProfile)
  775. {
  776. $oGrantRecord = $this->GetClassActionGrant($iProfile, $sClass, $sAction);
  777. if (is_null($oGrantRecord))
  778. {
  779. continue; // loop to the next profile
  780. }
  781. else
  782. {
  783. $iInstancePermission = UR_ALLOWED_YES;
  784. // update the list of attributes with those allowed for this profile
  785. //
  786. $oSearch = DBObjectSearch::FromOQL_AllData("SELECT URP_AttributeGrant WHERE actiongrantid = :actiongrantid");
  787. $oSet = new DBObjectSet($oSearch, array(), array('actiongrantid' => $oGrantRecord->GetKey()));
  788. $aProfileAttributes = $oSet->GetColumnAsArray('attcode', false);
  789. if (count($aProfileAttributes) == 0)
  790. {
  791. $aAllAttributes = array_keys(MetaModel::ListAttributeDefs($sClass));
  792. $aAttributes = array_merge($aAttributes, $aAllAttributes);
  793. }
  794. else
  795. {
  796. $aAttributes = array_merge($aAttributes, $aProfileAttributes);
  797. }
  798. }
  799. }
  800. $aRes = array(
  801. 'permission' => $iInstancePermission,
  802. 'attributes' => $aAttributes,
  803. );
  804. $this->m_aObjectActionGrants[$oUser->GetKey()][$sClass][$iObjectRef][$iActionCode] = $aRes;
  805. return $aRes;
  806. }
  807. public function IsActionAllowed($oUser, $sClass, $iActionCode, $oInstanceSet = null)
  808. {
  809. if (is_null($oInstanceSet))
  810. {
  811. $aObjectPermissions = $this->GetObjectActionGrant($oUser, $sClass, $iActionCode);
  812. return $aObjectPermissions['permission'];
  813. }
  814. $oInstanceSet->Rewind();
  815. while($oObject = $oInstanceSet->Fetch())
  816. {
  817. $aObjectPermissions = $this->GetObjectActionGrant($oUser, $sClass, $iActionCode, $oObject);
  818. $iInstancePermission = $aObjectPermissions['permission'];
  819. if (isset($iGlobalPermission))
  820. {
  821. if ($iInstancePermission != $iGlobalPermission)
  822. {
  823. $iGlobalPermission = UR_ALLOWED_DEPENDS;
  824. break;
  825. }
  826. }
  827. else
  828. {
  829. $iGlobalPermission = $iInstancePermission;
  830. }
  831. }
  832. $oInstanceSet->Rewind();
  833. if (isset($iGlobalPermission))
  834. {
  835. return $iGlobalPermission;
  836. }
  837. else
  838. {
  839. return UR_ALLOWED_NO;
  840. }
  841. }
  842. public function IsActionAllowedOnAttribute($oUser, $sClass, $sAttCode, $iActionCode, $oInstanceSet = null)
  843. {
  844. if (is_null($oInstanceSet))
  845. {
  846. $aObjectPermissions = $this->GetObjectActionGrant($oUser, $sClass, $iActionCode);
  847. $aAttributes = $aObjectPermissions['attributes'];
  848. if (in_array($sAttCode, $aAttributes))
  849. {
  850. return $aObjectPermissions['permission'];
  851. }
  852. else
  853. {
  854. return UR_ALLOWED_NO;
  855. }
  856. }
  857. $oInstanceSet->Rewind();
  858. while($oObject = $oInstanceSet->Fetch())
  859. {
  860. $aObjectPermissions = $this->GetObjectActionGrant($oUser, $sClass, $iActionCode, $oObject);
  861. $aAttributes = $aObjectPermissions['attributes'];
  862. if (in_array($sAttCode, $aAttributes))
  863. {
  864. $iInstancePermission = $aObjectPermissions['permission'];
  865. }
  866. else
  867. {
  868. $iInstancePermission = UR_ALLOWED_NO;
  869. }
  870. if (isset($iGlobalPermission))
  871. {
  872. if ($iInstancePermission != $iGlobalPermission)
  873. {
  874. $iGlobalPermission = UR_ALLOWED_DEPENDS;
  875. }
  876. }
  877. else
  878. {
  879. $iGlobalPermission = $iInstancePermission;
  880. }
  881. }
  882. $oInstanceSet->Rewind();
  883. if (isset($iGlobalPermission))
  884. {
  885. return $iGlobalPermission;
  886. }
  887. else
  888. {
  889. return UR_ALLOWED_NO;
  890. }
  891. }
  892. // This verb has been made public to allow the development of an accurate feedback for the current configuration
  893. public function GetClassStimulusGrant($iProfile, $sClass, $sStimulusCode)
  894. {
  895. if (isset($this->m_aClassStimulusGrants[$iProfile][$sClass][$sStimulusCode]))
  896. {
  897. return $this->m_aClassStimulusGrants[$iProfile][$sClass][$sStimulusCode];
  898. }
  899. // Get the permission for this profile/class/stimulus
  900. $oSearch = DBObjectSearch::FromOQL_AllData("SELECT URP_StimulusGrant WHERE class = :class AND stimulus = :stimulus AND profileid = :profile AND permission = 'yes'");
  901. $oSet = new DBObjectSet($oSearch, array(), array('class'=>$sClass, 'stimulus'=>$sStimulusCode, 'profile'=>$iProfile));
  902. if ($oSet->Count() >= 1)
  903. {
  904. $oGrantRecord = $oSet->Fetch();
  905. }
  906. else
  907. {
  908. $oGrantRecord = null;
  909. }
  910. $this->m_aClassStimulusGrants[$iProfile][$sClass][$sStimulusCode] = $oGrantRecord;
  911. return $oGrantRecord;
  912. }
  913. public function IsStimulusAllowed($oUser, $sClass, $sStimulusCode, $oInstanceSet = null)
  914. {
  915. // Note: this code is VERY close to the code of IsActionAllowed()
  916. if (is_null($oInstanceSet))
  917. {
  918. $iInstancePermission = UR_ALLOWED_NO;
  919. foreach($this->GetMatchingProfiles($oUser, $sClass) as $iProfile)
  920. {
  921. $oGrantRecord = $this->GetClassStimulusGrant($iProfile, $sClass, $sStimulusCode);
  922. if (!is_null($oGrantRecord))
  923. {
  924. // no need to fetch the record, we've requested the records having permission = 'yes'
  925. $iInstancePermission = UR_ALLOWED_YES;
  926. }
  927. }
  928. return $iInstancePermission;
  929. }
  930. $oInstanceSet->Rewind();
  931. while($oObject = $oInstanceSet->Fetch())
  932. {
  933. $iInstancePermission = UR_ALLOWED_NO;
  934. foreach($this->GetMatchingProfiles($oUser, $sClass, $oObject) as $iProfile)
  935. {
  936. $oGrantRecord = $this->GetClassStimulusGrant($iProfile, $sClass, $sStimulusCode);
  937. if (!is_null($oGrantRecord))
  938. {
  939. // no need to fetch the record, we've requested the records having permission = 'yes'
  940. $iInstancePermission = UR_ALLOWED_YES;
  941. }
  942. }
  943. if (isset($iGlobalPermission))
  944. {
  945. if ($iInstancePermission != $iGlobalPermission)
  946. {
  947. $iGlobalPermission = UR_ALLOWED_DEPENDS;
  948. }
  949. }
  950. else
  951. {
  952. $iGlobalPermission = $iInstancePermission;
  953. }
  954. }
  955. $oInstanceSet->Rewind();
  956. if (isset($iGlobalPermission))
  957. {
  958. return $iGlobalPermission;
  959. }
  960. else
  961. {
  962. return UR_ALLOWED_NO;
  963. }
  964. }
  965. // Copied from GetMatchingProfilesByDim()
  966. // adapted to the optimized implementation of GetSelectFilter()
  967. // Note: shares the cache m_aProPros with GetMatchingProfilesByDim()
  968. // Returns null if any object is readable
  969. // an array of allowed projections otherwise (could be an empty array if none is allowed)
  970. protected function GetReadableProjectionsByDim($oUser, $sClass, $oDimension)
  971. {
  972. //
  973. // Given a dimension, lists the values for which the user will be allowed to read the objects
  974. //
  975. $iUser = $oUser->GetKey();
  976. $iDimension = $oDimension->GetKey();
  977. $aRes = array();
  978. if (array_key_exists($iUser, $this->m_aUserProfiles))
  979. {
  980. foreach ($this->m_aUserProfiles[$iUser] as $iProfile => $oProfile)
  981. {
  982. // user projection to be cached on a given page !
  983. if (!isset($this->m_aProPros[$iProfile][$iDimension]))
  984. {
  985. // No projection for a given profile: default to 'any'
  986. return null;
  987. }
  988. $aUserProjection = $this->m_aProPros[$iProfile][$iDimension]->ProjectUser($oUser);
  989. if (is_null($aUserProjection))
  990. {
  991. // No projection for a given profile: default to 'any'
  992. return null;
  993. }
  994. $aRes = array_unique(array_merge($aRes, $aUserProjection));
  995. }
  996. }
  997. return $aRes;
  998. }
  999. // Note: shares the cache m_aProPros with GetReadableProjectionsByDim()
  1000. protected function GetMatchingProfilesByDim($oUser, $oObject, $oDimension)
  1001. {
  1002. //
  1003. // List profiles for which the user projection overlaps the object projection in the given dimension
  1004. //
  1005. $iUser = $oUser->GetKey();
  1006. $sClass = get_class($oObject);
  1007. $iPKey = $oObject->GetKey();
  1008. $iDimension = $oDimension->GetKey();
  1009. if (isset($this->m_aClassProjs[$sClass][$iDimension]))
  1010. {
  1011. $aObjectProjection = $this->m_aClassProjs[$sClass][$iDimension]->ProjectObject($oObject);
  1012. }
  1013. else
  1014. {
  1015. // No projection for a given class: default to 'any'
  1016. $aObjectProjection = null;
  1017. }
  1018. $aRes = array();
  1019. if (array_key_exists($iUser, $this->m_aUserProfiles))
  1020. {
  1021. foreach ($this->m_aUserProfiles[$iUser] as $iProfile => $oProfile)
  1022. {
  1023. if (is_null($aObjectProjection))
  1024. {
  1025. $aRes[] = $iProfile;
  1026. }
  1027. else
  1028. {
  1029. // user projection to be cached on a given page !
  1030. if (isset($this->m_aProPros[$iProfile][$iDimension]))
  1031. {
  1032. $aUserProjection = $this->m_aProPros[$iProfile][$iDimension]->ProjectUser($oUser);
  1033. }
  1034. else
  1035. {
  1036. // No projection for a given profile: default to 'any'
  1037. $aUserProjection = null;
  1038. }
  1039. if (is_null($aUserProjection))
  1040. {
  1041. $aRes[] = $iProfile;
  1042. }
  1043. else
  1044. {
  1045. $aMatchingValues = array_intersect($aObjectProjection, $aUserProjection);
  1046. if (count($aMatchingValues) > 0)
  1047. {
  1048. $aRes[] = $iProfile;
  1049. }
  1050. }
  1051. }
  1052. }
  1053. }
  1054. return $aRes;
  1055. }
  1056. protected $m_aMatchingProfiles = array(); // cache of the matching profiles for a given user/object
  1057. protected function GetMatchingProfiles($oUser, $sClass, /*DBObject*/ $oObject = null)
  1058. {
  1059. $iUser = $oUser->GetKey();
  1060. if(is_null($oObject))
  1061. {
  1062. $iObjectRef = -999;
  1063. }
  1064. else
  1065. {
  1066. $iObjectRef = $oObject->GetKey();
  1067. }
  1068. //
  1069. // List profiles for which the user projection overlaps the object projection in each and every dimension
  1070. // Caches the result
  1071. //
  1072. $aTest = @$this->m_aMatchingProfiles[$iUser][$sClass][$iObjectRef];
  1073. if (is_array($aTest))
  1074. {
  1075. return $aTest;
  1076. }
  1077. if (is_null($oObject))
  1078. {
  1079. if (array_key_exists($iUser, $this->m_aUserProfiles))
  1080. {
  1081. $aRes = array_keys($this->m_aUserProfiles[$iUser]);
  1082. }
  1083. else
  1084. {
  1085. // no profile has been defined for this user
  1086. $aRes = array();
  1087. }
  1088. }
  1089. else
  1090. {
  1091. $aProfileRes = array();
  1092. foreach ($this->m_aDimensions as $iDimension => $oDimension)
  1093. {
  1094. foreach ($this->GetMatchingProfilesByDim($oUser, $oObject, $oDimension) as $iProfile)
  1095. {
  1096. @$aProfileRes[$iProfile] += 1;
  1097. }
  1098. }
  1099. $aRes = array();
  1100. $iDimCount = count($this->m_aDimensions);
  1101. foreach ($aProfileRes as $iProfile => $iMatches)
  1102. {
  1103. if ($iMatches == $iDimCount)
  1104. {
  1105. $aRes[] = $iProfile;
  1106. }
  1107. }
  1108. }
  1109. // store into the cache
  1110. $this->m_aMatchingProfiles[$iUser][$sClass][$iObjectRef] = $aRes;
  1111. return $aRes;
  1112. }
  1113. public function FlushPrivileges()
  1114. {
  1115. $this->CacheData();
  1116. }
  1117. }
  1118. //
  1119. // Create simple profiles into our user management model:
  1120. // - administrator
  1121. // - readers
  1122. // - contributors
  1123. //
  1124. class SetupProfiles
  1125. {
  1126. protected static $m_aDimensions = array(
  1127. 'organization' => array(
  1128. 'description' => '',
  1129. 'type' => 'Organization',
  1130. ),
  1131. );
  1132. protected static $m_aActions = array(
  1133. UR_ACTION_READ => 'Read',
  1134. UR_ACTION_MODIFY => 'Modify',
  1135. UR_ACTION_DELETE => 'Delete',
  1136. UR_ACTION_BULK_READ => 'Bulk Read',
  1137. UR_ACTION_BULK_MODIFY => 'Bulk Modify',
  1138. UR_ACTION_BULK_DELETE => 'Bulk Delete',
  1139. );
  1140. // Note: It is possible to specify the same class in several modules
  1141. //
  1142. protected static $m_aModules = array();
  1143. protected static $m_aProfiles = array();
  1144. protected static function DoCreateClassProjection($iDimension, $sClass)
  1145. {
  1146. $oNewObj = MetaModel::NewObject("URP_ClassProjection");
  1147. $oNewObj->Set('dimensionid', $iDimension);
  1148. $oNewObj->Set('class', $sClass);
  1149. $oNewObj->Set('attribute', '');
  1150. $iId = $oNewObj->DBInsertNoReload();
  1151. return $iId;
  1152. }
  1153. protected static function DoCreateDimension($sName, $aDimensionData)
  1154. {
  1155. $oNewObj = MetaModel::NewObject("URP_Dimensions");
  1156. $oNewObj->Set('name', $sName);
  1157. $oNewObj->Set('description', $aDimensionData['description']);
  1158. $oNewObj->Set('type', $aDimensionData['type']);
  1159. $iId = $oNewObj->DBInsertNoReload();
  1160. return $iId;
  1161. }
  1162. protected static function DoCreateProfileProjection($iProfile, $iDimension)
  1163. {
  1164. $oNewObj = MetaModel::NewObject("URP_ProfileProjection");
  1165. $oNewObj->Set('profileid', $iProfile);
  1166. $oNewObj->Set('dimensionid', $iDimension);
  1167. $oNewObj->Set('value', '<any>');
  1168. $oNewObj->Set('attribute', '');
  1169. $iId = $oNewObj->DBInsertNoReload();
  1170. return $iId;
  1171. }
  1172. protected static function DoCreateActionGrant($iProfile, $iAction, $sClass, $bPermission = true)
  1173. {
  1174. $oNewObj = MetaModel::NewObject("URP_ActionGrant");
  1175. $oNewObj->Set('profileid', $iProfile);
  1176. $oNewObj->Set('permission', $bPermission ? 'yes' : 'no');
  1177. $oNewObj->Set('class', $sClass);
  1178. $oNewObj->Set('action', self::$m_aActions[$iAction]);
  1179. $iId = $oNewObj->DBInsertNoReload();
  1180. return $iId;
  1181. }
  1182. protected static function DoCreateStimulusGrant($iProfile, $sStimulusCode, $sClass)
  1183. {
  1184. $oNewObj = MetaModel::NewObject("URP_StimulusGrant");
  1185. $oNewObj->Set('profileid', $iProfile);
  1186. $oNewObj->Set('permission', 'yes');
  1187. $oNewObj->Set('class', $sClass);
  1188. $oNewObj->Set('stimulus', $sStimulusCode);
  1189. $iId = $oNewObj->DBInsertNoReload();
  1190. return $iId;
  1191. }
  1192. protected static function DoCreateAdminProfile()
  1193. {
  1194. $oNewObj = MetaModel::NewObject("URP_Profiles");
  1195. $oNewObj->Set('name', 'Administrator');
  1196. $oNewObj->Set('description', 'Has the rights on everything (bypassing any control)');
  1197. $iNewId = $oNewObj->DBInsertNoReload();
  1198. if ($iNewId != ADMIN_PROFILE_ID)
  1199. {
  1200. throw new CoreException('Admin profile could not be created with its standard id', array('requested'=>ADMIN_PROFILE_ID, 'obtained'=>$iNewId));
  1201. }
  1202. }
  1203. protected static function DoCreateOneProfile($sName, $aProfileData)
  1204. {
  1205. $sDescription = $aProfileData['description'];
  1206. if (strlen(trim($aProfileData['write_modules'])) == 0)
  1207. {
  1208. $aWriteModules = array();
  1209. }
  1210. else
  1211. {
  1212. $aWriteModules = explode(',', trim($aProfileData['write_modules']));
  1213. }
  1214. $aStimuli = $aProfileData['stimuli'];
  1215. $oNewObj = MetaModel::NewObject("URP_Profiles");
  1216. $oNewObj->Set('name', $sName);
  1217. $oNewObj->Set('description', $sDescription);
  1218. $iProfile = $oNewObj->DBInsertNoReload();
  1219. // Project in every dimension
  1220. //
  1221. $oDimensionSet = new DBObjectSet(DBObjectSearch::FromOQL_AllData("SELECT URP_Dimensions"));
  1222. while ($oDimension = $oDimensionSet->Fetch())
  1223. {
  1224. $iDimension = $oDimension->GetKey();
  1225. self::DoCreateProfileProjection($iProfile, $iDimension);
  1226. }
  1227. // Grant read rights for everything
  1228. //
  1229. foreach (MetaModel::GetClasses('bizmodel') as $sClass)
  1230. {
  1231. // Skip non instantiable classes
  1232. if (MetaModel::IsAbstract($sClass)) continue;
  1233. self::DoCreateActionGrant($iProfile, UR_ACTION_READ, $sClass);
  1234. self::DoCreateActionGrant($iProfile, UR_ACTION_BULK_READ, $sClass);
  1235. }
  1236. // Grant write for given modules
  1237. // Start by compiling the information, because some modules may overlap
  1238. $aWriteableClasses = array();
  1239. foreach ($aWriteModules as $sModule)
  1240. {
  1241. //$oPage->p('Granting write access for the module"'.$sModule.'" - '.count(self::$m_aModules[$sModule]).' classes');
  1242. foreach (self::$m_aModules[$sModule] as $sClass)
  1243. {
  1244. $aWriteableClasses[$sClass] = true;
  1245. }
  1246. }
  1247. foreach ($aWriteableClasses as $sClass => $foo)
  1248. {
  1249. // Skip non instantiable classes
  1250. if (MetaModel::IsAbstract($sClass)) continue;
  1251. if (!MetaModel::IsValidClass($sClass))
  1252. {
  1253. throw new CoreException("Invalid class name '$sClass'");
  1254. }
  1255. self::DoCreateActionGrant($iProfile, UR_ACTION_MODIFY, $sClass);
  1256. self::DoCreateActionGrant($iProfile, UR_ACTION_DELETE, $sClass);
  1257. self::DoCreateActionGrant($iProfile, UR_ACTION_BULK_MODIFY, $sClass);
  1258. // By default, do not allow bulk deletion operations for standard users
  1259. // self::DoCreateActionGrant($iProfile, UR_ACTION_BULK_DELETE, $sClass);
  1260. }
  1261. // Grant stimuli for given classes
  1262. foreach ($aStimuli as $sClass => $sAllowedStimuli)
  1263. {
  1264. if (!MetaModel::IsValidClass($sClass))
  1265. {
  1266. // Could be a class defined in a module that wasn't installed
  1267. continue;
  1268. //throw new CoreException("Invalid class name '$sClass'");
  1269. }
  1270. if ($sAllowedStimuli == 'any')
  1271. {
  1272. $aAllowedStimuli = array_keys(MetaModel::EnumStimuli($sClass));
  1273. }
  1274. elseif ($sAllowedStimuli == 'none')
  1275. {
  1276. $aAllowedStimuli = array();
  1277. }
  1278. else
  1279. {
  1280. $aAllowedStimuli = explode(',', $sAllowedStimuli);
  1281. }
  1282. foreach ($aAllowedStimuli as $sStimulusCode)
  1283. {
  1284. self::DoCreateStimulusGrant($iProfile, $sStimulusCode, $sClass);
  1285. }
  1286. }
  1287. }
  1288. public static function DoCreateDimensions()
  1289. {
  1290. $aClass = MetaModel::GetClasses();
  1291. foreach(self::$m_aDimensions as $sName => $aDimensionData)
  1292. {
  1293. $iDimension = self::DoCreateDimension($sName, $aDimensionData);
  1294. foreach($aClass as $sClass)
  1295. {
  1296. // Skip non instantiable classes
  1297. if (MetaModel::IsAbstract($sClass)) continue;
  1298. if (!MetaModel::IsValidClass($sClass))
  1299. {
  1300. throw new CoreException("Invalid class name '$sClass'");
  1301. }
  1302. self::DoCreateClassProjection($iDimension, $sClass);
  1303. }
  1304. }
  1305. }
  1306. public static function DoCreateProfiles()
  1307. {
  1308. self::DoCreateAdminProfile();
  1309. foreach(self::$m_aProfiles as $sName => $aProfileData)
  1310. {
  1311. self::DoCreateOneProfile($sName, $aProfileData);
  1312. }
  1313. }
  1314. public static function ComputeBasicProfiles()
  1315. {
  1316. // In this profiling scheme, one single module represents all the classes
  1317. //
  1318. self::$m_aModules = array(
  1319. 'UserData' => MetaModel::GetClasses('bizmodel'),
  1320. );
  1321. self::$m_aProfiles = array(
  1322. 'Reader' => array(
  1323. 'description' => 'Person having a ready-only access to the data',
  1324. 'write_modules' => '',
  1325. 'stimuli' => array(
  1326. ),
  1327. ),
  1328. 'Writer' => array(
  1329. 'description' => 'Contributor to the contents (read + write access)',
  1330. 'write_modules' => 'UserData',
  1331. 'stimuli' => array(
  1332. // any class => 'any'
  1333. ),
  1334. ),
  1335. );
  1336. }
  1337. public static function ComputeITILProfiles()
  1338. {
  1339. // In this profiling scheme, modules are based on ITIL recommendations
  1340. //
  1341. self::$m_aModules = array(
  1342. /*
  1343. 'WriteModule' => array(
  1344. 'someclass',
  1345. 'anotherclass',
  1346. ),
  1347. */
  1348. 'General' => MetaModel::GetClasses('structure'),
  1349. 'Documentation' => MetaModel::GetClasses('documentation'),
  1350. 'Configuration' => MetaModel::GetClasses('configmgmt'),
  1351. 'Incident' => MetaModel::GetClasses('incidentmgmt'),
  1352. 'Problem' => MetaModel::GetClasses('problemmgmt'),
  1353. 'Change' => MetaModel::GetClasses('changemgmt'),
  1354. 'Service' => MetaModel::GetClasses('servicemgmt'),
  1355. 'Call' => MetaModel::GetClasses('requestmgmt'),
  1356. 'KnownError' => MetaModel::GetClasses('knownerrormgmt'),
  1357. );
  1358. self::$m_aProfiles = array(
  1359. 'Configuration Manager' => array(
  1360. 'description' => 'Person in charge of the documentation of the managed CIs',
  1361. 'write_modules' => 'General,Documentation,Configuration',
  1362. 'stimuli' => array(
  1363. //'bizServer' => 'none',
  1364. //'bizContract' => 'none',
  1365. //'bizIncidentTicket' => 'none',
  1366. //'bizChangeTicket' => 'any',
  1367. ),
  1368. ),
  1369. 'Service Desk Agent' => array(
  1370. 'description' => 'Person in charge of creating incident reports',
  1371. 'write_modules' => 'Incident,Call',
  1372. 'stimuli' => array(
  1373. 'Incident' => 'ev_assign',
  1374. 'UserRequest' => 'ev_assign',
  1375. ),
  1376. ),
  1377. 'Support Agent' => array(
  1378. 'description' => 'Person analyzing and solving the current incidents or problems',
  1379. 'write_modules' => 'Incident,Problem,KnownError',
  1380. 'stimuli' => array(
  1381. 'Incident' => 'ev_assign,ev_reassign,ev_resolve,ev_close',
  1382. 'UserRequest' => 'ev_assign,ev_reassign,ev_resolve,ev_close,ev_freeze',
  1383. ),
  1384. ),
  1385. 'Change Implementor' => array(
  1386. 'description' => 'Person executing the changes',
  1387. 'write_modules' => 'Change',
  1388. 'stimuli' => array(
  1389. 'NormalChange' => 'ev_plan,ev_replan,ev_implement,ev_monitor',
  1390. 'EmergencyChange' => 'ev_plan,ev_replan,ev_implement,ev_monitor',
  1391. 'RoutineChange' => 'ev_plan,ev_replan,ev_implement,ev_monitor',
  1392. ),
  1393. ),
  1394. 'Change Supervisor' => array(
  1395. 'description' => 'Person responsible for the overall change execution',
  1396. 'write_modules' => 'Change',
  1397. 'stimuli' => array(
  1398. 'NormalChange' => 'ev_validate,ev_reject,ev_assign,ev_reopen,ev_finish',
  1399. 'EmergencyChange' => 'ev_assign,ev_reopen,ev_finish',
  1400. 'RoutineChange' => 'ev_assign,ev_reopen,ev_finish',
  1401. ),
  1402. ),
  1403. 'Change Approver' => array(
  1404. 'description' => 'Person who could be impacted by some changes',
  1405. 'write_modules' => 'Change',
  1406. 'stimuli' => array(
  1407. 'NormalChange' => 'ev_approve,ev_notapprove',
  1408. 'EmergencyChange' => 'ev_approve,ev_notapprove',
  1409. 'RoutineChange' => 'none',
  1410. ),
  1411. ),
  1412. 'Service Manager' => array(
  1413. 'description' => 'Person responsible for the service delivered to the [internal] customer',
  1414. 'write_modules' => 'Service',
  1415. 'stimuli' => array(
  1416. ),
  1417. ),
  1418. 'Document author' => array(
  1419. 'description' => 'Any person who could contribute to documentation',
  1420. 'write_modules' => 'Documentation',
  1421. 'stimuli' => array(
  1422. ),
  1423. ),
  1424. );
  1425. }
  1426. }
  1427. UserRights::SelectModule('UserRightsProjection');
  1428. ?>