Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 43 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
DeliveryContainerService | |
0.00% |
0 / 43 |
|
0.00% |
0 / 3 |
272 | |
0.00% |
0 / 1 |
validateLtiParams | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
getActiveFeatures | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
72 | |||
getLtiFeaturesList | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
20 |
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 | namespace oat\ltiDeliveryProvider\model\delivery; |
22 | |
23 | use oat\oatbox\session\SessionService; |
24 | use oat\taoDeliveryRdf\model\DeliveryContainerService as DeliveryRdfContainerService; |
25 | use oat\taoLti\models\classes\LtiException; |
26 | use oat\taoLti\models\classes\LtiLaunchData; |
27 | use oat\taoLti\models\classes\LtiMessages\LtiErrorMessage; |
28 | use oat\taoLti\models\classes\LtiVariableMissingException; |
29 | use oat\taoLti\models\classes\TaoLtiSession; |
30 | |
31 | /** |
32 | * Override the DeliveryContainerService in order to filter the plugin list based on the security flag. |
33 | * |
34 | * @author Aleh Hutnikau, <hutnikau@1pt.com> |
35 | */ |
36 | class DeliveryContainerService extends DeliveryRdfContainerService |
37 | { |
38 | public const CUSTOM_LTI_SECURE = 'custom_secure'; |
39 | |
40 | public const CUSTOM_LTI_TAO_FEATURES = 'custom_x_tao_features'; |
41 | |
42 | /** |
43 | * Validate and prepare launch variables |
44 | * @param LtiLaunchData $launchData |
45 | * @throws LtiException |
46 | * @throws \oat\taoLti\models\classes\LtiVariableMissingException |
47 | */ |
48 | protected function validateLtiParams(LtiLaunchData $launchData) |
49 | { |
50 | if ($launchData->hasVariable(self::CUSTOM_LTI_SECURE)) { |
51 | $val = mb_strtolower($launchData->getVariable(self::CUSTOM_LTI_SECURE)); |
52 | if ($val !== 'true' && $val !== 'false') { |
53 | throw new LtiException( |
54 | __('Wrong value of "secure" variable.'), |
55 | LtiErrorMessage::ERROR_INVALID_PARAMETER |
56 | ); |
57 | } |
58 | } |
59 | } |
60 | |
61 | /** |
62 | * @inheritdoc |
63 | */ |
64 | protected function getActiveFeatures(\core_kernel_classes_Resource $delivery) |
65 | { |
66 | $result = parent::getActiveFeatures($delivery); |
67 | $currentSession = $this->getServiceLocator()->get(SessionService::SERVICE_ID)->getCurrentSession(); |
68 | if ($currentSession instanceof TaoLtiSession) { |
69 | $launchData = $currentSession->getLaunchData(); |
70 | $allFeatures = $this->getAllAvailableFeatures(); |
71 | $ltiFeatures = $this->getLtiFeaturesList($launchData); |
72 | $enabledLtiFeatures = []; |
73 | $disabledLtiFeatures = []; |
74 | foreach ($ltiFeatures as $featureName => $featureStatus) { |
75 | if (empty($allFeatures[$featureName])) { |
76 | continue; |
77 | } |
78 | |
79 | if (filter_var($featureStatus, FILTER_VALIDATE_BOOLEAN)) { |
80 | $enabledLtiFeatures[] = $featureName; |
81 | } else { |
82 | $disabledLtiFeatures[] = $featureName; |
83 | } |
84 | } |
85 | |
86 | // TODO: Deprecated method, to be removed in version 11.0.0 |
87 | $this->validateLtiParams($launchData); |
88 | if (!isset($ltiFeatures['security']) && $launchData->hasVariable(self::CUSTOM_LTI_SECURE)) { |
89 | if ($launchData->getVariable(self::CUSTOM_LTI_SECURE) === 'true') { |
90 | $enabledLtiFeatures[] = 'security'; |
91 | } else { |
92 | $disabledLtiFeatures[] = 'security'; |
93 | } |
94 | } |
95 | |
96 | $result = array_merge($result, $enabledLtiFeatures); |
97 | $result = array_diff($result, $disabledLtiFeatures); |
98 | } |
99 | |
100 | return array_unique($result); |
101 | } |
102 | |
103 | /** |
104 | * @param LtiLaunchData $launchData |
105 | * @return array |
106 | */ |
107 | private function getLtiFeaturesList(LtiLaunchData $launchData) |
108 | { |
109 | $ltiFeatures = []; |
110 | try { |
111 | if (!$launchData->hasVariable(self::CUSTOM_LTI_TAO_FEATURES)) { |
112 | return $ltiFeatures; |
113 | } |
114 | |
115 | $toolsLtiVariable = $launchData->getVariable(self::CUSTOM_LTI_TAO_FEATURES); |
116 | $features = json_decode($toolsLtiVariable, true); |
117 | if (is_array($features)) { |
118 | $ltiFeatures = $features; |
119 | } |
120 | } catch (LtiVariableMissingException $e) { |
121 | $this->logWarning('Cannot get LTI parameter: ' . self::CUSTOM_LTI_TAO_FEATURES, [ |
122 | 'message' => $e->getMessage(), |
123 | 'file' => $e->getFile(), |
124 | 'line' => $e->getLine(), |
125 | ]); |
126 | } |
127 | |
128 | return $ltiFeatures; |
129 | } |
130 | } |