userrightsmatrix.class.inc.php 15 KB

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