1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2014 SURFnet bv |
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 Ramsey\Uuid\Uuid; |
23
|
|
|
use Surfnet\Stepup\Helper\JsonHelper; |
24
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Exception\BadCommandRequestException; |
25
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Command\AbstractCommand; |
26
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\UpdateConfigurationCommand; |
27
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Pipeline\TransactionAwarePipeline; |
28
|
|
|
use Surfnet\StepupMiddleware\ManagementBundle\Validator\Constraints\HasValidConfigurationStructure; |
29
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
30
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
31
|
|
|
use Symfony\Component\HttpFoundation\Request; |
32
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
33
|
|
|
|
34
|
|
|
class ConfigurationController extends AbstractController |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
public function __construct( |
37
|
|
|
private readonly TransactionAwarePipeline $pipeline, |
38
|
|
|
private readonly ValidatorInterface $validator, |
39
|
|
|
) { |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function update(Request $request): JsonResponse |
43
|
|
|
{ |
44
|
|
|
$this->denyAccessUnlessGranted('ROLE_MANAGEMENT'); |
45
|
|
|
|
46
|
|
|
$violations = $this->validator->validate($request->getContent(), new HasValidConfigurationStructure()); |
47
|
|
|
if ($violations->count() > 0) { |
48
|
|
|
throw BadCommandRequestException::withViolations('Invalid configure institutions request', $violations); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$command = new UpdateConfigurationCommand(); |
52
|
|
|
$command->configuration = $request->getContent(); |
53
|
|
|
$command->UUID = (string)Uuid::uuid4(); |
54
|
|
|
|
55
|
|
|
return $this->handleCommand($request, $command); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private function handleCommand(Request $request, AbstractCommand $command): JsonResponse |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$this->pipeline->process($command); |
61
|
|
|
|
62
|
|
|
$serverName = $request->server->get('SERVER_NAME') ?: $request->server->get('SERVER_ADDR'); |
63
|
|
|
|
64
|
|
|
return new JsonResponse([ |
|
|
|
|
65
|
|
|
'status' => 'OK', |
66
|
|
|
'processed_by' => $serverName, |
67
|
|
|
'applied_at' => (new DateTime())->format(DateTime::ISO8601), |
68
|
|
|
]); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|