userrightsmatrix.class.inc.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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("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_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"=> "", "label"=>"user", "description"=>"user account", "allowed_values"=>null, "sql"=>"userid", "is_null_allowed"=>false, "depends_on"=>array())));
  63. MetaModel::Init_AddAttribute(new AttributeExternalField("login", array("label"=>"Login", "description"=>"Login", "allowed_values"=>null, "extkey_attcode"=> 'userid', "target_attcode"=>"login")));
  64. 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())));
  65. 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())));
  66. 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())));
  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"=> "", "label"=>"user", "description"=>"user account", "allowed_values"=>null, "sql"=>"userid", "is_null_allowed"=>false, "depends_on"=>array())));
  95. MetaModel::Init_AddAttribute(new AttributeExternalField("login", array("label"=>"Login", "description"=>"Login", "allowed_values"=>null, "extkey_attcode"=> 'userid', "target_attcode"=>"login")));
  96. 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())));
  97. 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())));
  98. 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())));
  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"=> "", "label"=>"user", "description"=>"user account", "allowed_values"=>null, "sql"=>"userid", "is_null_allowed"=>false, "depends_on"=>array())));
  127. MetaModel::Init_AddAttribute(new AttributeExternalField("login", array("label"=>"Login", "description"=>"Login", "allowed_values"=>null, "extkey_attcode"=> 'userid', "target_attcode"=>"login")));
  128. 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())));
  129. 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())));
  130. 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())));
  131. 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())));
  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. $iUserId = $oUser->DBInsertNoReload();
  159. $this->SetupUser($iUserId, true);
  160. return true;
  161. }
  162. public function IsAdministrator($iUserId)
  163. {
  164. return ($iUserId == 1);
  165. }
  166. public function Setup()
  167. {
  168. // Users must be added manually
  169. // This procedure will then update the matrix when a new user is found or a new class/attribute appears
  170. $oUserSet = new DBObjectSet(DBObjectSearch::FromSibuSQL("UserRightsMatrixUsers"));
  171. while ($oUser = $oUserSet->Fetch())
  172. {
  173. $this->SetupUser($oUser->GetKey());
  174. }
  175. return true;
  176. }
  177. protected function SetupUser($iUserId, $bNewUser = false)
  178. {
  179. foreach(array('bizmodel', 'application', 'gui', 'core/cmdb') as $sCategory)
  180. {
  181. foreach (MetaModel::GetClasses($sCategory) as $sClass)
  182. {
  183. foreach (self::$m_aActionCodes as $iActionCode => $sAction)
  184. {
  185. if ($bNewUser)
  186. {
  187. $bAddCell = true;
  188. }
  189. else
  190. {
  191. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT UserRightsMatrixClassGrant WHERE class = '$sClass' AND action = '$sAction' AND userid = $iUserId"));
  192. $bAddCell = ($oSet->Count() < 1);
  193. }
  194. if ($bAddCell)
  195. {
  196. // Create a new entry
  197. $oMyClassGrant = MetaModel::NewObject("UserRightsMatrixClassGrant");
  198. $oMyClassGrant->Set("userid", $iUserId);
  199. $oMyClassGrant->Set("class", $sClass);
  200. $oMyClassGrant->Set("action", $sAction);
  201. $oMyClassGrant->Set("permission", "yes");
  202. $iId = $oMyClassGrant->DBInsertNoReload();
  203. }
  204. }
  205. foreach (MetaModel::EnumStimuli($sClass) as $sStimulusCode => $oStimulus)
  206. {
  207. if ($bNewUser)
  208. {
  209. $bAddCell = true;
  210. }
  211. else
  212. {
  213. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT UserRightsMatrixClassStimulusGrant WHERE class = '$sClass' AND stimulus = '$sStimulusCode' AND userid = $iUserId"));
  214. $bAddCell = ($oSet->Count() < 1);
  215. }
  216. if ($bAddCell)
  217. {
  218. // Create a new entry
  219. $oMyClassGrant = MetaModel::NewObject("UserRightsMatrixClassStimulusGrant");
  220. $oMyClassGrant->Set("userid", $iUserId);
  221. $oMyClassGrant->Set("class", $sClass);
  222. $oMyClassGrant->Set("stimulus", $sStimulusCode);
  223. $oMyClassGrant->Set("permission", "yes");
  224. $iId = $oMyClassGrant->DBInsertNoReload();
  225. }
  226. }
  227. foreach (MetaModel::GetAttributesList($sClass) as $sAttCode)
  228. {
  229. if ($bNewUser)
  230. {
  231. $bAddCell = true;
  232. }
  233. else
  234. {
  235. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT UserRightsMatrixAttributeGrant WHERE class = '$sClass' AND attcode = '$sAttCode' AND userid = $iUserId"));
  236. $bAddCell = ($oSet->Count() < 1);
  237. }
  238. if ($bAddCell)
  239. {
  240. foreach (array('read', 'modify') as $sAction)
  241. {
  242. // Create a new entry
  243. $oMyAttGrant = MetaModel::NewObject("UserRightsMatrixAttributeGrant");
  244. $oMyAttGrant->Set("userid", $iUserId);
  245. $oMyAttGrant->Set("class", $sClass);
  246. $oMyAttGrant->Set("attcode", $sAttCode);
  247. $oMyAttGrant->Set("action", $sAction);
  248. $oMyAttGrant->Set("permission", "yes");
  249. $iId = $oMyAttGrant->DBInsertNoReload();
  250. }
  251. }
  252. }
  253. }
  254. }
  255. // Create the "My Bookmarks" menu item (parent_id = 0, rank = 6)
  256. if ($bNewUser)
  257. {
  258. $bAddMenu = true;
  259. }
  260. else
  261. {
  262. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT menuNode WHERE type = 'user' AND parent_id = 0 AND user_id = $iUserId"));
  263. $bAddMenu = ($oSet->Count() < 1);
  264. }
  265. if ($bAddMenu)
  266. {
  267. $oMenu = MetaModel::NewObject('menuNode');
  268. $oMenu->Set('type', 'user');
  269. $oMenu->Set('parent_id', 0); // It's a toplevel entry
  270. $oMenu->Set('rank', 6); // Located just above the Admin Tools section (=7)
  271. $oMenu->Set('name', 'My Bookmarks');
  272. $oMenu->Set('label', 'My Favorite Items');
  273. $oMenu->Set('hyperlink', 'UI.php');
  274. $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>');
  275. $oMenu->Set('user_id', $iUserId);
  276. $oMenu->DBInsert();
  277. }
  278. }
  279. public function Init()
  280. {
  281. // Could be loaded in a shared memory (?)
  282. return true;
  283. }
  284. public function CheckCredentials($sUserName, $sPassword)
  285. {
  286. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT UserRightsMatrixUsers WHERE login = '$sUserName'"));
  287. if ($oSet->Count() < 1)
  288. {
  289. // todo: throw an exception?
  290. return false;
  291. }
  292. $oLogin = $oSet->Fetch();
  293. if ($oLogin->Get('password') == $sPassword)
  294. {
  295. return $oLogin->Get('userid');
  296. }
  297. // todo: throw an exception?
  298. return false;
  299. }
  300. public function GetUserId($sUserName)
  301. {
  302. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT UserRightsMatrixUsers WHERE login = '$sUserName'"));
  303. if ($oSet->Count() < 1)
  304. {
  305. // todo: throw an exception?
  306. return false;
  307. }
  308. $oLogin = $oSet->Fetch();
  309. return $oLogin->Get('userid');
  310. }
  311. public function GetFilter($sUserName, $sClass)
  312. {
  313. $oNullFilter = new DBObjectSearch($sClass);
  314. return $oNullFilter;
  315. }
  316. public function IsActionAllowed($iUserId, $sClass, $iActionCode, dbObjectSet $aInstances)
  317. {
  318. if (!array_key_exists($iActionCode, self::$m_aActionCodes))
  319. {
  320. return UR_ALLOWED_NO;
  321. }
  322. $sAction = self::$m_aActionCodes[$iActionCode];
  323. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT UserRightsMatrixClassGrant WHERE class = '$sClass' AND action = '$sAction' AND userid = '$iUserId'"));
  324. if ($oSet->Count() < 1)
  325. {
  326. return UR_ALLOWED_NO;
  327. }
  328. $oGrantRecord = $oSet->Fetch();
  329. switch ($oGrantRecord->Get('permission'))
  330. {
  331. case 'yes':
  332. $iRetCode = UR_ALLOWED_YES;
  333. break;
  334. case 'no':
  335. default:
  336. $iRetCode = UR_ALLOWED_NO;
  337. break;
  338. }
  339. return $iRetCode;
  340. }
  341. public function IsActionAllowedOnAttribute($iUserId, $sClass, $sAttCode, $iActionCode, dbObjectSet $aInstances)
  342. {
  343. if (!array_key_exists($iActionCode, self::$m_aActionCodes))
  344. {
  345. return UR_ALLOWED_NO;
  346. }
  347. $sAction = self::$m_aActionCodes[$iActionCode];
  348. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT UserRightsMatrixAttributeGrant WHERE class = '$sClass' AND attcode = '$sAttCode' AND action = '$sAction' AND userid = '$iUserId'"));
  349. if ($oSet->Count() < 1)
  350. {
  351. return UR_ALLOWED_NO;
  352. }
  353. $oGrantRecord = $oSet->Fetch();
  354. switch ($oGrantRecord->Get('permission'))
  355. {
  356. case 'yes':
  357. $iRetCode = UR_ALLOWED_YES;
  358. break;
  359. case 'no':
  360. default:
  361. $iRetCode = UR_ALLOWED_NO;
  362. break;
  363. }
  364. return $iRetCode;
  365. }
  366. public function IsStimulusAllowed($iUserId, $sClass, $sStimulusCode, dbObjectSet $aInstances)
  367. {
  368. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT UserRightsMatrixClassStimulusGrant WHERE class = '$sClass' AND stimulus = '$sStimulusCode' AND userid = '$iUserId'"));
  369. if ($oSet->Count() < 1)
  370. {
  371. return UR_ALLOWED_NO;
  372. }
  373. $oGrantRecord = $oSet->Fetch();
  374. switch ($oGrantRecord->Get('permission'))
  375. {
  376. case 'yes':
  377. $iRetCode = UR_ALLOWED_YES;
  378. break;
  379. case 'no':
  380. default:
  381. $iRetCode = UR_ALLOWED_NO;
  382. break;
  383. }
  384. return $iRetCode;
  385. }
  386. }
  387. UserRights::SelectModule('UserRightsMatrix');
  388. ?>