Passed
Push — bugfix/authz-ra-determination-... ( 33b0b5 )
by Michiel
15:54
created

ProfileService::getRoleFromListing()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 8
rs 10
1
<?php
2
3
/**
4
 * Copyright 2019 SURFnet B.V.
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\Identity\Service;
20
21
use Psr\Log\LoggerInterface;
22
use Surfnet\Stepup\Configuration\Value\InstitutionRole;
23
use Surfnet\Stepup\Identity\Value\IdentityId;
24
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Service\AuthorizationContextService;
25
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\RaListing;
26
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\RaListingRepository;
27
use Surfnet\StepupMiddleware\ApiBundle\Identity\Value\AuthorityRole;
28
use Surfnet\StepupMiddleware\ApiBundle\Identity\Value\AuthorizedInstitutionCollection;
29
use Surfnet\StepupMiddleware\ApiBundle\Identity\Value\Profile;
30
use function sprintf;
31
32
class ProfileService extends AbstractSearchService
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class ProfileService
Loading history...
33
{
34
    /**
35
     * @var RaListingRepository
36
     */
37
    private $raListingRepository;
38
39
    /**
40
     * @var IdentityService
41
     */
42
    private $identityService;
43
44
    /**
45
     * @var AuthorizationContextService
46
     */
47
    private $authorizationService;
48
49
    /**
50
     * @var LoggerInterface
51
     */
52
    private $logger;
53
54
    public function __construct(
55
        RaListingRepository $raListingRepository,
56
        IdentityService $identityService,
57
        AuthorizationContextService $institutionAuthorizationService,
58
        LoggerInterface $logger
59
    ) {
60
        $this->raListingRepository = $raListingRepository;
61
        $this->identityService = $identityService;
62
        $this->authorizationService = $institutionAuthorizationService;
63
        $this->logger = $logger;
64
    }
65
66
    /**
67
     * Uses the identityId to first load the ra credentials (if present)
68
     * These credentials are then used to test what type of administrator we are dealing with ((S)RA(A)). Next the
69
     * authorizations are retrieved from the InstitutionAuthorizationRepository. Not that we distinguish between
70
     * implicit and explicitly appointed roles. The implicit roles are based on the institution configuration
71
     * (SELECT_RAA). Finally identity is retrieved for the provided identityId. This data is then merged in a Profile
72
     * value object.
73
     *
74
     * When the profile is incorrect, for example because no identity can be found, null is returned instead of a
75
     * Profile. Its possible to retrieve profile data for a non RA user, in that case no authorization data is set
76
     * on the profile. The same goes for the SRAA user. As that user is allowed all authorizations for all institutions.
77
     * An additional isSraa flag is set to true for these administrators.
78
     *
79
     * @param string $identityId
0 ignored issues
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
80
     * @return Profile|null
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
81
     */
82
    public function createProfile($identityId)
83
    {
84
        $identity = $this->identityService->find($identityId);
85
86
        if ($identity === null) {
87
            $this->logger->notice(sprintf('No Identity found with IdentityId %s', $identityId));
88
            return null;
89
        }
90
        $this->logger->notice(sprintf('Found IdentityId "%s" NameId "%s"', $identityId, $identity->nameId ));
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces before closing parenthesis; 1 found
Loading history...
91
92
        $raListing = $this->raListingRepository->findByIdentityId(new IdentityId($identityId));
93
        $isRa = $this->getRoleFromListing($raListing, AuthorityRole::ROLE_RA);
0 ignored issues
show
Bug introduced by
It seems like $raListing can also be of type null; however, parameter $raListing of Surfnet\StepupMiddleware...e::getRoleFromListing() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

93
        $isRa = $this->getRoleFromListing(/** @scrutinizer ignore-type */ $raListing, AuthorityRole::ROLE_RA);
Loading history...
94
        $isRaa = $this->getRoleFromListing($raListing, AuthorityRole::ROLE_RAA);
95
96
        $this->logger->notice(
97
            sprintf(
98
                'Based on RaListing Identity %s has roles(RA: %s, RAA: %s)',
99
                $identityId,
100
                $isRa ? "YES" : "NO",
101
                $isRaa ? "YES" : "NO"
102
            )
103
        );
104
105
106
        if ($raListing === null) {
107
            $this->logger->notice(sprintf('No RA listing found for IdentityId %s', $identityId));
108
            return null;
109
        }
110
111
        $authorizationContextRa = $this->authorizationService->buildInstitutionAuthorizationContext(
112
            new IdentityId($identityId),
113
            InstitutionRole::useRa()
114
        );
115
        $authorizations = AuthorizedInstitutionCollection::from(
116
            $authorizationContextRa->getInstitutions()
117
        );
118
119
        $this->logger->notice(sprintf('IdentityId "%s" is RA for: %s', $identityId, json_encode($authorizationContextRa->getInstitutions()->jsonSerialize())));
120
121
        if ($isRaa) {
122
            $authorizationContextRaa = $this->authorizationService->buildInstitutionAuthorizationContext(
123
                new IdentityId($identityId),
124
                InstitutionRole::useRaa()
125
            );
126
127
            $this->logger->notice(sprintf('IdentityId "%s" is RAA for: %s', $identityId, json_encode($authorizationContextRaa->getInstitutions()->jsonSerialize())));
128
129
            $authorizations = AuthorizedInstitutionCollection::from(
130
                $authorizationContextRa->getInstitutions(),
131
                $authorizationContextRaa->getInstitutions()
132
            );
133
        }
134
135
        return new Profile(
136
            $identity,
137
            $authorizations,
138
            $authorizationContextRa->isActorSraa()
139
        );
140
    }
141
142
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $role should have a doc-comment as per coding-style.
Loading history...
143
     * @param array<int, RaListing> $raListing
144
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
145
    private function getRoleFromListing(array $raListing, string $role): bool
0 ignored issues
show
Coding Style introduced by
Private method name "ProfileService::getRoleFromListing" must be prefixed with an underscore
Loading history...
146
    {
147
        foreach ($raListing as $listing) {
148
            if ($listing->role->getRole() === $role) {
149
                return true;
150
            }
151
        }
152
        return false;
153
    }
154
}
155