userrights.class.inc.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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. // Used to build select queries showing only objects visible for the given user
  48. abstract public function GetSelectFilter($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. MetaModel::Init_AddAttribute(new AttributeLinkedSetIndirect("allowed_org_list", array("linked_class"=>"URP_UserOrg", "ext_key_to_me"=>"userid", "ext_key_to_remote"=>"allowed_org_id", "allowed_values"=>null, "count_min"=>1, "count_max"=>0, "depends_on"=>array())));
  81. // Display lists
  82. MetaModel::Init_SetZListItems('details', array('contactid', 'first_name', 'email', 'login', 'language', 'profile_list', 'allowed_org_list')); // Attributes to be displayed for the complete details
  83. MetaModel::Init_SetZListItems('list', array('finalclass', 'first_name', 'last_name', 'login')); // Attributes to be displayed for a list
  84. // Search criteria
  85. MetaModel::Init_SetZListItems('standard_search', array('login', 'contactid')); // Criteria of the std search form
  86. MetaModel::Init_SetZListItems('advanced_search', array('login', 'contactid')); // Criteria of the advanced search form
  87. }
  88. abstract public function CheckCredentials($sPassword);
  89. abstract public function TrustWebServerContext();
  90. abstract public function CanChangePassword();
  91. abstract public function ChangePassword($sOldPassword, $sNewPassword);
  92. /*
  93. * Overload the standard behavior
  94. */
  95. public function DoCheckToWrite()
  96. {
  97. parent::DoCheckToWrite();
  98. // Note: This MUST be factorized later: declare unique keys (set of columns) in the data model
  99. $aChanges = $this->ListChanges();
  100. if (array_key_exists('login', $aChanges))
  101. {
  102. $sNewLogin = $aChanges['login'];
  103. $oSearch = DBObjectSearch::FromOQL_AllData("SELECT User WHERE login = :newlogin");
  104. $oSet = new DBObjectSet($oSearch, array(), array('newlogin' => $sNewLogin));
  105. if ($oSet->Count() > 0)
  106. {
  107. $this->m_aCheckIssues[] = Dict::Format('Class:User/Error:LoginMustBeUnique', $sNewLogin);
  108. }
  109. }
  110. }
  111. function GetGrantAsHtml($sClass, $iAction)
  112. {
  113. if (UserRights::IsActionAllowed($sClass, $iAction, null, $this))
  114. {
  115. return '<span style="background-color: #ddffdd;">'.Dict::S('UI:UserManagement:ActionAllowed:Yes').'</span>';
  116. }
  117. else
  118. {
  119. return '<span style="background-color: #ffdddd;">'.Dict::S('UI:UserManagement:ActionAllowed:No').'</span>';
  120. }
  121. }
  122. function DoShowGrantSumary($oPage, $sClassCategory)
  123. {
  124. if (UserRights::IsAdministrator($this))
  125. {
  126. // Looks dirty, but ok that's THE ONE
  127. $oPage->p(Dict::S('UI:UserManagement:AdminProfile+'));
  128. return;
  129. }
  130. $oKPI = new ExecutionKPI();
  131. $aDisplayData = array();
  132. foreach (MetaModel::GetClasses($sClassCategory) as $sClass)
  133. {
  134. $aClassStimuli = MetaModel::EnumStimuli($sClass);
  135. if (count($aClassStimuli) > 0)
  136. {
  137. $aStimuli = array();
  138. foreach ($aClassStimuli as $sStimulusCode => $oStimulus)
  139. {
  140. if (UserRights::IsStimulusAllowed($sClass, $sStimulusCode, null, $this))
  141. {
  142. $aStimuli[] = '<span title="'.$sStimulusCode.': '.htmlentities($oStimulus->GetDescription()).'">'.htmlentities($oStimulus->GetLabel()).'</span>';
  143. }
  144. }
  145. $sStimuli = implode(', ', $aStimuli);
  146. }
  147. else
  148. {
  149. $sStimuli = '<em title="'.Dict::S('UI:UserManagement:NoLifeCycleApplicable+').'">'.Dict::S('UI:UserManagement:NoLifeCycleApplicable').'</em>';
  150. }
  151. $aDisplayData[] = array(
  152. 'class' => MetaModel::GetName($sClass),
  153. 'read' => $this->GetGrantAsHtml($sClass, UR_ACTION_READ),
  154. 'bulkread' => $this->GetGrantAsHtml($sClass, UR_ACTION_BULK_READ),
  155. 'write' => $this->GetGrantAsHtml($sClass, UR_ACTION_MODIFY),
  156. 'bulkwrite' => $this->GetGrantAsHtml($sClass, UR_ACTION_BULK_MODIFY),
  157. 'stimuli' => $sStimuli,
  158. );
  159. }
  160. $oKPI->ComputeAndReport('Computation of user rights');
  161. $aDisplayConfig = array();
  162. $aDisplayConfig['class'] = array('label' => Dict::S('UI:UserManagement:Class'), 'description' => Dict::S('UI:UserManagement:Class+'));
  163. $aDisplayConfig['read'] = array('label' => Dict::S('UI:UserManagement:Action:Read'), 'description' => Dict::S('UI:UserManagement:Action:Read+'));
  164. $aDisplayConfig['bulkread'] = array('label' => Dict::S('UI:UserManagement:Action:BulkRead'), 'description' => Dict::S('UI:UserManagement:Action:BulkRead+'));
  165. $aDisplayConfig['write'] = array('label' => Dict::S('UI:UserManagement:Action:Modify'), 'description' => Dict::S('UI:UserManagement:Action:Modify+'));
  166. $aDisplayConfig['bulkwrite'] = array('label' => Dict::S('UI:UserManagement:Action:BulkModify'), 'description' => Dict::S('UI:UserManagement:Action:BulkModify+'));
  167. $aDisplayConfig['stimuli'] = array('label' => Dict::S('UI:UserManagement:Action:Stimuli'), 'description' => Dict::S('UI:UserManagement:Action:Stimuli+'));
  168. $oPage->table($aDisplayConfig, $aDisplayData);
  169. }
  170. function DisplayBareRelations(WebPage $oPage, $bEditMode = false)
  171. {
  172. parent::DisplayBareRelations($oPage, $bEditMode);
  173. if (!$bEditMode)
  174. {
  175. $oPage->SetCurrentTab(Dict::S('UI:UserManagement:GrantMatrix'));
  176. $this->DoShowGrantSumary($oPage, 'bizmodel');
  177. // debug
  178. if (false)
  179. {
  180. $oPage->SetCurrentTab('More on user rigths (dev only)');
  181. $oPage->add("<h3>User rights</h3>\n");
  182. $this->DoShowGrantSumary($oPage, 'addon/userrights');
  183. $oPage->add("<h3>Change log</h3>\n");
  184. $this->DoShowGrantSumary($oPage, 'core/cmdb');
  185. $oPage->add("<h3>Application</h3>\n");
  186. $this->DoShowGrantSumary($oPage, 'application');
  187. $oPage->add("<h3>GUI</h3>\n");
  188. $this->DoShowGrantSumary($oPage, 'gui');
  189. }
  190. }
  191. }
  192. }
  193. /**
  194. * Abstract class for all types of "internal" authentication i.e. users
  195. * for which the application is supplied a login and a password opposed
  196. * to "external" users for whom the authentication is performed outside
  197. * of the application (by the web server for example).
  198. * Note that "internal" users do not necessary correspond to a local authentication
  199. * they may be authenticated by a remote system, like in authent-ldap.
  200. */
  201. abstract class UserInternal extends User
  202. {
  203. // Nothing special, just a base class to categorize this type of authenticated users
  204. public static function Init()
  205. {
  206. $aParams = array
  207. (
  208. "category" => "core",
  209. "key_type" => "autoincrement",
  210. "name_attcode" => "login",
  211. "state_attcode" => "",
  212. "reconc_keys" => array('login'),
  213. "db_table" => "priv_internalUser",
  214. "db_key_field" => "id",
  215. "db_finalclass_field" => "",
  216. );
  217. MetaModel::Init_Params($aParams);
  218. MetaModel::Init_InheritAttributes();
  219. // Display lists
  220. MetaModel::Init_SetZListItems('details', array('contactid', 'first_name', 'email', 'login', 'language', 'profile_list', 'allowed_org_list')); // Attributes to be displayed for the complete details
  221. MetaModel::Init_SetZListItems('list', array('finalclass', 'first_name', 'last_name', 'login')); // Attributes to be displayed for a list
  222. // Search criteria
  223. MetaModel::Init_SetZListItems('standard_search', array('login', 'contactid')); // Criteria of the std search form
  224. MetaModel::Init_SetZListItems('advanced_search', array('login', 'contactid')); // Criteria of the advanced search form
  225. }
  226. }
  227. /**
  228. * User management core API
  229. *
  230. * @package iTopORM
  231. */
  232. class UserRights
  233. {
  234. protected static $m_oAddOn;
  235. protected static $m_oUser;
  236. protected static $m_oRealUser;
  237. public static function SelectModule($sModuleName)
  238. {
  239. if (!class_exists($sModuleName))
  240. {
  241. throw new CoreException("Could not select this module, '$sModuleName' in not a valid class name");
  242. return;
  243. }
  244. if (!is_subclass_of($sModuleName, 'UserRightsAddOnAPI'))
  245. {
  246. throw new CoreException("Could not select this module, the class '$sModuleName' is not derived from UserRightsAddOnAPI");
  247. return;
  248. }
  249. self::$m_oAddOn = new $sModuleName;
  250. self::$m_oAddOn->Init();
  251. self::$m_oUser = null;
  252. self::$m_oRealUser = null;
  253. }
  254. public static function GetModuleInstance()
  255. {
  256. return self::$m_oAddOn;
  257. }
  258. // Installation: create the very first user
  259. public static function CreateAdministrator($sAdminUser, $sAdminPwd, $sLanguage = 'EN US')
  260. {
  261. $bRes = self::$m_oAddOn->CreateAdministrator($sAdminUser, $sAdminPwd, $sLanguage);
  262. self::FlushPrivileges(true /* reset admin cache */);
  263. return $bRes;
  264. }
  265. // Installation (e.g: give default values for users)
  266. public static function Setup()
  267. {
  268. // to be discussed...
  269. $bRes = self::$m_oAddOn->Setup();
  270. self::FlushPrivileges(true /* reset admin cache */);
  271. return $bRes;
  272. }
  273. protected static function IsLoggedIn()
  274. {
  275. if (self::$m_oUser == null)
  276. {
  277. return false;
  278. }
  279. else
  280. {
  281. return true;
  282. }
  283. }
  284. public static function Login($sName, $sAuthentication = 'any')
  285. {
  286. $oUser = self::FindUser($sName, $sAuthentication);
  287. if (is_null($oUser))
  288. {
  289. return false;
  290. }
  291. self::$m_oUser = $oUser;
  292. Dict::SetUserLanguage(self::GetUserLanguage());
  293. return true;
  294. }
  295. public static function CheckCredentials($sName, $sPassword, $sAuthentication = 'any')
  296. {
  297. $oUser = self::FindUser($sName, $sAuthentication);
  298. if (is_null($oUser))
  299. {
  300. return false;
  301. }
  302. if (!$oUser->CheckCredentials($sPassword))
  303. {
  304. return false;
  305. }
  306. return true;
  307. }
  308. public static function TrustWebServerContext()
  309. {
  310. if (!is_null(self::$m_oUser))
  311. {
  312. return self::$m_oUser->TrustWebServerContext();
  313. }
  314. else
  315. {
  316. return false;
  317. }
  318. }
  319. public static function CanChangePassword()
  320. {
  321. if (!is_null(self::$m_oUser))
  322. {
  323. return self::$m_oUser->CanChangePassword();
  324. }
  325. else
  326. {
  327. return false;
  328. }
  329. }
  330. public static function CanLogOff()
  331. {
  332. if (!is_null(self::$m_oUser))
  333. {
  334. return self::$m_oUser->CanLogOff();
  335. }
  336. else
  337. {
  338. return false;
  339. }
  340. }
  341. public static function ChangePassword($sCurrentPassword, $sNewPassword)
  342. {
  343. if (!is_null(self::$m_oUser))
  344. {
  345. return self::$m_oUser->ChangePassword($sCurrentPassword, $sNewPassword);
  346. }
  347. else
  348. {
  349. return false;
  350. }
  351. }
  352. public static function Impersonate($sName, $sPassword)
  353. {
  354. if (!self::CheckLogin()) return false;
  355. $oUser = self::FindUser($sName);
  356. if (is_null($oUser))
  357. {
  358. return false;
  359. }
  360. if (!$oUser->CheckCredentials($sPassword))
  361. {
  362. return false;
  363. }
  364. self::$m_oRealUser = self::$m_oUser;
  365. self::$m_oUser = $oUser;
  366. Dict::SetUserLanguage(self::GetUserLanguage());
  367. return true;
  368. }
  369. public static function GetUser()
  370. {
  371. if (is_null(self::$m_oUser))
  372. {
  373. return '';
  374. }
  375. else
  376. {
  377. return self::$m_oUser->Get('login');
  378. }
  379. }
  380. public static function GetUserLanguage()
  381. {
  382. if (is_null(self::$m_oUser))
  383. {
  384. return 'EN US';
  385. }
  386. else
  387. {
  388. return self::$m_oUser->Get('language');
  389. }
  390. }
  391. public static function GetUserId($sName = '')
  392. {
  393. if (empty($sName))
  394. {
  395. // return current user id
  396. if (is_null(self::$m_oUser))
  397. {
  398. return null;
  399. }
  400. return self::$m_oUser->GetKey();
  401. }
  402. else
  403. {
  404. // find the id out of the login string
  405. $oUser = self::$m_oAddOn->FindUser($sName);
  406. if (is_null($oUser))
  407. {
  408. return null;
  409. }
  410. return $oUser->GetKey();
  411. }
  412. }
  413. public static function GetContactId($sName = '')
  414. {
  415. if (empty($sName))
  416. {
  417. $oUser = self::$m_oUser;
  418. }
  419. else
  420. {
  421. $oUser = FindUser($sName);
  422. }
  423. if (is_null($oUser))
  424. {
  425. return '';
  426. }
  427. return $oUser->Get('contactid');
  428. }
  429. public static function IsImpersonated()
  430. {
  431. if (is_null(self::$m_oRealUser))
  432. {
  433. return false;
  434. }
  435. return true;
  436. }
  437. public static function GetRealUser()
  438. {
  439. if (is_null(self::$m_oRealUser))
  440. {
  441. return '';
  442. }
  443. return self::$m_oRealUser->Get('login');
  444. }
  445. public static function GetRealUserId()
  446. {
  447. if (is_null(self::$m_oRealUser))
  448. {
  449. return '';
  450. }
  451. return self::$m_oRealUser->GetKey();
  452. }
  453. protected static function CheckLogin()
  454. {
  455. if (!self::IsLoggedIn())
  456. {
  457. //throw new UserRightException('No user logged in', array());
  458. return false;
  459. }
  460. return true;
  461. }
  462. public static function GetSelectFilter($sClass)
  463. {
  464. // Need to load some records before the login is performed (user preferences)
  465. if (MetaModel::HasCategory($sClass, 'alwaysreadable')) return true;
  466. // When initializing, we need to let everything pass trough
  467. if (!self::CheckLogin()) return true;
  468. if (self::IsAdministrator()) return true;
  469. // this module is forbidden for non admins.... BUT I NEED IT HERE TO DETERMINE USER RIGHTS
  470. if (MetaModel::HasCategory($sClass, 'addon/userrights')) return true;
  471. // the rest is allowed (#@# to be improved)
  472. if (!MetaModel::HasCategory($sClass, 'bizmodel')) return true;
  473. return self::$m_oAddOn->GetSelectFilter(self::$m_oUser, $sClass);
  474. }
  475. public static function IsActionAllowed($sClass, $iActionCode, /*dbObjectSet*/ $oInstanceSet = null, $oUser = null)
  476. {
  477. // When initializing, we need to let everything pass trough
  478. if (!self::CheckLogin()) return true;
  479. if (self::IsAdministrator($oUser)) return true;
  480. // #@# Temporary?????
  481. // The read access is controlled in MetaModel::MakeSelectQuery()
  482. if ($iActionCode == UR_ACTION_READ) return true;
  483. // this module is forbidden for non admins
  484. if (MetaModel::HasCategory($sClass, 'addon/userrights')) return false;
  485. // the rest is allowed (#@# to be improved)
  486. if (!MetaModel::HasCategory($sClass, 'bizmodel')) return true;
  487. if (is_null($oUser))
  488. {
  489. $oUser = self::$m_oUser;
  490. }
  491. return self::$m_oAddOn->IsActionAllowed($oUser, $sClass, $iActionCode, $oInstanceSet);
  492. }
  493. public static function IsStimulusAllowed($sClass, $sStimulusCode, /*dbObjectSet*/ $oInstanceSet = null, $oUser = null)
  494. {
  495. // When initializing, we need to let everything pass trough
  496. if (!self::CheckLogin()) return true;
  497. if (self::IsAdministrator($oUser)) return true;
  498. // this module is forbidden for non admins
  499. if (MetaModel::HasCategory($sClass, 'addon/userrights')) return false;
  500. // the rest is allowed (#@# to be improved)
  501. if (!MetaModel::HasCategory($sClass, 'bizmodel')) return true;
  502. if (is_null($oUser))
  503. {
  504. $oUser = self::$m_oUser;
  505. }
  506. return self::$m_oAddOn->IsStimulusAllowed($oUser, $sClass, $sStimulusCode, $oInstanceSet);
  507. }
  508. public static function IsActionAllowedOnAttribute($sClass, $sAttCode, $iActionCode, /*dbObjectSet*/ $oInstanceSet = null, $oUser = null)
  509. {
  510. // When initializing, we need to let everything pass trough
  511. if (!self::CheckLogin()) return true;
  512. if (self::IsAdministrator($oUser)) return true;
  513. // this module is forbidden for non admins
  514. if (MetaModel::HasCategory($sClass, 'addon/userrights')) return false;
  515. // the rest is allowed (#@# to be improved)
  516. if (!MetaModel::HasCategory($sClass, 'bizmodel')) return true;
  517. if (is_null($oUser))
  518. {
  519. $oUser = self::$m_oUser;
  520. }
  521. return self::$m_oAddOn->IsActionAllowedOnAttribute($oUser, $sClass, $sAttCode, $iActionCode, $oInstanceSet);
  522. }
  523. static $m_aAdmins = array();
  524. public static function IsAdministrator($oUser = null)
  525. {
  526. if (!self::CheckLogin()) return false;
  527. if (is_null($oUser))
  528. {
  529. $oUser = self::$m_oUser;
  530. }
  531. $iUser = $oUser->GetKey();
  532. if (!isset(self::$m_aAdmins[$iUser]))
  533. {
  534. self::$m_aAdmins[$iUser] = self::$m_oAddOn->IsAdministrator($oUser);
  535. }
  536. return self::$m_aAdmins[$iUser];
  537. }
  538. /**
  539. * Reset cached data
  540. * @param Bool Reset admin cache as well
  541. * @return void
  542. */
  543. // Reset cached data
  544. //
  545. public static function FlushPrivileges($bResetAdminCache = false)
  546. {
  547. if ($bResetAdminCache)
  548. {
  549. self::$m_aAdmins = array();
  550. }
  551. return self::$m_oAddOn->FlushPrivileges();
  552. }
  553. static $m_aCacheUsers;
  554. /**
  555. * Find a user based on its login and its type of authentication
  556. * @param string $sLogin Login/identifier of the user
  557. * @param string $sAuthentication Type of authentication used: internal|external|any
  558. * @return User The found user or null
  559. */
  560. protected static function FindUser($sLogin, $sAuthentication = 'any')
  561. {
  562. if ($sAuthentication == 'any')
  563. {
  564. $oUser = self::FindUser($sLogin, 'internal');
  565. if ($oUser == null)
  566. {
  567. $oUser = self::FindUser($sLogin, 'external');
  568. }
  569. }
  570. else
  571. {
  572. if (!isset(self::$m_aCacheUsers))
  573. {
  574. self::$m_aCacheUsers = array('internal' => array(), 'external' => array());
  575. }
  576. if (!isset(self::$m_aCacheUsers[$sAuthentication][$sLogin]))
  577. {
  578. switch($sAuthentication)
  579. {
  580. case 'external':
  581. $sBaseClass = 'UserExternal';
  582. break;
  583. case 'internal':
  584. $sBaseClass = 'UserInternal';
  585. break;
  586. default:
  587. echo "<p>sAuthentication = $sAuthentication</p>\n";
  588. assert(false); // should never happen
  589. }
  590. $oSearch = DBObjectSearch::FromOQL("SELECT $sBaseClass WHERE login = :login");
  591. $oSet = new DBObjectSet($oSearch, array(), array('login' => $sLogin));
  592. $oUser = $oSet->fetch();
  593. self::$m_aCacheUsers[$sAuthentication][$sLogin] = $oUser;
  594. }
  595. $oUser = self::$m_aCacheUsers[$sAuthentication][$sLogin];
  596. }
  597. return $oUser;
  598. }
  599. }
  600. ?>