|
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\ORM\EntityRepository; |
|
22
|
|
|
use Doctrine\ORM\Query\Expr\Join; |
|
23
|
|
|
use Surfnet\Stepup\Configuration\Value\InstitutionRole; |
|
24
|
|
|
use Surfnet\Stepup\Identity\Collection\InstitutionCollection; |
|
25
|
|
|
use Surfnet\Stepup\Identity\Value\IdentityId; |
|
26
|
|
|
use Surfnet\Stepup\Identity\Value\Institution; |
|
27
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Value\InstitutionRoleSet; |
|
28
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Entity\InstitutionAuthorization; |
|
29
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\InstitutionListing; |
|
30
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\RaListing; |
|
31
|
|
|
|
|
32
|
|
|
class InstitutionListingRepository extends EntityRepository |
|
33
|
|
|
{ |
|
34
|
|
|
public function save(InstitutionListing $institution) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->getEntityManager()->persist($institution); |
|
37
|
|
|
$this->getEntityManager()->flush(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function addIfNotExists(Institution $institution) |
|
41
|
|
|
{ |
|
42
|
|
|
$existsQuery = $this->createQueryBuilder('i') |
|
43
|
|
|
->where('i.institution = :institution') |
|
44
|
|
|
->setParameter('institution', (string) $institution) |
|
45
|
|
|
->getQuery() |
|
46
|
|
|
->getOneOrNullResult(); |
|
47
|
|
|
|
|
48
|
|
|
if ($existsQuery) { |
|
49
|
|
|
return; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$listing = InstitutionListing::createFrom($institution); |
|
53
|
|
|
|
|
54
|
|
|
$this->save($listing); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param InstitutionRoleSet $roleRequirements |
|
59
|
|
|
* @param IdentityId $actorId |
|
60
|
|
|
* @return InstitutionCollection |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getInstitutionsForRaa(InstitutionRoleSet $roleRequirements, IdentityId $actorId) |
|
63
|
|
|
{ |
|
64
|
|
|
$qb = $this->createQueryBuilder('i') |
|
65
|
|
|
->select("a.institution") |
|
66
|
|
|
->innerJoin(RaListing::class, 'r', Join::WITH, "i.institution = r.raInstitution") |
|
67
|
|
|
->leftJoin( |
|
68
|
|
|
InstitutionAuthorization::class, |
|
69
|
|
|
'a', |
|
70
|
|
|
Join::WITH, |
|
71
|
|
|
"i.institution = a.institutionRelation AND a.institutionRole IN (:authorizationRoles)" |
|
72
|
|
|
) |
|
73
|
|
|
->where("r.identityId = :identityId AND r.role IN(:roles)") |
|
74
|
|
|
->groupBy("a.institution"); |
|
75
|
|
|
|
|
76
|
|
|
$qb->setParameter('identityId', (string)$actorId); |
|
77
|
|
|
$qb->setParameter( |
|
78
|
|
|
'authorizationRoles', |
|
79
|
|
|
$this->getAuthorizationRoles( |
|
80
|
|
|
$roleRequirements, |
|
81
|
|
|
[InstitutionRole::ROLE_USE_RA => InstitutionRole::ROLE_USE_RA, InstitutionRole::ROLE_USE_RAA => InstitutionRole::ROLE_USE_RAA] |
|
82
|
|
|
) |
|
83
|
|
|
); |
|
84
|
|
|
$qb->setParameter( |
|
85
|
|
|
'roles', |
|
86
|
|
|
$this->getAuthorizationRoles( |
|
87
|
|
|
$roleRequirements, |
|
88
|
|
|
[InstitutionRole::ROLE_USE_RA => 'ra', InstitutionRole::ROLE_USE_RAA => 'raa'] |
|
89
|
|
|
) |
|
90
|
|
|
); |
|
91
|
|
|
|
|
92
|
|
|
$institutions = $qb->getQuery()->getArrayResult(); |
|
93
|
|
|
|
|
94
|
|
|
$result = new InstitutionCollection(); |
|
95
|
|
|
foreach ($institutions as $institution) { |
|
96
|
|
|
$result->add(new Institution((string)$institution['institution'])); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $result; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param IdentityId $actorId |
|
104
|
|
|
* @return InstitutionCollection |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getInstitutionsForSelectRaa(IdentityId $actorId) |
|
107
|
|
|
{ |
|
108
|
|
|
$qb = $this->createQueryBuilder('i') |
|
109
|
|
|
->select("a.institutionRelation") |
|
110
|
|
|
->innerJoin(RaListing::class, 'r', Join::WITH, "i.institution = r.raInstitution") |
|
111
|
|
|
->leftJoin( |
|
112
|
|
|
InstitutionAuthorization::class, |
|
113
|
|
|
'a', |
|
114
|
|
|
Join::WITH, |
|
115
|
|
|
"i.institution = a.institution AND a.institutionRole IN (:authorizationRoles)" |
|
116
|
|
|
) |
|
117
|
|
|
->where("r.identityId = :identityId AND r.role IN(:roles)") |
|
118
|
|
|
->groupBy("a.institutionRelation"); |
|
119
|
|
|
|
|
120
|
|
|
$qb->setParameter('identityId', (string)$actorId); |
|
121
|
|
|
$qb->setParameter( |
|
122
|
|
|
'authorizationRoles', |
|
123
|
|
|
[InstitutionRole::ROLE_SELECT_RAA] |
|
124
|
|
|
); |
|
125
|
|
|
$qb->setParameter( |
|
126
|
|
|
'roles', |
|
127
|
|
|
['raa'] |
|
128
|
|
|
); |
|
129
|
|
|
$institutions = $qb->getQuery()->getArrayResult(); |
|
130
|
|
|
|
|
131
|
|
|
$result = new InstitutionCollection(); |
|
132
|
|
|
foreach ($institutions as $institution) { |
|
133
|
|
|
$result->add(new Institution((string)$institution['institutionRelation'])); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return $result; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param Institution $institution |
|
142
|
|
|
* @return InstitutionCollection |
|
143
|
|
|
*/ |
|
144
|
|
|
public function getInstitutionsForSelectRaaAsSraa(Institution $institution) |
|
145
|
|
|
{ |
|
146
|
|
|
$qb = $this->createQueryBuilder('i') |
|
147
|
|
|
->select("a.institution") |
|
148
|
|
|
->innerJoin( |
|
149
|
|
|
InstitutionAuthorization::class, |
|
150
|
|
|
'a', |
|
151
|
|
|
Join::WITH, |
|
152
|
|
|
"i.institution = a.institution AND a.institutionRole IN (:authorizationRoles)" |
|
153
|
|
|
) |
|
154
|
|
|
->where("a.institution = :institution") |
|
155
|
|
|
->groupBy("a.institution"); |
|
156
|
|
|
|
|
157
|
|
|
$qb->setParameter('institution', (string)$institution); |
|
158
|
|
|
$qb->setParameter( |
|
159
|
|
|
'authorizationRoles', |
|
160
|
|
|
[InstitutionRole::ROLE_SELECT_RAA] |
|
161
|
|
|
); |
|
162
|
|
|
|
|
163
|
|
|
$institutions = $qb->getQuery()->getArrayResult(); |
|
164
|
|
|
|
|
165
|
|
|
$result = new InstitutionCollection(); |
|
166
|
|
|
foreach ($institutions as $institution) { |
|
167
|
|
|
$result->add(new Institution((string)$institution['institution'])); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
return $result; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @param InstitutionRoleSet $roleRequirements |
|
175
|
|
|
* @param array $map |
|
176
|
|
|
* @return array |
|
177
|
|
|
*/ |
|
178
|
|
|
private function getAuthorizationRoles(InstitutionRoleSet $roleRequirements, array $map) |
|
179
|
|
|
{ |
|
180
|
|
|
$result = []; |
|
181
|
|
|
foreach ($roleRequirements->getRoles() as $role) { |
|
182
|
|
|
$result[] = $map[(string)$role]; |
|
183
|
|
|
} |
|
184
|
|
|
return $result; |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|