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