userrightsmatrix.class.inc.php 14 KB

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