userrightsmatrix.class.inc.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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, true /* skip security */);
  123. $this->SetupUser($iUserId, true);
  124. return true;
  125. }
  126. public function IsAdministrator($oUser)
  127. {
  128. return ($oUser->GetKey() == 1);
  129. }
  130. public function IsPortalUser($oUser)
  131. {
  132. return ($oUser->GetKey() == 1);
  133. }
  134. public function Setup()
  135. {
  136. // Users must be added manually
  137. // This procedure will then update the matrix when a new user is found or a new class/attribute appears
  138. $oUserSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT User"));
  139. while ($oUser = $oUserSet->Fetch())
  140. {
  141. $this->SetupUser($oUser->GetKey());
  142. }
  143. return true;
  144. }
  145. protected function SetupUser($iUserId, $bNewUser = false)
  146. {
  147. foreach(array('bizmodel', 'application', 'gui', 'core/cmdb') as $sCategory)
  148. {
  149. foreach (MetaModel::GetClasses($sCategory) as $sClass)
  150. {
  151. foreach (self::$m_aActionCodes as $iActionCode => $sAction)
  152. {
  153. if ($bNewUser)
  154. {
  155. $bAddCell = true;
  156. }
  157. else
  158. {
  159. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT UserRightsMatrixClassGrant WHERE class = '$sClass' AND action = '$sAction' AND userid = $iUserId"));
  160. $bAddCell = ($oSet->Count() < 1);
  161. }
  162. if ($bAddCell)
  163. {
  164. // Create a new entry
  165. $oMyClassGrant = MetaModel::NewObject("UserRightsMatrixClassGrant");
  166. $oMyClassGrant->Set("userid", $iUserId);
  167. $oMyClassGrant->Set("class", $sClass);
  168. $oMyClassGrant->Set("action", $sAction);
  169. $oMyClassGrant->Set("permission", "yes");
  170. $iId = $oMyClassGrant->DBInsertNoReload();
  171. }
  172. }
  173. foreach (MetaModel::EnumStimuli($sClass) as $sStimulusCode => $oStimulus)
  174. {
  175. if ($bNewUser)
  176. {
  177. $bAddCell = true;
  178. }
  179. else
  180. {
  181. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT UserRightsMatrixClassStimulusGrant WHERE class = '$sClass' AND stimulus = '$sStimulusCode' AND userid = $iUserId"));
  182. $bAddCell = ($oSet->Count() < 1);
  183. }
  184. if ($bAddCell)
  185. {
  186. // Create a new entry
  187. $oMyClassGrant = MetaModel::NewObject("UserRightsMatrixClassStimulusGrant");
  188. $oMyClassGrant->Set("userid", $iUserId);
  189. $oMyClassGrant->Set("class", $sClass);
  190. $oMyClassGrant->Set("stimulus", $sStimulusCode);
  191. $oMyClassGrant->Set("permission", "yes");
  192. $iId = $oMyClassGrant->DBInsertNoReload();
  193. }
  194. }
  195. foreach (MetaModel::GetAttributesList($sClass) as $sAttCode)
  196. {
  197. if ($bNewUser)
  198. {
  199. $bAddCell = true;
  200. }
  201. else
  202. {
  203. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT UserRightsMatrixAttributeGrant WHERE class = '$sClass' AND attcode = '$sAttCode' AND userid = $iUserId"));
  204. $bAddCell = ($oSet->Count() < 1);
  205. }
  206. if ($bAddCell)
  207. {
  208. foreach (array('read', 'modify') as $sAction)
  209. {
  210. // Create a new entry
  211. $oMyAttGrant = MetaModel::NewObject("UserRightsMatrixAttributeGrant");
  212. $oMyAttGrant->Set("userid", $iUserId);
  213. $oMyAttGrant->Set("class", $sClass);
  214. $oMyAttGrant->Set("attcode", $sAttCode);
  215. $oMyAttGrant->Set("action", $sAction);
  216. $oMyAttGrant->Set("permission", "yes");
  217. $iId = $oMyAttGrant->DBInsertNoReload();
  218. }
  219. }
  220. }
  221. }
  222. }
  223. /*
  224. // Create the "My Bookmarks" menu item (parent_id = 0, rank = 6)
  225. if ($bNewUser)
  226. {
  227. $bAddMenu = true;
  228. }
  229. else
  230. {
  231. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT menuNode WHERE type = 'user' AND parent_id = 0 AND user_id = $iUserId"));
  232. $bAddMenu = ($oSet->Count() < 1);
  233. }
  234. if ($bAddMenu)
  235. {
  236. $oMenu = MetaModel::NewObject('menuNode');
  237. $oMenu->Set('type', 'user');
  238. $oMenu->Set('parent_id', 0); // It's a toplevel entry
  239. $oMenu->Set('rank', 6); // Located just above the Admin Tools section (=7)
  240. $oMenu->Set('name', 'My Bookmarks');
  241. $oMenu->Set('label', 'My Favorite Items');
  242. $oMenu->Set('hyperlink', 'UI.php');
  243. $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>');
  244. $oMenu->Set('user_id', $iUserId);
  245. $oMenu->DBInsert();
  246. }
  247. */
  248. }
  249. public function Init()
  250. {
  251. // Could be loaded in a shared memory (?)
  252. return true;
  253. }
  254. public function GetSelectFilter($oUser, $sClass)
  255. {
  256. $oNullFilter = new DBObjectSearch($sClass);
  257. return $oNullFilter;
  258. }
  259. public function IsActionAllowed($oUser, $sClass, $iActionCode, $oInstanceSet = null)
  260. {
  261. if (!array_key_exists($iActionCode, self::$m_aActionCodes))
  262. {
  263. return UR_ALLOWED_NO;
  264. }
  265. $sAction = self::$m_aActionCodes[$iActionCode];
  266. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT UserRightsMatrixClassGrant WHERE class = '$sClass' AND action = '$sAction' AND userid = '{$oUser->GetKey()}'"));
  267. if ($oSet->Count() < 1)
  268. {
  269. return UR_ALLOWED_NO;
  270. }
  271. $oGrantRecord = $oSet->Fetch();
  272. switch ($oGrantRecord->Get('permission'))
  273. {
  274. case 'yes':
  275. $iRetCode = UR_ALLOWED_YES;
  276. break;
  277. case 'no':
  278. default:
  279. $iRetCode = UR_ALLOWED_NO;
  280. break;
  281. }
  282. return $iRetCode;
  283. }
  284. public function IsActionAllowedOnAttribute($oUser, $sClass, $sAttCode, $iActionCode, $oInstanceSet = null)
  285. {
  286. if (!array_key_exists($iActionCode, self::$m_aActionCodes))
  287. {
  288. return UR_ALLOWED_NO;
  289. }
  290. $sAction = self::$m_aActionCodes[$iActionCode];
  291. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT UserRightsMatrixAttributeGrant WHERE class = '$sClass' AND attcode = '$sAttCode' AND action = '$sAction' AND userid = '{$oUser->GetKey()}'"));
  292. if ($oSet->Count() < 1)
  293. {
  294. return UR_ALLOWED_NO;
  295. }
  296. $oGrantRecord = $oSet->Fetch();
  297. switch ($oGrantRecord->Get('permission'))
  298. {
  299. case 'yes':
  300. $iRetCode = UR_ALLOWED_YES;
  301. break;
  302. case 'no':
  303. default:
  304. $iRetCode = UR_ALLOWED_NO;
  305. break;
  306. }
  307. return $iRetCode;
  308. }
  309. public function IsStimulusAllowed($oUser, $sClass, $sStimulusCode, $oInstanceSet = null)
  310. {
  311. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT UserRightsMatrixClassStimulusGrant WHERE class = '$sClass' AND stimulus = '$sStimulusCode' AND userid = '{$oUser->GetKey()}'"));
  312. if ($oSet->Count() < 1)
  313. {
  314. return UR_ALLOWED_NO;
  315. }
  316. $oGrantRecord = $oSet->Fetch();
  317. switch ($oGrantRecord->Get('permission'))
  318. {
  319. case 'yes':
  320. $iRetCode = UR_ALLOWED_YES;
  321. break;
  322. case 'no':
  323. default:
  324. $iRetCode = UR_ALLOWED_NO;
  325. break;
  326. }
  327. return $iRetCode;
  328. }
  329. public function FlushPrivileges()
  330. {
  331. }
  332. }
  333. UserRights::SelectModule('UserRightsMatrix');
  334. ?>