userrightsmatrix.class.inc.php 13 KB

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