|
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\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 reconfigureAction(Request $request) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->denyAccessUnlessGranted(['ROLE_MANAGEMENT']); |
|
48
|
|
|
|
|
49
|
|
|
$configuration = JsonHelper::decode($request->getContent()); |
|
50
|
|
|
|
|
51
|
|
|
$violations = $this->getValidator()->validate($configuration, new ValidReconfigureInstitutionsRequest()); |
|
52
|
|
|
if ($violations->count() > 0) { |
|
53
|
|
|
throw BadCommandRequestException::withViolations('Invalid reconfigure institutions request', $violations); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if (empty($configuration)) { |
|
57
|
|
|
$this->getLogger()->notice(sprintf('No institutions to reconfigure: empty configuration received')); |
|
58
|
|
|
|
|
59
|
|
|
return new JsonResponse([ |
|
60
|
|
|
'status' => 'OK', |
|
61
|
|
|
'processed_by' => $request->server->get('SERVER_NAME') ?: $request->server->get('SERVER_ADDR'), |
|
62
|
|
|
'applied_at' => (new DateTime())->format(DateTime::ISO8601), |
|
63
|
|
|
]); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$commands = []; |
|
67
|
|
|
foreach ($configuration as $institution => $options) { |
|
68
|
|
|
$command = new ReconfigureInstitutionConfigurationOptionsCommand(); |
|
69
|
|
|
$command->UUID = (string) Uuid::uuid4(); |
|
70
|
|
|
$command->institution = $institution; |
|
|
|
|
|
|
71
|
|
|
$command->useRaLocationsOption = $options['use_ra_locations']; |
|
72
|
|
|
$command->showRaaContactInformationOption = $options['show_raa_contact_information']; |
|
73
|
|
|
|
|
74
|
|
|
$commands[] = $command; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$this->getLogger()->notice( |
|
78
|
|
|
sprintf('Executing %s reconfigure institution configuration options commands', count($commands)) |
|
79
|
|
|
); |
|
80
|
|
|
|
|
81
|
|
|
$this->handleCommands($commands); |
|
82
|
|
|
|
|
83
|
|
|
return new JsonResponse([ |
|
84
|
|
|
'status' => 'OK', |
|
85
|
|
|
'processed_by' => $request->server->get('SERVER_NAME') ?: $request->server->get('SERVER_ADDR'), |
|
86
|
|
|
'applied_at' => (new DateTime())->format(DateTime::ISO8601), |
|
87
|
|
|
]); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param Command[] $commands |
|
92
|
|
|
* @throws Exception |
|
93
|
|
|
*/ |
|
94
|
|
|
private function handleCommands(array $commands) |
|
95
|
|
|
{ |
|
96
|
|
|
$pipeline = $this->getPipeline(); |
|
97
|
|
|
$connectionHelper = $this->getConnectionHelper(); |
|
98
|
|
|
|
|
99
|
|
|
$connectionHelper->beginTransaction(); |
|
100
|
|
|
|
|
101
|
|
|
foreach ($commands as $command) { |
|
102
|
|
|
try { |
|
103
|
|
|
$pipeline->process($command); |
|
104
|
|
|
} catch (ForbiddenException $e) { |
|
105
|
|
|
$connectionHelper->rollBack(); |
|
106
|
|
|
|
|
107
|
|
|
throw new AccessDeniedHttpException( |
|
108
|
|
|
sprintf('Processing of command "%s" is forbidden for this client', $command), |
|
109
|
|
|
$e |
|
110
|
|
|
); |
|
111
|
|
|
} catch (Exception $exception) { |
|
112
|
|
|
$connectionHelper->rollBack(); |
|
113
|
|
|
|
|
114
|
|
|
throw $exception; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$connectionHelper->commit(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return DataCollectingValidator |
|
123
|
|
|
*/ |
|
124
|
|
|
private function getValidator() |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->get('validator'); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return Logger |
|
131
|
|
|
*/ |
|
132
|
|
|
private function getLogger() |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->get('logger'); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @return Pipeline |
|
139
|
|
|
*/ |
|
140
|
|
|
private function getPipeline() |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->get('pipeline'); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @return DBALConnectionHelper |
|
147
|
|
|
*/ |
|
148
|
|
|
private function getConnectionHelper() |
|
149
|
|
|
{ |
|
150
|
|
|
return $this->get('surfnet_stepup_middleware_management.dbal_connection_helper'); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
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.