Completed
Push — feature/fga-sraa-authorization ( 8efd63 )
by Michiel
17:06
created

InstitutionAuthorizationService   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 5
dl 0
loc 42
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A buildInstitutionAuthorizationContext() 0 13 2
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
 */
18
19
namespace Surfnet\StepupMiddleware\ApiBundle\Authorization\Service;
20
21
use Surfnet\Stepup\Identity\Value\IdentityId;
22
use Surfnet\Stepup\Identity\Value\Institution;
23
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Value\InstitutionAuthorizationContext;
24
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Value\InstitutionRoleSet;
25
use Surfnet\StepupMiddleware\ApiBundle\Exception\InvalidArgumentException;
26
use Surfnet\StepupMiddleware\ApiBundle\Identity\Service\IdentityService;
27
use Surfnet\StepupMiddleware\ApiBundle\Identity\Service\SraaService;
28
29
/**
30
 * Creates InstitutionAuthorizationContext
31
 *
32
 * The Context is enriched with the 'isSraa' setting. It verifies if the
33
 * actor id matches that of one of the SRAA's.
34
 */
35
class InstitutionAuthorizationService
36
{
37
    /**
38
     * @var SraaService
39
     */
40
    private $sraaService;
41
42
    /**
43
     * @var IdentityService
44
     */
45
    private $identityService;
46
47
    public function __construct(SraaService $sraaService, IdentityService $identityService)
48
    {
49
        $this->sraaService = $sraaService;
50
        $this->identityService = $identityService;
51
    }
52
53
    /**
54
     * Build the InstitutionAuthorizationContext for use in queries
55
     *
56
     * The additional test is performed to indicate if the actor is SRAA.
57
     *
58
     * @param Institution $actorInstitution
59
     * @param InstitutionRoleSet $roleRequirements
60
     * @param IdentityId $actorId
61
     * @return InstitutionAuthorizationContext
62
     */
63
    public function buildInstitutionAuthorizationContext(Institution $actorInstitution, InstitutionRoleSet $roleRequirements, IdentityId $actorId)
64
    {
65
        $identity = $this->identityService->find((string) $actorId);
66
67
        if (!$identity) {
68
            throw new InvalidArgumentException('The provided id is not associated with any known identity');
69
        }
70
71
        $sraa = $this->sraaService->findByNameId($identity->nameId);
72
        $isSraa = !is_null($sraa);
73
74
        return new InstitutionAuthorizationContext($actorInstitution, $roleRequirements, $isSraa);
75
    }
76
}
77