Passed
Push — bugfix/5.1-profile ( 934735...50d43f )
by Michiel
04:55
created

AuthorizationContextService::isSraa()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 8
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.
85
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
86
    public function buildInstitutionAuthorizationContext(
87
        IdentityId $actorId,
88
        RegistrationAuthorityRole $role
89
    ): InstitutionAuthorizationContext {
90
        $isSraa = $this->isSraa($actorId);
91
        if ($isSraa) {
92
            $institutions = new InstitutionCollection();
93
            $configuredInstitutions = $this->institutionRepository->findAll();
94
            foreach ($configuredInstitutions as $institution) {
95
                $institutions->add(new Institution((string)$institution->institution));
96
            }
97
        } else {
98
            // Get the institutions the identity is RA(A) for.
99
            $institutions = $this->authorizationRepository->getInstitutionsForRole($role, $actorId);
100
        }
101
        return new InstitutionAuthorizationContext($institutions, $isSraa);
102
    }
103
104
    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...
105
    {
106
        $identity = $this->identityService->find((string)$actorId);
107
        if (!$identity) {
108
            throw new InvalidArgumentException('The provided id is not associated with any known identity');
109
        }
110
        $sraa = $this->sraaService->findByNameId($identity->nameId);
111
        return !is_null($sraa);
112
    }
113
}
114