userrightsmatrix.class.inc.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. <?php
  2. /**
  3. * UserRightsMatrix
  4. * User management Module
  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 UserRightsMatrixUsers extends DBObject
  15. {
  16. public static function Init()
  17. {
  18. $aParams = array
  19. (
  20. "category" => "addon/userrights",
  21. "key_type" => "autoincrement",
  22. "key_label" => "",
  23. "name_attcode" => "login",
  24. "state_attcode" => "",
  25. "reconc_keys" => array(),
  26. "db_table" => "priv_ur_matrixusers",
  27. "db_key_field" => "id",
  28. "db_finalclass_field" => "",
  29. );
  30. MetaModel::Init_Params($aParams);
  31. //MetaModel::Init_InheritAttributes();
  32. MetaModel::Init_AddAttribute(new AttributeInteger("userid", array("allowed_values"=>null, "sql"=>"userid", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  33. MetaModel::Init_AddAttribute(new AttributeString("login", array("allowed_values"=>null, "sql"=>"login", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  34. MetaModel::Init_AddAttribute(new AttributeString("password", array("allowed_values"=>null, "sql"=>"pwd", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  35. //MetaModel::Init_InheritFilters();
  36. MetaModel::Init_AddFilterFromAttribute("userid");
  37. MetaModel::Init_AddFilterFromAttribute("login");
  38. }
  39. }
  40. class UserRightsMatrixClassGrant extends DBObject
  41. {
  42. public static function Init()
  43. {
  44. $aParams = array
  45. (
  46. "category" => "addon/userrights",
  47. "key_type" => "autoincrement",
  48. "key_label" => "",
  49. "name_attcode" => "",
  50. "state_attcode" => "",
  51. "reconc_keys" => array(),
  52. "db_table" => "priv_ur_matrixclasses",
  53. "db_key_field" => "id",
  54. "db_finalclass_field" => "",
  55. );
  56. MetaModel::Init_Params($aParams);
  57. //MetaModel::Init_InheritAttributes();
  58. MetaModel::Init_AddAttribute(new AttributeExternalKey("userid", array("targetclass"=>"UserRightsMatrixUsers", "jointype"=> "", "allowed_values"=>null, "sql"=>"userid", "is_null_allowed"=>false, "on_target_delete"=>DEL_MANUAL, "depends_on"=>array())));
  59. MetaModel::Init_AddAttribute(new AttributeExternalField("login", array("allowed_values"=>null, "extkey_attcode"=> 'userid', "target_attcode"=>"login")));
  60. MetaModel::Init_AddAttribute(new AttributeString("class", array("allowed_values"=>null, "sql"=>"class", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  61. MetaModel::Init_AddAttribute(new AttributeString("action", array("allowed_values"=>null, "sql"=>"action", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  62. MetaModel::Init_AddAttribute(new AttributeEnum("permission", array("allowed_values"=>new ValueSetEnum('yes,no'), "sql"=>"permission", "default_value"=>"yes", "is_null_allowed"=>false, "depends_on"=>array())));
  63. //MetaModel::Init_InheritFilters();
  64. MetaModel::Init_AddFilterFromAttribute("userid");
  65. MetaModel::Init_AddFilterFromAttribute("login");
  66. MetaModel::Init_AddFilterFromAttribute("class");
  67. MetaModel::Init_AddFilterFromAttribute("action");
  68. }
  69. }
  70. class UserRightsMatrixClassStimulusGrant extends DBObject
  71. {
  72. public static function Init()
  73. {
  74. $aParams = array
  75. (
  76. "category" => "addon/userrights",
  77. "key_type" => "autoincrement",
  78. "key_label" => "",
  79. "name_attcode" => "",
  80. "state_attcode" => "",
  81. "reconc_keys" => array(),
  82. "db_table" => "priv_ur_matrixclassesstimulus",
  83. "db_key_field" => "id",
  84. "db_finalclass_field" => "",
  85. );
  86. MetaModel::Init_Params($aParams);
  87. //MetaModel::Init_InheritAttributes();
  88. MetaModel::Init_AddAttribute(new AttributeExternalKey("userid", array("targetclass"=>"UserRightsMatrixUsers", "jointype"=> "", "allowed_values"=>null, "sql"=>"userid", "is_null_allowed"=>false, "on_target_delete"=>DEL_MANUAL, "depends_on"=>array())));
  89. MetaModel::Init_AddAttribute(new AttributeExternalField("login", array("allowed_values"=>null, "extkey_attcode"=> 'userid', "target_attcode"=>"login")));
  90. MetaModel::Init_AddAttribute(new AttributeString("class", array("allowed_values"=>null, "sql"=>"class", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  91. MetaModel::Init_AddAttribute(new AttributeString("stimulus", array("allowed_values"=>null, "sql"=>"action", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  92. MetaModel::Init_AddAttribute(new AttributeEnum("permission", array("allowed_values"=>new ValueSetEnum('yes,no'), "sql"=>"permission", "default_value"=>"yes", "is_null_allowed"=>false, "depends_on"=>array())));
  93. //MetaModel::Init_InheritFilters();
  94. MetaModel::Init_AddFilterFromAttribute("userid");
  95. MetaModel::Init_AddFilterFromAttribute("login");
  96. MetaModel::Init_AddFilterFromAttribute("class");
  97. MetaModel::Init_AddFilterFromAttribute("stimulus");
  98. }
  99. }
  100. class UserRightsMatrixAttributeGrant extends DBObject
  101. {
  102. public static function Init()
  103. {
  104. $aParams = array
  105. (
  106. "category" => "addon/userrights",
  107. "key_type" => "autoincrement",
  108. "key_label" => "",
  109. "name_attcode" => "",
  110. "state_attcode" => "",
  111. "reconc_keys" => array(),
  112. "db_table" => "priv_ur_matrixattributes",
  113. "db_key_field" => "id",
  114. "db_finalclass_field" => "",
  115. );
  116. MetaModel::Init_Params($aParams);
  117. //MetaModel::Init_InheritAttributes();
  118. MetaModel::Init_AddAttribute(new AttributeExternalKey("userid", array("targetclass"=>"UserRightsMatrixUsers", "jointype"=> "", "allowed_values"=>null, "sql"=>"userid", "is_null_allowed"=>false, "on_target_delete"=>DEL_MANUAL, "depends_on"=>array())));
  119. MetaModel::Init_AddAttribute(new AttributeExternalField("login", array("allowed_values"=>null, "extkey_attcode"=> 'userid', "target_attcode"=>"login")));
  120. MetaModel::Init_AddAttribute(new AttributeString("class", array("allowed_values"=>null, "sql"=>"class", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  121. MetaModel::Init_AddAttribute(new AttributeString("attcode", array("allowed_values"=>null, "sql"=>"attcode", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  122. MetaModel::Init_AddAttribute(new AttributeString("action", array("allowed_values"=>null, "sql"=>"action", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  123. MetaModel::Init_AddAttribute(new AttributeEnum("permission", array("allowed_values"=>new ValueSetEnum('yes,no'), "sql"=>"permission", "default_value"=>"yes", "is_null_allowed"=>false, "depends_on"=>array())));
  124. //MetaModel::Init_InheritFilters();
  125. MetaModel::Init_AddFilterFromAttribute("userid");
  126. MetaModel::Init_AddFilterFromAttribute("login");
  127. MetaModel::Init_AddFilterFromAttribute("class");
  128. MetaModel::Init_AddFilterFromAttribute("attcode");
  129. MetaModel::Init_AddFilterFromAttribute("action");
  130. }
  131. }
  132. class UserRightsMatrix extends UserRightsAddOnAPI
  133. {
  134. static public $m_aActionCodes = array(
  135. UR_ACTION_READ => 'read',
  136. UR_ACTION_MODIFY => 'modify',
  137. UR_ACTION_DELETE => 'delete',
  138. UR_ACTION_BULK_READ => 'bulk read',
  139. UR_ACTION_BULK_MODIFY => 'bulk modify',
  140. UR_ACTION_BULK_DELETE => 'bulk delete',
  141. );
  142. // Installation: create the very first user
  143. public function CreateAdministrator($sAdminUser, $sAdminPwd)
  144. {
  145. // Maybe we should check that no other user with userid == 0 exists
  146. $oUser = new UserRightsMatrixUsers();
  147. $oUser->Set('login', $sAdminUser);
  148. $oUser->Set('password', $sAdminPwd);
  149. $oUser->Set('userid', 1); // one is for root !
  150. // Create a change to record the history of the User object
  151. $oChange = MetaModel::NewObject("CMDBChange");
  152. $oChange->Set("date", time());
  153. $oChange->Set("userinfo", "Initialization");
  154. $iChangeId = $oChange->DBInsert();
  155. // Now record the admin user object
  156. $iUserId = $oUser->DBInsertTrackedNoReload($oChange);
  157. $this->SetupUser($iUserId, true);
  158. return true;
  159. }
  160. public function IsAdministrator($iUserId)
  161. {
  162. return ($iUserId == 1);
  163. }
  164. public function Setup()
  165. {
  166. // Users must be added manually
  167. // This procedure will then update the matrix when a new user is found or a new class/attribute appears
  168. $oUserSet = new DBObjectSet(DBObjectSearch::FromSibuSQL("UserRightsMatrixUsers"));
  169. while ($oUser = $oUserSet->Fetch())
  170. {
  171. $this->SetupUser($oUser->GetKey());
  172. }
  173. return true;
  174. }
  175. protected function SetupUser($iUserId, $bNewUser = false)
  176. {
  177. foreach(array('bizmodel', 'application', 'gui', 'core/cmdb') as $sCategory)
  178. {
  179. foreach (MetaModel::GetClasses($sCategory) as $sClass)
  180. {
  181. foreach (self::$m_aActionCodes as $iActionCode => $sAction)
  182. {
  183. if ($bNewUser)
  184. {
  185. $bAddCell = true;
  186. }
  187. else
  188. {
  189. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT UserRightsMatrixClassGrant WHERE class = '$sClass' AND action = '$sAction' AND userid = $iUserId"));
  190. $bAddCell = ($oSet->Count() < 1);
  191. }
  192. if ($bAddCell)
  193. {
  194. // Create a new entry
  195. $oMyClassGrant = MetaModel::NewObject("UserRightsMatrixClassGrant");
  196. $oMyClassGrant->Set("userid", $iUserId);
  197. $oMyClassGrant->Set("class", $sClass);
  198. $oMyClassGrant->Set("action", $sAction);
  199. $oMyClassGrant->Set("permission", "yes");
  200. $iId = $oMyClassGrant->DBInsertNoReload();
  201. }
  202. }
  203. foreach (MetaModel::EnumStimuli($sClass) as $sStimulusCode => $oStimulus)
  204. {
  205. if ($bNewUser)
  206. {
  207. $bAddCell = true;
  208. }
  209. else
  210. {
  211. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT UserRightsMatrixClassStimulusGrant WHERE class = '$sClass' AND stimulus = '$sStimulusCode' AND userid = $iUserId"));
  212. $bAddCell = ($oSet->Count() < 1);
  213. }
  214. if ($bAddCell)
  215. {
  216. // Create a new entry
  217. $oMyClassGrant = MetaModel::NewObject("UserRightsMatrixClassStimulusGrant");
  218. $oMyClassGrant->Set("userid", $iUserId);
  219. $oMyClassGrant->Set("class", $sClass);
  220. $oMyClassGrant->Set("stimulus", $sStimulusCode);
  221. $oMyClassGrant->Set("permission", "yes");
  222. $iId = $oMyClassGrant->DBInsertNoReload();
  223. }
  224. }
  225. foreach (MetaModel::GetAttributesList($sClass) as $sAttCode)
  226. {
  227. if ($bNewUser)
  228. {
  229. $bAddCell = true;
  230. }
  231. else
  232. {
  233. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT UserRightsMatrixAttributeGrant WHERE class = '$sClass' AND attcode = '$sAttCode' AND userid = $iUserId"));
  234. $bAddCell = ($oSet->Count() < 1);
  235. }
  236. if ($bAddCell)
  237. {
  238. foreach (array('read', 'modify') as $sAction)
  239. {
  240. // Create a new entry
  241. $oMyAttGrant = MetaModel::NewObject("UserRightsMatrixAttributeGrant");
  242. $oMyAttGrant->Set("userid", $iUserId);
  243. $oMyAttGrant->Set("class", $sClass);
  244. $oMyAttGrant->Set("attcode", $sAttCode);
  245. $oMyAttGrant->Set("action", $sAction);
  246. $oMyAttGrant->Set("permission", "yes");
  247. $iId = $oMyAttGrant->DBInsertNoReload();
  248. }
  249. }
  250. }
  251. }
  252. }
  253. // Create the "My Bookmarks" menu item (parent_id = 0, rank = 6)
  254. if ($bNewUser)
  255. {
  256. $bAddMenu = true;
  257. }
  258. else
  259. {
  260. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT menuNode WHERE type = 'user' AND parent_id = 0 AND user_id = $iUserId"));
  261. $bAddMenu = ($oSet->Count() < 1);
  262. }
  263. if ($bAddMenu)
  264. {
  265. $oMenu = MetaModel::NewObject('menuNode');
  266. $oMenu->Set('type', 'user');
  267. $oMenu->Set('parent_id', 0); // It's a toplevel entry
  268. $oMenu->Set('rank', 6); // Located just above the Admin Tools section (=7)
  269. $oMenu->Set('name', 'My Bookmarks');
  270. $oMenu->Set('label', 'My Favorite Items');
  271. $oMenu->Set('hyperlink', 'UI.php');
  272. $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>');
  273. $oMenu->Set('user_id', $iUserId);
  274. $oMenu->DBInsert();
  275. }
  276. }
  277. public function Init()
  278. {
  279. // Could be loaded in a shared memory (?)
  280. return true;
  281. }
  282. public function CheckCredentials($sUserName, $sPassword)
  283. {
  284. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT UserRightsMatrixUsers WHERE login = '$sUserName'"));
  285. if ($oSet->Count() < 1)
  286. {
  287. // todo: throw an exception?
  288. return false;
  289. }
  290. $oLogin = $oSet->Fetch();
  291. if ($oLogin->Get('password') == $sPassword)
  292. {
  293. return $oLogin->Get('userid');
  294. }
  295. // todo: throw an exception?
  296. return false;
  297. }
  298. public function GetUserId($sUserName)
  299. {
  300. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT UserRightsMatrixUsers WHERE login = '$sUserName'"));
  301. if ($oSet->Count() < 1)
  302. {
  303. // todo: throw an exception?
  304. return false;
  305. }
  306. $oLogin = $oSet->Fetch();
  307. return $oLogin->Get('userid');
  308. }
  309. // this module does not handle localization
  310. public function GetUserLanguage($sUserName)
  311. {
  312. return 'EN US';
  313. }
  314. public function GetContactId($sUserName)
  315. {
  316. // this module has no link with the business data
  317. return null;
  318. }
  319. public function GetFilter($sUserName, $sClass)
  320. {
  321. $oNullFilter = new DBObjectSearch($sClass);
  322. return $oNullFilter;
  323. }
  324. public function IsActionAllowed($iUserId, $sClass, $iActionCode, $oInstanceSet = null)
  325. {
  326. if (!array_key_exists($iActionCode, self::$m_aActionCodes))
  327. {
  328. return UR_ALLOWED_NO;
  329. }
  330. $sAction = self::$m_aActionCodes[$iActionCode];
  331. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT UserRightsMatrixClassGrant WHERE class = '$sClass' AND action = '$sAction' AND userid = '$iUserId'"));
  332. if ($oSet->Count() < 1)
  333. {
  334. return UR_ALLOWED_NO;
  335. }
  336. $oGrantRecord = $oSet->Fetch();
  337. switch ($oGrantRecord->Get('permission'))
  338. {
  339. case 'yes':
  340. $iRetCode = UR_ALLOWED_YES;
  341. break;
  342. case 'no':
  343. default:
  344. $iRetCode = UR_ALLOWED_NO;
  345. break;
  346. }
  347. return $iRetCode;
  348. }
  349. public function IsActionAllowedOnAttribute($iUserId, $sClass, $sAttCode, $iActionCode, $oInstanceSet = null)
  350. {
  351. if (!array_key_exists($iActionCode, self::$m_aActionCodes))
  352. {
  353. return UR_ALLOWED_NO;
  354. }
  355. $sAction = self::$m_aActionCodes[$iActionCode];
  356. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT UserRightsMatrixAttributeGrant WHERE class = '$sClass' AND attcode = '$sAttCode' AND action = '$sAction' AND userid = '$iUserId'"));
  357. if ($oSet->Count() < 1)
  358. {
  359. return UR_ALLOWED_NO;
  360. }
  361. $oGrantRecord = $oSet->Fetch();
  362. switch ($oGrantRecord->Get('permission'))
  363. {
  364. case 'yes':
  365. $iRetCode = UR_ALLOWED_YES;
  366. break;
  367. case 'no':
  368. default:
  369. $iRetCode = UR_ALLOWED_NO;
  370. break;
  371. }
  372. return $iRetCode;
  373. }
  374. public function IsStimulusAllowed($iUserId, $sClass, $sStimulusCode, $oInstanceSet = null)
  375. {
  376. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT UserRightsMatrixClassStimulusGrant WHERE class = '$sClass' AND stimulus = '$sStimulusCode' AND userid = '$iUserId'"));
  377. if ($oSet->Count() < 1)
  378. {
  379. return UR_ALLOWED_NO;
  380. }
  381. $oGrantRecord = $oSet->Fetch();
  382. switch ($oGrantRecord->Get('permission'))
  383. {
  384. case 'yes':
  385. $iRetCode = UR_ALLOWED_YES;
  386. break;
  387. case 'no':
  388. default:
  389. $iRetCode = UR_ALLOWED_NO;
  390. break;
  391. }
  392. return $iRetCode;
  393. }
  394. public function FlushPrivileges()
  395. {
  396. }
  397. }
  398. UserRights::SelectModule('UserRightsMatrix');
  399. ?>