userrights.class.inc.php 24 KB

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