userrightsmatrix.class.inc.php 16 KB

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