|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2016 SURFnet B.V. |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace Surfnet\StepupMiddleware\ManagementBundle\Controller; |
|
20
|
|
|
|
|
21
|
|
|
use DateTime; |
|
22
|
|
|
use Exception; |
|
23
|
|
|
use Liip\FunctionalTestBundle\Validator\DataCollectingValidator; |
|
24
|
|
|
use Psr\Log\LoggerInterface; |
|
25
|
|
|
use Rhumsaa\Uuid\Uuid; |
|
26
|
|
|
use Surfnet\Stepup\Configuration\Value\Institution; |
|
27
|
|
|
use Surfnet\Stepup\Helper\JsonHelper; |
|
28
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Service\AllowedSecondFactorListService; |
|
29
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Service\InstitutionConfigurationOptionsService; |
|
30
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Exception\BadCommandRequestException; |
|
31
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Command\Command; |
|
32
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\ReconfigureInstitutionConfigurationOptionsCommand; |
|
33
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Exception\ForbiddenException; |
|
34
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Pipeline\TransactionAwarePipeline; |
|
35
|
|
|
use Surfnet\StepupMiddleware\ManagementBundle\Service\DBALConnectionHelper; |
|
36
|
|
|
use Surfnet\StepupMiddleware\ManagementBundle\Validator\Constraints\ValidReconfigureInstitutionsRequest; |
|
37
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
38
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
39
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
40
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
|
44
|
|
|
*/ |
|
45
|
|
|
final class InstitutionConfigurationController extends Controller |
|
46
|
|
|
{ |
|
47
|
|
|
/** |
|
48
|
|
|
* @return InstitutionConfigurationOptionsService |
|
49
|
|
|
*/ |
|
50
|
|
|
private $institutionConfigurationOptionsService; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return DataCollectingValidator |
|
54
|
|
|
*/ |
|
55
|
|
|
private $validator; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return AllowedSecondFactorListService |
|
59
|
|
|
*/ |
|
60
|
|
|
private $allowedSecondFactorListService; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return LoggerInterface |
|
64
|
|
|
*/ |
|
65
|
|
|
private $logger; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return TransactionAwarePipeline |
|
69
|
|
|
*/ |
|
70
|
|
|
private $pipeline; |
|
71
|
|
|
|
|
72
|
|
|
public function __construct( |
|
73
|
|
|
InstitutionConfigurationOptionsService $institutionConfigurationOptionsService, |
|
74
|
|
|
DataCollectingValidator $dataCollectingValidator, |
|
75
|
|
|
AllowedSecondFactorListService $allowedSecondFactorListService, |
|
76
|
|
|
LoggerInterface $logger, |
|
77
|
|
|
TransactionAwarePipeline $pipeline, |
|
78
|
|
|
DBALConnectionHelper $dbalConnectionHelper |
|
79
|
|
|
) { |
|
80
|
|
|
$this->institutionConfigurationOptionsService = $institutionConfigurationOptionsService; |
|
81
|
|
|
$this->validator = $dataCollectingValidator; |
|
82
|
|
|
$this->allowedSecondFactorListService = $allowedSecondFactorListService; |
|
83
|
|
|
$this->logger = $logger; |
|
84
|
|
|
$this->pipeline = $pipeline; |
|
85
|
|
|
$this->connectionHelper = $dbalConnectionHelper; |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function showAction() |
|
89
|
|
|
{ |
|
90
|
|
|
$this->denyAccessUnlessGranted(['ROLE_MANAGEMENT']); |
|
91
|
|
|
|
|
92
|
|
|
$institutionConfigurationOptions = $this->institutionConfigurationOptionsService |
|
93
|
|
|
->findAllInstitutionConfigurationOptions(); |
|
94
|
|
|
|
|
95
|
|
|
$allowedSecondFactorMap = $this->allowedSecondFactorListService->getAllowedSecondFactorMap(); |
|
96
|
|
|
|
|
97
|
|
|
$overview = []; |
|
98
|
|
|
foreach ($institutionConfigurationOptions as $options) { |
|
99
|
|
|
// Load the numberOfTokensPerIdentity from the institution config options service |
|
100
|
|
|
$numberOfTokensPerIdentity = $this->institutionConfigurationOptionsService |
|
101
|
|
|
->getMaxNumberOfTokensFor(new Institution($options->institution->getInstitution())); |
|
102
|
|
|
|
|
103
|
|
|
$overview[$options->institution->getInstitution()] = [ |
|
104
|
|
|
'use_ra_locations' => $options->useRaLocationsOption, |
|
105
|
|
|
'show_raa_contact_information' => $options->showRaaContactInformationOption, |
|
106
|
|
|
'verify_email' => $options->verifyEmailOption, |
|
107
|
|
|
'number_of_tokens_per_identity' => $numberOfTokensPerIdentity, |
|
108
|
|
|
'allowed_second_factors' => $allowedSecondFactorMap->getAllowedSecondFactorListFor( |
|
109
|
|
|
$options->institution |
|
110
|
|
|
), |
|
111
|
|
|
'use_ra' => $options->useRaOption, |
|
112
|
|
|
'use_raa' => $options->useRaaOption, |
|
113
|
|
|
'select_raa' => $options->selectRaaOption, |
|
114
|
|
|
]; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return new JsonResponse($overview); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function reconfigureAction(Request $request) |
|
121
|
|
|
{ |
|
122
|
|
|
$this->denyAccessUnlessGranted(['ROLE_MANAGEMENT']); |
|
123
|
|
|
|
|
124
|
|
|
$configuration = JsonHelper::decode($request->getContent()); |
|
125
|
|
|
|
|
126
|
|
|
$violations = $this->validator->validate($configuration, new ValidReconfigureInstitutionsRequest()); |
|
127
|
|
|
if ($violations->count() > 0) { |
|
128
|
|
|
throw BadCommandRequestException::withViolations('Invalid reconfigure institutions request', $violations); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
if (empty($configuration)) { |
|
132
|
|
|
$this->logger->notice(sprintf('No institutions to reconfigure: empty configuration received')); |
|
133
|
|
|
|
|
134
|
|
|
return new JsonResponse([ |
|
135
|
|
|
'status' => 'OK', |
|
136
|
|
|
'processed_by' => $request->server->get('SERVER_NAME') ?: $request->server->get('SERVER_ADDR'), |
|
137
|
|
|
'applied_at' => (new DateTime())->format(DateTime::ISO8601), |
|
138
|
|
|
]); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$commands = []; |
|
142
|
|
|
foreach ($configuration as $institution => $options) { |
|
143
|
|
|
$command = new ReconfigureInstitutionConfigurationOptionsCommand(); |
|
144
|
|
|
$command->UUID = (string) Uuid::uuid4(); |
|
145
|
|
|
$command->institution = $institution; |
|
|
|
|
|
|
146
|
|
|
$command->useRaLocationsOption = $options['use_ra_locations']; |
|
147
|
|
|
$command->showRaaContactInformationOption = $options['show_raa_contact_information']; |
|
148
|
|
|
$command->verifyEmailOption = $options['verify_email']; |
|
149
|
|
|
$command->numberOfTokensPerIdentityOption = $options['number_of_tokens_per_identity']; |
|
150
|
|
|
$command->allowedSecondFactors = $options['allowed_second_factors']; |
|
151
|
|
|
|
|
152
|
|
|
// The useRa, useRaa and selectRaa options are optional |
|
153
|
|
|
$command->useRaOption = isset($options['use_ra']) ? $options['use_ra'] : null; |
|
154
|
|
|
$command->useRaaOption = isset($options['use_raa']) ? $options['use_raa'] : null; |
|
155
|
|
|
$command->selectRaaOption = isset($options['select_raa']) ? $options['select_raa'] : null; |
|
156
|
|
|
|
|
157
|
|
|
$commands[] = $command; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
$this->logger->notice( |
|
161
|
|
|
sprintf('Executing %s reconfigure institution configuration options commands', count($commands)) |
|
162
|
|
|
); |
|
163
|
|
|
|
|
164
|
|
|
$this->handleCommands($commands); |
|
165
|
|
|
|
|
166
|
|
|
return new JsonResponse([ |
|
167
|
|
|
'status' => 'OK', |
|
168
|
|
|
'processed_by' => $request->server->get('SERVER_NAME') ?: $request->server->get('SERVER_ADDR'), |
|
169
|
|
|
'applied_at' => (new DateTime())->format(DateTime::ISO8601), |
|
170
|
|
|
]); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @param Command[] $commands |
|
175
|
|
|
* @throws Exception |
|
176
|
|
|
*/ |
|
177
|
|
|
private function handleCommands(array $commands) |
|
178
|
|
|
{ |
|
179
|
|
|
$connectionHelper = $this->connectionHelper; |
|
180
|
|
|
|
|
181
|
|
|
$connectionHelper->beginTransaction(); |
|
182
|
|
|
|
|
183
|
|
|
foreach ($commands as $command) { |
|
184
|
|
|
try { |
|
185
|
|
|
$this->pipeline->process($command); |
|
186
|
|
|
} catch (ForbiddenException $e) { |
|
187
|
|
|
$connectionHelper->rollBack(); |
|
188
|
|
|
|
|
189
|
|
|
throw new AccessDeniedHttpException( |
|
190
|
|
|
sprintf('Processing of command "%s" is forbidden for this client', $command), |
|
191
|
|
|
$e |
|
192
|
|
|
); |
|
193
|
|
|
} catch (Exception $exception) { |
|
194
|
|
|
$connectionHelper->rollBack(); |
|
195
|
|
|
|
|
196
|
|
|
throw $exception; |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
$connectionHelper->commit(); |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: