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\Configuration\Value\InstitutionRole; |
22
|
|
|
use Surfnet\Stepup\Identity\Value\IdentityId; |
23
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Value\InstitutionAuthorizationContext; |
24
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Exception\InvalidArgumentException; |
25
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\AuthorizationRepository; |
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 AuthorizationContextService |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @var SraaService |
39
|
|
|
*/ |
40
|
|
|
private $sraaService; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var IdentityService |
44
|
|
|
*/ |
45
|
|
|
private $identityService; |
46
|
|
|
/** |
47
|
|
|
* @var AuthorizationRepository |
48
|
|
|
*/ |
49
|
|
|
private $authorizationRepository; |
50
|
|
|
|
51
|
|
|
public function __construct( |
52
|
|
|
SraaService $sraaService, |
53
|
|
|
IdentityService $identityService, |
54
|
|
|
AuthorizationRepository $authorizationRepository |
55
|
|
|
) { |
56
|
|
|
$this->sraaService = $sraaService; |
57
|
|
|
$this->identityService = $identityService; |
58
|
|
|
$this->authorizationRepository = $authorizationRepository; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Build the InstitutionAuthorizationContext to be used for authorization filtering on institutions in queries |
63
|
|
|
* |
64
|
|
|
* The additional test is performed to indicate if the actor is SRAA. |
65
|
|
|
* |
66
|
|
|
* @param IdentityId $actorId |
67
|
|
|
* @param InstitutionRole $role |
68
|
|
|
* @return InstitutionAuthorizationContext |
69
|
|
|
*/ |
70
|
|
|
public function buildInstitutionAuthorizationContext(IdentityId $actorId, InstitutionRole $role) |
71
|
|
|
{ |
72
|
|
|
$identity = $this->identityService->find((string) $actorId); |
73
|
|
|
|
74
|
|
|
if (!$identity) { |
75
|
|
|
throw new InvalidArgumentException('The provided id is not associated with any known identity'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$sraa = $this->sraaService->findByNameId($identity->nameId); |
79
|
|
|
$isSraa = !is_null($sraa); |
80
|
|
|
|
81
|
|
|
$institutions = $this->authorizationRepository->getInstitutionsForRole($role, $actorId); |
82
|
|
|
|
83
|
|
|
return new InstitutionAuthorizationContext($institutions, $isSraa); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|