Completed
Push — bugfix/fix-auth-filter ( b05c6b...5039f0 )
by
unknown
02:40
created

InstitutionAuthorizationService::getInstitutions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
cc 2
nc 2
nop 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 Doctrine\ORM\Query\Expr\Join;
22
use Surfnet\Stepup\Configuration\Value\InstitutionRole;
23
use Surfnet\Stepup\Identity\Collection\InstitutionCollection;
24
use Surfnet\Stepup\Identity\Value\IdentityId;
25
use Surfnet\Stepup\Identity\Value\Institution;
26
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Value\InstitutionAuthorizationContext;
27
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Value\InstitutionRoleSet;
28
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Entity\InstitutionAuthorization;
29
use Surfnet\StepupMiddleware\ApiBundle\Exception\InvalidArgumentException;
30
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\RaListing;
31
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\InstitutionListingRepository;
32
use Surfnet\StepupMiddleware\ApiBundle\Identity\Service\IdentityService;
33
use Surfnet\StepupMiddleware\ApiBundle\Identity\Service\SraaService;
34
35
/**
36
 * Creates InstitutionAuthorizationContext
37
 *
38
 * The Context is enriched with the 'isSraa' setting. It verifies if the
39
 * actor id matches that of one of the SRAA's.
40
 */
41
class InstitutionAuthorizationService
42
{
43
    /**
44
     * @var SraaService
45
     */
46
    private $sraaService;
47
48
    /**
49
     * @var IdentityService
50
     */
51
    private $identityService;
52
    /**
53
     * @var InstitutionListingRepository
54
     */
55
    private $institutionListingRepository;
56
57
    public function __construct(
58
        SraaService $sraaService,
59
        IdentityService $identityService,
60
        InstitutionListingRepository $institutionListingRepository
61
    ) {
62
        $this->sraaService = $sraaService;
63
        $this->identityService = $identityService;
64
        $this->institutionListingRepository = $institutionListingRepository;
65
    }
66
67
    /**
68
     * Build the InstitutionAuthorizationContext for use in queries
69
     *
70
     * The additional test is performed to indicate if the actor is SRAA.
71
     *
72
     * @param Institution $actorInstitution
73
     * @param InstitutionRoleSet $roleRequirements
74
     * @param IdentityId $actorId
75
     * @return InstitutionAuthorizationContext
76
     */
77
    public function buildInstitutionAuthorizationContext(Institution $actorInstitution, InstitutionRoleSet $roleRequirements, IdentityId $actorId)
78
    {
79
        $identity = $this->identityService->find((string) $actorId);
80
81
        if (!$identity) {
82
            throw new InvalidArgumentException('The provided id is not associated with any known identity');
83
        }
84
85
        $sraa = $this->sraaService->findByNameId($identity->nameId);
86
        $isSraa = !is_null($sraa);
87
88
        $institutions = $this->institutionListingRepository->getInstitutions($roleRequirements, $actorId);
89
90
        return new InstitutionAuthorizationContext($actorInstitution, $roleRequirements, $institutions, $isSraa);
91
    }
92
93
}
94