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 Doctrine\ORM\Query\Expr\Join; |
26
|
|
|
use Surfnet\Stepup\Identity\Collection\InstitutionCollection; |
27
|
|
|
use Surfnet\Stepup\Identity\Value\IdentityId; |
28
|
|
|
use Surfnet\Stepup\Identity\Value\Institution; |
29
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Filter\InstitutionAuthorizationRepositoryFilter; |
30
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Value\InstitutionAuthorizationContextInterface; |
31
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\RaCandidate; |
32
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\VettedSecondFactor; |
33
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\RaCandidateQuery; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
37
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
38
|
|
|
*/ |
39
|
|
|
class RaCandidateRepository extends EntityRepository |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* @var InstitutionAuthorizationRepositoryFilter |
43
|
|
|
*/ |
44
|
|
|
private $authorizationRepositoryFilter; |
45
|
|
|
|
46
|
|
|
public function __construct( |
47
|
|
|
EntityManager $em, |
48
|
|
|
Mapping\ClassMetadata $class, |
49
|
|
|
InstitutionAuthorizationRepositoryFilter $authorizationRepositoryFilter |
50
|
|
|
) { |
51
|
|
|
parent::__construct($em, $class); |
52
|
|
|
$this->authorizationRepositoryFilter = $authorizationRepositoryFilter; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param RaCandidate $raCandidate |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
|
public function merge(RaCandidate $raCandidate) |
60
|
|
|
{ |
61
|
|
|
$raCandidate = $this->getEntityManager()->merge($raCandidate); |
62
|
|
|
$this->getEntityManager()->persist($raCandidate); |
63
|
|
|
$this->getEntityManager()->flush(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param IdentityId $identityId |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
|
|
public function removeByIdentityId(IdentityId $identityId) |
71
|
|
|
{ |
72
|
|
|
$raCandidate = $this->findByIdentityId($identityId); |
73
|
|
|
|
74
|
|
|
if (!$raCandidate) { |
75
|
|
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->getEntityManager()->remove($raCandidate); |
79
|
|
|
$this->getEntityManager()->flush(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param Institution $institution |
84
|
|
|
* @param InstitutionCollection $raInstitutions |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
public function removeInstitutionsNotInList(Institution $institution, InstitutionCollection $raInstitutions) |
88
|
|
|
{ |
89
|
|
|
$raCandidates = $this->createQueryBuilder('rac') |
90
|
|
|
->where('rac.raInstitution = :raInstitution') |
91
|
|
|
->andWhere('rac.institution NOT IN (:institutions)') |
92
|
|
|
->setParameter('raInstitution', $institution) |
93
|
|
|
->setParameter('institutions', $raInstitutions->serialize()) |
94
|
|
|
->getQuery() |
95
|
|
|
->getResult(); |
96
|
|
|
|
97
|
|
|
$em = $this->getEntityManager(); |
98
|
|
|
foreach ($raCandidates as $raCandidate) { |
99
|
|
|
$em->remove($raCandidate); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$em->flush(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param Institution $raInstitution |
107
|
|
|
* @return void |
108
|
|
|
*/ |
109
|
|
|
public function removeByRaInstitution(Institution $raInstitution) |
110
|
|
|
{ |
111
|
|
|
$raCandidates = $this->findByRaInstitution($raInstitution); |
112
|
|
|
|
113
|
|
|
if (empty($raCandidates)) { |
114
|
|
|
return; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$em = $this->getEntityManager(); |
118
|
|
|
foreach ($raCandidates as $raCandidate) { |
119
|
|
|
$em->remove($raCandidate); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$em->flush(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param IdentityId $identityId |
127
|
|
|
* @param Institution $raInstitution |
128
|
|
|
* @return void |
129
|
|
|
*/ |
130
|
|
|
public function removeByIdentityIdAndRaInstitution(IdentityId $identityId, Institution $raInstitution) |
131
|
|
|
{ |
132
|
|
|
$raCandidate = $this->findByIdentityIdAndRaInstitution($identityId, $raInstitution); |
133
|
|
|
|
134
|
|
|
if (!$raCandidate) { |
135
|
|
|
return; |
136
|
|
|
} |
137
|
|
|
$em = $this->getEntityManager(); |
138
|
|
|
$em->remove($raCandidate); |
139
|
|
|
$em->flush(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param string[] $nameIds |
144
|
|
|
* @return void |
145
|
|
|
*/ |
146
|
|
|
public function removeByNameIds($nameIds) |
147
|
|
|
{ |
148
|
|
|
$raCandidates = $this->findByNameIds($nameIds); |
149
|
|
|
|
150
|
|
|
$em = $this->getEntityManager(); |
151
|
|
|
foreach ($raCandidates as $raCandidate) { |
152
|
|
|
$em->remove($raCandidate); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$em->flush(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param RaCandidateQuery $query |
160
|
|
|
* @return \Doctrine\ORM\Query |
161
|
|
|
*/ |
162
|
|
|
public function createSearchQuery(RaCandidateQuery $query) |
163
|
|
|
{ |
164
|
|
|
$queryBuilder = $this->createQueryBuilder('rac'); |
165
|
|
|
|
166
|
|
|
// Modify query to filter on authorization |
167
|
|
|
$this->authorizationRepositoryFilter->filter( |
168
|
|
|
$queryBuilder, |
169
|
|
|
$query->authorizationContext, |
170
|
|
|
'rac.raInstitution', |
171
|
|
|
'iac' |
172
|
|
|
); |
173
|
|
|
|
174
|
|
|
if ($query->institution) { |
175
|
|
|
$queryBuilder |
176
|
|
|
->andWhere('rac.institution = :institution') |
177
|
|
|
->setParameter('institution', $query->institution); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
if ($query->commonName) { |
181
|
|
|
$queryBuilder |
182
|
|
|
->andWhere('MATCH_AGAINST(rac.commonName, :commonName) > 0') |
183
|
|
|
->setParameter('commonName', $query->commonName); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
if ($query->email) { |
187
|
|
|
$queryBuilder |
188
|
|
|
->andWhere('MATCH_AGAINST(rac.email, :email) > 0') |
189
|
|
|
->setParameter('email', $query->email); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
if (!empty($query->secondFactorTypes)) { |
193
|
|
|
$queryBuilder |
194
|
|
|
->innerJoin(VettedSecondFactor::class, 'vsf', Join::WITH, 'rac.identityId = vsf.identityId') |
195
|
|
|
->andWhere('vsf.type IN (:secondFactorTypes)') |
196
|
|
|
->setParameter('secondFactorTypes', $query->secondFactorTypes); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$queryBuilder->groupBy('rac.identityId'); |
200
|
|
|
|
201
|
|
|
return $queryBuilder->getQuery(); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param RaCandidateQuery $query |
206
|
|
|
* @return \Doctrine\ORM\Query |
207
|
|
|
*/ |
208
|
|
|
public function createOptionsQuery(RaCandidateQuery $query) |
209
|
|
|
{ |
210
|
|
|
$queryBuilder = $this->createQueryBuilder('rac') |
211
|
|
|
->select('rac.institution') |
212
|
|
|
->groupBy('rac.institution'); |
213
|
|
|
|
214
|
|
|
// Modify query to filter on authorization |
215
|
|
|
$this->authorizationRepositoryFilter->filter( |
216
|
|
|
$queryBuilder, |
217
|
|
|
$query->authorizationContext, |
218
|
|
|
'rac.raInstitution', |
219
|
|
|
'iac' |
220
|
|
|
); |
221
|
|
|
|
222
|
|
|
return $queryBuilder->getQuery(); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param string[] $sraaList |
227
|
|
|
* @return RaCandidate[] |
228
|
|
|
*/ |
229
|
|
|
public function findByNameIds(array $sraaList) |
230
|
|
|
{ |
231
|
|
|
return $this->createQueryBuilder('rac') |
232
|
|
|
->where('rac.nameId IN (:sraaList)') |
233
|
|
|
->setParameter('sraaList', $sraaList) |
234
|
|
|
->getQuery() |
235
|
|
|
->getResult(); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @param string $identityId |
240
|
|
|
* @return null|RaCandidate |
241
|
|
|
* @throws \Doctrine\ORM\NonUniqueResultException |
242
|
|
|
*/ |
243
|
|
|
public function findByIdentityId($identityId) |
244
|
|
|
{ |
245
|
|
|
return $this->createQueryBuilder('rac') |
246
|
|
|
->where('rac.identityId = :identityId') |
247
|
|
|
->setParameter('identityId', $identityId) |
248
|
|
|
->getQuery() |
249
|
|
|
->getOneOrNullResult(); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param string $identityId |
254
|
|
|
* @param Institution $raInstitution |
255
|
|
|
* @return null|RaCandidate |
256
|
|
|
* @throws \Doctrine\ORM\NonUniqueResultException |
257
|
|
|
*/ |
258
|
|
|
public function findByIdentityIdAndRaInstitution($identityId, Institution $raInstitution) |
259
|
|
|
{ |
260
|
|
|
return $this->createQueryBuilder('rac') |
261
|
|
|
->where('rac.identityId = :identityId') |
262
|
|
|
->andWhere('rac.raInstitution = :raInstitution') |
263
|
|
|
->setParameter('identityId', $identityId) |
264
|
|
|
->setParameter('raInstitution', $raInstitution) |
265
|
|
|
->getQuery() |
266
|
|
|
->getOneOrNullResult(); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @param string $identityId |
272
|
|
|
* @param InstitutionAuthorizationContextInterface $authorizationContext |
273
|
|
|
* @return RaCandidate[] |
274
|
|
|
*/ |
275
|
|
|
public function findAllRaasByIdentityIdAndRaInstitution($identityId, InstitutionAuthorizationContextInterface $authorizationContext) |
276
|
|
|
{ |
277
|
|
|
$queryBuilder = $this->createQueryBuilder('rac') |
278
|
|
|
->where('rac.identityId = :identityId') |
279
|
|
|
->setParameter('identityId', $identityId) |
280
|
|
|
->orderBy('rac.raInstitution'); |
281
|
|
|
|
282
|
|
|
// Modify query to filter on authorization |
283
|
|
|
$this->authorizationRepositoryFilter->filter( |
284
|
|
|
$queryBuilder, |
285
|
|
|
$authorizationContext, |
286
|
|
|
'rac.raInstitution', |
287
|
|
|
'iac' |
288
|
|
|
); |
289
|
|
|
|
290
|
|
|
return $queryBuilder->getQuery()->getResult(); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* @param Institution $raInstitution |
295
|
|
|
* @return ArrayCollection|RaCandidate[] |
|
|
|
|
296
|
|
|
* @throws \Doctrine\ORM\NonUniqueResultException |
297
|
|
|
*/ |
298
|
|
|
public function findByRaInstitution(Institution $raInstitution) |
299
|
|
|
{ |
300
|
|
|
return $this->createQueryBuilder('rac') |
301
|
|
|
->where('rac.raInstitution = :raInstitution') |
302
|
|
|
->setParameter('raInstitution', $raInstitution) |
303
|
|
|
->getQuery() |
304
|
|
|
->getResult(); |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|
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.