userrightsprofile.class.inc.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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_InheritFilters();
  95. MetaModel::Init_AddFilterFromAttribute("name");
  96. MetaModel::Init_AddFilterFromAttribute("description");
  97. }
  98. }
  99. class URP_UserProfile extends DBObject
  100. {
  101. public static function Init()
  102. {
  103. $aParams = array
  104. (
  105. "category" => "addon/userrights",
  106. "name" => "user_profile",
  107. "description" => "user profiles",
  108. "key_type" => "autoincrement",
  109. "key_label" => "",
  110. "name_attcode" => "",
  111. "state_attcode" => "",
  112. "reconc_keys" => array(),
  113. "db_table" => "priv_urp_userprofile",
  114. "db_key_field" => "id",
  115. "db_finalclass_field" => "",
  116. );
  117. MetaModel::Init_Params($aParams);
  118. //MetaModel::Init_InheritAttributes();
  119. 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())));
  120. MetaModel::Init_AddAttribute(new AttributeExternalField("userlogin", array("label"=>"Login", "description"=>"User's login", "allowed_values"=>null, "extkey_attcode"=> 'userid', "target_attcode"=>"login")));
  121. 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())));
  122. MetaModel::Init_AddAttribute(new AttributeExternalField("profile", array("label"=>"Profile", "description"=>"Profile name", "allowed_values"=>null, "extkey_attcode"=> 'profileid', "target_attcode"=>"name")));
  123. //MetaModel::Init_InheritFilters();
  124. MetaModel::Init_AddFilterFromAttribute("userid");
  125. MetaModel::Init_AddFilterFromAttribute("profileid");
  126. }
  127. }
  128. class URP_ProfileProjection extends DBObject
  129. {
  130. public static function Init()
  131. {
  132. $aParams = array
  133. (
  134. "category" => "addon/userrights",
  135. "name" => "profile_projection",
  136. "description" => "profile projections",
  137. "key_type" => "autoincrement",
  138. "key_label" => "",
  139. "name_attcode" => "",
  140. "state_attcode" => "",
  141. "reconc_keys" => array(),
  142. "db_table" => "priv_urp_profileprojection",
  143. "db_key_field" => "id",
  144. "db_finalclass_field" => "",
  145. );
  146. MetaModel::Init_Params($aParams);
  147. //MetaModel::Init_InheritAttributes();
  148. 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())));
  149. MetaModel::Init_AddAttribute(new AttributeExternalField("dimension", array("label"=>"Dimension", "description"=>"application dimension", "allowed_values"=>null, "extkey_attcode"=> 'dimensionid', "target_attcode"=>"name")));
  150. 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())));
  151. MetaModel::Init_AddAttribute(new AttributeExternalField("profile", array("label"=>"Profile", "description"=>"Profile name", "allowed_values"=>null, "extkey_attcode"=> 'profileid', "target_attcode"=>"name")));
  152. MetaModel::Init_AddAttribute(new AttributeString("value", array("label"=>"Value expression", "description"=>"OQL expression (using \$user) | constant", "allowed_values"=>null, "sql"=>"value", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  153. //MetaModel::Init_InheritFilters();
  154. MetaModel::Init_AddFilterFromAttribute("dimensionid");
  155. MetaModel::Init_AddFilterFromAttribute("profileid");
  156. }
  157. public function ProjectUser(URP_Users $oUser)
  158. {
  159. // #@# to be implemented
  160. }
  161. }
  162. class URP_ClassProjection extends DBObject
  163. {
  164. public static function Init()
  165. {
  166. $aParams = array
  167. (
  168. "category" => "addon/userrights",
  169. "name" => "class_projection",
  170. "description" => "class projections",
  171. "key_type" => "autoincrement",
  172. "key_label" => "",
  173. "name_attcode" => "",
  174. "state_attcode" => "",
  175. "reconc_keys" => array(),
  176. "db_table" => "priv_urp_classprojection",
  177. "db_key_field" => "id",
  178. "db_finalclass_field" => "",
  179. );
  180. MetaModel::Init_Params($aParams);
  181. //MetaModel::Init_InheritAttributes();
  182. 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())));
  183. MetaModel::Init_AddAttribute(new AttributeExternalField("dimension", array("label"=>"Dimension", "description"=>"application dimension", "allowed_values"=>null, "extkey_attcode"=> 'dimensionid', "target_attcode"=>"name")));
  184. 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())));
  185. MetaModel::Init_AddAttribute(new AttributeString("value", array("label"=>"Value expression", "description"=>"OQL expression (using \$this) | constant", "allowed_values"=>null, "sql"=>"value", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  186. //MetaModel::Init_InheritFilters();
  187. MetaModel::Init_AddFilterFromAttribute("dimensionid");
  188. MetaModel::Init_AddFilterFromAttribute("class");
  189. }
  190. }
  191. class URP_ClassGrant extends DBObject
  192. {
  193. public static function Init()
  194. {
  195. $aParams = array
  196. (
  197. "category" => "addon/userrights",
  198. "name" => "class_permission",
  199. "description" => "permissions on classes",
  200. "key_type" => "autoincrement",
  201. "key_label" => "",
  202. "name_attcode" => "",
  203. "state_attcode" => "",
  204. "reconc_keys" => array(),
  205. "db_table" => "priv_urp_grant_classes",
  206. "db_key_field" => "id",
  207. "db_finalclass_field" => "",
  208. );
  209. MetaModel::Init_Params($aParams);
  210. //MetaModel::Init_InheritAttributes();
  211. // Common to all grant classes (could be factorized by class inheritence, but this has to be benchmarked)
  212. 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())));
  213. MetaModel::Init_AddAttribute(new AttributeExternalField("profile", array("label"=>"Profile", "description"=>"usage profile", "allowed_values"=>null, "extkey_attcode"=> 'profileid', "target_attcode"=>"name")));
  214. 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())));
  215. 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())));
  216. 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())));
  217. //MetaModel::Init_InheritFilters();
  218. // Common to all grant classes (could be factorized by class inheritence, but this has to be benchmarked)
  219. MetaModel::Init_AddFilterFromAttribute("profileid");
  220. MetaModel::Init_AddFilterFromAttribute("profile");
  221. MetaModel::Init_AddFilterFromAttribute("class");
  222. MetaModel::Init_AddFilterFromAttribute("action");
  223. }
  224. }
  225. class URP_StimulusGrant extends DBObject
  226. {
  227. public static function Init()
  228. {
  229. $aParams = array
  230. (
  231. "category" => "addon/userrights",
  232. "name" => "stimulus_permission",
  233. "description" => "permissions on stimilus in the life cycle of the object",
  234. "key_type" => "autoincrement",
  235. "key_label" => "",
  236. "name_attcode" => "",
  237. "state_attcode" => "",
  238. "reconc_keys" => array(),
  239. "db_table" => "priv_urp_grant_stimulus",
  240. "db_key_field" => "id",
  241. "db_finalclass_field" => "",
  242. );
  243. MetaModel::Init_Params($aParams);
  244. //MetaModel::Init_InheritAttributes();
  245. // Common to all grant classes (could be factorized by class inheritence, but this has to be benchmarked)
  246. 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())));
  247. MetaModel::Init_AddAttribute(new AttributeExternalField("profile", array("label"=>"Profile", "description"=>"usage profile", "allowed_values"=>null, "extkey_attcode"=> 'profileid', "target_attcode"=>"name")));
  248. 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())));
  249. 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())));
  250. 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())));
  251. //MetaModel::Init_InheritFilters();
  252. // Common to all grant classes (could be factorized by class inheritence, but this has to be benchmarked)
  253. MetaModel::Init_AddFilterFromAttribute("profileid");
  254. MetaModel::Init_AddFilterFromAttribute("profile");
  255. MetaModel::Init_AddFilterFromAttribute("class");
  256. MetaModel::Init_AddFilterFromAttribute("stimulus");
  257. }
  258. }
  259. class URP_AttributeGrant extends DBObject
  260. {
  261. public static function Init()
  262. {
  263. $aParams = array
  264. (
  265. "category" => "addon/userrights",
  266. "name" => "attribute_permission",
  267. "description" => "permissions at the attributes level",
  268. "key_type" => "autoincrement",
  269. "key_label" => "",
  270. "name_attcode" => "",
  271. "state_attcode" => "",
  272. "reconc_keys" => array(),
  273. "db_table" => "priv_urp_grant_attributes",
  274. "db_key_field" => "id",
  275. "db_finalclass_field" => "",
  276. );
  277. MetaModel::Init_Params($aParams);
  278. //MetaModel::Init_InheritAttributes();
  279. // Common to all grant classes (could be factorized by class inheritence, but this has to be benchmarked)
  280. 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())));
  281. MetaModel::Init_AddAttribute(new AttributeExternalField("profile", array("label"=>"Profile", "description"=>"usage profile", "allowed_values"=>null, "extkey_attcode"=> 'profileid', "target_attcode"=>"name")));
  282. 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())));
  283. 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())));
  284. 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())));
  285. 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())));
  286. //MetaModel::Init_InheritFilters();
  287. // Common to all grant classes (could be factorized by class inheritence, but this has to be benchmarked)
  288. MetaModel::Init_AddFilterFromAttribute("profileid");
  289. MetaModel::Init_AddFilterFromAttribute("profile");
  290. MetaModel::Init_AddFilterFromAttribute("class");
  291. MetaModel::Init_AddFilterFromAttribute("attcode");
  292. MetaModel::Init_AddFilterFromAttribute("action");
  293. }
  294. }
  295. class UserRightsProfile extends UserRightsAddOnAPI
  296. {
  297. static public $m_aActionCodes = array(
  298. UR_ACTION_READ => 'read',
  299. UR_ACTION_MODIFY => 'modify',
  300. UR_ACTION_DELETE => 'delete',
  301. UR_ACTION_BULK_READ => 'bulk read',
  302. UR_ACTION_BULK_MODIFY => 'bulk modify',
  303. UR_ACTION_BULK_DELETE => 'bulk delete',
  304. );
  305. // Installation: create the very first user
  306. public function CreateAdministrator($sAdminUser, $sAdminPwd, $sAdminEmail, $sFirstName, $sLastName, $sPhoneNumber)
  307. {
  308. // Maybe we should check that no other user with userid == 0 exists
  309. $oUser = new URP_Users();
  310. $oUser->Set('login', $sAdminUser);
  311. $oUser->Set('password', $sAdminPwd);
  312. $oUser->Set('email', $sAdminEmail);
  313. $oUser->Set('firstname', $sFirstName);
  314. $oUser->Set('lastname', $sLastName);
  315. $oUser->Set('phonenumber', $sPhoneNumber);
  316. $oUser->Set('userid', 1); // one is for root !
  317. $iUserId = $oUser->DBInsertNoReload();
  318. $this->SetupUser($iUserId, true);
  319. return true;
  320. }
  321. public function Setup()
  322. {
  323. // Dimensions/Profiles/Classes/Attributes/Stimuli could be added anytime
  324. // This procedure will then update the matrix with expected default values
  325. //
  326. $oProfileSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_Profiles"));
  327. while ($oProfile = $oProfileSet->Fetch())
  328. {
  329. $this->SetupProfile($oProfile);
  330. }
  331. $oDimensionSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_Dimensions"));
  332. while ($oDimension = $oDimensionSet->Fetch())
  333. {
  334. $this->SetupDimension($oDimension);
  335. }
  336. return true;
  337. }
  338. protected function SetupDimension($oDimension, $bNewDimension = false)
  339. {
  340. $iDimensionId = $oDimension->GetKey();
  341. // Create projections, for any class where it applies
  342. //
  343. foreach(array('bizmodel', 'application', 'gui', 'core/cmdb') as $sCategory)
  344. {
  345. foreach (MetaModel::GetClasses($sCategory) as $sClass)
  346. {
  347. if ($bNewDimension)
  348. {
  349. $bAddCell = true;
  350. }
  351. else
  352. {
  353. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_ClassProjection WHERE class = '$sClass' AND dimensionid = $iDimensionId"));
  354. $bAddCell = ($oSet->Count() < 1);
  355. }
  356. if ($bAddCell)
  357. {
  358. // Create a new entry
  359. $oCProj = MetaModel::NewObject("URP_ClassProjection");
  360. $oCProj->Set("dimensionid", $iDimensionId);
  361. $oCProj->Set("class", $sClass);
  362. $oCProj->Set("value", "true");
  363. $iId = $oCProj->DBInsertNoReload();
  364. }
  365. }
  366. }
  367. // Create projections, for any existing profile
  368. //
  369. $oProfileSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_Profiles"));
  370. while ($oProfile = $oProfileSet->Fetch())
  371. {
  372. $iProfileId = $oProfile->GetKey();
  373. if ($bNewDimension)
  374. {
  375. $bAddCell = true;
  376. }
  377. else
  378. {
  379. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_ProfileProjection WHERE dimensionid = $iDimensionId AND profileid = $iProfileId"));
  380. $bAddCell = ($oSet->Count() < 1);
  381. }
  382. if ($bAddCell)
  383. {
  384. // Create a new entry
  385. $oDProj = MetaModel::NewObject("URP_ProfileProjection");
  386. $oDProj->Set("dimensionid", $iDimensionId);
  387. $oDProj->Set("profileid", $iProfileId);
  388. $oDProj->Set("value", "true");
  389. $iId = $oDProj->DBInsertNoReload();
  390. }
  391. }
  392. }
  393. protected function SetupProfile($oProfile, $bNewProfile = false)
  394. {
  395. $iProfileId = $oProfile->GetKey();
  396. // Create grant records, for any class where it applies
  397. //
  398. foreach(array('bizmodel', 'application', 'gui', 'core/cmdb') as $sCategory)
  399. {
  400. foreach (MetaModel::GetClasses($sCategory) as $sClass)
  401. {
  402. foreach (self::$m_aActionCodes as $iActionCode => $sAction)
  403. {
  404. if ($bNewProfile)
  405. {
  406. $bAddCell = true;
  407. }
  408. else
  409. {
  410. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_ClassGrant WHERE class = '$sClass' AND action = '$sAction' AND profileid = $iProfileId"));
  411. $bAddCell = ($oSet->Count() < 1);
  412. }
  413. if ($bAddCell)
  414. {
  415. // Create a new entry
  416. $oMyClassGrant = MetaModel::NewObject("URP_ClassGrant");
  417. $oMyClassGrant->Set("profileid", $iProfileId);
  418. $oMyClassGrant->Set("class", $sClass);
  419. $oMyClassGrant->Set("action", $sAction);
  420. $oMyClassGrant->Set("permission", "yes");
  421. $iId = $oMyClassGrant->DBInsertNoReload();
  422. }
  423. }
  424. foreach (MetaModel::EnumStimuli($sClass) as $sStimulusCode => $oStimulus)
  425. {
  426. if ($bNewProfile)
  427. {
  428. $bAddCell = true;
  429. }
  430. else
  431. {
  432. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_StimulusGrant WHERE class = '$sClass' AND stimulus = '$sStimulusCode' AND profileid = $iProfileId"));
  433. $bAddCell = ($oSet->Count() < 1);
  434. }
  435. if ($bAddCell)
  436. {
  437. // Create a new entry
  438. $oMyClassGrant = MetaModel::NewObject("URP_StimulusGrant");
  439. $oMyClassGrant->Set("profileid", $iProfileId);
  440. $oMyClassGrant->Set("class", $sClass);
  441. $oMyClassGrant->Set("stimulus", $sStimulusCode);
  442. $oMyClassGrant->Set("permission", "yes");
  443. $iId = $oMyClassGrant->DBInsertNoReload();
  444. }
  445. }
  446. foreach (MetaModel::GetAttributesList($sClass) as $sAttCode)
  447. {
  448. if ($bNewProfile)
  449. {
  450. $bAddCell = true;
  451. }
  452. else
  453. {
  454. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_AttributeGrant WHERE class = '$sClass' AND attcode = '$sAttCode' AND profileid = $iProfileId"));
  455. $bAddCell = ($oSet->Count() < 1);
  456. }
  457. if ($bAddCell)
  458. {
  459. foreach (array('read', 'modify') as $sAction)
  460. {
  461. // Create a new entry
  462. $oMyAttGrant = MetaModel::NewObject("URP_AttributeGrant");
  463. $oMyAttGrant->Set("profileid", $iProfileId);
  464. $oMyAttGrant->Set("class", $sClass);
  465. $oMyAttGrant->Set("attcode", $sAttCode);
  466. $oMyAttGrant->Set("action", $sAction);
  467. $oMyAttGrant->Set("permission", "yes");
  468. $iId = $oMyAttGrant->DBInsertNoReload();
  469. }
  470. }
  471. }
  472. }
  473. }
  474. // Create the "My Bookmarks" menu item (parent_id = 0, rank = 6)
  475. if ($bNewProfile)
  476. {
  477. $bAddMenu = true;
  478. }
  479. else
  480. {
  481. //$oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT menuNode WHERE type = 'user' AND parent_id = 0 AND user_id = $iUserId"));
  482. //$bAddMenu = ($oSet->Count() < 1);
  483. }
  484. $bAddMenu = false;
  485. if ($bAddMenu)
  486. {
  487. $oMenu = MetaModel::NewObject('menuNode');
  488. $oMenu->Set('type', 'user');
  489. $oMenu->Set('parent_id', 0); // It's a toplevel entry
  490. $oMenu->Set('rank', 6); // Located just above the Admin Tools section (=7)
  491. $oMenu->Set('name', 'My Bookmarks');
  492. $oMenu->Set('label', 'My Favorite Items');
  493. $oMenu->Set('hyperlink', 'UI.php');
  494. $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>');
  495. $oMenu->Set('user_id', $iUserId);
  496. $oMenu->DBInsert();
  497. }
  498. }
  499. public function Init()
  500. {
  501. // Could be loaded in a shared memory (?)
  502. return true;
  503. }
  504. public function CheckCredentials($sUserName, $sPassword)
  505. {
  506. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_Users WHERE login = '$sUserName'"));
  507. if ($oSet->Count() < 1)
  508. {
  509. // todo: throw an exception?
  510. return false;
  511. }
  512. $oLogin = $oSet->Fetch();
  513. if ($oLogin->Get('password') == $sPassword)
  514. {
  515. return true;
  516. }
  517. // todo: throw an exception?
  518. return false;
  519. }
  520. public function GetFilter($sUserName, $sClass)
  521. {
  522. $oNullFilter = new DBObjectSearch($sClass);
  523. return $oNullFilter;
  524. }
  525. public function IsActionAllowed($sUserName, $sClass, $iActionCode, dbObjectSet $aInstances)
  526. {
  527. if (!array_key_exists($iActionCode, self::$m_aActionCodes))
  528. {
  529. return UR_ALLOWED_NO;
  530. }
  531. $sAction = self::$m_aActionCodes[$iActionCode];
  532. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_ClassGrant WHERE class = '$sClass' AND action = '$sAction' AND login = '$sUserName'"));
  533. if ($oSet->Count() < 1)
  534. {
  535. return UR_ALLOWED_NO;
  536. }
  537. $oGrantRecord = $oSet->Fetch();
  538. switch ($oGrantRecord->Get('permission'))
  539. {
  540. case 'yes':
  541. $iRetCode = UR_ALLOWED_YES;
  542. break;
  543. case 'no':
  544. default:
  545. $iRetCode = UR_ALLOWED_NO;
  546. break;
  547. }
  548. return $iRetCode;
  549. }
  550. public function IsActionAllowedOnAttribute($sUserName, $sClass, $sAttCode, $iActionCode, dbObjectSet $aInstances)
  551. {
  552. if (!array_key_exists($iActionCode, self::$m_aActionCodes))
  553. {
  554. return UR_ALLOWED_NO;
  555. }
  556. $sAction = self::$m_aActionCodes[$iActionCode];
  557. $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'"));
  558. if ($oSet->Count() < 1)
  559. {
  560. return UR_ALLOWED_NO;
  561. }
  562. $oGrantRecord = $oSet->Fetch();
  563. switch ($oGrantRecord->Get('permission'))
  564. {
  565. case 'yes':
  566. $iRetCode = UR_ALLOWED_YES;
  567. break;
  568. case 'no':
  569. default:
  570. $iRetCode = UR_ALLOWED_NO;
  571. break;
  572. }
  573. return $iRetCode;
  574. }
  575. public function IsStimulusAllowed($sUserName, $sClass, $sStimulusCode, dbObjectSet $aInstances)
  576. {
  577. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT URP_StimulusGrant WHERE class = '$sClass' AND stimulus = '$sStimulusCode' AND login = '$sUserName'"));
  578. if ($oSet->Count() < 1)
  579. {
  580. return UR_ALLOWED_NO;
  581. }
  582. $oGrantRecord = $oSet->Fetch();
  583. switch ($oGrantRecord->Get('permission'))
  584. {
  585. case 'yes':
  586. $iRetCode = UR_ALLOWED_YES;
  587. break;
  588. case 'no':
  589. default:
  590. $iRetCode = UR_ALLOWED_NO;
  591. break;
  592. }
  593. return $iRetCode;
  594. }
  595. }
  596. UserRights::SelectModule('UserRightsProfile');
  597. ?>