userrightsmatrix.class.inc.php 16 KB

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