1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2019 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\Identity\Service; |
20
|
|
|
|
21
|
|
|
use Psr\Log\LoggerInterface; |
22
|
|
|
use Surfnet\Stepup\Configuration\Value\InstitutionRole; |
23
|
|
|
use Surfnet\Stepup\Identity\Value\IdentityId; |
24
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Service\AuthorizationContextService; |
25
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\RaListing; |
26
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\RaListingRepository; |
27
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Value\AuthorityRole; |
28
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Value\AuthorizedInstitutionCollection; |
29
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Value\Profile; |
30
|
|
|
use function sprintf; |
31
|
|
|
|
32
|
|
|
class ProfileService extends AbstractSearchService |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var RaListingRepository |
36
|
|
|
*/ |
37
|
|
|
private $raListingRepository; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var IdentityService |
41
|
|
|
*/ |
42
|
|
|
private $identityService; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var AuthorizationContextService |
46
|
|
|
*/ |
47
|
|
|
private $authorizationService; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var LoggerInterface |
51
|
|
|
*/ |
52
|
|
|
private $logger; |
53
|
|
|
|
54
|
|
|
public function __construct( |
55
|
|
|
RaListingRepository $raListingRepository, |
56
|
|
|
IdentityService $identityService, |
57
|
|
|
AuthorizationContextService $institutionAuthorizationService, |
58
|
|
|
LoggerInterface $logger |
59
|
|
|
) { |
60
|
|
|
$this->raListingRepository = $raListingRepository; |
61
|
|
|
$this->identityService = $identityService; |
62
|
|
|
$this->authorizationService = $institutionAuthorizationService; |
63
|
|
|
$this->logger = $logger; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Uses the identityId to first load the ra credentials (if present) |
68
|
|
|
* These credentials are then used to test what type of administrator we are dealing with ((S)RA(A)). Next the |
69
|
|
|
* authorizations are retrieved from the InstitutionAuthorizationRepository. Not that we distinguish between |
70
|
|
|
* implicit and explicitly appointed roles. The implicit roles are based on the institution configuration |
71
|
|
|
* (SELECT_RAA). Finally identity is retrieved for the provided identityId. This data is then merged in a Profile |
72
|
|
|
* value object. |
73
|
|
|
* |
74
|
|
|
* When the profile is incorrect, for example because no identity can be found, null is returned instead of a |
75
|
|
|
* Profile. Its possible to retrieve profile data for a non RA user, in that case no authorization data is set |
76
|
|
|
* on the profile. The same goes for the SRAA user. As that user is allowed all authorizations for all institutions. |
77
|
|
|
* An additional isSraa flag is set to true for these administrators. |
78
|
|
|
* |
79
|
|
|
* @param string $identityId |
|
|
|
|
80
|
|
|
* @return Profile|null |
|
|
|
|
81
|
|
|
*/ |
82
|
|
|
public function createProfile($identityId) |
83
|
|
|
{ |
84
|
|
|
$identity = $this->identityService->find($identityId); |
85
|
|
|
|
86
|
|
|
if ($identity === null) { |
87
|
|
|
$this->logger->notice(sprintf('No Identity found with IdentityId %s', $identityId)); |
88
|
|
|
return null; |
89
|
|
|
} |
90
|
|
|
$this->logger->notice(sprintf('Found IdentityId "%s" NameId "%s"', $identityId, $identity->nameId )); |
|
|
|
|
91
|
|
|
|
92
|
|
|
$raListing = $this->raListingRepository->findByIdentityId(new IdentityId($identityId)); |
93
|
|
|
$isRa = $this->getRoleFromListing($raListing, AuthorityRole::ROLE_RA); |
|
|
|
|
94
|
|
|
$isRaa = $this->getRoleFromListing($raListing, AuthorityRole::ROLE_RAA); |
95
|
|
|
|
96
|
|
|
$this->logger->notice( |
97
|
|
|
sprintf( |
98
|
|
|
'Based on RaListing Identity %s has roles(RA: %s, RAA: %s)', |
99
|
|
|
$identityId, |
100
|
|
|
$isRa ? "YES" : "NO", |
101
|
|
|
$isRaa ? "YES" : "NO" |
102
|
|
|
) |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
if ($raListing === null) { |
107
|
|
|
$this->logger->notice(sprintf('No RA listing found for IdentityId %s', $identityId)); |
108
|
|
|
return null; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$authorizationContextRa = $this->authorizationService->buildInstitutionAuthorizationContext( |
112
|
|
|
new IdentityId($identityId), |
113
|
|
|
InstitutionRole::useRa() |
114
|
|
|
); |
115
|
|
|
$authorizations = AuthorizedInstitutionCollection::from( |
116
|
|
|
$authorizationContextRa->getInstitutions() |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
$this->logger->notice(sprintf('IdentityId "%s" is RA for: %s', $identityId, json_encode($authorizationContextRa->getInstitutions()->jsonSerialize()))); |
120
|
|
|
|
121
|
|
|
if ($isRaa) { |
122
|
|
|
$authorizationContextRaa = $this->authorizationService->buildInstitutionAuthorizationContext( |
123
|
|
|
new IdentityId($identityId), |
124
|
|
|
InstitutionRole::useRaa() |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
$this->logger->notice(sprintf('IdentityId "%s" is RAA for: %s', $identityId, json_encode($authorizationContextRaa->getInstitutions()->jsonSerialize()))); |
128
|
|
|
|
129
|
|
|
$authorizations = AuthorizedInstitutionCollection::from( |
130
|
|
|
$authorizationContextRa->getInstitutions(), |
131
|
|
|
$authorizationContextRaa->getInstitutions() |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return new Profile( |
136
|
|
|
$identity, |
137
|
|
|
$authorizations, |
138
|
|
|
$authorizationContextRa->isActorSraa() |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
|
|
|
|
143
|
|
|
* @param array<int, RaListing> $raListing |
144
|
|
|
*/ |
|
|
|
|
145
|
|
|
private function getRoleFromListing(array $raListing, string $role): bool |
|
|
|
|
146
|
|
|
{ |
147
|
|
|
foreach ($raListing as $listing) { |
148
|
|
|
if ($listing->role->getRole() === $role) { |
149
|
|
|
return true; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
return false; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|