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
|
|
|
|
22
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
23
|
|
|
use Doctrine\ORM\Mapping; |
24
|
|
|
use Doctrine\ORM\EntityManager; |
25
|
|
|
use Doctrine\ORM\EntityRepository; |
26
|
|
|
use Doctrine\ORM\Query\Expr\Join; |
27
|
|
|
use Surfnet\Stepup\Identity\Collection\InstitutionCollection; |
28
|
|
|
use Surfnet\Stepup\Identity\Value\IdentityId; |
29
|
|
|
use Surfnet\Stepup\Identity\Value\Institution; |
30
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Filter\InstitutionAuthorizationRepositoryFilter; |
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
|
|
|
$this->getEntityManager()->merge($raCandidate); |
62
|
|
|
$this->getEntityManager()->flush(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param IdentityId $identityId |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
|
|
public function removeByIdentityId(IdentityId $identityId) |
70
|
|
|
{ |
71
|
|
|
$raCandidate = $this->findByIdentityId($identityId); |
72
|
|
|
|
73
|
|
|
if (!$raCandidate) { |
74
|
|
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->getEntityManager()->remove($raCandidate); |
78
|
|
|
$this->getEntityManager()->flush(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param Institution $institution |
83
|
|
|
* @param InstitutionCollection $raInstitutions |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
|
|
public function removeInstitutionsNotInList(Institution $institution, InstitutionCollection $raInstitutions) |
87
|
|
|
{ |
88
|
|
|
$raCandidates = $this->createQueryBuilder('rac') |
89
|
|
|
->where('rac.raInstitution = :raInstitution') |
90
|
|
|
->andWhere('rac.institution NOT IN (:institutions)') |
91
|
|
|
->setParameter('raInstitution', $institution) |
92
|
|
|
->setParameter('institutions', $raInstitutions->serialize()) |
93
|
|
|
->getQuery() |
94
|
|
|
->getResult(); |
95
|
|
|
|
96
|
|
|
$em = $this->getEntityManager(); |
97
|
|
|
foreach ($raCandidates as $raCandidate) { |
98
|
|
|
$em->remove($raCandidate); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$em->flush(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param Institution $raInstitution |
106
|
|
|
* @return void |
107
|
|
|
*/ |
108
|
|
|
public function removeByRaInstitution(Institution $raInstitution) |
109
|
|
|
{ |
110
|
|
|
$raCandidates = $this->findByRaInstitution($raInstitution); |
111
|
|
|
|
112
|
|
|
if (empty($raCandidates)) { |
113
|
|
|
return; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$em = $this->getEntityManager(); |
117
|
|
|
foreach ($raCandidates as $raCandidate) { |
118
|
|
|
$em->remove($raCandidate); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$em->flush(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param IdentityId $identityId |
126
|
|
|
* @param Institution $raInstitution |
127
|
|
|
* @return void |
128
|
|
|
*/ |
129
|
|
|
public function removeByIdentityIdAndRaInstitution(IdentityId $identityId, Institution $raInstitution) |
130
|
|
|
{ |
131
|
|
|
$raCandidate = $this->findByIdentityIdAndRaInstitution($identityId, $raInstitution); |
132
|
|
|
|
133
|
|
|
if (!$raCandidate) { |
134
|
|
|
return; |
135
|
|
|
} |
136
|
|
|
$em = $this->getEntityManager(); |
137
|
|
|
$em->remove($raCandidate); |
138
|
|
|
$em->flush(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string[] $nameIds |
143
|
|
|
* @return void |
144
|
|
|
*/ |
145
|
|
|
public function removeByNameIds($nameIds) |
146
|
|
|
{ |
147
|
|
|
$raCandidates = $this->findByNameIds($nameIds); |
148
|
|
|
|
149
|
|
|
$em = $this->getEntityManager(); |
150
|
|
|
foreach ($raCandidates as $raCandidate) { |
151
|
|
|
$em->remove($raCandidate); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$em->flush(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param RaCandidateQuery $query |
159
|
|
|
* @return \Doctrine\ORM\Query |
160
|
|
|
*/ |
161
|
|
|
public function createSearchQuery(RaCandidateQuery $query) |
162
|
|
|
{ |
163
|
|
|
$queryBuilder = $this->createQueryBuilder('rac'); |
164
|
|
|
|
165
|
|
|
// Modify query to filter on authorization |
166
|
|
|
$this->authorizationRepositoryFilter->filter($queryBuilder, $query->authorizationContext, 'rac.identityId', 'rac.institution', 'iac'); |
167
|
|
|
|
168
|
|
|
if ($query->institution) { |
169
|
|
|
$queryBuilder |
170
|
|
|
->andWhere('rac.institution = :institution') |
171
|
|
|
->setParameter('institution', $query->institution); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if ($query->commonName) { |
175
|
|
|
$queryBuilder |
176
|
|
|
->andWhere('MATCH_AGAINST(rac.commonName, :commonName) > 0') |
177
|
|
|
->setParameter('commonName', $query->commonName); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
if ($query->email) { |
181
|
|
|
$queryBuilder |
182
|
|
|
->andWhere('MATCH_AGAINST(rac.email, :email) > 0') |
183
|
|
|
->setParameter('email', $query->email); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
if (!empty($query->secondFactorTypes)) { |
187
|
|
|
$queryBuilder |
188
|
|
|
->innerJoin(VettedSecondFactor::class, 'vsf', Join::WITH, 'rac.identityId = vsf.identityId') |
189
|
|
|
->andWhere('vsf.type IN (:secondFactorTypes)') |
190
|
|
|
->setParameter('secondFactorTypes', $query->secondFactorTypes); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return $queryBuilder->getQuery(); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param string[] $sraaList |
198
|
|
|
* @return RaCandidate[] |
199
|
|
|
*/ |
200
|
|
|
public function findByNameIds(array $sraaList) |
201
|
|
|
{ |
202
|
|
|
return $this->createQueryBuilder('rac') |
203
|
|
|
->where('rac.nameId IN (:sraaList)') |
204
|
|
|
->setParameter('sraaList', $sraaList) |
205
|
|
|
->getQuery() |
206
|
|
|
->getResult(); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param string $identityId |
211
|
|
|
* @return null|RaCandidate |
212
|
|
|
* @throws \Doctrine\ORM\NonUniqueResultException |
213
|
|
|
*/ |
214
|
|
|
public function findByIdentityId($identityId) |
215
|
|
|
{ |
216
|
|
|
return $this->createQueryBuilder('rac') |
217
|
|
|
->where('rac.identityId = :identityId') |
218
|
|
|
->setParameter('identityId', $identityId) |
219
|
|
|
->getQuery() |
220
|
|
|
->getOneOrNullResult(); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @param string $identityId |
225
|
|
|
* @param Institution $raInstitution |
226
|
|
|
* @return null|RaCandidate |
227
|
|
|
* @throws \Doctrine\ORM\NonUniqueResultException |
228
|
|
|
*/ |
229
|
|
|
public function findByIdentityIdAndRaInstitution($identityId, Institution $raInstitution) |
230
|
|
|
{ |
231
|
|
|
return $this->createQueryBuilder('rac') |
232
|
|
|
->where('rac.identityId = :identityId') |
233
|
|
|
->andWhere('rac.raInstitution = :raInstitution') |
234
|
|
|
->setParameter('identityId', $identityId) |
235
|
|
|
->setParameter('raInstitution', $raInstitution) |
236
|
|
|
->getQuery() |
237
|
|
|
->getOneOrNullResult(); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @param Institution $raInstitution |
242
|
|
|
* @return ArrayCollection|RaCandidate[] |
|
|
|
|
243
|
|
|
* @throws \Doctrine\ORM\NonUniqueResultException |
244
|
|
|
*/ |
245
|
|
|
public function findByRaInstitution(Institution $raInstitution) |
246
|
|
|
{ |
247
|
|
|
return $this->createQueryBuilder('rac') |
248
|
|
|
->where('rac.raInstitution = :raInstitution') |
249
|
|
|
->setParameter('raInstitution', $raInstitution) |
250
|
|
|
->getQuery() |
251
|
|
|
->getResult(); |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
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.