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 Rhumsaa\Uuid\Uuid; |
23
|
|
|
use Surfnet\Stepup\Helper\JsonHelper; |
24
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Service\WhitelistService; |
25
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Command\Command; |
26
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Exception\ForbiddenException; |
27
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\AddToWhitelistCommand; |
28
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\RemoveFromWhitelistCommand; |
29
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\ReplaceWhitelistCommand; |
30
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Pipeline\TransactionAwarePipeline; |
31
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
32
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
33
|
|
|
use Symfony\Component\HttpFoundation\Request; |
34
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
35
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
39
|
|
|
*/ |
40
|
|
|
class WhitelistController extends Controller |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* @return TransactionAwarePipeline |
44
|
|
|
*/ |
45
|
|
|
private $pipeline; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var WhitelistService |
49
|
|
|
*/ |
50
|
|
|
private $whitelistService; |
51
|
|
|
|
52
|
|
|
public function __construct(TransactionAwarePipeline $pipeline, WhitelistService $whitelistService) |
53
|
|
|
{ |
54
|
|
|
$this->pipeline = $pipeline; |
55
|
|
|
$this->whitelistService = $whitelistService; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
View Code Duplication |
public function replaceWhitelistAction(Request $request) |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$this->denyAccessUnlessGranted(['ROLE_MANAGEMENT']); |
61
|
|
|
|
62
|
|
|
$command = new ReplaceWhitelistCommand(); |
63
|
|
|
$command->UUID = (string) Uuid::uuid4(); |
64
|
|
|
$command->institutions = $this->getInstitutionsFromBody($request); |
65
|
|
|
|
66
|
|
|
return $this->handleCommand($request, $command); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
View Code Duplication |
public function addToWhitelistAction(Request $request) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$this->denyAccessUnlessGranted(['ROLE_MANAGEMENT']); |
72
|
|
|
|
73
|
|
|
$command = new AddToWhitelistCommand(); |
74
|
|
|
$command->UUID = (string) Uuid::uuid4(); |
75
|
|
|
$command->institutionsToBeAdded = $this->getInstitutionsFromBody($request); |
76
|
|
|
|
77
|
|
|
return $this->handleCommand($request, $command); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
View Code Duplication |
public function removeFromWhitelistAction(Request $request) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$this->denyAccessUnlessGranted(['ROLE_MANAGEMENT']); |
83
|
|
|
|
84
|
|
|
$command = new RemoveFromWhitelistCommand(); |
85
|
|
|
$command->UUID = (string) Uuid::uuid4(); |
86
|
|
|
$command->institutionsToBeRemoved = $this->getInstitutionsFromBody($request); |
87
|
|
|
|
88
|
|
|
return $this->handleCommand($request, $command); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function showWhitelistAction() |
92
|
|
|
{ |
93
|
|
|
$entries = $this->whitelistService->getAllEntries(); |
94
|
|
|
|
95
|
|
|
return new JsonResponse(['institutions' => $entries->getValues()]); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param Request $request |
100
|
|
|
* @param Command $command |
101
|
|
|
* @return JsonResponse |
102
|
|
|
*/ |
103
|
|
|
private function handleCommand(Request $request, Command $command) |
104
|
|
|
{ |
105
|
|
|
try { |
106
|
|
|
$this->pipeline->process($command); |
107
|
|
|
} catch (ForbiddenException $e) { |
108
|
|
|
throw new AccessDeniedHttpException( |
109
|
|
|
sprintf('Processing of command "%s" is forbidden for this client', $command), |
110
|
|
|
$e |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$serverName = $request->server->get('SERVER_NAME') ?: $request->server->get('SERVER_ADDR'); |
115
|
|
|
$response = new JsonResponse([ |
116
|
|
|
'status' => 'OK', |
117
|
|
|
'processed_by' => $serverName, |
118
|
|
|
'applied_at' => (new DateTime())->format(DateTime::ISO8601), |
119
|
|
|
]); |
120
|
|
|
|
121
|
|
|
return $response; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param Request $request |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
|
|
private function getInstitutionsFromBody(Request $request) |
129
|
|
|
{ |
130
|
|
|
$decoded = JsonHelper::decode($request->getContent()); |
131
|
|
|
|
132
|
|
|
if (!isset($decoded['institutions']) || !is_array($decoded['institutions'])) { |
133
|
|
|
throw new BadRequestHttpException( |
134
|
|
|
'Request must contain json object with property "institutions" containing an array of institutions' |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $decoded['institutions']; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.