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\Common\Collections\ArrayCollection; |
22
|
|
|
use Doctrine\ORM\EntityManager; |
23
|
|
|
use Doctrine\ORM\EntityRepository; |
24
|
|
|
use Doctrine\ORM\Mapping; |
25
|
|
|
use Surfnet\Stepup\Identity\Value\IdentityId; |
26
|
|
|
use Surfnet\Stepup\Identity\Value\Institution; |
27
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Filter\InstitutionAuthorizationRepositoryFilter; |
28
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Value\InstitutionAuthorizationContextInterface; |
29
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Exception\RuntimeException; |
30
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\RaListing; |
31
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\RaListingQuery; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
35
|
|
|
*/ |
36
|
|
|
class RaListingRepository extends EntityRepository |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* @var InstitutionAuthorizationRepositoryFilter |
40
|
|
|
*/ |
41
|
|
|
private $authorizationRepositoryFilter; |
42
|
|
|
|
43
|
|
|
public function __construct( |
44
|
|
|
EntityManager $em, |
45
|
|
|
Mapping\ClassMetadata $class, |
46
|
|
|
InstitutionAuthorizationRepositoryFilter $authorizationRepositoryFilter |
47
|
|
|
) { |
48
|
|
|
parent::__construct($em, $class); |
49
|
|
|
$this->authorizationRepositoryFilter = $authorizationRepositoryFilter; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param IdentityId $identityId The RA's identity id. |
54
|
|
|
* @return null|RaListing[] |
|
|
|
|
55
|
|
|
*/ |
56
|
|
|
public function findByIdentityId(IdentityId $identityId) |
57
|
|
|
{ |
58
|
|
|
return parent::findBy(['identityId' => (string) $identityId]); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param IdentityId $identityId The RA's identity id. |
63
|
|
|
* @param Institution $raInstitution |
64
|
|
|
* @return null|RaListing |
|
|
|
|
65
|
|
|
*/ |
66
|
|
|
public function findByIdentityIdAndRaInstitution(IdentityId $identityId, Institution $raInstitution) |
67
|
|
|
{ |
68
|
|
|
return parent::findOneBy([ |
|
|
|
|
69
|
|
|
'identityId' => (string) $identityId, |
70
|
|
|
'raInstitution' => (string) $raInstitution, |
71
|
|
|
]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param IdentityId $identityId The RA's identity id. |
77
|
|
|
* @param Institution $raInstitution |
78
|
|
|
* @param InstitutionAuthorizationContextInterface $authorizationContext |
79
|
|
|
* @return null|RaListing |
80
|
|
|
*/ |
81
|
|
|
public function findByIdentityIdAndRaInstitutionWithContext( |
82
|
|
|
IdentityId $identityId, |
83
|
|
|
Institution $raInstitution, |
84
|
|
|
InstitutionAuthorizationContextInterface $authorizationContext |
85
|
|
|
) { |
86
|
|
|
$queryBuilder = $this->createQueryBuilder('r') |
87
|
|
|
->where('r.identityId = :identityId') |
88
|
|
|
->andWhere('r.raInstitution = :raInstitution') |
89
|
|
|
->setParameter('identityId', $identityId) |
90
|
|
|
->setParameter('raInstitution', (string)$raInstitution) |
91
|
|
|
->orderBy('r.raInstitution'); |
92
|
|
|
|
93
|
|
|
// Modify query to filter on authorization |
94
|
|
|
$this->authorizationRepositoryFilter->filter( |
95
|
|
|
$queryBuilder, |
96
|
|
|
$authorizationContext, |
97
|
|
|
'r.raInstitution', |
98
|
|
|
'iac' |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
return $queryBuilder->getQuery()->getOneOrNullResult(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param IdentityId $identityId The RA's identity id. |
106
|
|
|
* @param Institution $institution |
107
|
|
|
* @return RaListing[] |
108
|
|
|
*/ |
109
|
|
|
public function findByIdentityIdAndInstitution(IdentityId $identityId, Institution $institution) |
110
|
|
|
{ |
111
|
|
|
return parent::findBy([ |
|
|
|
|
112
|
|
|
'identityId' => (string) $identityId, |
113
|
|
|
'institution' => (string) $institution, |
114
|
|
|
]); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function save(RaListing $raListingEntry) |
118
|
|
|
{ |
119
|
|
|
$this->getEntityManager()->persist($raListingEntry); |
120
|
|
|
$this->getEntityManager()->flush(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity) The amount of if statements do not necessarily make the method |
125
|
|
|
* @SuppressWarnings(PHPMD.NPathComplexity) below complex or hard to maintain. |
126
|
|
|
* |
127
|
|
|
* @param RaListingQuery $query |
128
|
|
|
* @return \Doctrine\ORM\Query |
129
|
|
|
*/ |
130
|
|
|
public function createSearchQuery(RaListingQuery $query) |
131
|
|
|
{ |
132
|
|
|
$queryBuilder = $this->createQueryBuilder('r'); |
133
|
|
|
|
134
|
|
|
if ($query->institution) { |
135
|
|
|
$queryBuilder |
136
|
|
|
->andWhere('r.institution = :institution') |
137
|
|
|
->setParameter('institution', $query->institution); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if ($query->identityId) { |
141
|
|
|
$queryBuilder |
142
|
|
|
->andWhere('r.identityId = :identityId') |
143
|
|
|
->setParameter('identityId', (string) $query->identityId); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if ($query->name) { |
147
|
|
|
$queryBuilder |
148
|
|
|
->andWhere('r.commonName LIKE :name') |
149
|
|
|
->setParameter('name', sprintf('%%%s%%', $query->name)); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if ($query->email) { |
153
|
|
|
$queryBuilder |
154
|
|
|
->andWhere('r.email LIKE :email') |
155
|
|
|
->setParameter('email', sprintf('%%%s%%', $query->email)); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if ($query->role) { |
159
|
|
|
$queryBuilder |
160
|
|
|
->andWhere('r.role = :role') |
161
|
|
|
->setParameter('role', (string) $query->role); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
if ($query->raInstitution) { |
165
|
|
|
$queryBuilder |
166
|
|
|
->andWhere('r.raInstitution = :raInstitution') |
167
|
|
|
->setParameter('raInstitution', (string) $query->raInstitution); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
// Modify query to filter on authorization |
171
|
|
|
$this->authorizationRepositoryFilter->filter( |
172
|
|
|
$queryBuilder, |
173
|
|
|
$query->authorizationContext, |
174
|
|
|
'r.raInstitution', |
175
|
|
|
'iac' |
176
|
|
|
); |
177
|
|
|
|
178
|
|
|
if (!$query->orderBy) { |
179
|
|
|
return $queryBuilder->getQuery(); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$orderDirection = $query->orderDirection === 'asc' ? 'ASC' : 'DESC'; |
183
|
|
|
|
184
|
|
|
switch ($query->orderBy) { |
185
|
|
|
case 'commonName': |
186
|
|
|
$queryBuilder->orderBy('r.commonName', $orderDirection); |
187
|
|
|
break; |
188
|
|
|
default: |
189
|
|
|
throw new RuntimeException(sprintf('Unknown order by column "%s"', $query->orderBy)); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return $queryBuilder->getQuery(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param RaListingQuery $query |
197
|
|
|
* @return \Doctrine\ORM\Query |
198
|
|
|
*/ |
199
|
|
|
public function createOptionsQuery(RaListingQuery $query) |
200
|
|
|
{ |
201
|
|
|
$queryBuilder = $this->createQueryBuilder('r') |
202
|
|
|
->select('r.institution, r.raInstitution') |
203
|
|
|
->groupBy('r.institution, r.raInstitution'); |
204
|
|
|
|
205
|
|
|
// Modify query to filter on authorization |
206
|
|
|
$this->authorizationRepositoryFilter->filter( |
207
|
|
|
$queryBuilder, |
208
|
|
|
$query->authorizationContext, |
209
|
|
|
'r.raInstitution', |
210
|
|
|
'iac' |
211
|
|
|
); |
212
|
|
|
|
213
|
|
|
return $queryBuilder->getQuery(); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param Institution $raInstitution |
218
|
|
|
* @return ArrayCollection |
219
|
|
|
*/ |
220
|
|
|
public function listRasFor(Institution $raInstitution) |
221
|
|
|
{ |
222
|
|
|
$listings = $this->createQueryBuilder('rl') |
223
|
|
|
->where('rl.raInstitution = :institution') |
224
|
|
|
->setParameter('institution', $raInstitution) |
225
|
|
|
->getQuery() |
226
|
|
|
->getResult(); |
227
|
|
|
|
228
|
|
|
return new ArrayCollection($listings); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @param IdentityId $identityId |
233
|
|
|
* @return void |
234
|
|
|
*/ |
235
|
|
View Code Duplication |
public function removeByIdentityId(IdentityId $identityId) |
|
|
|
|
236
|
|
|
{ |
237
|
|
|
$this->getEntityManager()->createQueryBuilder() |
238
|
|
|
->delete($this->_entityName, 'ral') |
239
|
|
|
->where('ral.identityId = :identityId') |
240
|
|
|
->andWhere('ral.raInstitution = :institution') |
241
|
|
|
->setParameter('identityId', $identityId->getIdentityId()) |
242
|
|
|
->getQuery() |
243
|
|
|
->execute(); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param IdentityId $identityId |
248
|
|
|
* @param Institution $raInstitution |
249
|
|
|
* @return void |
250
|
|
|
*/ |
251
|
|
View Code Duplication |
public function removeByIdentityIdAndRaInstitution(IdentityId $identityId, Institution $raInstitution) |
|
|
|
|
252
|
|
|
{ |
253
|
|
|
$this->getEntityManager()->createQueryBuilder() |
254
|
|
|
->delete($this->_entityName, 'ral') |
255
|
|
|
->where('ral.identityId = :identityId') |
256
|
|
|
->andWhere('ral.raInstitution = :institution') |
257
|
|
|
->setParameter('identityId', $identityId->getIdentityId()) |
258
|
|
|
->setParameter('institution', $raInstitution) |
259
|
|
|
->getQuery() |
260
|
|
|
->execute(); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @param IdentityId $identityId |
265
|
|
|
* @param Institution $institution |
266
|
|
|
* @return void |
267
|
|
|
*/ |
268
|
|
View Code Duplication |
public function removeByIdentityIdAndInstitution(IdentityId $identityId, Institution $institution) |
|
|
|
|
269
|
|
|
{ |
270
|
|
|
$this->getEntityManager()->createQueryBuilder() |
271
|
|
|
->delete($this->_entityName, 'ral') |
272
|
|
|
->where('ral.identityId = :identityId') |
273
|
|
|
->andWhere('ral.institution = :institution') |
274
|
|
|
->setParameter('identityId', $identityId->getIdentityId()) |
275
|
|
|
->setParameter('institution', $institution) |
276
|
|
|
->getQuery() |
277
|
|
|
->execute(); |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.