Passed
Push — bugfix/fix-deprovision-anonymi... ( f0980d...4f3cb3 )
by
unknown
05:23
created

DeprovisionService::deprovision()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 13
nc 2
nop 1
dl 0
loc 17
rs 9.8333
c 1
b 0
f 0
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
 */
0 ignored issues
show
Coding Style introduced by
Missing @link tag in file comment
Loading history...
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
class DeprovisionService implements DeprovisionServiceInterface
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class DeprovisionService
Loading history...
37
{
38
    public function __construct(
39
        private readonly Pipeline                                                                           $pipeline,
40
        private readonly IdentityRepository                                                                 $eventSourcingRepository,
41
        private readonly ApiIdentityRepository                                                              $apiRepository,
42
        private readonly LoggerInterface                                                                    $logger,
43
        private readonly SraaRepository                                                                     $sraaRepository,
44
        private readonly RaListingRepository                                                                $raListingRepository,
45
    ) {
46
    }
47
48
    public function readUserData(string $collabPersonId): array
49
    {
50
        try {
51
            $this->logger->debug(sprintf('Searching user identified by: %s', $collabPersonId));
52
            $identity = $this->getIdentityByNameId($collabPersonId);
53
            return $this->eventSourcingRepository->obtainInformation(new IdentityId($identity->id));
54
        } catch (UserNotFoundException $e) {
55
            $this->logger->notice(
56
                $e->getMessage(),
57
            );
58
            return [];
59
        }
60
    }
61
62
    public function deprovision(string $collabPersonId): void
63
    {
64
        $this->logger->debug(sprintf('Searching user identified by: %s', $collabPersonId));
65
        try {
66
            $user = $this->getIdentityByNameId($collabPersonId);
67
        } catch (UserNotFoundException $e) {
68
            $this->logger->notice(
69
                $e->getMessage(),
70
            );
71
            return;
72
        }
73
        $command = new ForgetIdentityCommand();
74
        $command->UUID = (string)Uuid::uuid4();
75
        $command->nameId = $collabPersonId;
76
        $command->institution = (string)$user->institution;
77
        $this->logger->debug('Processing the ForgetIdentityCommand');
78
        $this->pipeline->process($command);
79
    }
80
81
    private function getIdentityByNameId(string $collabPersonId): Identity
0 ignored issues
show
Coding Style introduced by
Private method name "DeprovisionService::getIdentityByNameId" must be prefixed with an underscore
Loading history...
82
    {
83
        $user = $this->apiRepository->findOneByNameId($collabPersonId);
84
        if (!$user instanceof Identity) {
85
            throw new UserNotFoundException(
86
                sprintf(
87
                    'User identified by: %s was not found. Unable to provide deprovision data.',
88
                    $collabPersonId,
89
                ),
90
            );
91
        }
92
        return $user;
93
    }
94
95
    public function assertIsAllowed(string $collabPersonId): void
96
    {
97
        $nameId = new NameId($collabPersonId);
98
        $identity = $this->apiRepository->findOneByNameId($nameId);
99
100
        if ($identity === null) {
101
            throw new RuntimeException('Cannot forget an identity that does not exist.');
102
        }
103
104
        if ($this->sraaRepository->contains($identity->nameId)) {
105
            throw new RuntimeException('Cannot forget an identity that is currently accredited as an SRAA');
106
        }
107
108
        if ($this->raListingRepository->contains(new IdentityId($identity->id))) {
109
            throw new RuntimeException('Cannot forget an identity that is currently accredited as an RA(A)');
110
        }
111
    }
112
}
113