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 Rhumsaa\Uuid\Uuid; |
25
|
|
|
use Surfnet\Stepup\Helper\JsonHelper; |
26
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Service\InstitutionConfigurationOptionsService; |
27
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Exception\BadCommandRequestException; |
28
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Command\Command; |
29
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\ReconfigureInstitutionConfigurationOptionsCommand; |
|
|
|
|
30
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Exception\ForbiddenException; |
31
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Pipeline\Pipeline; |
32
|
|
|
use Surfnet\StepupMiddleware\ManagementBundle\Service\DBALConnectionHelper; |
33
|
|
|
use Surfnet\StepupMiddleware\ManagementBundle\Validator\Constraints\ValidReconfigureInstitutionsRequest; |
34
|
|
|
use Symfony\Bridge\Monolog\Logger; |
35
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
36
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
37
|
|
|
use Symfony\Component\HttpFoundation\Request; |
38
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
42
|
|
|
*/ |
43
|
|
|
final class InstitutionConfigurationController extends Controller |
44
|
|
|
{ |
45
|
|
|
public function showAction() |
46
|
|
|
{ |
47
|
|
|
$this->denyAccessUnlessGranted(['ROLE_MANAGEMENT']); |
48
|
|
|
|
49
|
|
|
$institutionConfigurationOptions = $this->getInstitutionConfigurationOptionsService() |
|
|
|
|
50
|
|
|
->findAllInstitutionConfigurationOptions(); |
51
|
|
|
|
52
|
|
|
$overview = []; |
53
|
|
|
foreach ($institutionConfigurationOptions as $options) { |
54
|
|
|
$overview[$options->institution->getInstitution()] = [ |
55
|
|
|
'use_ra_locations' => $options->useRaLocationsOption, |
56
|
|
|
'show_raa_contact_information' => $options->showRaaContactInformationOption, |
57
|
|
|
]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return new JsonResponse($overview); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function reconfigureAction(Request $request) |
64
|
|
|
{ |
65
|
|
|
$this->denyAccessUnlessGranted(['ROLE_MANAGEMENT']); |
66
|
|
|
|
67
|
|
|
$configuration = JsonHelper::decode($request->getContent()); |
68
|
|
|
|
69
|
|
|
$violations = $this->getValidator()->validate($configuration, new ValidReconfigureInstitutionsRequest()); |
70
|
|
|
if ($violations->count() > 0) { |
71
|
|
|
throw BadCommandRequestException::withViolations('Invalid reconfigure institutions request', $violations); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (empty($configuration)) { |
75
|
|
|
$this->getLogger()->notice(sprintf('No institutions to reconfigure: empty configuration received')); |
76
|
|
|
|
77
|
|
|
return new JsonResponse([ |
78
|
|
|
'status' => 'OK', |
79
|
|
|
'processed_by' => $request->server->get('SERVER_NAME') ?: $request->server->get('SERVER_ADDR'), |
80
|
|
|
'applied_at' => (new DateTime())->format(DateTime::ISO8601), |
81
|
|
|
]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$commands = []; |
85
|
|
|
foreach ($configuration as $institution => $options) { |
86
|
|
|
$command = new ReconfigureInstitutionConfigurationOptionsCommand(); |
87
|
|
|
$command->UUID = (string) Uuid::uuid4(); |
88
|
|
|
$command->institution = $institution; |
|
|
|
|
89
|
|
|
$command->useRaLocationsOption = $options['use_ra_locations']; |
90
|
|
|
$command->showRaaContactInformationOption = $options['show_raa_contact_information']; |
91
|
|
|
|
92
|
|
|
$commands[] = $command; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$this->getLogger()->notice( |
96
|
|
|
sprintf('Executing %s reconfigure institution configuration options commands', count($commands)) |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
$this->handleCommands($commands); |
100
|
|
|
|
101
|
|
|
return new JsonResponse([ |
102
|
|
|
'status' => 'OK', |
103
|
|
|
'processed_by' => $request->server->get('SERVER_NAME') ?: $request->server->get('SERVER_ADDR'), |
104
|
|
|
'applied_at' => (new DateTime())->format(DateTime::ISO8601), |
105
|
|
|
]); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param Command[] $commands |
110
|
|
|
* @throws Exception |
111
|
|
|
*/ |
112
|
|
|
private function handleCommands(array $commands) |
113
|
|
|
{ |
114
|
|
|
$pipeline = $this->getPipeline(); |
115
|
|
|
$connectionHelper = $this->getConnectionHelper(); |
116
|
|
|
|
117
|
|
|
$connectionHelper->beginTransaction(); |
118
|
|
|
|
119
|
|
|
foreach ($commands as $command) { |
120
|
|
|
try { |
121
|
|
|
$pipeline->process($command); |
122
|
|
|
} catch (ForbiddenException $e) { |
123
|
|
|
$connectionHelper->rollBack(); |
124
|
|
|
|
125
|
|
|
throw new AccessDeniedHttpException( |
126
|
|
|
sprintf('Processing of command "%s" is forbidden for this client', $command), |
127
|
|
|
$e |
128
|
|
|
); |
129
|
|
|
} catch (Exception $exception) { |
130
|
|
|
$connectionHelper->rollBack(); |
131
|
|
|
|
132
|
|
|
throw $exception; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$connectionHelper->commit(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return InstitutionConfigurationOptionsService |
141
|
|
|
*/ |
142
|
|
|
private function getInstitutionConfigurationOptionsService() |
143
|
|
|
{ |
144
|
|
|
return $this->get('surfnet_stepup_middleware_api.service.institution_configuration_options'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return DataCollectingValidator |
149
|
|
|
*/ |
150
|
|
|
private function getValidator() |
151
|
|
|
{ |
152
|
|
|
return $this->get('validator'); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return Logger |
157
|
|
|
*/ |
158
|
|
|
private function getLogger() |
159
|
|
|
{ |
160
|
|
|
return $this->get('logger'); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return Pipeline |
165
|
|
|
*/ |
166
|
|
|
private function getPipeline() |
167
|
|
|
{ |
168
|
|
|
return $this->get('pipeline'); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @return DBALConnectionHelper |
173
|
|
|
*/ |
174
|
|
|
private function getConnectionHelper() |
175
|
|
|
{ |
176
|
|
|
return $this->get('surfnet_stepup_middleware_management.dbal_connection_helper'); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.