userrightsprofile.class.inc.php 54 KB

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