Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
95.12% |
39 / 41 |
|
75.00% |
6 / 8 |
CRAP | |
0.00% |
0 / 1 |
TestTakerAuthorizationService | |
95.12% |
39 / 41 |
|
75.00% |
6 / 8 |
17 | |
0.00% |
0 / 1 |
verifyStartAuthorization | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
verifyResumeAuthorization | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
4 | |||
isProctored | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
isActiveUnSecureDelivery | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
isSecure | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
4 | |||
isMissingProctorAuthorization | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
throwUnAuthorizedException | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
isSuitable | |
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) 2016 (original work) Open Assessment Technologies SA; |
19 | * |
20 | */ |
21 | |
22 | namespace oat\taoProctoring\model\authorization; |
23 | |
24 | use common_Exception; |
25 | use oat\oatbox\service\ConfigurableService; |
26 | use oat\taoDeliveryRdf\model\DeliveryContainerService; |
27 | use oat\taoDelivery\model\execution\DeliveryExecution; |
28 | use oat\taoDelivery\model\execution\DeliveryExecutionInterface; |
29 | use oat\taoProctoring\model\DelegatedServiceHandler; |
30 | use oat\taoProctoring\model\delivery\DeliverySyncService; |
31 | use oat\taoProctoring\model\execution\DeliveryExecution as ProctoredDeliveryExecution; |
32 | use oat\taoDelivery\model\authorization\UnAuthorizedException; |
33 | use oat\oatbox\user\User; |
34 | use oat\taoProctoring\model\ProctorService; |
35 | use oat\generis\model\OntologyAwareTrait; |
36 | use oat\taoTests\models\runner\plugins\TestPlugin; |
37 | |
38 | /** |
39 | * Manage the Delivery authorization. |
40 | * |
41 | * @author Bertrand Chevrier <bertrand@taotesting.com> |
42 | */ |
43 | class TestTakerAuthorizationService extends ConfigurableService implements |
44 | TestTakerAuthorizationInterface, |
45 | DelegatedServiceHandler |
46 | { |
47 | use OntologyAwareTrait; |
48 | |
49 | /** |
50 | * @deprecated moved to \oat\taoProctoring\model\delivery\DeliverySyncService::PROCTORED_BY_DEFAULT |
51 | */ |
52 | public const PROCTORED_BY_DEFAULT = 'proctored_by_default'; |
53 | |
54 | /** |
55 | * (non-PHPdoc) |
56 | * @see \oat\taoDelivery\model\authorization\AuthorizationProvider::verifyStartAuthorization() |
57 | * @param $deliveryId |
58 | * @param User $user |
59 | */ |
60 | public function verifyStartAuthorization($deliveryId, User $user) |
61 | { |
62 | // always allow start |
63 | } |
64 | |
65 | /** |
66 | * (non-PHPdoc) |
67 | * @see \oat\taoDelivery\model\authorization\AuthorizationProvider::verifyResumeAuthorization() |
68 | * @param DeliveryExecutionInterface $deliveryExecution |
69 | * @param User $user |
70 | * @throws UnAuthorizedException |
71 | */ |
72 | public function verifyResumeAuthorization(DeliveryExecutionInterface $deliveryExecution, User $user) |
73 | { |
74 | $state = $deliveryExecution->getState()->getUri(); |
75 | |
76 | if ( |
77 | in_array($state, [ |
78 | ProctoredDeliveryExecution::STATE_FINISHED, |
79 | ProctoredDeliveryExecution::STATE_CANCELED, |
80 | ProctoredDeliveryExecution::STATE_TERMINATED]) |
81 | ) { |
82 | throw new UnAuthorizedException( |
83 | _url('index', 'DeliveryServer', 'taoProctoring'), |
84 | 'Terminated/Finished delivery execution "' . $deliveryExecution->getIdentifier() . '" cannot be resumed' |
85 | ); |
86 | } |
87 | |
88 | if ( |
89 | $this->isMissingProctorAuthorization($deliveryExecution, $user) |
90 | && !$this->isActiveUnSecureDelivery($deliveryExecution, $state) |
91 | ) { |
92 | $this->throwUnAuthorizedException($deliveryExecution); |
93 | } |
94 | } |
95 | |
96 | /** |
97 | * Check if delivery id proctored |
98 | * |
99 | * @param string $deliveryId |
100 | * @param User $user |
101 | * @return bool |
102 | * @internal param core_kernel_classes_Resource $delivery |
103 | * @throws \core_kernel_persistence_Exception |
104 | */ |
105 | public function isProctored($deliveryId, User $user) |
106 | { |
107 | $delivery = $this->getResource($deliveryId); |
108 | $proctored = $delivery->getOnePropertyValue($this->getProperty(ProctorService::ACCESSIBLE_PROCTOR)); |
109 | |
110 | if ($proctored instanceof \core_kernel_classes_Resource) { |
111 | $isProctored = $proctored->getUri() == ProctorService::ACCESSIBLE_PROCTOR_ENABLED; |
112 | } else { |
113 | $deliverySyncService = $this->getServiceLocator()->get(DeliverySyncService::SERVICE_ID); |
114 | $isProctored = $deliverySyncService->isProctoredByDefault(); |
115 | } |
116 | |
117 | return $isProctored; |
118 | } |
119 | |
120 | /** |
121 | * @param DeliveryExecution $deliveryExecution |
122 | * @param string $state |
123 | * @return bool |
124 | * @throws common_Exception |
125 | */ |
126 | public function isActiveUnSecureDelivery(DeliveryExecution $deliveryExecution, $state) |
127 | { |
128 | return $state === DeliveryExecutionInterface::STATE_ACTIVE && !$this->isSecure($deliveryExecution); |
129 | } |
130 | |
131 | /** |
132 | * @param DeliveryExecution $deliveryExecution |
133 | * @return bool |
134 | * @throws common_Exception |
135 | */ |
136 | public function isSecure(DeliveryExecution $deliveryExecution) |
137 | { |
138 | $deliveryContainerService = $this->getServiceLocator()->get(DeliveryContainerService::SERVICE_ID); |
139 | $enabledPlugins = $deliveryContainerService->getPlugins($deliveryExecution); |
140 | |
141 | $secure = false; |
142 | foreach ($enabledPlugins as $plugin) { |
143 | if ($plugin instanceof TestPlugin && $plugin->getId() === 'blurPause') { |
144 | $secure = true; |
145 | break; |
146 | } |
147 | } |
148 | |
149 | return $secure; |
150 | } |
151 | |
152 | protected function isMissingProctorAuthorization(DeliveryExecutionInterface $deliveryExecution, User $user) |
153 | { |
154 | return $this->isProctored($deliveryExecution->getDelivery()->getUri(), $user) |
155 | && $deliveryExecution->getState()->getUri() !== ProctoredDeliveryExecution::STATE_AUTHORIZED; |
156 | } |
157 | |
158 | /** |
159 | * Throw the appropriate Exception |
160 | * |
161 | * @param DeliveryExecution $deliveryExecution |
162 | * @throws UnAuthorizedException |
163 | */ |
164 | protected function throwUnAuthorizedException(DeliveryExecution $deliveryExecution) |
165 | { |
166 | $errorPage = _url( |
167 | 'awaitingAuthorization', |
168 | 'DeliveryServer', |
169 | 'taoProctoring', |
170 | [ |
171 | 'deliveryExecution' => $deliveryExecution->getIdentifier(), |
172 | ] |
173 | ); |
174 | |
175 | throw new UnAuthorizedException($errorPage, 'Proctor authorization missing'); |
176 | } |
177 | |
178 | public function isSuitable(User $user, $deliveryId = null) |
179 | { |
180 | return true; |
181 | } |
182 | } |