userrightsprojection.class.inc.php 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600
  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, $bEditMode);
  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 IsPortalUser($oUser)
  578. {
  579. return true;
  580. // See implementation of userrightsprofile
  581. }
  582. public function Setup()
  583. {
  584. SetupProfiles::ComputeITILProfiles();
  585. //SetupProfiles::ComputeBasicProfiles();
  586. SetupProfiles::DoCreateDimensions();
  587. SetupProfiles::DoCreateProfiles();
  588. return true;
  589. }
  590. public function Init()
  591. {
  592. MetaModel::RegisterPlugin('userrights', 'ACbyProfile', array($this, 'CacheData'));
  593. }
  594. protected $m_aDimensions = array(); // id -> object
  595. protected $m_aClassProj = array(); // class,dimensionid -> object
  596. protected $m_aProfiles = array(); // id -> object
  597. protected $m_aUserProfiles = array(); // userid,profileid -> object
  598. protected $m_aProPro = array(); // profileid,dimensionid -> object
  599. protected $m_aAdmins = array(); // id of users being linked to the well-known admin profile
  600. protected $m_aClassActionGrants = array(); // profile, class, action -> permission
  601. protected $m_aClassStimulusGrants = array(); // profile, class, stimulus -> permission
  602. protected $m_aObjectActionGrants = array(); // userid, class, id, action -> permission, list of attributes
  603. public function CacheData()
  604. {
  605. // Could be loaded in a shared memory (?)
  606. $oDimensionSet = new DBObjectSet(DBObjectSearch::FromOQL_AllData("SELECT URP_Dimensions"));
  607. $this->m_aDimensions = array();
  608. while ($oDimension = $oDimensionSet->Fetch())
  609. {
  610. $this->m_aDimensions[$oDimension->GetKey()] = $oDimension;
  611. }
  612. $oClassProjSet = new DBObjectSet(DBObjectSearch::FromOQL_AllData("SELECT URP_ClassProjection"));
  613. $this->m_aClassProjs = array();
  614. while ($oClassProj = $oClassProjSet->Fetch())
  615. {
  616. $this->m_aClassProjs[$oClassProj->Get('class')][$oClassProj->Get('dimensionid')] = $oClassProj;
  617. }
  618. $oProfileSet = new DBObjectSet(DBObjectSearch::FromOQL_AllData("SELECT URP_Profiles"));
  619. $this->m_aProfiles = array();
  620. while ($oProfile = $oProfileSet->Fetch())
  621. {
  622. $this->m_aProfiles[$oProfile->GetKey()] = $oProfile;
  623. }
  624. $oUserProfileSet = new DBObjectSet(DBObjectSearch::FromOQL_AllData("SELECT URP_UserProfile"));
  625. $this->m_aUserProfiles = array();
  626. $this->m_aAdmins = array();
  627. while ($oUserProfile = $oUserProfileSet->Fetch())
  628. {
  629. $this->m_aUserProfiles[$oUserProfile->Get('userid')][$oUserProfile->Get('profileid')] = $oUserProfile;
  630. if ($oUserProfile->Get('profileid') == ADMIN_PROFILE_ID)
  631. {
  632. $this->m_aAdmins[] = $oUserProfile->Get('userid');
  633. }
  634. }
  635. $oProProSet = new DBObjectSet(DBObjectSearch::FromOQL_AllData("SELECT URP_ProfileProjection"));
  636. $this->m_aProPros = array();
  637. while ($oProPro = $oProProSet->Fetch())
  638. {
  639. $this->m_aProPros[$oProPro->Get('profileid')][$oProPro->Get('dimensionid')] = $oProPro;
  640. }
  641. /*
  642. echo "<pre>\n";
  643. print_r($this->m_aDimensions);
  644. print_r($this->m_aClassProjs);
  645. print_r($this->m_aProfiles);
  646. print_r($this->m_aUserProfiles);
  647. print_r($this->m_aProPros);
  648. echo "</pre>\n";
  649. exit;
  650. */
  651. return true;
  652. }
  653. public function GetSelectFilter($oUser, $sClass)
  654. {
  655. $aConditions = array();
  656. foreach ($this->m_aDimensions as $iDimension => $oDimension)
  657. {
  658. $oClassProj = @$this->m_aClassProjs[$sClass][$iDimension];
  659. if (is_null($oClassProj))
  660. {
  661. // Authorize any for this dimension, then no additional criteria is required
  662. continue;
  663. }
  664. // 1 - Get class projection info
  665. //
  666. $oExpression = null;
  667. $sExpr = $oClassProj->Get('value');
  668. if ($sExpr == '<this>')
  669. {
  670. $sColumn = $oClassProj->Get('attribute');
  671. if (empty($sColumn))
  672. {
  673. $oExpression = new FieldExpression('id', $sClass);
  674. }
  675. else
  676. {
  677. $oExpression = new FieldExpression($sColumn, $sClass);
  678. }
  679. }
  680. elseif (($sExpr == '<any>') || ($sExpr == ''))
  681. {
  682. // Authorize any for this dimension, then no additional criteria is required
  683. continue;
  684. }
  685. elseif (strtolower(substr($sExpr, 0, 6)) == 'select')
  686. {
  687. throw new CoreException('Sorry, projections by the mean of OQL are not supported currently, please specify an attribute instead', array('class' => $sClass, 'expression' => $sExpr));
  688. }
  689. else
  690. {
  691. // Constant value(s)
  692. // unsupported
  693. throw new CoreException('Sorry, constant projections are not supported currently, please specify an attribute instead', array('class' => $sClass, 'expression' => $sExpr));
  694. // $aRes = explode(';', trim($sExpr));
  695. }
  696. // 2 - Get profile projection info and use it if needed
  697. //
  698. $aProjections = self::GetReadableProjectionsByDim($oUser, $sClass, $oDimension);
  699. if (is_null($aProjections))
  700. {
  701. // Authorize any for this dimension, then no additional criteria is required
  702. continue;
  703. }
  704. elseif (count($aProjections) == 0)
  705. {
  706. // Authorize none, then exit as quickly as possible
  707. return false;
  708. }
  709. else
  710. {
  711. // Authorize the given set of values
  712. $oListExpr = ListExpression::FromScalars($aProjections);
  713. $oCondition = new BinaryExpression($oExpression, 'IN', $oListExpr);
  714. $aConditions[] = $oCondition;
  715. }
  716. }
  717. if (count($aConditions) == 0)
  718. {
  719. // allow all
  720. return true;
  721. }
  722. else
  723. {
  724. $oFilter = new DBObjectSearch($sClass);
  725. foreach($aConditions as $oCondition)
  726. {
  727. $oFilter->AddConditionExpression($oCondition);
  728. }
  729. //return true;
  730. return $oFilter;
  731. }
  732. }
  733. // This verb has been made public to allow the development of an accurate feedback for the current configuration
  734. public function GetClassActionGrant($iProfile, $sClass, $sAction)
  735. {
  736. if (isset($this->m_aClassActionGrants[$iProfile][$sClass][$sAction]))
  737. {
  738. return $this->m_aClassActionGrants[$iProfile][$sClass][$sAction];
  739. }
  740. // Get the permission for this profile/class/action
  741. $oSearch = DBObjectSearch::FromOQL_AllData("SELECT URP_ActionGrant WHERE class = :class AND action = :action AND profileid = :profile AND permission = 'yes'");
  742. $oSet = new DBObjectSet($oSearch, array(), array('class'=>$sClass, 'action'=>$sAction, 'profile'=>$iProfile));
  743. if ($oSet->Count() >= 1)
  744. {
  745. $oGrantRecord = $oSet->Fetch();
  746. }
  747. else
  748. {
  749. $sParentClass = MetaModel::GetParentPersistentClass($sClass);
  750. if (empty($sParentClass))
  751. {
  752. $oGrantRecord = null;
  753. }
  754. else
  755. {
  756. $oGrantRecord = $this->GetClassActionGrant($iProfile, $sParentClass, $sAction);
  757. }
  758. }
  759. $this->m_aClassActionGrants[$iProfile][$sClass][$sAction] = $oGrantRecord;
  760. return $oGrantRecord;
  761. }
  762. protected function GetObjectActionGrant($oUser, $sClass, $iActionCode, /*DBObject*/ $oObject = null)
  763. {
  764. if(is_null($oObject))
  765. {
  766. $iObjectRef = -999;
  767. }
  768. else
  769. {
  770. $iObjectRef = $oObject->GetKey();
  771. }
  772. // load and cache permissions for the current user on the given object
  773. //
  774. $aTest = @$this->m_aObjectActionGrants[$oUser->GetKey()][$sClass][$iObjectRef][$iActionCode];
  775. if (is_array($aTest)) return $aTest;
  776. $sAction = self::$m_aActionCodes[$iActionCode];
  777. $iInstancePermission = UR_ALLOWED_NO;
  778. $aAttributes = array();
  779. foreach($this->GetMatchingProfiles($oUser, $sClass, $oObject) as $iProfile)
  780. {
  781. $oGrantRecord = $this->GetClassActionGrant($iProfile, $sClass, $sAction);
  782. if (is_null($oGrantRecord))
  783. {
  784. continue; // loop to the next profile
  785. }
  786. else
  787. {
  788. $iInstancePermission = UR_ALLOWED_YES;
  789. // update the list of attributes with those allowed for this profile
  790. //
  791. $oSearch = DBObjectSearch::FromOQL_AllData("SELECT URP_AttributeGrant WHERE actiongrantid = :actiongrantid");
  792. $oSet = new DBObjectSet($oSearch, array(), array('actiongrantid' => $oGrantRecord->GetKey()));
  793. $aProfileAttributes = $oSet->GetColumnAsArray('attcode', false);
  794. if (count($aProfileAttributes) == 0)
  795. {
  796. $aAllAttributes = array_keys(MetaModel::ListAttributeDefs($sClass));
  797. $aAttributes = array_merge($aAttributes, $aAllAttributes);
  798. }
  799. else
  800. {
  801. $aAttributes = array_merge($aAttributes, $aProfileAttributes);
  802. }
  803. }
  804. }
  805. $aRes = array(
  806. 'permission' => $iInstancePermission,
  807. 'attributes' => $aAttributes,
  808. );
  809. $this->m_aObjectActionGrants[$oUser->GetKey()][$sClass][$iObjectRef][$iActionCode] = $aRes;
  810. return $aRes;
  811. }
  812. public function IsActionAllowed($oUser, $sClass, $iActionCode, $oInstanceSet = null)
  813. {
  814. if (is_null($oInstanceSet))
  815. {
  816. $aObjectPermissions = $this->GetObjectActionGrant($oUser, $sClass, $iActionCode);
  817. return $aObjectPermissions['permission'];
  818. }
  819. $oInstanceSet->Rewind();
  820. while($oObject = $oInstanceSet->Fetch())
  821. {
  822. $aObjectPermissions = $this->GetObjectActionGrant($oUser, $sClass, $iActionCode, $oObject);
  823. $iInstancePermission = $aObjectPermissions['permission'];
  824. if (isset($iGlobalPermission))
  825. {
  826. if ($iInstancePermission != $iGlobalPermission)
  827. {
  828. $iGlobalPermission = UR_ALLOWED_DEPENDS;
  829. break;
  830. }
  831. }
  832. else
  833. {
  834. $iGlobalPermission = $iInstancePermission;
  835. }
  836. }
  837. $oInstanceSet->Rewind();
  838. if (isset($iGlobalPermission))
  839. {
  840. return $iGlobalPermission;
  841. }
  842. else
  843. {
  844. return UR_ALLOWED_NO;
  845. }
  846. }
  847. public function IsActionAllowedOnAttribute($oUser, $sClass, $sAttCode, $iActionCode, $oInstanceSet = null)
  848. {
  849. if (is_null($oInstanceSet))
  850. {
  851. $aObjectPermissions = $this->GetObjectActionGrant($oUser, $sClass, $iActionCode);
  852. $aAttributes = $aObjectPermissions['attributes'];
  853. if (in_array($sAttCode, $aAttributes))
  854. {
  855. return $aObjectPermissions['permission'];
  856. }
  857. else
  858. {
  859. return UR_ALLOWED_NO;
  860. }
  861. }
  862. $oInstanceSet->Rewind();
  863. while($oObject = $oInstanceSet->Fetch())
  864. {
  865. $aObjectPermissions = $this->GetObjectActionGrant($oUser, $sClass, $iActionCode, $oObject);
  866. $aAttributes = $aObjectPermissions['attributes'];
  867. if (in_array($sAttCode, $aAttributes))
  868. {
  869. $iInstancePermission = $aObjectPermissions['permission'];
  870. }
  871. else
  872. {
  873. $iInstancePermission = UR_ALLOWED_NO;
  874. }
  875. if (isset($iGlobalPermission))
  876. {
  877. if ($iInstancePermission != $iGlobalPermission)
  878. {
  879. $iGlobalPermission = UR_ALLOWED_DEPENDS;
  880. }
  881. }
  882. else
  883. {
  884. $iGlobalPermission = $iInstancePermission;
  885. }
  886. }
  887. $oInstanceSet->Rewind();
  888. if (isset($iGlobalPermission))
  889. {
  890. return $iGlobalPermission;
  891. }
  892. else
  893. {
  894. return UR_ALLOWED_NO;
  895. }
  896. }
  897. // This verb has been made public to allow the development of an accurate feedback for the current configuration
  898. public function GetClassStimulusGrant($iProfile, $sClass, $sStimulusCode)
  899. {
  900. if (isset($this->m_aClassStimulusGrants[$iProfile][$sClass][$sStimulusCode]))
  901. {
  902. return $this->m_aClassStimulusGrants[$iProfile][$sClass][$sStimulusCode];
  903. }
  904. // Get the permission for this profile/class/stimulus
  905. $oSearch = DBObjectSearch::FromOQL_AllData("SELECT URP_StimulusGrant WHERE class = :class AND stimulus = :stimulus AND profileid = :profile AND permission = 'yes'");
  906. $oSet = new DBObjectSet($oSearch, array(), array('class'=>$sClass, 'stimulus'=>$sStimulusCode, 'profile'=>$iProfile));
  907. if ($oSet->Count() >= 1)
  908. {
  909. $oGrantRecord = $oSet->Fetch();
  910. }
  911. else
  912. {
  913. $oGrantRecord = null;
  914. }
  915. $this->m_aClassStimulusGrants[$iProfile][$sClass][$sStimulusCode] = $oGrantRecord;
  916. return $oGrantRecord;
  917. }
  918. public function IsStimulusAllowed($oUser, $sClass, $sStimulusCode, $oInstanceSet = null)
  919. {
  920. // Note: this code is VERY close to the code of IsActionAllowed()
  921. if (is_null($oInstanceSet))
  922. {
  923. $iInstancePermission = UR_ALLOWED_NO;
  924. foreach($this->GetMatchingProfiles($oUser, $sClass) as $iProfile)
  925. {
  926. $oGrantRecord = $this->GetClassStimulusGrant($iProfile, $sClass, $sStimulusCode);
  927. if (!is_null($oGrantRecord))
  928. {
  929. // no need to fetch the record, we've requested the records having permission = 'yes'
  930. $iInstancePermission = UR_ALLOWED_YES;
  931. }
  932. }
  933. return $iInstancePermission;
  934. }
  935. $oInstanceSet->Rewind();
  936. while($oObject = $oInstanceSet->Fetch())
  937. {
  938. $iInstancePermission = UR_ALLOWED_NO;
  939. foreach($this->GetMatchingProfiles($oUser, $sClass, $oObject) as $iProfile)
  940. {
  941. $oGrantRecord = $this->GetClassStimulusGrant($iProfile, $sClass, $sStimulusCode);
  942. if (!is_null($oGrantRecord))
  943. {
  944. // no need to fetch the record, we've requested the records having permission = 'yes'
  945. $iInstancePermission = UR_ALLOWED_YES;
  946. }
  947. }
  948. if (isset($iGlobalPermission))
  949. {
  950. if ($iInstancePermission != $iGlobalPermission)
  951. {
  952. $iGlobalPermission = UR_ALLOWED_DEPENDS;
  953. }
  954. }
  955. else
  956. {
  957. $iGlobalPermission = $iInstancePermission;
  958. }
  959. }
  960. $oInstanceSet->Rewind();
  961. if (isset($iGlobalPermission))
  962. {
  963. return $iGlobalPermission;
  964. }
  965. else
  966. {
  967. return UR_ALLOWED_NO;
  968. }
  969. }
  970. // Copied from GetMatchingProfilesByDim()
  971. // adapted to the optimized implementation of GetSelectFilter()
  972. // Note: shares the cache m_aProPros with GetMatchingProfilesByDim()
  973. // Returns null if any object is readable
  974. // an array of allowed projections otherwise (could be an empty array if none is allowed)
  975. protected function GetReadableProjectionsByDim($oUser, $sClass, $oDimension)
  976. {
  977. //
  978. // Given a dimension, lists the values for which the user will be allowed to read the objects
  979. //
  980. $iUser = $oUser->GetKey();
  981. $iDimension = $oDimension->GetKey();
  982. $aRes = array();
  983. if (array_key_exists($iUser, $this->m_aUserProfiles))
  984. {
  985. foreach ($this->m_aUserProfiles[$iUser] as $iProfile => $oProfile)
  986. {
  987. // user projection to be cached on a given page !
  988. if (!isset($this->m_aProPros[$iProfile][$iDimension]))
  989. {
  990. // No projection for a given profile: default to 'any'
  991. return null;
  992. }
  993. $aUserProjection = $this->m_aProPros[$iProfile][$iDimension]->ProjectUser($oUser);
  994. if (is_null($aUserProjection))
  995. {
  996. // No projection for a given profile: default to 'any'
  997. return null;
  998. }
  999. $aRes = array_unique(array_merge($aRes, $aUserProjection));
  1000. }
  1001. }
  1002. return $aRes;
  1003. }
  1004. // Note: shares the cache m_aProPros with GetReadableProjectionsByDim()
  1005. protected function GetMatchingProfilesByDim($oUser, $oObject, $oDimension)
  1006. {
  1007. //
  1008. // List profiles for which the user projection overlaps the object projection in the given dimension
  1009. //
  1010. $iUser = $oUser->GetKey();
  1011. $sClass = get_class($oObject);
  1012. $iPKey = $oObject->GetKey();
  1013. $iDimension = $oDimension->GetKey();
  1014. if (isset($this->m_aClassProjs[$sClass][$iDimension]))
  1015. {
  1016. $aObjectProjection = $this->m_aClassProjs[$sClass][$iDimension]->ProjectObject($oObject);
  1017. }
  1018. else
  1019. {
  1020. // No projection for a given class: default to 'any'
  1021. $aObjectProjection = null;
  1022. }
  1023. $aRes = array();
  1024. if (array_key_exists($iUser, $this->m_aUserProfiles))
  1025. {
  1026. foreach ($this->m_aUserProfiles[$iUser] as $iProfile => $oProfile)
  1027. {
  1028. if (is_null($aObjectProjection))
  1029. {
  1030. $aRes[] = $iProfile;
  1031. }
  1032. else
  1033. {
  1034. // user projection to be cached on a given page !
  1035. if (isset($this->m_aProPros[$iProfile][$iDimension]))
  1036. {
  1037. $aUserProjection = $this->m_aProPros[$iProfile][$iDimension]->ProjectUser($oUser);
  1038. }
  1039. else
  1040. {
  1041. // No projection for a given profile: default to 'any'
  1042. $aUserProjection = null;
  1043. }
  1044. if (is_null($aUserProjection))
  1045. {
  1046. $aRes[] = $iProfile;
  1047. }
  1048. else
  1049. {
  1050. $aMatchingValues = array_intersect($aObjectProjection, $aUserProjection);
  1051. if (count($aMatchingValues) > 0)
  1052. {
  1053. $aRes[] = $iProfile;
  1054. }
  1055. }
  1056. }
  1057. }
  1058. }
  1059. return $aRes;
  1060. }
  1061. protected $m_aMatchingProfiles = array(); // cache of the matching profiles for a given user/object
  1062. protected function GetMatchingProfiles($oUser, $sClass, /*DBObject*/ $oObject = null)
  1063. {
  1064. $iUser = $oUser->GetKey();
  1065. if(is_null($oObject))
  1066. {
  1067. $iObjectRef = -999;
  1068. }
  1069. else
  1070. {
  1071. $iObjectRef = $oObject->GetKey();
  1072. }
  1073. //
  1074. // List profiles for which the user projection overlaps the object projection in each and every dimension
  1075. // Caches the result
  1076. //
  1077. $aTest = @$this->m_aMatchingProfiles[$iUser][$sClass][$iObjectRef];
  1078. if (is_array($aTest))
  1079. {
  1080. return $aTest;
  1081. }
  1082. if (is_null($oObject))
  1083. {
  1084. if (array_key_exists($iUser, $this->m_aUserProfiles))
  1085. {
  1086. $aRes = array_keys($this->m_aUserProfiles[$iUser]);
  1087. }
  1088. else
  1089. {
  1090. // no profile has been defined for this user
  1091. $aRes = array();
  1092. }
  1093. }
  1094. else
  1095. {
  1096. $aProfileRes = array();
  1097. foreach ($this->m_aDimensions as $iDimension => $oDimension)
  1098. {
  1099. foreach ($this->GetMatchingProfilesByDim($oUser, $oObject, $oDimension) as $iProfile)
  1100. {
  1101. @$aProfileRes[$iProfile] += 1;
  1102. }
  1103. }
  1104. $aRes = array();
  1105. $iDimCount = count($this->m_aDimensions);
  1106. foreach ($aProfileRes as $iProfile => $iMatches)
  1107. {
  1108. if ($iMatches == $iDimCount)
  1109. {
  1110. $aRes[] = $iProfile;
  1111. }
  1112. }
  1113. }
  1114. // store into the cache
  1115. $this->m_aMatchingProfiles[$iUser][$sClass][$iObjectRef] = $aRes;
  1116. return $aRes;
  1117. }
  1118. public function FlushPrivileges()
  1119. {
  1120. $this->CacheData();
  1121. }
  1122. }
  1123. //
  1124. // Create simple profiles into our user management model:
  1125. // - administrator
  1126. // - readers
  1127. // - contributors
  1128. //
  1129. class SetupProfiles
  1130. {
  1131. protected static $m_aDimensions = array(
  1132. 'organization' => array(
  1133. 'description' => '',
  1134. 'type' => 'Organization',
  1135. ),
  1136. );
  1137. protected static $m_aActions = array(
  1138. UR_ACTION_READ => 'Read',
  1139. UR_ACTION_MODIFY => 'Modify',
  1140. UR_ACTION_DELETE => 'Delete',
  1141. UR_ACTION_BULK_READ => 'Bulk Read',
  1142. UR_ACTION_BULK_MODIFY => 'Bulk Modify',
  1143. UR_ACTION_BULK_DELETE => 'Bulk Delete',
  1144. );
  1145. // Note: It is possible to specify the same class in several modules
  1146. //
  1147. protected static $m_aModules = array();
  1148. protected static $m_aProfiles = array();
  1149. protected static function DoCreateClassProjection($iDimension, $sClass)
  1150. {
  1151. $oNewObj = MetaModel::NewObject("URP_ClassProjection");
  1152. $oNewObj->Set('dimensionid', $iDimension);
  1153. $oNewObj->Set('class', $sClass);
  1154. $oNewObj->Set('attribute', '');
  1155. $iId = $oNewObj->DBInsertNoReload();
  1156. return $iId;
  1157. }
  1158. protected static function DoCreateDimension($sName, $aDimensionData)
  1159. {
  1160. $oNewObj = MetaModel::NewObject("URP_Dimensions");
  1161. $oNewObj->Set('name', $sName);
  1162. $oNewObj->Set('description', $aDimensionData['description']);
  1163. $oNewObj->Set('type', $aDimensionData['type']);
  1164. $iId = $oNewObj->DBInsertNoReload();
  1165. return $iId;
  1166. }
  1167. protected static function DoCreateProfileProjection($iProfile, $iDimension)
  1168. {
  1169. $oNewObj = MetaModel::NewObject("URP_ProfileProjection");
  1170. $oNewObj->Set('profileid', $iProfile);
  1171. $oNewObj->Set('dimensionid', $iDimension);
  1172. $oNewObj->Set('value', '<any>');
  1173. $oNewObj->Set('attribute', '');
  1174. $iId = $oNewObj->DBInsertNoReload();
  1175. return $iId;
  1176. }
  1177. protected static function DoCreateActionGrant($iProfile, $iAction, $sClass, $bPermission = true)
  1178. {
  1179. $oNewObj = MetaModel::NewObject("URP_ActionGrant");
  1180. $oNewObj->Set('profileid', $iProfile);
  1181. $oNewObj->Set('permission', $bPermission ? 'yes' : 'no');
  1182. $oNewObj->Set('class', $sClass);
  1183. $oNewObj->Set('action', self::$m_aActions[$iAction]);
  1184. $iId = $oNewObj->DBInsertNoReload();
  1185. return $iId;
  1186. }
  1187. protected static function DoCreateStimulusGrant($iProfile, $sStimulusCode, $sClass)
  1188. {
  1189. $oNewObj = MetaModel::NewObject("URP_StimulusGrant");
  1190. $oNewObj->Set('profileid', $iProfile);
  1191. $oNewObj->Set('permission', 'yes');
  1192. $oNewObj->Set('class', $sClass);
  1193. $oNewObj->Set('stimulus', $sStimulusCode);
  1194. $iId = $oNewObj->DBInsertNoReload();
  1195. return $iId;
  1196. }
  1197. protected static function DoCreateAdminProfile()
  1198. {
  1199. $oNewObj = MetaModel::NewObject("URP_Profiles");
  1200. $oNewObj->Set('name', 'Administrator');
  1201. $oNewObj->Set('description', 'Has the rights on everything (bypassing any control)');
  1202. $iNewId = $oNewObj->DBInsertNoReload();
  1203. if ($iNewId != ADMIN_PROFILE_ID)
  1204. {
  1205. throw new CoreException('Admin profile could not be created with its standard id', array('requested'=>ADMIN_PROFILE_ID, 'obtained'=>$iNewId));
  1206. }
  1207. }
  1208. protected static function DoCreateOneProfile($sName, $aProfileData)
  1209. {
  1210. $sDescription = $aProfileData['description'];
  1211. if (strlen(trim($aProfileData['write_modules'])) == 0)
  1212. {
  1213. $aWriteModules = array();
  1214. }
  1215. else
  1216. {
  1217. $aWriteModules = explode(',', trim($aProfileData['write_modules']));
  1218. }
  1219. $aStimuli = $aProfileData['stimuli'];
  1220. $oNewObj = MetaModel::NewObject("URP_Profiles");
  1221. $oNewObj->Set('name', $sName);
  1222. $oNewObj->Set('description', $sDescription);
  1223. $iProfile = $oNewObj->DBInsertNoReload();
  1224. // Project in every dimension
  1225. //
  1226. $oDimensionSet = new DBObjectSet(DBObjectSearch::FromOQL_AllData("SELECT URP_Dimensions"));
  1227. while ($oDimension = $oDimensionSet->Fetch())
  1228. {
  1229. $iDimension = $oDimension->GetKey();
  1230. self::DoCreateProfileProjection($iProfile, $iDimension);
  1231. }
  1232. // Grant read rights for everything
  1233. //
  1234. foreach (MetaModel::GetClasses('bizmodel') as $sClass)
  1235. {
  1236. // Skip non instantiable classes
  1237. if (MetaModel::IsAbstract($sClass)) continue;
  1238. self::DoCreateActionGrant($iProfile, UR_ACTION_READ, $sClass);
  1239. self::DoCreateActionGrant($iProfile, UR_ACTION_BULK_READ, $sClass);
  1240. }
  1241. // Grant write for given modules
  1242. // Start by compiling the information, because some modules may overlap
  1243. $aWriteableClasses = array();
  1244. foreach ($aWriteModules as $sModule)
  1245. {
  1246. //$oPage->p('Granting write access for the module"'.$sModule.'" - '.count(self::$m_aModules[$sModule]).' classes');
  1247. foreach (self::$m_aModules[$sModule] as $sClass)
  1248. {
  1249. $aWriteableClasses[$sClass] = true;
  1250. }
  1251. }
  1252. foreach ($aWriteableClasses as $sClass => $foo)
  1253. {
  1254. // Skip non instantiable classes
  1255. if (MetaModel::IsAbstract($sClass)) continue;
  1256. if (!MetaModel::IsValidClass($sClass))
  1257. {
  1258. throw new CoreException("Invalid class name '$sClass'");
  1259. }
  1260. self::DoCreateActionGrant($iProfile, UR_ACTION_MODIFY, $sClass);
  1261. self::DoCreateActionGrant($iProfile, UR_ACTION_DELETE, $sClass);
  1262. self::DoCreateActionGrant($iProfile, UR_ACTION_BULK_MODIFY, $sClass);
  1263. // By default, do not allow bulk deletion operations for standard users
  1264. // self::DoCreateActionGrant($iProfile, UR_ACTION_BULK_DELETE, $sClass);
  1265. }
  1266. // Grant stimuli for given classes
  1267. foreach ($aStimuli as $sClass => $sAllowedStimuli)
  1268. {
  1269. if (!MetaModel::IsValidClass($sClass))
  1270. {
  1271. // Could be a class defined in a module that wasn't installed
  1272. continue;
  1273. //throw new CoreException("Invalid class name '$sClass'");
  1274. }
  1275. if ($sAllowedStimuli == 'any')
  1276. {
  1277. $aAllowedStimuli = array_keys(MetaModel::EnumStimuli($sClass));
  1278. }
  1279. elseif ($sAllowedStimuli == 'none')
  1280. {
  1281. $aAllowedStimuli = array();
  1282. }
  1283. else
  1284. {
  1285. $aAllowedStimuli = explode(',', $sAllowedStimuli);
  1286. }
  1287. foreach ($aAllowedStimuli as $sStimulusCode)
  1288. {
  1289. self::DoCreateStimulusGrant($iProfile, $sStimulusCode, $sClass);
  1290. }
  1291. }
  1292. }
  1293. public static function DoCreateDimensions()
  1294. {
  1295. $aClass = MetaModel::GetClasses();
  1296. foreach(self::$m_aDimensions as $sName => $aDimensionData)
  1297. {
  1298. $iDimension = self::DoCreateDimension($sName, $aDimensionData);
  1299. foreach($aClass as $sClass)
  1300. {
  1301. // Skip non instantiable classes
  1302. if (MetaModel::IsAbstract($sClass)) continue;
  1303. if (!MetaModel::IsValidClass($sClass))
  1304. {
  1305. throw new CoreException("Invalid class name '$sClass'");
  1306. }
  1307. self::DoCreateClassProjection($iDimension, $sClass);
  1308. }
  1309. }
  1310. }
  1311. public static function DoCreateProfiles()
  1312. {
  1313. self::DoCreateAdminProfile();
  1314. foreach(self::$m_aProfiles as $sName => $aProfileData)
  1315. {
  1316. self::DoCreateOneProfile($sName, $aProfileData);
  1317. }
  1318. }
  1319. public static function ComputeBasicProfiles()
  1320. {
  1321. // In this profiling scheme, one single module represents all the classes
  1322. //
  1323. self::$m_aModules = array(
  1324. 'UserData' => MetaModel::GetClasses('bizmodel'),
  1325. );
  1326. self::$m_aProfiles = array(
  1327. 'Reader' => array(
  1328. 'description' => 'Person having a ready-only access to the data',
  1329. 'write_modules' => '',
  1330. 'stimuli' => array(
  1331. ),
  1332. ),
  1333. 'Writer' => array(
  1334. 'description' => 'Contributor to the contents (read + write access)',
  1335. 'write_modules' => 'UserData',
  1336. 'stimuli' => array(
  1337. // any class => 'any'
  1338. ),
  1339. ),
  1340. );
  1341. }
  1342. public static function ComputeITILProfiles()
  1343. {
  1344. // In this profiling scheme, modules are based on ITIL recommendations
  1345. //
  1346. self::$m_aModules = array(
  1347. /*
  1348. 'WriteModule' => array(
  1349. 'someclass',
  1350. 'anotherclass',
  1351. ),
  1352. */
  1353. 'General' => MetaModel::GetClasses('structure'),
  1354. 'Documentation' => MetaModel::GetClasses('documentation'),
  1355. 'Configuration' => MetaModel::GetClasses('configmgmt'),
  1356. 'Incident' => MetaModel::GetClasses('incidentmgmt'),
  1357. 'Problem' => MetaModel::GetClasses('problemmgmt'),
  1358. 'Change' => MetaModel::GetClasses('changemgmt'),
  1359. 'Service' => MetaModel::GetClasses('servicemgmt'),
  1360. 'Call' => MetaModel::GetClasses('requestmgmt'),
  1361. 'KnownError' => MetaModel::GetClasses('knownerrormgmt'),
  1362. );
  1363. self::$m_aProfiles = array(
  1364. 'Configuration Manager' => array(
  1365. 'description' => 'Person in charge of the documentation of the managed CIs',
  1366. 'write_modules' => 'General,Documentation,Configuration',
  1367. 'stimuli' => array(
  1368. //'bizServer' => 'none',
  1369. //'bizContract' => 'none',
  1370. //'bizIncidentTicket' => 'none',
  1371. //'bizChangeTicket' => 'any',
  1372. ),
  1373. ),
  1374. 'Service Desk Agent' => array(
  1375. 'description' => 'Person in charge of creating incident reports',
  1376. 'write_modules' => 'Incident,Call',
  1377. 'stimuli' => array(
  1378. 'Incident' => 'ev_assign',
  1379. 'UserRequest' => 'ev_assign',
  1380. ),
  1381. ),
  1382. 'Support Agent' => array(
  1383. 'description' => 'Person analyzing and solving the current incidents or problems',
  1384. 'write_modules' => 'Incident,Problem,KnownError',
  1385. 'stimuli' => array(
  1386. 'Incident' => 'ev_assign,ev_reassign,ev_resolve,ev_close',
  1387. 'UserRequest' => 'ev_assign,ev_reassign,ev_resolve,ev_close,ev_freeze',
  1388. ),
  1389. ),
  1390. 'Change Implementor' => array(
  1391. 'description' => 'Person executing the changes',
  1392. 'write_modules' => 'Change',
  1393. 'stimuli' => array(
  1394. 'NormalChange' => 'ev_plan,ev_replan,ev_implement,ev_monitor',
  1395. 'EmergencyChange' => 'ev_plan,ev_replan,ev_implement,ev_monitor',
  1396. 'RoutineChange' => 'ev_plan,ev_replan,ev_implement,ev_monitor',
  1397. ),
  1398. ),
  1399. 'Change Supervisor' => array(
  1400. 'description' => 'Person responsible for the overall change execution',
  1401. 'write_modules' => 'Change',
  1402. 'stimuli' => array(
  1403. 'NormalChange' => 'ev_validate,ev_reject,ev_assign,ev_reopen,ev_finish',
  1404. 'EmergencyChange' => 'ev_assign,ev_reopen,ev_finish',
  1405. 'RoutineChange' => 'ev_assign,ev_reopen,ev_finish',
  1406. ),
  1407. ),
  1408. 'Change Approver' => array(
  1409. 'description' => 'Person who could be impacted by some changes',
  1410. 'write_modules' => 'Change',
  1411. 'stimuli' => array(
  1412. 'NormalChange' => 'ev_approve,ev_notapprove',
  1413. 'EmergencyChange' => 'ev_approve,ev_notapprove',
  1414. 'RoutineChange' => 'none',
  1415. ),
  1416. ),
  1417. 'Service Manager' => array(
  1418. 'description' => 'Person responsible for the service delivered to the [internal] customer',
  1419. 'write_modules' => 'Service',
  1420. 'stimuli' => array(
  1421. ),
  1422. ),
  1423. 'Document author' => array(
  1424. 'description' => 'Any person who could contribute to documentation',
  1425. 'write_modules' => 'Documentation',
  1426. 'stimuli' => array(
  1427. ),
  1428. ),
  1429. );
  1430. }
  1431. }
  1432. UserRights::SelectModule('UserRightsProjection');
  1433. ?>