userrightsmatrix.class.inc.php 13 KB

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