Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 5 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| SessionService | |
0.00% |
0 / 5 |
|
0.00% |
0 / 5 |
42 | |
0.00% |
0 / 1 |
| getCurrentSession | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getCurrentUser | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getTenantId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| isAnonymous | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setSession | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2018 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\oatbox\session; |
| 23 | |
| 24 | use oat\oatbox\service\ConfigurableService; |
| 25 | use oat\oatbox\user\AnonymousUser; |
| 26 | |
| 27 | /** |
| 28 | * Represents a Session on Generis. |
| 29 | * |
| 30 | * @access private |
| 31 | * @author Joel Bout, <joel@taotesting.com> |
| 32 | * @package generis |
| 33 | */ |
| 34 | class SessionService extends ConfigurableService |
| 35 | { |
| 36 | public const SERVICE_ID = 'generis/session'; |
| 37 | |
| 38 | /** |
| 39 | * Returns the currently active session |
| 40 | * @return \common_session_Session |
| 41 | */ |
| 42 | public function getCurrentSession() |
| 43 | { |
| 44 | return \common_session_SessionManager::getSession(); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Returns the current user |
| 49 | * @throws \common_exception_Error |
| 50 | * @return \oat\oatbox\user\User |
| 51 | */ |
| 52 | public function getCurrentUser() |
| 53 | { |
| 54 | return $this->getCurrentSession()->getUser(); |
| 55 | } |
| 56 | |
| 57 | public function getTenantId(): string |
| 58 | { |
| 59 | return (string)(getenv('TENANT_ID') ?: rtrim(ROOT_URL, '/')); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Is the current session anonymous or associated to a user? |
| 64 | * @return boolean |
| 65 | */ |
| 66 | public function isAnonymous() |
| 67 | { |
| 68 | return $this->getCurrentUser() instanceof AnonymousUser; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Starts a new session and stores it in the session if stateful |
| 73 | * |
| 74 | * @param \common_session_Session $session |
| 75 | * @return boolean |
| 76 | */ |
| 77 | public function setSession(\common_session_Session $session) |
| 78 | { |
| 79 | return \common_session_SessionManager::startSession($session); |
| 80 | } |
| 81 | } |