userrightsprofile.class.inc.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. <?php
  2. /**
  3. * UserRightsProfile
  4. * User management Module, basing the right on profiles and a matrix (similar to UserRightsMatrix, but profiles and other decorations have been added)
  5. *
  6. * @package iTopORM
  7. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  8. * @author Denis Flaven <denisflave@free.fr>
  9. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  10. * @link www.itop.com
  11. * @since 1.0
  12. * @version 1.1.1.1 $
  13. */
  14. class URP_Users extends DBObject
  15. {
  16. public static function Init()
  17. {
  18. $aParams = array
  19. (
  20. "category" => "addon/userrights",
  21. "name" => "user",
  22. "description" => "users and credentials",
  23. "key_type" => "autoincrement",
  24. "key_label" => "",
  25. "name_attcode" => "login",
  26. "state_attcode" => "",
  27. "reconc_keys" => array(),
  28. "db_table" => "priv_urp_users",
  29. "db_key_field" => "id",
  30. "db_finalclass_field" => "",
  31. );
  32. MetaModel::Init_Params($aParams);
  33. //MetaModel::Init_InheritAttributes();
  34. MetaModel::Init_AddAttribute(new AttributeInteger("userid", array("label"=>"User id", "description"=>"User identifier (depends on the business model)", "allowed_values"=>null, "sql"=>"userid", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  35. MetaModel::Init_AddAttribute(new AttributeString("login", array("label"=>"login", "description"=>"user identification string", "allowed_values"=>null, "sql"=>"login", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  36. MetaModel::Init_AddAttribute(new AttributeString("password", array("label"=>"password", "description"=>"user authentication string", "allowed_values"=>null, "sql"=>"pwd", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  37. MetaModel::Init_AddAttribute(new AttributeString("email", array("label"=>"email", "description"=>"email address", "allowed_values"=>null, "sql"=>"email", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  38. MetaModel::Init_AddAttribute(new AttributeString("firstname", array("label"=>"firstname", "description"=>"first name", "allowed_values"=>null, "sql"=>"firstname", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  39. MetaModel::Init_AddAttribute(new AttributeString("lastname", array("label"=>"lastname", "description"=>"last name", "allowed_values"=>null, "sql"=>"lastname", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  40. //MetaModel::Init_InheritFilters();
  41. MetaModel::Init_AddFilterFromAttribute("userid");
  42. MetaModel::Init_AddFilterFromAttribute("login");
  43. }
  44. }
  45. class URP_Profiles extends DBObject
  46. {
  47. public static function Init()
  48. {
  49. $aParams = array
  50. (
  51. "category" => "addon/userrights",
  52. "name" => "profile",
  53. "description" => "usage profiles",
  54. "key_type" => "autoincrement",
  55. "key_label" => "",
  56. "name_attcode" => "name",
  57. "state_attcode" => "",
  58. "reconc_keys" => array(),
  59. "db_table" => "priv_urp_profiles",
  60. "db_key_field" => "id",
  61. "db_finalclass_field" => "",
  62. );
  63. MetaModel::Init_Params($aParams);
  64. //MetaModel::Init_InheritAttributes();
  65. MetaModel::Init_AddAttribute(new AttributeString("name", array("label"=>"name", "description"=>"label", "allowed_values"=>null, "sql"=>"name", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  66. MetaModel::Init_AddAttribute(new AttributeString("description", array("label"=>"description", "description"=>"one line description", "allowed_values"=>null, "sql"=>"description", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  67. //MetaModel::Init_InheritFilters();
  68. MetaModel::Init_AddFilterFromAttribute("name");
  69. MetaModel::Init_AddFilterFromAttribute("description");
  70. }
  71. }
  72. class URP_Dimensions extends DBObject
  73. {
  74. public static function Init()
  75. {
  76. $aParams = array
  77. (
  78. "category" => "addon/userrights",
  79. "name" => "dimension",
  80. "description" => "application dimension (defining silos)",
  81. "key_type" => "autoincrement",
  82. "key_label" => "",
  83. "name_attcode" => "name",
  84. "state_attcode" => "",
  85. "reconc_keys" => array(),
  86. "db_table" => "priv_urp_dimensions",
  87. "db_key_field" => "id",
  88. "db_finalclass_field" => "",
  89. );
  90. MetaModel::Init_Params($aParams);
  91. //MetaModel::Init_InheritAttributes();
  92. MetaModel::Init_AddAttribute(new AttributeString("name", array("label"=>"name", "description"=>"label", "allowed_values"=>null, "sql"=>"name", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  93. MetaModel::Init_AddAttribute(new AttributeString("description", array("label"=>"description", "description"=>"one line description", "allowed_values"=>null, "sql"=>"description", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  94. MetaModel::Init_AddAttribute(new AttributeString("type", array("label"=>"type", "description"=>"class name or data type (projection unit)", "allowed_values"=>new ValueSetEnumClasses('bizmodel', 'String,Integer'), "sql"=>"type", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  95. //MetaModel::Init_InheritFilters();
  96. MetaModel::Init_AddFilterFromAttribute("name");
  97. MetaModel::Init_AddFilterFromAttribute("description");
  98. MetaModel::Init_AddFilterFromAttribute("type");
  99. }
  100. public function CheckProjectionSpec($oProjectionSpec)
  101. {
  102. $sExpression = $oProjectionSpec->Get('value');
  103. $sAttribute = $oProjectionSpec->Get('attribute');
  104. // Shortcut: "any value" or "no value" means no projection
  105. if (empty($sExpression)) return;
  106. if ($sExpression == '<any>') return;
  107. // 1st - compute the data type for the dimension
  108. //
  109. $sType = $this->Get('type');
  110. if (MetaModel::IsValidClass($sType))
  111. {
  112. $sExpectedType = $sType;
  113. }
  114. else
  115. {
  116. $sExpectedType = '_scalar_';
  117. }
  118. // 2nd - compute the data type for the projection
  119. //
  120. $bIsOql = true;
  121. $sExpressionClass = '';
  122. try
  123. {
  124. $oObjectSearch = DBObjectSearch::FromOQL($sExpression);
  125. $sExpressionClass = $oObjectSearch->GetClass();
  126. }
  127. catch (OqlException $e)
  128. {
  129. $bIsOql = false;
  130. }
  131. if ($bIsOql)
  132. {
  133. if (empty($sAttribute))
  134. {
  135. $sFoundType = $sExpressionClass;
  136. }
  137. else
  138. {
  139. if (!MetaModel::IsValidAttCode($sExpressionClass, $sAttribute))
  140. {
  141. throw new CoreException('Unkown attribute code in projection specification', array('found' => $sAttribute, 'expecting' => MetaModel::GetAttributesList($sExpressionClass), 'class' => $sExpressionClass, 'projection' => $oProjectionSpec));
  142. }
  143. $oAttDef = MetaModel::GetAttributeDef($sExpressionClass, $sAttribute);
  144. if ($oAttDef->IsExternalKey())
  145. {
  146. $sFoundType = $oAttDef->GetTargetClass();
  147. }
  148. else
  149. {
  150. $sFoundType = '_scalar_';
  151. }
  152. }
  153. }
  154. else
  155. {
  156. $sFoundType = '_scalar_';
  157. }
  158. // Compare the dimension type and projection type
  159. if ($sFoundType != $sExpectedType)
  160. {
  161. throw new CoreException('Wrong type in projection specification', array('found' => $sFoundType, 'expecting' => $sExpectedType, 'expression' => $sExpression, 'attribute' => $sAttribute, 'projection' => $oProjectionSpec));
  162. }
  163. }
  164. }
  165. class URP_UserProfile extends DBObject
  166. {
  167. public static function Init()
  168. {
  169. $aParams = array
  170. (
  171. "category" => "addon/userrights",
  172. "name" => "user_profile",
  173. "description" => "user profiles",
  174. "key_type" => "autoincrement",
  175. "key_label" => "",
  176. "name_attcode" => "",
  177. "state_attcode" => "",
  178. "reconc_keys" => array(),
  179. "db_table" => "priv_urp_userprofile",
  180. "db_key_field" => "id",
  181. "db_finalclass_field" => "",
  182. );
  183. MetaModel::Init_Params($aParams);
  184. //MetaModel::Init_InheritAttributes();
  185. MetaModel::Init_AddAttribute(new AttributeExternalKey("userid", array("targetclass"=>"URP_Users", "jointype"=> "", "label"=>"User", "description"=>"user account", "allowed_values"=>null, "sql"=>"userid", "is_null_allowed"=>false, "depends_on"=>array())));
  186. MetaModel::Init_AddAttribute(new AttributeExternalField("userlogin", array("label"=>"Login", "description"=>"User's login", "allowed_values"=>null, "extkey_attcode"=> 'userid', "target_attcode"=>"login")));
  187. MetaModel::Init_AddAttribute(new AttributeExternalKey("profileid", array("targetclass"=>"URP_Profiles", "jointype"=> "", "label"=>"profile", "description"=>"usage profile", "allowed_values"=>null, "sql"=>"profileid", "is_null_allowed"=>false, "depends_on"=>array())));
  188. MetaModel::Init_AddAttribute(new AttributeExternalField("profile", array("label"=>"Profile", "description"=>"Profile name", "allowed_values"=>null, "extkey_attcode"=> 'profileid', "target_attcode"=>"name")));
  189. //MetaModel::Init_InheritFilters();
  190. MetaModel::Init_AddFilterFromAttribute("userid");
  191. MetaModel::Init_AddFilterFromAttribute("profileid");
  192. }
  193. }
  194. class URP_ProfileProjection extends DBObject
  195. {
  196. public static function Init()
  197. {
  198. $aParams = array
  199. (
  200. "category" => "addon/userrights",
  201. "name" => "profile_projection",
  202. "description" => "profile projections",
  203. "key_type" => "autoincrement",
  204. "key_label" => "",
  205. "name_attcode" => "",
  206. "state_attcode" => "",
  207. "reconc_keys" => array(),
  208. "db_table" => "priv_urp_profileprojection",
  209. "db_key_field" => "id",
  210. "db_finalclass_field" => "",
  211. );
  212. MetaModel::Init_Params($aParams);
  213. //MetaModel::Init_InheritAttributes();
  214. MetaModel::Init_AddAttribute(new AttributeExternalKey("dimensionid", array("targetclass"=>"URP_Dimensions", "jointype"=> "", "label"=>"Dimension", "description"=>"application dimension", "allowed_values"=>null, "sql"=>"dimensionid", "is_null_allowed"=>false, "depends_on"=>array())));
  215. MetaModel::Init_AddAttribute(new AttributeExternalField("dimension", array("label"=>"Dimension", "description"=>"application dimension", "allowed_values"=>null, "extkey_attcode"=> 'dimensionid', "target_attcode"=>"name")));
  216. MetaModel::Init_AddAttribute(new AttributeExternalKey("profileid", array("targetclass"=>"URP_Profiles", "jointype"=> "", "label"=>"profile", "description"=>"usage profile", "allowed_values"=>null, "sql"=>"profileid", "is_null_allowed"=>false, "depends_on"=>array())));
  217. MetaModel::Init_AddAttribute(new AttributeExternalField("profile", array("label"=>"Profile", "description"=>"Profile name", "allowed_values"=>null, "extkey_attcode"=> 'profileid', "target_attcode"=>"name")));
  218. MetaModel::Init_AddAttribute(new AttributeString("value", array("label"=>"Value expression", "description"=>"OQL expression (using \$user) | constant | <any>", "allowed_values"=>null, "sql"=>"value", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  219. MetaModel::Init_AddAttribute(new AttributeString("attribute", array("label"=>"Attribute", "description"=>"Target attribute code (optional)", "allowed_values"=>null, "sql"=>"attribute", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  220. //MetaModel::Init_InheritFilters();
  221. MetaModel::Init_AddFilterFromAttribute("dimensionid");
  222. MetaModel::Init_AddFilterFromAttribute("profileid");
  223. }
  224. public function ProjectUser(URP_Users $oUser)
  225. {
  226. $sExpr = $this->Get('value');
  227. if (strtolower(substr($sExpr, 0, 6)) == 'select')
  228. {
  229. $sColumn = $this->Get('attribute');
  230. // SELECT...
  231. $oValueSetDef = new ValueSetObjects($sExpr, $sColumn);
  232. $aValues = $oValueSetDef->GetValues(array('user' => $oUser), '');
  233. $aRes = array_values($aValues);
  234. }
  235. elseif ($sExpr == '<any>')
  236. {
  237. $aRes = null;
  238. }
  239. else
  240. {
  241. // Constant value(s)
  242. $aRes = explode(';', trim($sExpr));
  243. }
  244. return $aRes;
  245. }
  246. }
  247. class URP_ClassProjection extends DBObject
  248. {
  249. public static function Init()
  250. {
  251. $aParams = array
  252. (
  253. "category" => "addon/userrights",
  254. "name" => "class_projection",
  255. "description" => "class projections",
  256. "key_type" => "autoincrement",
  257. "key_label" => "",
  258. "name_attcode" => "",
  259. "state_attcode" => "",
  260. "reconc_keys" => array(),
  261. "db_table" => "priv_urp_classprojection",
  262. "db_key_field" => "id",
  263. "db_finalclass_field" => "",
  264. );
  265. MetaModel::Init_Params($aParams);
  266. //MetaModel::Init_InheritAttributes();
  267. MetaModel::Init_AddAttribute(new AttributeExternalKey("dimensionid", array("targetclass"=>"URP_Dimensions", "jointype"=> "", "label"=>"Dimension", "description"=>"application dimension", "allowed_values"=>null, "sql"=>"dimensionid", "is_null_allowed"=>false, "depends_on"=>array())));
  268. MetaModel::Init_AddAttribute(new AttributeExternalField("dimension", array("label"=>"Dimension", "description"=>"application dimension", "allowed_values"=>null, "extkey_attcode"=> 'dimensionid', "target_attcode"=>"name")));
  269. MetaModel::Init_AddAttribute(new AttributeString("class", array("label"=>"Class", "description"=>"Target class", "allowed_values"=>null, "sql"=>"class", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  270. MetaModel::Init_AddAttribute(new AttributeString("value", array("label"=>"Value expression", "description"=>"OQL expression (using \$this) | constant | <any>", "allowed_values"=>null, "sql"=>"value", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  271. MetaModel::Init_AddAttribute(new AttributeString("attribute", array("label"=>"Attribute", "description"=>"Target attribute code (optional)", "allowed_values"=>null, "sql"=>"attribute", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  272. //MetaModel::Init_InheritFilters();
  273. MetaModel::Init_AddFilterFromAttribute("dimensionid");
  274. MetaModel::Init_AddFilterFromAttribute("class");
  275. }
  276. public function ProjectObject($oObject)
  277. {
  278. $sExpr = $this->Get('value');
  279. if (strtolower(substr($sExpr, 0, 6)) == 'select')
  280. {
  281. $sColumn = $this->Get('attribute');
  282. // SELECT...
  283. $oValueSetDef = new ValueSetObjects($sExpr, $sColumn);
  284. $aValues = $oValueSetDef->GetValues(array('user' => $oObject), '');
  285. $aRes = array_values($aValues);
  286. }
  287. elseif ($sExpr == '<any>')
  288. {
  289. $aRes = null;
  290. }
  291. else
  292. {
  293. // Constant value(s)
  294. $aRes = explode(';', trim($sExpr));
  295. }
  296. return $aRes;
  297. }
  298. }
  299. class URP_ActionGrant extends DBObject
  300. {
  301. public static function Init()
  302. {
  303. $aParams = array
  304. (
  305. "category" => "addon/userrights",
  306. "name" => "action_permission",
  307. "description" => "permissions on classes",
  308. "key_type" => "autoincrement",
  309. "key_label" => "",
  310. "name_attcode" => "",
  311. "state_attcode" => "",
  312. "reconc_keys" => array(),
  313. "db_table" => "priv_urp_grant_actions",
  314. "db_key_field" => "id",
  315. "db_finalclass_field" => "",
  316. );
  317. MetaModel::Init_Params($aParams);
  318. //MetaModel::Init_InheritAttributes();
  319. // Common to all grant classes (could be factorized by class inheritence, but this has to be benchmarked)
  320. MetaModel::Init_AddAttribute(new AttributeExternalKey("profileid", array("targetclass"=>"URP_Profiles", "jointype"=> "", "label"=>"Profile", "description"=>"usage profile", "allowed_values"=>null, "sql"=>"profileid", "is_null_allowed"=>false, "depends_on"=>array())));
  321. MetaModel::Init_AddAttribute(new AttributeExternalField("profile", array("label"=>"Profile", "description"=>"usage profile", "allowed_values"=>null, "extkey_attcode"=> 'profileid', "target_attcode"=>"name")));
  322. MetaModel::Init_AddAttribute(new AttributeString("class", array("label"=>"class", "description"=>"class name", "allowed_values"=>null, "sql"=>"class", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  323. MetaModel::Init_AddAttribute(new AttributeEnum("permission", array("label"=>"permission", "description"=>"allowed or not allowed?", "allowed_values"=>new ValueSetEnum('yes,no'), "sql"=>"permission", "default_value"=>"yes", "is_null_allowed"=>false, "depends_on"=>array())));
  324. MetaModel::Init_AddAttribute(new AttributeString("action", array("label"=>"action", "description"=>"operations to perform on the given class", "allowed_values"=>null, "sql"=>"action", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  325. //MetaModel::Init_InheritFilters();
  326. // Common to all grant classes (could be factorized by class inheritence, but this has to be benchmarked)
  327. MetaModel::Init_AddFilterFromAttribute("profileid");
  328. MetaModel::Init_AddFilterFromAttribute("profile");
  329. MetaModel::Init_AddFilterFromAttribute("class");
  330. MetaModel::Init_AddFilterFromAttribute("action");
  331. }
  332. }
  333. class URP_StimulusGrant extends DBObject
  334. {
  335. public static function Init()
  336. {
  337. $aParams = array
  338. (
  339. "category" => "addon/userrights",
  340. "name" => "stimulus_permission",
  341. "description" => "permissions on stimilus in the life cycle of the object",
  342. "key_type" => "autoincrement",
  343. "key_label" => "",
  344. "name_attcode" => "",
  345. "state_attcode" => "",
  346. "reconc_keys" => array(),
  347. "db_table" => "priv_urp_grant_stimulus",
  348. "db_key_field" => "id",
  349. "db_finalclass_field" => "",
  350. );
  351. MetaModel::Init_Params($aParams);
  352. //MetaModel::Init_InheritAttributes();
  353. // Common to all grant classes (could be factorized by class inheritence, but this has to be benchmarked)
  354. MetaModel::Init_AddAttribute(new AttributeExternalKey("profileid", array("targetclass"=>"URP_Profiles", "jointype"=> "", "label"=>"Profile", "description"=>"usage profile", "allowed_values"=>null, "sql"=>"profileid", "is_null_allowed"=>false, "depends_on"=>array())));
  355. MetaModel::Init_AddAttribute(new AttributeExternalField("profile", array("label"=>"Profile", "description"=>"usage profile", "allowed_values"=>null, "extkey_attcode"=> 'profileid', "target_attcode"=>"name")));
  356. MetaModel::Init_AddAttribute(new AttributeString("class", array("label"=>"class", "description"=>"class name", "allowed_values"=>null, "sql"=>"class", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  357. MetaModel::Init_AddAttribute(new AttributeEnum("permission", array("label"=>"permission", "description"=>"allowed or not allowed?", "allowed_values"=>new ValueSetEnum('yes,no'), "sql"=>"permission", "default_value"=>"yes", "is_null_allowed"=>false, "depends_on"=>array())));
  358. MetaModel::Init_AddAttribute(new AttributeString("stimulus", array("label"=>"stimulus", "description"=>"stimulus code", "allowed_values"=>null, "sql"=>"action", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  359. //MetaModel::Init_InheritFilters();
  360. // Common to all grant classes (could be factorized by class inheritence, but this has to be benchmarked)
  361. MetaModel::Init_AddFilterFromAttribute("profileid");
  362. MetaModel::Init_AddFilterFromAttribute("profile");
  363. MetaModel::Init_AddFilterFromAttribute("class");
  364. MetaModel::Init_AddFilterFromAttribute("stimulus");
  365. }
  366. }
  367. class URP_AttributeGrant extends DBObject
  368. {
  369. public static function Init()
  370. {
  371. $aParams = array
  372. (
  373. "category" => "addon/userrights",
  374. "name" => "attribute_permission",
  375. "description" => "permissions at the attributes level",
  376. "key_type" => "autoincrement",
  377. "key_label" => "",
  378. "name_attcode" => "",
  379. "state_attcode" => "",
  380. "reconc_keys" => array(),
  381. "db_table" => "priv_urp_grant_attributes",
  382. "db_key_field" => "id",
  383. "db_finalclass_field" => "",
  384. );
  385. MetaModel::Init_Params($aParams);
  386. //MetaModel::Init_InheritAttributes();
  387. MetaModel::Init_AddAttribute(new AttributeExternalKey("actiongrantid", array("targetclass"=>"URP_ActionGrant", "jointype"=> "", "label"=>"Action grant", "description"=>"action grant", "allowed_values"=>null, "sql"=>"actiongrantid", "is_null_allowed"=>false, "depends_on"=>array())));
  388. MetaModel::Init_AddAttribute(new AttributeString("attcode", array("label"=>"attribute", "description"=>"attribute code", "allowed_values"=>null, "sql"=>"attcode", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  389. //MetaModel::Init_InheritFilters();
  390. MetaModel::Init_AddFilterFromAttribute("actiongrantid");
  391. MetaModel::Init_AddFilterFromAttribute("attcode");
  392. }
  393. }
  394. class UserRightsProfile extends UserRightsAddOnAPI
  395. {
  396. static public $m_aActionCodes = array(
  397. UR_ACTION_READ => 'read',
  398. UR_ACTION_MODIFY => 'modify',
  399. UR_ACTION_DELETE => 'delete',
  400. UR_ACTION_BULK_READ => 'bulk read',
  401. UR_ACTION_BULK_MODIFY => 'bulk modify',
  402. UR_ACTION_BULK_DELETE => 'bulk delete',
  403. );
  404. // Installation: create the very first user
  405. public function CreateAdministrator($sAdminUser, $sAdminPwd, $sAdminEmail, $sFirstName, $sLastName, $sPhoneNumber)
  406. {
  407. // Maybe we should check that no other user with userid == 0 exists
  408. $oUser = new URP_Users();
  409. $oUser->Set('login', $sAdminUser);
  410. $oUser->Set('password', $sAdminPwd);
  411. $oUser->Set('email', $sAdminEmail);
  412. $oUser->Set('firstname', $sFirstName);
  413. $oUser->Set('lastname', $sLastName);
  414. $oUser->Set('phonenumber', $sPhoneNumber);
  415. $oUser->Set('userid', 1); // one is for root !
  416. $iUserId = $oUser->DBInsertNoReload();
  417. $this->SetupUser($iUserId, true);
  418. return true;
  419. }
  420. public function Setup()
  421. {
  422. // Dimensions/Profiles/Classes/Attributes/Stimuli could be added anytime
  423. // This procedure will then update the matrix with expected default values
  424. //
  425. $oProfileSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_Profiles"));
  426. while ($oProfile = $oProfileSet->Fetch())
  427. {
  428. $this->SetupProfile($oProfile);
  429. }
  430. $oDimensionSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_Dimensions"));
  431. while ($oDimension = $oDimensionSet->Fetch())
  432. {
  433. $this->SetupDimension($oDimension);
  434. }
  435. return true;
  436. }
  437. protected function SetupDimension($oDimension, $bNewDimension = false)
  438. {
  439. $iDimensionId = $oDimension->GetKey();
  440. // Create projections, for any class where it applies
  441. //
  442. foreach(array('bizmodel', 'application', 'gui', 'core/cmdb') as $sCategory)
  443. {
  444. foreach (MetaModel::GetClasses($sCategory) as $sClass)
  445. {
  446. if ($bNewDimension)
  447. {
  448. $bAddCell = true;
  449. }
  450. else
  451. {
  452. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_ClassProjection WHERE class = :class AND dimensionid = :dimension"), array(), array('class'=>$sClass, 'dimension'=>$iDimensionId));
  453. $bAddCell = ($oSet->Count() < 1);
  454. }
  455. if ($bAddCell)
  456. {
  457. // Create a new entry
  458. $oCProj = MetaModel::NewObject("URP_ClassProjection");
  459. $oCProj->Set("dimensionid", $iDimensionId);
  460. $oCProj->Set("class", $sClass);
  461. $oCProj->Set("value", "true");
  462. $iId = $oCProj->DBInsertNoReload();
  463. }
  464. }
  465. }
  466. // Create projections, for any existing profile
  467. //
  468. $oProfileSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_Profiles"));
  469. while ($oProfile = $oProfileSet->Fetch())
  470. {
  471. $iProfileId = $oProfile->GetKey();
  472. if ($bNewDimension)
  473. {
  474. $bAddCell = true;
  475. }
  476. else
  477. {
  478. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_ProfileProjection WHERE dimensionid = :dimension AND profileid = :profile"), array(), array('dimension'=>$iDimensionId, 'profile'=>$iProfileId));
  479. $bAddCell = ($oSet->Count() < 1);
  480. }
  481. if ($bAddCell)
  482. {
  483. // Create a new entry
  484. $oDProj = MetaModel::NewObject("URP_ProfileProjection");
  485. $oDProj->Set("dimensionid", $iDimensionId);
  486. $oDProj->Set("profileid", $iProfileId);
  487. $oDProj->Set("value", "true");
  488. $iId = $oDProj->DBInsertNoReload();
  489. }
  490. }
  491. }
  492. protected function SetupProfile($oProfile, $bNewProfile = false)
  493. {
  494. $iProfileId = $oProfile->GetKey();
  495. // Create grant records, for any class where it applies
  496. //
  497. foreach(array('bizmodel', 'application', 'gui', 'core/cmdb') as $sCategory)
  498. {
  499. foreach (MetaModel::GetClasses($sCategory) as $sClass)
  500. {
  501. foreach (self::$m_aActionCodes as $iActionCode => $sAction)
  502. {
  503. if ($bNewProfile)
  504. {
  505. $bAddCell = true;
  506. }
  507. else
  508. {
  509. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_ActionGrant WHERE class = :class AND action = :action AND profileid = :profile"), array(), array('class'=>$sClass, 'action'=>$sAction, 'profile'=>$iProfileId));
  510. $bAddCell = ($oSet->Count() < 1);
  511. }
  512. if ($bAddCell)
  513. {
  514. // Create a new entry
  515. $oMyActionGrant = MetaModel::NewObject("URP_ActionGrant");
  516. $oMyActionGrant->Set("profileid", $iProfileId);
  517. $oMyActionGrant->Set("class", $sClass);
  518. $oMyActionGrant->Set("action", $sAction);
  519. $oMyActionGrant->Set("permission", "yes");
  520. $iId = $oMyActionGrant->DBInsertNoReload();
  521. }
  522. }
  523. foreach (MetaModel::EnumStimuli($sClass) as $sStimulusCode => $oStimulus)
  524. {
  525. if ($bNewProfile)
  526. {
  527. $bAddCell = true;
  528. }
  529. else
  530. {
  531. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_StimulusGrant WHERE class = :class AND stimulus = :stimulus AND profileid = :profile"), array(), array('class'=>$sClass, 'stimulus'=>$sStimulusCode, 'profile'=>$iProfileId));
  532. $bAddCell = ($oSet->Count() < 1);
  533. }
  534. if ($bAddCell)
  535. {
  536. // Create a new entry
  537. $oMyStGrant = MetaModel::NewObject("URP_StimulusGrant");
  538. $oMyStGrant->Set("profileid", $iProfileId);
  539. $oMyStGrant->Set("class", $sClass);
  540. $oMyStGrant->Set("stimulus", $sStimulusCode);
  541. $oMyStGrant->Set("permission", "yes");
  542. $iId = $oMyStGrant->DBInsertNoReload();
  543. }
  544. }
  545. }
  546. }
  547. // Create the "My Bookmarks" menu item (parent_id = 0, rank = 6)
  548. if ($bNewProfile)
  549. {
  550. $bAddMenu = true;
  551. }
  552. else
  553. {
  554. //$oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT menuNode WHERE type = 'user' AND parent_id = 0 AND user_id = $iUserId"));
  555. //$bAddMenu = ($oSet->Count() < 1);
  556. }
  557. $bAddMenu = false;
  558. if ($bAddMenu)
  559. {
  560. $oMenu = MetaModel::NewObject('menuNode');
  561. $oMenu->Set('type', 'user');
  562. $oMenu->Set('parent_id', 0); // It's a toplevel entry
  563. $oMenu->Set('rank', 6); // Located just above the Admin Tools section (=7)
  564. $oMenu->Set('name', 'My Bookmarks');
  565. $oMenu->Set('label', 'My Favorite Items');
  566. $oMenu->Set('hyperlink', 'UI.php');
  567. $oMenu->Set('template', '<p></p><p></p><p style="text-align:center; font-family:Georgia, Times, serif; font-size:32px;">My bookmarks</p><p style="text-align:center; font-family:Georgia, Times, serif; font-size:14px;"><i>This section contains my most favorite search results</i></p>');
  568. $oMenu->Set('user_id', $iUserId);
  569. $oMenu->DBInsert();
  570. }
  571. }
  572. public function Init()
  573. {
  574. MetaModel::RegisterPlugin('userrights', 'ACbyProfile', array($this, 'CacheData'));
  575. }
  576. protected $m_aUsers = array(); // id -> object
  577. protected $m_aDimensions = array(); // id -> object
  578. protected $m_aClassProj = array(); // class,dimensionid -> object
  579. protected $m_aProfiles = array(); // id -> object
  580. protected $m_aUserProfiles = array(); // userid,profileid -> object
  581. protected $m_aProPro = array(); // profileid,dimensionid -> object
  582. protected $m_aClassActionGrants = array(); // profile, class, action -> permission
  583. protected $m_aObjectActionGrants = array(); // userid, class, id, action -> permission, list of attributes
  584. public function CacheData()
  585. {
  586. // Could be loaded in a shared memory (?)
  587. $oUserSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_Users"));
  588. while ($oUser = $oUserSet->Fetch())
  589. {
  590. $this->m_aUsers[$oUser->GetKey()] = $oUser;
  591. }
  592. $oDimensionSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_Dimensions"));
  593. while ($oDimension = $oDimensionSet->Fetch())
  594. {
  595. $this->m_aDimensions[$oDimension->GetKey()] = $oDimension;
  596. }
  597. $oClassProjSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_ClassProjection"));
  598. while ($oClassProj = $oClassProjSet->Fetch())
  599. {
  600. $this->m_aClassProjs[$oClassProj->Get('class')][$oClassProj->Get('dimensionid')] = $oClassProj;
  601. }
  602. $oProfileSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_Profiles"));
  603. while ($oProfile = $oProfileSet->Fetch())
  604. {
  605. $this->m_aProfiles[$oProfile->GetKey()] = $oProfile;
  606. }
  607. $oUserProfileSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_UserProfile"));
  608. while ($oUserProfile = $oUserProfileSet->Fetch())
  609. {
  610. $this->m_aUserProfiles[$oUserProfile->Get('userid')][$oUserProfile->Get('profileid')] = $oUserProfile;
  611. }
  612. $oProProSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_ProfileProjection"));
  613. while ($oProPro = $oProProSet->Fetch())
  614. {
  615. $this->m_aProPros[$oProPro->Get('profileid')][$oProPro->Get('dimensionid')] = $oProPro;
  616. }
  617. /*
  618. echo "<pre>\n";
  619. print_r($this->m_aUsers);
  620. print_r($this->m_aDimensions);
  621. print_r($this->m_aClassProjs);
  622. print_r($this->m_aProfiles);
  623. print_r($this->m_aUserProfiles);
  624. print_r($this->m_aProPros);
  625. echo "</pre>\n";
  626. exit;
  627. */
  628. return true;
  629. }
  630. public function CheckCredentials($sUserName, $sPassword)
  631. {
  632. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_Users WHERE login = :login"), array(), array('login' => $sUserName));
  633. if ($oSet->Count() < 1)
  634. {
  635. // todo: throw an exception?
  636. return false;
  637. }
  638. $oUser = $oSet->Fetch();
  639. if ($oUser->Get('password') == $sPassword)
  640. {
  641. return $oUser->GetKey();
  642. }
  643. // todo: throw an exception?
  644. return false;
  645. }
  646. public function GetUserId($sUserName)
  647. {
  648. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_Users WHERE login = :login"), array(), array('login' => $sUserName));
  649. if ($oSet->Count() < 1)
  650. {
  651. // todo: throw an exception?
  652. return false;
  653. }
  654. $oUser = $oSet->Fetch();
  655. return $oUser->GetKey();
  656. }
  657. public function GetFilter($sUserName, $sClass)
  658. {
  659. $oNullFilter = new DBObjectSearch($sClass);
  660. return $oNullFilter;
  661. }
  662. protected function GetClassActionGrant($iProfile, $sClass, $sAction)
  663. {
  664. $aTest = @$this->m_aClassActionGrants[$iProfile][$sClass][$sAction];
  665. if (isset($aTest)) return $aTest;
  666. // Get the permission for this profile/class/action
  667. $oSearch = DBObjectSearch::FromOQL("SELECT URP_ActionGrant WHERE class = :class AND action = :action AND profileid = :profile");
  668. $oSet = new DBObjectSet($oSearch, array(), array('class'=>$sClass, 'action'=>$sAction, 'profile'=>$iProfile));
  669. if ($oSet->Count() < 1)
  670. {
  671. return null;
  672. }
  673. $oGrantRecord = $oSet->Fetch();
  674. $this->m_aClassActionGrants[$iProfile][$sClass][$sAction] = $oGrantRecord;
  675. return $oGrantRecord;
  676. }
  677. protected function GetObjectActionGrant($oUser, $sClass, $iActionCode, $oObject)
  678. {
  679. // load and cache permissions for the current user on the given object
  680. //
  681. $aTest = @$this->m_aObjectActionGrants[$oUser->GetKey()][$sClass][$oObject->GetKey][$iActionCode];
  682. if (is_array($aTest)) return $aTest;
  683. $sAction = self::$m_aActionCodes[$iActionCode];
  684. $iInstancePermission = UR_ALLOWED_NO;
  685. $aAttributes = array();
  686. foreach($this->GetMatchingProfiles($oUser, $oObject) as $iProfile)
  687. {
  688. $oGrantRecord = $this->GetClassActionGrant($iProfile, $sClass, $sAction);
  689. if (is_null($oGrantRecord))
  690. {
  691. continue; // loop to the next profile
  692. }
  693. elseif ($oGrantRecord->Get('permission') == 'yes')
  694. {
  695. $iInstancePermission = UR_ALLOWED_YES;
  696. // merge the list of attributes allowed for this profile
  697. $oSearch = DBObjectSearch::FromOQL("SELECT URP_AttributeGrant WHERE actiongrantid = :actiongrantid");
  698. $oSet = new DBObjectSet($oSearch, array(), array('actiongrantid' => $oGrantRecord->GetKey()));
  699. $aAttributes = array_merge($aAttributes, $oSet->GetColumnAsArray('attcode', false));
  700. }
  701. }
  702. $aRes = array(
  703. 'permission' => $iInstancePermission,
  704. 'attributes' => $aAttributes,
  705. );
  706. $this->m_aObjectActionGrants[$oUser->GetKey()][$sClass][$oObject->GetKey()][$iActionCode] = $aRes;
  707. return $aRes;
  708. }
  709. public function IsActionAllowed($iUserId, $sClass, $iActionCode, dbObjectSet $oInstances)
  710. {
  711. $oUser = $this->m_aUsers[$iUserId];
  712. $oInstances->Rewind();
  713. while($oObject = $oInstances->Fetch())
  714. {
  715. $aObjectPermissions = $this->GetObjectActionGrant($oUser, $sClass, $iActionCode, $oObject);
  716. $iInstancePermission = $aObjectPermissions['permission'];
  717. if (isset($iGlobalPermission))
  718. {
  719. if ($iInstancePermission != $iGlobalPermission)
  720. {
  721. $iGlobalPermission = UR_ALLOWED_DEPENDS;
  722. }
  723. }
  724. else
  725. {
  726. $iGlobalPermission = $iInstancePermission;
  727. }
  728. }
  729. if (isset($iGlobalPermission))
  730. {
  731. return $iGlobalPermission;
  732. }
  733. else
  734. {
  735. return UR_ALLOWED_NO;
  736. }
  737. }
  738. public function IsActionAllowedOnAttribute($iUserId, $sClass, $sAttCode, $iActionCode, dbObjectSet $oInstances)
  739. {
  740. $oUser = $this->m_aUsers[$iUserId];
  741. $oInstances->Rewind();
  742. while($oObject = $oInstances->Fetch())
  743. {
  744. $aObjectPermissions = $this->GetObjectActionGrant($oUser, $sClass, $iActionCode, $oObject);
  745. $aAttributes = $aObjectPermissions['attributes'];
  746. if (in_array($sAttCode, $aAttributes))
  747. {
  748. $iInstancePermission = $aObjectPermissions['permission'];
  749. }
  750. else
  751. {
  752. $iInstancePermission = UR_ALLOWED_NO;
  753. }
  754. if (isset($iGlobalPermission))
  755. {
  756. if ($iInstancePermission != $iGlobalPermission)
  757. {
  758. $iGlobalPermission = UR_ALLOWED_DEPENDS;
  759. }
  760. }
  761. else
  762. {
  763. $iGlobalPermission = $iInstancePermission;
  764. }
  765. }
  766. if (isset($iGlobalPermission))
  767. {
  768. return $iGlobalPermission;
  769. }
  770. else
  771. {
  772. return UR_ALLOWED_NO;
  773. }
  774. }
  775. public function IsStimulusAllowed($iUserId, $sClass, $sStimulusCode, dbObjectSet $oInstances)
  776. {
  777. $oUser = $this->m_aUsers[$iUserId];
  778. // Note: this code is VERY close to the code of IsActionAllowed()
  779. $oInstances->Rewind();
  780. while($oObject = $oInstances->Fetch())
  781. {
  782. $iInstancePermission = UR_ALLOWED_NO;
  783. foreach($this->GetMatchingProfiles($oUser, $oObject) as $iProfile)
  784. {
  785. // Get the permission for this profile/class/stimulus
  786. $oSearch = DBObjectSearch::FromOQL("SELECT URP_StimulusGrant WHERE class = :class AND stimulus = :stimulus AND profileid = :profile");
  787. $oSet = new DBObjectSet($oSearch, array(), array('class'=>$sClass, 'stimulus'=>$sStimulusCode, 'profile'=>$iProfile));
  788. if ($oSet->Count() < 1)
  789. {
  790. return UR_ALLOWED_NO;
  791. }
  792. $oGrantRecord = $oSet->Fetch();
  793. $sPermission = $oGrantRecord->Get('permission');
  794. if ($sPermission == 'yes')
  795. {
  796. $iInstancePermission = UR_ALLOWED_YES;
  797. }
  798. }
  799. if (isset($iGlobalPermission))
  800. {
  801. if ($iInstancePermission != $iGlobalPermission)
  802. {
  803. $iGlobalPermission = UR_ALLOWED_DEPENDS;
  804. }
  805. }
  806. else
  807. {
  808. $iGlobalPermission = $iInstancePermission;
  809. }
  810. }
  811. if (isset($iGlobalPermission))
  812. {
  813. return $iGlobalPermission;
  814. }
  815. else
  816. {
  817. return UR_ALLOWED_NO;
  818. }
  819. }
  820. protected function GetMatchingProfilesByDim($oUser, $oObject, $oDimension)
  821. {
  822. //
  823. // List profiles for which the user projection overlaps the object projection in the given dimension
  824. //
  825. $iUser = $oUser->GetKey();
  826. $sClass = get_class($oObject);
  827. $iPKey = $oObject->GetKey();
  828. $iDimension = $oDimension->GetKey();
  829. $aObjectProjection = $this->m_aClassProjs[$sClass][$iDimension]->ProjectObject($oObject);
  830. $aRes = array();
  831. foreach ($this->m_aUserProfiles[$iUser] as $iProfile => $oProfile)
  832. {
  833. if (is_null($aObjectProjection))
  834. {
  835. $aRes[] = $iProfile;
  836. }
  837. else
  838. {
  839. // user projection to be cached on a given page !
  840. $aUserProjection = $this->m_aProPros[$iProfile][$iDimension]->ProjectUser($oUser);
  841. if (is_null($aUserProjection))
  842. {
  843. $aRes[] = $iProfile;
  844. }
  845. else
  846. {
  847. $aMatchingValues = array_intersect($aObjectProjection, $aUserProjection);
  848. if (count($aMatchingValues) > 0)
  849. {
  850. $aRes[] = $iProfile;
  851. }
  852. }
  853. }
  854. }
  855. return $aRes;
  856. }
  857. protected $m_aMatchingProfiles = array(); // cache of the matching profiles for a given user/object
  858. protected function GetMatchingProfiles($oUser, $oObject)
  859. {
  860. $iUser = $oUser->GetKey();
  861. $sClass = get_class($oObject);
  862. $iObject = $oObject->GetKey();
  863. //
  864. // List profiles for which the user projection overlaps the object projection in each and every dimension
  865. // Caches the result
  866. //
  867. $aTest = @$this->m_aMatchingProfiles[$iUser][$sClass][$iObject];
  868. if (is_array($aTest))
  869. {
  870. return $aTest;
  871. }
  872. $aProfileRes = array();
  873. foreach ($this->m_aDimensions as $iDimension => $oDimension)
  874. {
  875. foreach ($this->GetMatchingProfilesByDim($oUser, $oObject, $oDimension) as $iProfile)
  876. {
  877. @$aProfileRes[$iProfile] += 1;
  878. }
  879. }
  880. $aRes = array();
  881. $iDimCount = count($this->m_aDimensions);
  882. foreach ($aProfileRes as $iProfile => $iMatches)
  883. {
  884. if ($iMatches == $iDimCount)
  885. {
  886. $aRes[] = $iProfile;
  887. }
  888. }
  889. $this->m_aMatchingProfiles[$iUser][$sClass][$iObject] = $aRes;
  890. return $aRes;
  891. }
  892. }
  893. UserRights::SelectModule('UserRightsProfile');
  894. ?>