userrightsmatrix.class.inc.php 15 KB

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