userrightsprofile.class.inc.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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 (preg_match('/^\s*([a-zA-Z0-9;]+)\s*$/', $sExpr, $aMatches))
  228. {
  229. // Constant value(s)
  230. $aRes = explode(';', $aMatches[1]);
  231. }
  232. elseif ($sExpr == '<any>')
  233. {
  234. $aRes = array('<any>');
  235. }
  236. else
  237. {
  238. $sColumn = $this->Get('attribute');
  239. // SELECT...
  240. $oValueSetDef = new ValueSetObjects($sExpr, $sColumn);
  241. $aValues = $oValueSetDef->GetValues(array('user' => $oUser), '');
  242. $aRes = array_values($aValues);
  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 (preg_match('/^\s*([a-zA-Z0-9;]+)\s*$/', $sExpr, $aMatches))
  280. {
  281. // Constant value(s)
  282. $aRes = explode(';', $aMatches[1]);
  283. }
  284. elseif ($sExpr == '<any>')
  285. {
  286. $aRes = array('<any>');
  287. }
  288. else
  289. {
  290. $sColumn = $this->Get('attribute');
  291. // SELECT...
  292. $oValueSetDef = new ValueSetObjects($sExpr, $sColumn);
  293. $aValues = $oValueSetDef->GetValues(array('this' => $oObject), '');
  294. $aRes = array_values($aValues);
  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"=>"action", "description"=>"operations to perform on the given class", "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 = '$sClass' AND dimensionid = $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 = $iDimensionId AND profileid = $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 = '$sClass' AND action = '$sAction' AND profileid = $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 = '$sClass' AND stimulus = '$sStimulusCode' AND profileid = $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. // Could be loaded in a shared memory (?)
  575. return true;
  576. }
  577. public function CheckCredentials($sUserName, $sPassword)
  578. {
  579. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_Users WHERE login = '$sUserName'"));
  580. if ($oSet->Count() < 1)
  581. {
  582. // todo: throw an exception?
  583. return false;
  584. }
  585. $oLogin = $oSet->Fetch();
  586. if ($oLogin->Get('password') == $sPassword)
  587. {
  588. return true;
  589. }
  590. // todo: throw an exception?
  591. return false;
  592. }
  593. public function GetFilter($sUserName, $sClass)
  594. {
  595. $oNullFilter = new DBObjectSearch($sClass);
  596. return $oNullFilter;
  597. }
  598. public function IsActionAllowed($sUserName, $sClass, $iActionCode, dbObjectSet $aInstances)
  599. {
  600. // #@# temporary
  601. return true;
  602. if (!array_key_exists($iActionCode, self::$m_aActionCodes))
  603. {
  604. return UR_ALLOWED_NO;
  605. }
  606. $sAction = self::$m_aActionCodes[$iActionCode];
  607. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_ActionGrant WHERE class = '$sClass' AND action = '$sAction' AND login = '$sUserName'"));
  608. if ($oSet->Count() < 1)
  609. {
  610. return UR_ALLOWED_NO;
  611. }
  612. $oGrantRecord = $oSet->Fetch();
  613. switch ($oGrantRecord->Get('permission'))
  614. {
  615. case 'yes':
  616. $iRetCode = UR_ALLOWED_YES;
  617. break;
  618. case 'no':
  619. default:
  620. $iRetCode = UR_ALLOWED_NO;
  621. break;
  622. }
  623. return $iRetCode;
  624. }
  625. public function IsActionAllowedOnAttribute($sUserName, $sClass, $sAttCode, $iActionCode, dbObjectSet $aInstances)
  626. {
  627. // #@# temporary
  628. return true;
  629. if (!array_key_exists($iActionCode, self::$m_aActionCodes))
  630. {
  631. return UR_ALLOWED_NO;
  632. }
  633. $sAction = self::$m_aActionCodes[$iActionCode];
  634. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_AttributeGrant WHERE URP_AttributeGrant.class = '$sClass' AND URP_AttributeGrant.attcode = '$sAttCode' AND URP_AttributeGrant.action = '$sAction' AND URP_AttributeGrant.login = '$sUserName'"));
  635. if ($oSet->Count() < 1)
  636. {
  637. return UR_ALLOWED_NO;
  638. }
  639. $oGrantRecord = $oSet->Fetch();
  640. switch ($oGrantRecord->Get('permission'))
  641. {
  642. case 'yes':
  643. $iRetCode = UR_ALLOWED_YES;
  644. break;
  645. case 'no':
  646. default:
  647. $iRetCode = UR_ALLOWED_NO;
  648. break;
  649. }
  650. return $iRetCode;
  651. }
  652. public function IsStimulusAllowed($sUserName, $sClass, $sStimulusCode, dbObjectSet $aInstances)
  653. {
  654. // #@# temporary
  655. return true;
  656. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_StimulusGrant WHERE class = '$sClass' AND stimulus = '$sStimulusCode' AND login = '$sUserName'"));
  657. if ($oSet->Count() < 1)
  658. {
  659. return UR_ALLOWED_NO;
  660. }
  661. $oGrantRecord = $oSet->Fetch();
  662. switch ($oGrantRecord->Get('permission'))
  663. {
  664. case 'yes':
  665. $iRetCode = UR_ALLOWED_YES;
  666. break;
  667. case 'no':
  668. default:
  669. $iRetCode = UR_ALLOWED_NO;
  670. break;
  671. }
  672. return $iRetCode;
  673. }
  674. }
  675. UserRights::SelectModule('UserRightsProfile');
  676. ?>