Passed
Push — main ( 1fbbbe...fd4d73 )
by Michiel
16:27 queued 12:04
created

buildSelectRaaInstitutionAuthorizationContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
/**
4
 * Copyright 2018 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\Authorization\Service;
20
21
use Surfnet\Stepup\Identity\Collection\InstitutionCollection;
22
use Surfnet\Stepup\Identity\Value\IdentityId;
23
use Surfnet\Stepup\Identity\Value\Institution;
24
use Surfnet\Stepup\Identity\Value\RegistrationAuthorityRole;
25
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Value\InstitutionAuthorizationContext;
26
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Repository\ConfiguredInstitutionRepository;
27
use Surfnet\StepupMiddleware\ApiBundle\Exception\InvalidArgumentException;
28
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\AuthorizationRepository;
29
use Surfnet\StepupMiddleware\ApiBundle\Identity\Service\IdentityService;
30
use Surfnet\StepupMiddleware\ApiBundle\Identity\Service\SraaService;
31
32
/**
33
 * Creates InstitutionAuthorizationContext
34
 *
35
 * The Context is enriched with the 'isSraa' setting. It verifies if the
36
 * actor id matches that of one of the SRAA's.
37
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
38
class AuthorizationContextService
39
{
40
    /**
41
     * @var SraaService
42
     */
43
    private $sraaService;
44
45
    /**
46
     * @var IdentityService
47
     */
48
    private $identityService;
49
50
    /**
51
     * @var ConfiguredInstitutionRepository
52
     */
53
    private $institutionRepository;
54
55
    /**
56
     * @var AuthorizationRepository
57
     */
58
    private $authorizationRepository;
59
60
    public function __construct(
61
        SraaService $sraaService,
62
        IdentityService $identityService,
63
        ConfiguredInstitutionRepository $institutionRepository,
64
        AuthorizationRepository $authorizationRepository
65
    ) {
66
        $this->sraaService = $sraaService;
67
        $this->identityService = $identityService;
68
        $this->institutionRepository = $institutionRepository;
69
        $this->authorizationRepository = $authorizationRepository;
70
    }
71
72
    public function buildSelectRaaInstitutionAuthorizationContext(IdentityId $actorId): InstitutionAuthorizationContext
73
    {
74
        $isSraa = $this->isSraa($actorId);
75
        // When building an auth context based on the select raa role, we use another query to retrieve the correct
76
        // institutions.
77
        $institutions = $this->authorizationRepository->getInstitutionsForSelectRaaRole($actorId);
78
        return new InstitutionAuthorizationContext($institutions, $isSraa);
79
    }
80
81
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $actorId should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $role should have a doc-comment as per coding-style.
Loading history...
82
     * Build the InstitutionAuthorizationContext to be used for authorization filtering on institutions  in queries
83
     *
84
     * The additional test is performed to indicate if the actor is SRAA. When the identity is SRAA, all Institutions
85
     * are added to the InstitutionAuthorizationContext
86
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
87
    public function buildInstitutionAuthorizationContext(
88
        IdentityId $actorId,
89
        RegistrationAuthorityRole $role
90
    ): InstitutionAuthorizationContext {
91
        $isSraa = $this->isSraa($actorId);
92
        if ($isSraa) {
93
            $institutions = new InstitutionCollection();
94
            $configuredInstitutions = $this->institutionRepository->findAll();
95
            foreach ($configuredInstitutions as $institution) {
96
                $institutions->add(new Institution((string)$institution->institution));
97
            }
98
        } else {
99
            // Get the institutions the identity is RA(A) for.
100
            $institutions = $this->authorizationRepository->getInstitutionsForRole($role, $actorId);
101
        }
102
        return new InstitutionAuthorizationContext($institutions, $isSraa);
103
    }
104
105
    private function isSraa(IdentityId $actorId)
0 ignored issues
show
Coding Style introduced by
Private method name "AuthorizationContextService::isSraa" must be prefixed with an underscore
Loading history...
106
    {
107
        $identity = $this->identityService->find((string)$actorId);
108
        if (!$identity) {
109
            throw new InvalidArgumentException('The provided id is not associated with any known identity');
110
        }
111
        $sraa = $this->sraaService->findByNameId($identity->nameId);
112
        return !is_null($sraa);
113
    }
114
}
115