userrights.class.inc.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. * User rights management API
  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 UserRightException extends CoreException
  25. {
  26. }
  27. define('UR_ALLOWED_NO', 0);
  28. define('UR_ALLOWED_YES', 1);
  29. define('UR_ALLOWED_DEPENDS', 2);
  30. define('UR_ACTION_READ', 1); // View an object
  31. define('UR_ACTION_MODIFY', 2); // Create/modify an object/attribute
  32. define('UR_ACTION_DELETE', 3); // Delete an object
  33. define('UR_ACTION_BULK_READ', 4); // Export multiple objects
  34. define('UR_ACTION_BULK_MODIFY', 5); // Create/modify multiple objects
  35. define('UR_ACTION_BULK_DELETE', 6); // Delete multiple objects
  36. define('UR_ACTION_APPLICATION_DEFINED', 10000); // Application specific actions (CSV import, View schema...)
  37. /**
  38. * User management module API
  39. *
  40. * @package iTopORM
  41. */
  42. abstract class UserRightsAddOnAPI
  43. {
  44. abstract public function Setup(); // initial installation
  45. abstract public function CreateAdministrator($sAdminUser, $sAdminPwd, $sLanguage = 'EN US'); // could be used during initial installation
  46. abstract public function Init(); // loads data (possible optimizations)
  47. // Cf UserContext...
  48. abstract public function GetFilter($sLogin, $sClass); // returns a filter object
  49. abstract public function IsActionAllowed($oUser, $sClass, $iActionCode, /*dbObjectSet*/ $oInstanceSet = null);
  50. abstract public function IsStimulusAllowed($oUser, $sClass, $sStimulusCode, /*dbObjectSet*/ $oInstanceSet = null);
  51. abstract public function IsActionAllowedOnAttribute($oUser, $sClass, $sAttCode, $iActionCode, /*dbObjectSet*/ $oInstanceSet = null);
  52. abstract public function IsAdministrator($oUser);
  53. abstract public function FlushPrivileges();
  54. }
  55. abstract class User extends cmdbAbstractObject
  56. {
  57. public static function Init()
  58. {
  59. $aParams = array
  60. (
  61. "category" => "core",
  62. "key_type" => "autoincrement",
  63. "name_attcode" => "login",
  64. "state_attcode" => "",
  65. "reconc_keys" => array(),
  66. "db_table" => "priv_user",
  67. "db_key_field" => "id",
  68. "db_finalclass_field" => "",
  69. "display_template" => "",
  70. );
  71. MetaModel::Init_Params($aParams);
  72. //MetaModel::Init_InheritAttributes();
  73. MetaModel::Init_AddAttribute(new AttributeExternalKey("contactid", array("targetclass"=>"Person", "allowed_values"=>null, "sql"=>"contactid", "is_null_allowed"=>true, "on_target_delete"=>DEL_MANUAL, "depends_on"=>array())));
  74. MetaModel::Init_AddAttribute(new AttributeExternalField("last_name", array("allowed_values"=>null, "extkey_attcode"=> 'contactid', "target_attcode"=>"name")));
  75. MetaModel::Init_AddAttribute(new AttributeExternalField("first_name", array("allowed_values"=>null, "extkey_attcode"=> 'contactid', "target_attcode"=>"first_name")));
  76. MetaModel::Init_AddAttribute(new AttributeExternalField("email", array("allowed_values"=>null, "extkey_attcode"=> 'contactid', "target_attcode"=>"email")));
  77. MetaModel::Init_AddAttribute(new AttributeString("login", array("allowed_values"=>null, "sql"=>"login", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  78. MetaModel::Init_AddAttribute(new AttributeApplicationLanguage("language", array("sql"=>"language", "default_value"=>"EN US", "is_null_allowed"=>false, "depends_on"=>array())));
  79. MetaModel::Init_AddAttribute(new AttributeLinkedSetIndirect("profile_list", array("linked_class"=>"URP_UserProfile", "ext_key_to_me"=>"userid", "ext_key_to_remote"=>"profileid", "allowed_values"=>null, "count_min"=>1, "count_max"=>0, "depends_on"=>array())));
  80. // Display lists
  81. MetaModel::Init_SetZListItems('details', array('contactid', 'first_name', 'email', 'login', 'language', 'profile_list')); // Attributes to be displayed for the complete details
  82. MetaModel::Init_SetZListItems('list', array('finalclass', 'first_name', 'last_name', 'login')); // Attributes to be displayed for a list
  83. // Search criteria
  84. MetaModel::Init_SetZListItems('standard_search', array('login', 'contactid')); // Criteria of the std search form
  85. MetaModel::Init_SetZListItems('advanced_search', array('login', 'contactid')); // Criteria of the advanced search form
  86. }
  87. abstract public function CheckCredentials($sPassword);
  88. abstract public function TrustWebServerContext();
  89. abstract public function CanChangePassword();
  90. abstract public function ChangePassword($sOldPassword, $sNewPassword);
  91. function GetGrantAsHtml($sClass, $iAction)
  92. {
  93. if (UserRights::IsActionAllowed($sClass, $iAction, null, $this))
  94. {
  95. return '<span style="background-color: #ddffdd;">'.Dict::S('UI:UserManagement:ActionAllowed:Yes').'</span>';
  96. }
  97. else
  98. {
  99. return '<span style="background-color: #ffdddd;">'.Dict::S('UI:UserManagement:ActionAllowed:No').'</span>';
  100. }
  101. }
  102. function DoShowGrantSumary($oPage, $sClassCategory)
  103. {
  104. if (UserRights::IsAdministrator($this))
  105. {
  106. // Looks dirty, but ok that's THE ONE
  107. $oPage->p(Dict::S('UI:UserManagement:AdminProfile+'));
  108. return;
  109. }
  110. $aDisplayData = array();
  111. foreach (MetaModel::GetClasses($sClassCategory) as $sClass)
  112. {
  113. // Skip non instantiable classes
  114. if (MetaModel::IsAbstract($sClass)) continue;
  115. $aClassStimuli = MetaModel::EnumStimuli($sClass);
  116. if (count($aClassStimuli) > 0)
  117. {
  118. $aStimuli = array();
  119. foreach ($aClassStimuli as $sStimulusCode => $oStimulus)
  120. {
  121. if (UserRights::IsStimulusAllowed($sClass, $sStimulusCode, null, $this))
  122. {
  123. $aStimuli[] = '<span title="'.$sStimulusCode.': '.htmlentities($oStimulus->GetDescription()).'">'.htmlentities($oStimulus->GetLabel()).'</span>';
  124. }
  125. }
  126. $sStimuli = implode(', ', $aStimuli);
  127. }
  128. else
  129. {
  130. $sStimuli = '<em title="'.Dict::S('UI:UserManagement:NoLifeCycleApplicable+').'">'.Dict::S('UI:UserManagement:NoLifeCycleApplicable').'</em>';
  131. }
  132. $aDisplayData[] = array(
  133. 'class' => MetaModel::GetName($sClass),
  134. 'read' => $this->GetGrantAsHtml($sClass, UR_ACTION_READ),
  135. 'bulkread' => $this->GetGrantAsHtml($sClass, UR_ACTION_BULK_READ),
  136. 'write' => $this->GetGrantAsHtml($sClass, UR_ACTION_MODIFY),
  137. 'bulkwrite' => $this->GetGrantAsHtml($sClass, UR_ACTION_BULK_MODIFY),
  138. 'stimuli' => $sStimuli,
  139. );
  140. }
  141. $aDisplayConfig = array();
  142. $aDisplayConfig['class'] = array('label' => Dict::S('UI:UserManagement:Class'), 'description' => Dict::S('UI:UserManagement:Class+'));
  143. $aDisplayConfig['read'] = array('label' => Dict::S('UI:UserManagement:Action:Read'), 'description' => Dict::S('UI:UserManagement:Action:Read+'));
  144. $aDisplayConfig['bulkread'] = array('label' => Dict::S('UI:UserManagement:Action:BulkRead'), 'description' => Dict::S('UI:UserManagement:Action:BulkRead+'));
  145. $aDisplayConfig['write'] = array('label' => Dict::S('UI:UserManagement:Action:Modify'), 'description' => Dict::S('UI:UserManagement:Action:Modify+'));
  146. $aDisplayConfig['bulkwrite'] = array('label' => Dict::S('UI:UserManagement:Action:BulkModify'), 'description' => Dict::S('UI:UserManagement:Action:BulkModify+'));
  147. $aDisplayConfig['stimuli'] = array('label' => Dict::S('UI:UserManagement:Action:Stimuli'), 'description' => Dict::S('UI:UserManagement:Action:Stimuli+'));
  148. $oPage->table($aDisplayConfig, $aDisplayData);
  149. }
  150. function DisplayBareRelations(WebPage $oPage, $bEditMode = false)
  151. {
  152. parent::DisplayBareRelations($oPage, $bEditMode);
  153. if (!$bEditMode)
  154. {
  155. $oPage->SetCurrentTab(Dict::S('UI:UserManagement:GrantMatrix'));
  156. $this->DoShowGrantSumary($oPage, 'bizmodel');
  157. // debug
  158. if (false)
  159. {
  160. $oPage->SetCurrentTab('More on user rigths (dev only)');
  161. $oPage->add("<h3>User rights</h3>\n");
  162. $this->DoShowGrantSumary($oPage, 'addon/userrights');
  163. $oPage->add("<h3>Change log</h3>\n");
  164. $this->DoShowGrantSumary($oPage, 'core/cmdb');
  165. $oPage->add("<h3>Application</h3>\n");
  166. $this->DoShowGrantSumary($oPage, 'application');
  167. $oPage->add("<h3>GUI</h3>\n");
  168. $this->DoShowGrantSumary($oPage, 'gui');
  169. }
  170. }
  171. }
  172. }
  173. /**
  174. * User management core API
  175. *
  176. * @package iTopORM
  177. */
  178. class UserRights
  179. {
  180. protected static $m_oAddOn;
  181. protected static $m_oUser;
  182. protected static $m_oRealUser;
  183. public static function SelectModule($sModuleName)
  184. {
  185. if (!class_exists($sModuleName))
  186. {
  187. throw new CoreException("Could not select this module, '$sModuleName' in not a valid class name");
  188. return;
  189. }
  190. if (!is_subclass_of($sModuleName, 'UserRightsAddOnAPI'))
  191. {
  192. throw new CoreException("Could not select this module, the class '$sModuleName' is not derived from UserRightsAddOnAPI");
  193. return;
  194. }
  195. self::$m_oAddOn = new $sModuleName;
  196. self::$m_oAddOn->Init();
  197. self::$m_oUser = null;
  198. self::$m_oRealUser = null;
  199. }
  200. public static function GetModuleInstance()
  201. {
  202. return self::$m_oAddOn;
  203. }
  204. // Installation: create the very first user
  205. public static function CreateAdministrator($sAdminUser, $sAdminPwd, $sLanguage = 'EN US')
  206. {
  207. return self::$m_oAddOn->CreateAdministrator($sAdminUser, $sAdminPwd, $sLanguage);
  208. }
  209. // Installation (e.g: give default values for users)
  210. public static function Setup()
  211. {
  212. // to be discussed...
  213. return self::$m_oAddOn->Setup();
  214. }
  215. protected static function IsLoggedIn()
  216. {
  217. if (self::$m_oUser == null)
  218. {
  219. return false;
  220. }
  221. else
  222. {
  223. return true;
  224. }
  225. }
  226. public static function Login($sName, $sPassword)
  227. {
  228. $oUser = self::FindUser($sName);
  229. if (is_null($oUser))
  230. {
  231. return false;
  232. }
  233. if (!$oUser->CheckCredentials($sPassword))
  234. {
  235. return false;
  236. }
  237. self::$m_oUser = $oUser;
  238. self::$m_oRealUser = $oUser;
  239. Dict::SetUserLanguage(self::GetUserLanguage());
  240. return true;
  241. }
  242. public static function TrustWebServerContext()
  243. {
  244. if (!is_null(self::$m_oUser))
  245. {
  246. return self::$m_oUser->TrustWebServerContext();
  247. }
  248. else
  249. {
  250. return false;
  251. }
  252. }
  253. public static function CanChangePassword()
  254. {
  255. if (!is_null(self::$m_oUser))
  256. {
  257. return self::$m_oUser->CanChangePassword();
  258. }
  259. else
  260. {
  261. return false;
  262. }
  263. }
  264. public static function ChangePassword($sCurrentPassword, $sNewPassword)
  265. {
  266. if (!is_null(self::$m_oUser))
  267. {
  268. return self::$m_oUser->ChangePassword($sCurrentPassword, $sNewPassword);
  269. }
  270. else
  271. {
  272. return false;
  273. }
  274. }
  275. public static function Impersonate($sName, $sPassword)
  276. {
  277. if (!self::CheckLogin()) return false;
  278. $oUser = self::FindUser($sName);
  279. if (is_null($oUser))
  280. {
  281. return false;
  282. }
  283. if (!$oUser->CheckCredentials($sPassword))
  284. {
  285. return false;
  286. }
  287. self::$m_oRealUser = self::$m_oUser;
  288. self::$m_oUser = $oUser;
  289. Dict::SetUserLanguage(self::GetUserLanguage());
  290. return true;
  291. }
  292. public static function GetUser()
  293. {
  294. if (is_null(self::$m_oUser))
  295. {
  296. return '';
  297. }
  298. else
  299. {
  300. return self::$m_oUser->Get('login');
  301. }
  302. }
  303. public static function GetUserLanguage()
  304. {
  305. if (is_null(self::$m_oUser))
  306. {
  307. return 'EN US';
  308. }
  309. else
  310. {
  311. return self::$m_oUser->Get('language');
  312. }
  313. }
  314. public static function GetUserId($sName = '')
  315. {
  316. if (empty($sName))
  317. {
  318. // return current user id
  319. if (is_null(self::$m_oUser))
  320. {
  321. return null;
  322. }
  323. return self::$m_oUser->GetKey();
  324. }
  325. else
  326. {
  327. // find the id out of the login string
  328. $oUser = self::$m_oAddOn->FindUser($sName);
  329. if (is_null($oUser))
  330. {
  331. return null;
  332. }
  333. return $oUser->GetKey();
  334. }
  335. }
  336. public static function GetContactId($sName = '')
  337. {
  338. if (empty($sName))
  339. {
  340. $oUser = self::$m_oUser;
  341. }
  342. else
  343. {
  344. $oUser = FindUser($sName);
  345. }
  346. if (is_null($oUser))
  347. {
  348. return '';
  349. }
  350. return $oUser->Get('contactid');
  351. }
  352. public static function IsImpersonated()
  353. {
  354. if (is_null(self::$m_oRealUser))
  355. {
  356. return false;
  357. }
  358. return true;
  359. }
  360. public static function GetRealUser()
  361. {
  362. if (is_null(self::$m_oRealUser))
  363. {
  364. return '';
  365. }
  366. return self::$m_oRealUser->Get('login');
  367. }
  368. public static function GetRealUserId()
  369. {
  370. if (is_null(self::$m_oRealUser))
  371. {
  372. return '';
  373. }
  374. return self::$m_oRealUser->GetKey();
  375. }
  376. protected static function CheckLogin()
  377. {
  378. if (!self::IsLoggedIn())
  379. {
  380. //throw new UserRightException('No user logged in', array());
  381. return false;
  382. }
  383. return true;
  384. }
  385. public static function GetFilter($sClass)
  386. {
  387. if (!self::CheckLogin()) return false;
  388. if (self::IsAdministrator()) return new DBObjectSearch($sClass);
  389. // this module is forbidden for non admins
  390. if (MetaModel::HasCategory($sClass, 'addon/userrights')) return false;
  391. // the rest is allowed (#@# to be improved)
  392. if (!MetaModel::HasCategory($sClass, 'bizmodel')) return new DBObjectSearch($sClass);
  393. return self::$m_oAddOn->GetFilter(self::$m_oUser->GetKey(), $sClass);
  394. }
  395. public static function IsActionAllowed($sClass, $iActionCode, /*dbObjectSet*/ $oInstanceSet = null, $oUser = null)
  396. {
  397. if (!self::CheckLogin()) return false;
  398. if (self::IsAdministrator($oUser)) return true;
  399. // this module is forbidden for non admins
  400. if (MetaModel::HasCategory($sClass, 'addon/userrights')) return false;
  401. // the rest is allowed (#@# to be improved)
  402. if (!MetaModel::HasCategory($sClass, 'bizmodel')) return true;
  403. if (is_null($oUser))
  404. {
  405. $oUser = self::$m_oUser;
  406. }
  407. return self::$m_oAddOn->IsActionAllowed($oUser, $sClass, $iActionCode, $oInstanceSet);
  408. }
  409. public static function IsStimulusAllowed($sClass, $sStimulusCode, /*dbObjectSet*/ $oInstanceSet = null, $oUser = null)
  410. {
  411. if (!self::CheckLogin()) return false;
  412. if (self::IsAdministrator($oUser)) return true;
  413. // this module is forbidden for non admins
  414. if (MetaModel::HasCategory($sClass, 'addon/userrights')) return false;
  415. // the rest is allowed (#@# to be improved)
  416. if (!MetaModel::HasCategory($sClass, 'bizmodel')) return true;
  417. if (is_null($oUser))
  418. {
  419. $oUser = self::$m_oUser;
  420. }
  421. return self::$m_oAddOn->IsStimulusAllowed($oUser, $sClass, $sStimulusCode, $oInstanceSet);
  422. }
  423. public static function IsActionAllowedOnAttribute($sClass, $sAttCode, $iActionCode, /*dbObjectSet*/ $oInstanceSet = null, $oUser = null)
  424. {
  425. if (!self::CheckLogin()) return false;
  426. if (self::IsAdministrator($oUser)) return true;
  427. // this module is forbidden for non admins
  428. if (MetaModel::HasCategory($sClass, 'addon/userrights')) return false;
  429. // the rest is allowed (#@# to be improved)
  430. if (!MetaModel::HasCategory($sClass, 'bizmodel')) return true;
  431. if (is_null($oUser))
  432. {
  433. $oUser = self::$m_oUser;
  434. }
  435. return self::$m_oAddOn->IsActionAllowedOnAttribute($oUser, $sClass, $sAttCode, $iActionCode, $oInstanceSet);
  436. }
  437. public static function IsAdministrator($oUser = null)
  438. {
  439. if (!self::CheckLogin()) return false;
  440. if (is_null($oUser))
  441. {
  442. $oUser = self::$m_oUser;
  443. }
  444. return self::$m_oAddOn->IsAdministrator($oUser);
  445. }
  446. public static function FlushPrivileges()
  447. {
  448. return self::$m_oAddOn->FlushPrivileges();
  449. }
  450. static $m_aCacheUsers;
  451. protected static function FindUser($sLogin)
  452. {
  453. if (!isset(self::$m_aCacheUsers))
  454. {
  455. self::$m_aCacheUsers = array();
  456. }
  457. if (!isset(self::$m_aCacheUsers[$sLogin]))
  458. {
  459. $oSearch = DBObjectSearch::FromOQL("SELECT User WHERE login = :login");
  460. $oSet = new DBObjectSet($oSearch, array(), array('login' => $sLogin));
  461. $oUser = $oSet->fetch();
  462. self::$m_aCacheUsers[$sLogin] = $oUser;
  463. }
  464. return self::$m_aCacheUsers[$sLogin];
  465. }
  466. }
  467. ?>