|
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 GuzzleHttp; |
|
23
|
|
|
use Rhumsaa\Uuid\Uuid; |
|
24
|
|
|
use Surfnet\Stepup\Identity\Value\Institution; |
|
25
|
|
|
use Surfnet\Stepup\Identity\Value\NameId; |
|
26
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Command\Command; |
|
27
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\ForgetIdentityCommand; |
|
28
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
29
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
30
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
31
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
|
32
|
|
|
use Symfony\Component\HttpKernel\Exception\ConflictHttpException; |
|
33
|
|
|
|
|
34
|
|
|
class RightToBeForgottenController extends Controller |
|
35
|
|
|
{ |
|
36
|
|
|
public function forgetIdentityAction(Request $request) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->denyAccessUnlessGranted(['ROLE_MANAGEMENT']); |
|
39
|
|
|
|
|
40
|
|
|
$payload = GuzzleHttp\json_decode($request->getContent(), true); |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
if (!isset($payload['name_id'])) { |
|
43
|
|
|
throw new BadRequestHttpException('Please specify a NameID in the property "name_id"'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
if (!isset($payload['institution'])) { |
|
47
|
|
|
throw new BadRequestHttpException('Please specify an institution in the property "institution"'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$this->assertMayForget(new NameId($payload['name_id']), new Institution($payload['institution'])); |
|
51
|
|
|
|
|
52
|
|
|
$command = new ForgetIdentityCommand(); |
|
53
|
|
|
$command->UUID = (string) Uuid::uuid4(); |
|
54
|
|
|
$command->nameId = $payload['name_id']; |
|
55
|
|
|
$command->institution = $payload['institution']; |
|
56
|
|
|
|
|
57
|
|
|
return $this->handleCommand($request, $command); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param Request $request |
|
62
|
|
|
* @param Command $command |
|
63
|
|
|
* @return JsonResponse |
|
64
|
|
|
*/ |
|
65
|
|
View Code Duplication |
private function handleCommand(Request $request, Command $command) |
|
|
|
|
|
|
66
|
|
|
{ |
|
67
|
|
|
/** @var \Surfnet\StepupMiddleware\CommandHandlingBundle\Pipeline\Pipeline $pipeline */ |
|
68
|
|
|
$pipeline = $this->get('pipeline'); |
|
69
|
|
|
$pipeline->process($command); |
|
70
|
|
|
|
|
71
|
|
|
$serverName = $request->server->get('SERVER_NAME') ?: $request->server->get('SERVER_ADDR'); |
|
72
|
|
|
$response = new JsonResponse([ |
|
73
|
|
|
'status' => 'OK', |
|
74
|
|
|
'processed_by' => $serverName, |
|
75
|
|
|
'applied_at' => (new DateTime())->format(DateTime::ISO8601) |
|
76
|
|
|
]); |
|
77
|
|
|
|
|
78
|
|
|
return $response; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param NameId $nameId |
|
83
|
|
|
* @param Institution $institution |
|
84
|
|
|
* @throws ConflictHttpException |
|
85
|
|
|
*/ |
|
86
|
|
|
private function assertMayForget(NameId $nameId, Institution $institution) |
|
87
|
|
|
{ |
|
88
|
|
|
$identityService = $this->get('surfnet_stepup_middleware_api.service.identity'); |
|
89
|
|
|
$credentials = |
|
90
|
|
|
$identityService->findRegistrationAuthorityCredentialsByNameIdAndInstitution($nameId, $institution); |
|
91
|
|
|
|
|
92
|
|
|
if ($credentials === null) { |
|
93
|
|
|
return; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if ($credentials->isSraa()) { |
|
97
|
|
|
throw new ConflictHttpException( |
|
98
|
|
|
'Identity is currently configured to act as an SRAA. ' . |
|
99
|
|
|
'Remove its NameID from the configuration and try again.' |
|
100
|
|
|
); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
if ($credentials->isRaa()) { |
|
104
|
|
|
$role = 'RAA'; |
|
105
|
|
|
} else { |
|
106
|
|
|
$role = 'RA'; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
throw new ConflictHttpException(sprintf( |
|
110
|
|
|
'Identity is currently accredited as an %s. Retract the accreditation and try again.', |
|
111
|
|
|
$role |
|
112
|
|
|
)); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.