Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.86% |
13 / 14 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| RestSessionFactory | |
92.86% |
13 / 14 |
|
75.00% |
3 / 4 |
10.04 | |
0.00% |
0 / 1 |
| createSessionFromRequest | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
| startSession | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getSessionBuilders | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
| isRestController | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 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; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\tao\model\session\restSessionFactory; |
| 23 | |
| 24 | use common_exception_InconsistentData; |
| 25 | use common_ext_ManifestNotFoundException; |
| 26 | use oat\oatbox\service\ConfigurableService; |
| 27 | use oat\oatbox\user\LoginFailedException; |
| 28 | use oat\tao\model\action\RestControllerInterface; |
| 29 | use oat\tao\model\routing\Resolver; |
| 30 | use ResolverException; |
| 31 | |
| 32 | /** |
| 33 | * Class RestSessionFactory |
| 34 | * |
| 35 | * A factory to build rest session based on configured adapters |
| 36 | * |
| 37 | * @package oat\tao\model\session\restSessionFactory |
| 38 | */ |
| 39 | class RestSessionFactory extends ConfigurableService |
| 40 | { |
| 41 | public const SERVICE_ID = 'tao/restSessionFactory'; |
| 42 | |
| 43 | public const OPTION_BUILDERS = 'builders'; |
| 44 | |
| 45 | /** |
| 46 | * Create a rest session based on builders. |
| 47 | * |
| 48 | * Give the request and resolver to builder to know if it is applicable |
| 49 | * If yes, create and start the session from it |
| 50 | * |
| 51 | * @param $request |
| 52 | * @param $resolver |
| 53 | * @return bool |
| 54 | * @throws LoginFailedException |
| 55 | */ |
| 56 | public function createSessionFromRequest($request, $resolver) |
| 57 | { |
| 58 | if (!$this->isRestController($resolver)) { |
| 59 | return false; |
| 60 | } |
| 61 | /** @var SessionBuilder $builder */ |
| 62 | foreach ($this->getSessionBuilders() as $builder) { |
| 63 | if ($builder->isApplicable($request)) { |
| 64 | return $this->startSession($builder->getSession($request)); |
| 65 | } |
| 66 | } |
| 67 | throw new LoginFailedException(['Request cannot be authenticated.']); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Start a session through SessionManager |
| 72 | * |
| 73 | * @param $session |
| 74 | * @return bool |
| 75 | */ |
| 76 | protected function startSession($session) |
| 77 | { |
| 78 | return \common_session_SessionManager::startSession($session); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Fetch rest session builder from the config |
| 83 | * |
| 84 | * @return SessionBuilder[] |
| 85 | */ |
| 86 | protected function getSessionBuilders() |
| 87 | { |
| 88 | $adapters = is_array($this->getOption(self::OPTION_BUILDERS)) ? $this->getOption(self::OPTION_BUILDERS) : []; |
| 89 | foreach ($adapters as $key => $adapter) { |
| 90 | if (!is_a($adapter, SessionBuilder::class, true)) { |
| 91 | throw new \LogicException('Session adapter must implement interface "SessionBuilder".'); |
| 92 | } |
| 93 | $adapters[$key] = $this->propagate(new $adapter()); |
| 94 | } |
| 95 | return $adapters; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Check if the requested controller is a RestController |
| 100 | * |
| 101 | * @param Resolver $resolver |
| 102 | * |
| 103 | * @return bool |
| 104 | * @throws ResolverException |
| 105 | * @throws common_exception_InconsistentData |
| 106 | * @throws common_ext_ManifestNotFoundException |
| 107 | */ |
| 108 | protected function isRestController(Resolver $resolver): bool |
| 109 | { |
| 110 | return is_subclass_of($resolver->getControllerClass(), RestControllerInterface::class, true); |
| 111 | } |
| 112 | } |