ProfileService   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
c 2
b 0
f 0
dl 0
loc 47
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createProfile() 0 26 2
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
 */
18
19
namespace Surfnet\StepupMiddleware\ApiBundle\Identity\Service;
20
21
use Surfnet\Stepup\Identity\Value\IdentityId;
22
use Surfnet\Stepup\Identity\Value\RegistrationAuthorityRole;
0 ignored issues
show
Bug introduced by
The type Surfnet\Stepup\Identity\...gistrationAuthorityRole was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Service\AuthorizationContextService;
24
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\Identity;
25
use Surfnet\StepupMiddleware\ApiBundle\Identity\Value\AuthorizedInstitutionCollection;
26
use Surfnet\StepupMiddleware\ApiBundle\Identity\Value\Profile;
27
28
/** @extends AbstractSearchService<Profile> */
29
class ProfileService extends AbstractSearchService
30
{
31
    public function __construct(
32
        private readonly IdentityService $identityService,
33
        private readonly AuthorizationContextService $authorizationService,
34
    ) {
35
    }
36
37
    /**
38
     * Uses the identityId to first load the ra credentials (if present)
39
     * These credentials are then used to test what type of administrator we are dealing with ((S)RA(A)). Next the
40
     * authorizations are retrieved from the InstitutionAuthorizationRepository. Not that we distinguish between
41
     * implicit and explicitly appointed roles. The implicit roles are based on the institution configuration
42
     * (SELECT_RAA). Finally identity is retrieved for the provided identityId. This data is then merged in a Profile
43
     * value object.
44
     *
45
     * When the profile is incorrect, for example because no identity can be found, null is returned instead of a
46
     * Profile. Its possible to retrieve profile data for a non RA user, in that case no authorization data is set
47
     * on the profile. The same goes for the SRAA user. As that user is allowed all authorizations for all institutions.
48
     * An additional isSraa flag is set to true for these administrators.
49
     */
50
    public function createProfile(string $identityId): ?Profile
51
    {
52
        $identity = $this->identityService->find($identityId);
53
        if (!$identity instanceof Identity) {
54
            return null;
55
        }
56
57
        $authorizationContextRa = $this->authorizationService->buildInstitutionAuthorizationContext(
58
            new IdentityId($identityId),
59
            RegistrationAuthorityRole::ra(),
60
        );
61
62
        $authorizationContextRaa = $this->authorizationService->buildInstitutionAuthorizationContext(
63
            new IdentityId($identityId),
64
            RegistrationAuthorityRole::raa(),
65
        );
66
67
        $authorizations = AuthorizedInstitutionCollection::from(
68
            $authorizationContextRa->getInstitutions(),
69
            $authorizationContextRaa->getInstitutions(),
70
        );
71
72
        return new Profile(
73
            $identity,
74
            $authorizations,
75
            $authorizationContextRa->isActorSraa(),
76
        );
77
    }
78
}
79