userrightsprofile.class.inc.php 54 KB

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