1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2022 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\ApiBundle\Service; |
20
|
|
|
|
21
|
|
|
use Psr\Log\LoggerInterface; |
22
|
|
|
use Ramsey\Uuid\Uuid; |
23
|
|
|
use RuntimeException; |
24
|
|
|
use Surfnet\Stepup\Identity\EventSourcing\IdentityRepository; |
25
|
|
|
use Surfnet\Stepup\Identity\Value\IdentityId; |
26
|
|
|
use Surfnet\Stepup\Identity\Value\NameId; |
27
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Exception\UserNotFoundException; |
28
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\Identity; |
29
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\IdentityRepository as ApiIdentityRepository; |
30
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\RaListingRepository; |
31
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\SraaRepository; |
32
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\ForgetIdentityCommand; |
33
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Pipeline\Pipeline; |
34
|
|
|
use function sprintf; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
38
|
|
|
*/ |
|
|
|
|
39
|
|
|
class DeprovisionService implements DeprovisionServiceInterface |
40
|
|
|
{ |
41
|
|
|
public function __construct( |
42
|
|
|
private readonly Pipeline $pipeline, |
43
|
|
|
private readonly IdentityRepository $eventSourcingRepository, |
44
|
|
|
private readonly ApiIdentityRepository $apiRepository, |
45
|
|
|
private readonly LoggerInterface $logger, |
46
|
|
|
private readonly SraaRepository $sraaRepository, |
47
|
|
|
private readonly RaListingRepository $raListingRepository, |
48
|
|
|
) { |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function readUserData(string $collabPersonId): array |
52
|
|
|
{ |
53
|
|
|
try { |
54
|
|
|
$this->logger->debug(sprintf('Searching user identified by: %s', $collabPersonId)); |
55
|
|
|
$identity = $this->getIdentityByNameId($collabPersonId); |
56
|
|
|
return $this->eventSourcingRepository->obtainInformation(new IdentityId($identity->id)); |
57
|
|
|
} catch (UserNotFoundException $e) { |
58
|
|
|
$this->logger->notice( |
59
|
|
|
$e->getMessage(), |
60
|
|
|
); |
61
|
|
|
return []; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function deprovision(string $collabPersonId): void |
66
|
|
|
{ |
67
|
|
|
$this->logger->debug(sprintf('Searching user identified by: %s', $collabPersonId)); |
68
|
|
|
try { |
69
|
|
|
$user = $this->getIdentityByNameId($collabPersonId); |
70
|
|
|
} catch (UserNotFoundException $e) { |
71
|
|
|
$this->logger->notice( |
72
|
|
|
$e->getMessage(), |
73
|
|
|
); |
74
|
|
|
return; |
75
|
|
|
} |
76
|
|
|
$command = new ForgetIdentityCommand(); |
77
|
|
|
$command->UUID = (string)Uuid::uuid4(); |
78
|
|
|
$command->nameId = $collabPersonId; |
79
|
|
|
$command->institution = (string)$user->institution; |
80
|
|
|
$this->logger->debug('Processing the ForgetIdentityCommand'); |
81
|
|
|
$this->pipeline->process($command); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function getIdentityByNameId(string $collabPersonId): Identity |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
$user = $this->apiRepository->findOneByNameId($collabPersonId); |
87
|
|
|
if (!$user instanceof Identity) { |
88
|
|
|
throw new UserNotFoundException( |
89
|
|
|
sprintf( |
90
|
|
|
'User identified by: %s was not found. Unable to provide deprovision data.', |
91
|
|
|
$collabPersonId, |
92
|
|
|
), |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
return $user; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function assertIsAllowed(string $collabPersonId): void |
99
|
|
|
{ |
100
|
|
|
$nameId = new NameId($collabPersonId); |
101
|
|
|
$identity = $this->apiRepository->findOneByNameId($nameId); |
102
|
|
|
|
103
|
|
|
if ($identity === null) { |
104
|
|
|
throw new RuntimeException('Cannot forget an identity that does not exist.'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if ($this->sraaRepository->contains($identity->nameId)) { |
108
|
|
|
throw new RuntimeException('Cannot forget an identity that is currently accredited as an SRAA'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if ($this->raListingRepository->contains(new IdentityId($identity->id))) { |
112
|
|
|
throw new RuntimeException('Cannot forget an identity that is currently accredited as an RA(A)'); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|