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
|
|
|
]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return new JsonResponse($overview); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function reconfigureAction(Request $request) |
118
|
|
|
{ |
119
|
|
|
$this->denyAccessUnlessGranted(['ROLE_MANAGEMENT']); |
120
|
|
|
|
121
|
|
|
$configuration = JsonHelper::decode($request->getContent()); |
122
|
|
|
|
123
|
|
|
$violations = $this->validator->validate($configuration, new ValidReconfigureInstitutionsRequest()); |
124
|
|
|
if ($violations->count() > 0) { |
125
|
|
|
throw BadCommandRequestException::withViolations('Invalid reconfigure institutions request', $violations); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if (empty($configuration)) { |
129
|
|
|
$this->logger->notice(sprintf('No institutions to reconfigure: empty configuration received')); |
130
|
|
|
|
131
|
|
|
return new JsonResponse([ |
132
|
|
|
'status' => 'OK', |
133
|
|
|
'processed_by' => $request->server->get('SERVER_NAME') ?: $request->server->get('SERVER_ADDR'), |
134
|
|
|
'applied_at' => (new DateTime())->format(DateTime::ISO8601), |
135
|
|
|
]); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$commands = []; |
139
|
|
|
foreach ($configuration as $institution => $options) { |
140
|
|
|
$command = new ReconfigureInstitutionConfigurationOptionsCommand(); |
141
|
|
|
$command->UUID = (string) Uuid::uuid4(); |
142
|
|
|
$command->institution = $institution; |
|
|
|
|
143
|
|
|
$command->useRaLocationsOption = $options['use_ra_locations']; |
144
|
|
|
$command->showRaaContactInformationOption = $options['show_raa_contact_information']; |
145
|
|
|
$command->verifyEmailOption = $options['verify_email']; |
146
|
|
|
$command->numberOfTokensPerIdentityOption = $options['number_of_tokens_per_identity']; |
147
|
|
|
$command->allowedSecondFactors = $options['allowed_second_factors']; |
148
|
|
|
|
149
|
|
|
$commands[] = $command; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$this->logger->notice( |
153
|
|
|
sprintf('Executing %s reconfigure institution configuration options commands', count($commands)) |
154
|
|
|
); |
155
|
|
|
|
156
|
|
|
$this->handleCommands($commands); |
157
|
|
|
|
158
|
|
|
return new JsonResponse([ |
159
|
|
|
'status' => 'OK', |
160
|
|
|
'processed_by' => $request->server->get('SERVER_NAME') ?: $request->server->get('SERVER_ADDR'), |
161
|
|
|
'applied_at' => (new DateTime())->format(DateTime::ISO8601), |
162
|
|
|
]); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param Command[] $commands |
167
|
|
|
* @throws Exception |
168
|
|
|
*/ |
169
|
|
|
private function handleCommands(array $commands) |
170
|
|
|
{ |
171
|
|
|
$connectionHelper = $this->connectionHelper; |
172
|
|
|
|
173
|
|
|
$connectionHelper->beginTransaction(); |
174
|
|
|
|
175
|
|
|
foreach ($commands as $command) { |
176
|
|
|
try { |
177
|
|
|
$this->pipeline->process($command); |
178
|
|
|
} catch (ForbiddenException $e) { |
179
|
|
|
$connectionHelper->rollBack(); |
180
|
|
|
|
181
|
|
|
throw new AccessDeniedHttpException( |
182
|
|
|
sprintf('Processing of command "%s" is forbidden for this client', $command), |
183
|
|
|
$e |
184
|
|
|
); |
185
|
|
|
} catch (Exception $exception) { |
186
|
|
|
$connectionHelper->rollBack(); |
187
|
|
|
|
188
|
|
|
throw $exception; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
$connectionHelper->commit(); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
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: