1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2014 SURFnet bv |
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\Repository; |
20
|
|
|
|
21
|
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; |
22
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
23
|
|
|
use Doctrine\ORM\Query\Expr\Join; |
24
|
|
|
use Surfnet\Stepup\Configuration\Value\InstitutionRole; |
25
|
|
|
use Surfnet\Stepup\Identity\Collection\InstitutionCollection; |
26
|
|
|
use Surfnet\Stepup\Identity\Value\IdentityId; |
27
|
|
|
use Surfnet\Stepup\Identity\Value\Institution; |
28
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Entity\ConfiguredInstitution; |
29
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Entity\InstitutionAuthorization; |
30
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\AuditLogEntry; |
31
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\Identity; |
32
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\RaListing; |
33
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Value\AuthorityRole; |
34
|
|
|
|
35
|
|
|
class AuthorizationRepository extends ServiceEntityRepository |
36
|
|
|
{ |
37
|
|
|
public function __construct(ManagerRegistry $registry) |
38
|
|
|
{ |
39
|
|
|
parent::__construct($registry, AuditLogEntry::class); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Return all institutions were the actor has the specified role for |
44
|
|
|
* The returned institutions are used to filter query results on |
45
|
|
|
* |
46
|
|
|
* @param InstitutionRole $role |
47
|
|
|
* @param IdentityId $actorId |
48
|
|
|
* @return InstitutionCollection |
49
|
|
|
*/ |
50
|
|
|
public function getInstitutionsForRole(InstitutionRole $role, IdentityId $actorId) |
51
|
|
|
{ |
52
|
|
|
$qb = $this->_em->createQueryBuilder() |
53
|
|
|
->select("a.institution") |
54
|
|
|
->from(ConfiguredInstitution::class, 'i') |
55
|
|
|
->innerJoin(RaListing::class, 'r', Join::WITH, "i.institution = r.raInstitution") |
56
|
|
|
->innerJoin( |
57
|
|
|
InstitutionAuthorization::class, |
58
|
|
|
'a', |
59
|
|
|
Join::WITH, |
60
|
|
|
"i.institution = a.institutionRelation AND a.institutionRole IN (:authorizationRoles)" |
61
|
|
|
) |
62
|
|
|
->where("r.identityId = :identityId AND r.role IN(:roles)") |
63
|
|
|
->groupBy("a.institution"); |
64
|
|
|
|
65
|
|
|
$qb->setParameter('identityId', (string)$actorId); |
66
|
|
|
$qb->setParameter( |
67
|
|
|
'authorizationRoles', |
68
|
|
|
$this->getAllowedInstitutionRoles($role) |
69
|
|
|
); |
70
|
|
|
$qb->setParameter( |
71
|
|
|
'roles', |
72
|
|
|
$this->getAllowedIdentityRoles($role) |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
$institutions = $qb->getQuery()->getArrayResult(); |
76
|
|
|
|
77
|
|
|
$result = new InstitutionCollection(); |
78
|
|
|
foreach ($institutions as $institution) { |
79
|
|
|
$result->add(new Institution((string)$institution['institution'])); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $result; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Finds the institutions that have the Select RAA authorization based on |
87
|
|
|
* the institution of the specified identity. |
88
|
|
|
*/ |
89
|
|
|
public function getInstitutionsForSelectRaaRole(IdentityId $actorId) |
90
|
|
|
{ |
91
|
|
|
$qb = $this->_em->createQueryBuilder() |
92
|
|
|
->select("ci.institution") |
93
|
|
|
->from(InstitutionAuthorization::class, 'ia') |
94
|
|
|
->innerJoin(ConfiguredInstitution::class, 'ci', Join::WITH, 'ia.institutionRelation = ci.institution') |
95
|
|
|
->innerJoin(Identity::class, 'i', Join::WITH, 'ia.institution = i.institution AND i.id = :identityId') |
96
|
|
|
->innerJoin(RaListing::class, 'ra', Join::WITH, 'i.id = ra.identityId AND ra.role = :authorizationRole') |
97
|
|
|
->where('ia.institutionRole = :institutionRole AND ra.role = :authorizationRole') |
98
|
|
|
->groupBy("ci.institution"); |
99
|
|
|
|
100
|
|
|
$qb->setParameter('identityId', (string)$actorId); |
101
|
|
|
// The identity requires RAA role to perform this search |
102
|
|
|
$qb->setParameter( |
103
|
|
|
'authorizationRole', |
104
|
|
|
AuthorityRole::ROLE_RAA |
105
|
|
|
); |
106
|
|
|
// Filter on the SELECT_RAA authorization in the institution authorization projection |
107
|
|
|
$qb->setParameter( |
108
|
|
|
'institutionRole', |
109
|
|
|
InstitutionRole::ROLE_SELECT_RAA |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
$institutions = $qb->getQuery()->getArrayResult(); |
113
|
|
|
|
114
|
|
|
$result = new InstitutionCollection(); |
115
|
|
|
foreach ($institutions as $institution) { |
116
|
|
|
$result->add(new Institution((string)$institution['institution'])); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $result; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* This is the mapping to look up allowed institution roles |
124
|
|
|
* - if the institution role is RA we should look if the configured institution has RA role |
125
|
|
|
* - if the institution role is RAA we should look if the configured institution has RAA role |
126
|
|
|
* |
127
|
|
|
* @param InstitutionRole $role |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
|
View Code Duplication |
private function getAllowedInstitutionRoles(InstitutionRole $role) |
|
|
|
|
131
|
|
|
{ |
132
|
|
|
switch (true) { |
133
|
|
|
case $role->equals(InstitutionRole::useRa()): |
134
|
|
|
return [InstitutionRole::ROLE_USE_RA]; |
135
|
|
|
case $role->equals(InstitutionRole::useRaa()): |
136
|
|
|
return [InstitutionRole::ROLE_USE_RAA]; |
137
|
|
|
default: |
138
|
|
|
return []; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* This is the mapping to look up allowed identity roles for a specific institution role |
144
|
|
|
* - if the institution role is RA we should look if the identity has a RA or RAA role |
145
|
|
|
* - if the institution role is RAA we should look if the identity has a RAA role |
146
|
|
|
* |
147
|
|
|
* @param InstitutionRole $role |
148
|
|
|
* @return array |
149
|
|
|
*/ |
150
|
|
View Code Duplication |
private function getAllowedIdentityRoles(InstitutionRole $role) |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
switch (true) { |
153
|
|
|
case $role->equals(InstitutionRole::useRa()): |
154
|
|
|
return [AuthorityRole::ROLE_RA, AuthorityRole::ROLE_RAA]; |
155
|
|
|
case $role->equals(InstitutionRole::useRaa()): |
156
|
|
|
return [AuthorityRole::ROLE_RAA]; |
157
|
|
|
default: |
158
|
|
|
return []; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.