userrights.class.inc.php 22 KB

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