userrights.class.inc.php 22 KB

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