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 GuzzleHttp; |
23
|
|
|
use Liip\FunctionalTestBundle\Validator\DataCollectingValidator; |
24
|
|
|
use Rhumsaa\Uuid\Uuid; |
25
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Command\Command; |
26
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\ReconfigureInstitutionConfigurationOptionsCommand; |
|
|
|
|
27
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Exception\ForbiddenException; |
28
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Pipeline\Pipeline; |
29
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Pipeline\TransactionAwarePipeline; |
30
|
|
|
use Surfnet\StepupMiddleware\ManagementBundle\Validator\Constraints\ValidReconfigureInstitutionsRequest; |
31
|
|
|
use Symfony\Bridge\Monolog\Logger; |
32
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
33
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
34
|
|
|
use Symfony\Component\HttpFoundation\Request; |
35
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
36
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
37
|
|
|
use Symfony\Component\Validator\ConstraintViolation; |
38
|
|
|
|
39
|
|
|
final class InstitutionConfigurationController extends Controller |
40
|
|
|
{ |
41
|
|
|
public function reconfigureAction(Request $request) |
42
|
|
|
{ |
43
|
|
|
$this->denyAccessUnlessGranted(['ROLE_MANAGEMENT']); |
44
|
|
|
|
45
|
|
|
$configuration = GuzzleHttp\json_decode($request->getContent(), true); |
|
|
|
|
46
|
|
|
|
47
|
|
|
$violations = $this->getValidator()->validate($configuration, new ValidReconfigureInstitutionsRequest()); |
48
|
|
|
if ($violations->count() > 0) { |
49
|
|
|
$errors = array_map(function (ConstraintViolation $violation) { |
50
|
|
|
return sprintf('%s: %s', $violation->getPropertyPath(), $violation->getMessage()); |
51
|
|
|
}, iterator_to_array($violations)); |
52
|
|
|
|
53
|
|
|
return new JsonResponse(['errors' => $errors], 400); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$commands = []; |
57
|
|
|
foreach ($configuration as $institution => $options) { |
58
|
|
|
$command = new ReconfigureInstitutionConfigurationOptionsCommand(); |
59
|
|
|
$command->UUID = (string) Uuid::uuid4(); |
60
|
|
|
$command->institution = $institution; |
|
|
|
|
61
|
|
|
$command->useRaLocationsOption = $options['use_ra_locations']; |
62
|
|
|
$command->showRaaContactInformationOption = $options['show_raa_contact_information']; |
63
|
|
|
|
64
|
|
|
$commands[] = $command; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (empty($commands)) { |
68
|
|
|
$this->getLogger()->notice('Institution configuration will not be reconfigured: no commands to execute.'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$pipeline = $this->getPipeline(); |
72
|
|
|
foreach ($commands as $command) { |
73
|
|
|
$this->handleCommand($pipeline, $command); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$serverName = $request->server->get('SERVER_NAME') ?: $request->server->get('SERVER_ADDR'); |
77
|
|
|
$response = new JsonResponse([ |
78
|
|
|
'status' => 'OK', |
79
|
|
|
'processed_by' => $serverName, |
80
|
|
|
'applied_at' => (new DateTime())->format(DateTime::ISO8601), |
81
|
|
|
]); |
82
|
|
|
|
83
|
|
|
return $response; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param Pipeline $pipeline |
88
|
|
|
* @param Command $command |
89
|
|
|
* @return JsonResponse |
|
|
|
|
90
|
|
|
*/ |
91
|
|
|
private function handleCommand(Pipeline $pipeline, Command $command) |
92
|
|
|
{ |
93
|
|
|
try { |
94
|
|
|
$pipeline->process($command); |
95
|
|
|
} catch (ForbiddenException $e) { |
96
|
|
|
throw new AccessDeniedHttpException( |
97
|
|
|
sprintf('Processing of command "%s" is forbidden for this client', $command), |
98
|
|
|
$e |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return DataCollectingValidator |
105
|
|
|
*/ |
106
|
|
|
private function getValidator() |
107
|
|
|
{ |
108
|
|
|
return $this->container->get('validator'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return Logger |
113
|
|
|
*/ |
114
|
|
|
private function getLogger() |
115
|
|
|
{ |
116
|
|
|
return $this->get('logger'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return TransactionAwarePipeline |
121
|
|
|
*/ |
122
|
|
|
private function getPipeline() |
123
|
|
|
{ |
124
|
|
|
return $this->get('pipeline'); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
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.