Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 39 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
AuthoringTool | |
0.00% |
0 / 39 |
|
0.00% |
0 / 5 |
72 | |
0.00% |
0 / 1 |
run | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
getValidatedLtiMessagePayload | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
launch | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
2 | |||
getLtiMessageOrRedirectToLogin | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
12 | |||
getAuthoringRoleService | |
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) 2023 (original work) Open Assessment Technologies SA; |
19 | */ |
20 | |
21 | declare(strict_types=1); |
22 | |
23 | namespace oat\taoLti\controller; |
24 | |
25 | use ActionEnforcingException; |
26 | use common_exception_Error; |
27 | use core_kernel_classes_Resource; |
28 | use helpers_Random; |
29 | use InterruptedActionException; |
30 | use OAT\Library\Lti1p3Core\Message\Payload\LtiMessagePayloadInterface; |
31 | use oat\tao\model\theme\ThemeService; |
32 | use oat\taoLti\models\classes\LtiException; |
33 | use oat\taoLti\models\classes\LtiMessages\LtiErrorMessage; |
34 | use oat\taoLti\models\classes\LtiService; |
35 | use oat\taoLti\models\classes\Tool\Exception\WrongLtiRolesException; |
36 | use oat\taoLti\models\classes\Tool\Service\AuthoringLtiRoleService; |
37 | use oat\taoLti\models\classes\Tool\Validation\Lti1p3Validator; |
38 | use oat\taoLti\models\classes\user\UserService; |
39 | use Psr\Container\ContainerExceptionInterface; |
40 | use Psr\Container\NotFoundExceptionInterface; |
41 | use tao_actions_Main; |
42 | use tao_models_classes_UserService; |
43 | |
44 | class AuthoringTool extends ToolModule |
45 | { |
46 | private const LTI_NO_MATCHING_REGISTRATION_FOUND_MESSAGE = 'No matching registration found tool side'; |
47 | |
48 | /** |
49 | * @throws LtiException |
50 | * @throws InterruptedActionException |
51 | * @throws common_exception_Error |
52 | */ |
53 | public function run(): void |
54 | { |
55 | if ($this->hasAccess(tao_actions_Main::class, 'entry')) { |
56 | $this->redirect(_url('entry', 'Main', 'tao', $_GET)); |
57 | } else { |
58 | throw new LtiException( |
59 | __('You are not authorized to access this resource'), |
60 | LtiErrorMessage::ERROR_UNAUTHORIZED |
61 | ); |
62 | } |
63 | } |
64 | |
65 | /** |
66 | * @throws LtiException |
67 | */ |
68 | protected function getValidatedLtiMessagePayload(): LtiMessagePayloadInterface |
69 | { |
70 | return $this->getServiceLocator() |
71 | ->getContainer() |
72 | ->get(Lti1p3Validator::class . 'Authoring') |
73 | ->getValidatedPayload($this->getPsrRequest()); |
74 | } |
75 | |
76 | /** |
77 | * @throws ActionEnforcingException |
78 | * @throws InterruptedActionException |
79 | * @throws LtiException |
80 | * @throws ContainerExceptionInterface |
81 | * @throws NotFoundExceptionInterface |
82 | * @throws common_exception_Error |
83 | * @throws WrongLtiRolesException |
84 | */ |
85 | public function launch(): void |
86 | { |
87 | $ltiMessage = $this->getLtiMessageOrRedirectToLogin(); |
88 | |
89 | $user = $this->getServiceLocator() |
90 | ->getContainer() |
91 | ->get(tao_models_classes_UserService::class) |
92 | ->addUser( |
93 | $ltiMessage->getUserIdentity()->getIdentifier(), |
94 | helpers_Random::generateString(UserService::PASSWORD_LENGTH), |
95 | new core_kernel_classes_Resource( |
96 | $this->getAuthoringRoleService()->getValidRole($ltiMessage->getRoles()) |
97 | ) |
98 | ); |
99 | |
100 | $this->getServiceLocator() |
101 | ->getContainer() |
102 | ->get(LtiService::class) |
103 | ->startLti1p3Session($ltiMessage, $user); |
104 | |
105 | $this->forward('run', null, null, $_GET); |
106 | } |
107 | |
108 | /** |
109 | * @throws ContainerExceptionInterface |
110 | * @throws InterruptedActionException |
111 | * @throws LtiException |
112 | * @throws NotFoundExceptionInterface |
113 | */ |
114 | private function getLtiMessageOrRedirectToLogin(): LtiMessagePayloadInterface |
115 | { |
116 | try { |
117 | $message = $this->getValidatedLtiMessagePayload(); |
118 | } catch (LtiException $exception) { |
119 | if ($exception->getMessage() !== self::LTI_NO_MATCHING_REGISTRATION_FOUND_MESSAGE) { |
120 | throw $exception; |
121 | } |
122 | |
123 | $this->getLogger()->warning( |
124 | sprintf( |
125 | 'Missing registration for current audience. Redirecting to the login page. Exception: %s', |
126 | $exception |
127 | ) |
128 | ); |
129 | $this->redirect(_url('login', 'Main', 'tao')); |
130 | } |
131 | |
132 | return $message; |
133 | } |
134 | |
135 | private function getAuthoringRoleService(): AuthoringLtiRoleService |
136 | { |
137 | return $this->getPsrContainer()->get(AuthoringLtiRoleService::class); |
138 | } |
139 | } |