Completed
Pull Request — release/3.1 (#301)
by
unknown
04:51 queued 02:19
created

RaCandidateRepository::removeByNameIds()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 1
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
//        $raCandidates = $this->findByIdentityId($identityId);
73
//
74
//        if (empty($raCandidates)) {
75
//            return;
76
//        }
77
//
78
//        foreach ($raCandidates as $candidate) {
79
//            $this->getEntityManager()->remove($candidate);
80
//        }
81
//        $this->getEntityManager()->flush();
82
//    }
83
//
84
//    /**
85
//     * @param Institution $institution
86
//     * @param InstitutionCollection $raInstitutions
87
//     * @return void
88
//     */
89
//    public function removeInstitutionsNotInList(Institution $institution, InstitutionCollection $raInstitutions)
90
//    {
91
//        $raCandidates = $this->createQueryBuilder('rac')
92
//            ->where('rac.raInstitution = :raInstitution')
93
//            ->andWhere('rac.institution NOT IN (:institutions)')
94
//            ->setParameter('raInstitution', $institution)
95
//            ->setParameter('institutions', $raInstitutions->serialize())
96
//            ->getQuery()
97
//            ->getResult();
98
//
99
//        $em = $this->getEntityManager();
100
//        foreach ($raCandidates as $raCandidate) {
101
//            $em->remove($raCandidate);
102
//        }
103
//
104
//        $em->flush();
105
//    }
106
//
107
//    /**
108
//     * @param Institution $raInstitution
109
//     * @return void
110
//     */
111
//    public function removeByRaInstitution(Institution $raInstitution)
112
//    {
113
//        $raCandidates = $this->findByRaInstitution($raInstitution);
114
//
115
//        if (empty($raCandidates)) {
116
//            return;
117
//        }
118
//
119
//        $em = $this->getEntityManager();
120
//        foreach ($raCandidates as $raCandidate) {
121
//            $em->remove($raCandidate);
122
//        }
123
//
124
//        $em->flush();
125
//    }
126
//
127
//    /**
128
//     * @param IdentityId $identityId
129
//     * @param Institution $raInstitution
130
//     * @return void
131
//     */
132
//    public function removeByIdentityIdAndRaInstitution(IdentityId $identityId, Institution $raInstitution)
133
//    {
134
//        $raCandidate = $this->findByIdentityIdAndRaInstitution($identityId, $raInstitution);
135
//
136
//        if (!$raCandidate) {
137
//            return;
138
//        }
139
//        $em = $this->getEntityManager();
140
//        $em->remove($raCandidate);
141
//        $em->flush();
142
//    }
143
//
144
//    /**
145
//     * @param string[] $nameIds
146
//     * @return void
147
//     */
148
//    public function removeByNameIds($nameIds)
149
//    {
150
//        $raCandidates = $this->findByNameIds($nameIds);
151
//
152
//        $em = $this->getEntityManager();
153
//        foreach ($raCandidates as $raCandidate) {
154
//            $em->remove($raCandidate);
155
//        }
156
//
157
//        $em->flush();
158
//    }
159
160
    /**
161
     * @param RaCandidateQuery $query
162
     * @return \Doctrine\ORM\Query
163
     */
164
    public function createSearchQuery(RaCandidateQuery $query)
165
    {
166
        $queryBuilder = $this->createQueryBuilder('rac');
167
168
        // Modify query to filter on authorization:
169
        // For the RA candidates we want the identities that we could make RA. Because we then need to look at the
170
        // select_raa's we have to look at the institution of the candidate because that's the institution we could
171
        // select RA's from. Hence the 'rac.institution'.
172
        $this->authorizationRepositoryFilter->filter(
173
            $queryBuilder,
174
            $query->authorizationContext,
175
            'rac.institution',
176
            'iac'
177
        );
178
179
        if ($query->institution) {
180
            $queryBuilder
181
                ->andWhere('rac.institution = :institution')
182
                ->setParameter('institution', $query->institution);
183
        }
184
185
        if ($query->commonName) {
186
            $queryBuilder
187
                ->andWhere('rac.commonName LIKE :commonName')
188
                ->setParameter('commonName', sprintf('%%%s%%', $query->commonName));
189
        }
190
191
        if ($query->email) {
192
            $queryBuilder
193
                ->andWhere('rac.email LIKE :email')
194
                ->setParameter('email', sprintf('%%%s%%', $query->email));
195
        }
196
197
        if (!empty($query->secondFactorTypes)) {
198
            $queryBuilder
199
                ->innerJoin(VettedSecondFactor::class, 'vsf', Join::WITH, 'rac.identityId = vsf.identityId')
200
                ->andWhere('vsf.type IN (:secondFactorTypes)')
201
                ->setParameter('secondFactorTypes', $query->secondFactorTypes);
202
        }
203
204
        if (!empty($query->raInstitution)) {
205
            $queryBuilder
206
                ->andWhere('rac.raInstitution = :raInstitution')
207
                ->setParameter('raInstitution', $query->raInstitution);
208
        }
209
210
        $queryBuilder->groupBy('rac.identityId');
211
212
        return $queryBuilder->getQuery();
213
    }
214
215
    /**
216
     * @param RaCandidateQuery $query
217
     * @return \Doctrine\ORM\Query
218
     */
219
    public function createOptionsQuery(RaCandidateQuery $query)
220
    {
221
        $queryBuilder = $this->createQueryBuilder('rac')
222
            ->select('rac.institution');
223
224
        // Modify query to filter on authorization:
225
        // For the RA candidates we want the identities that we could make RA. Because we then need to look at the
226
        // select_raa's we have to look at the institution of the candidate because that's the institution we could
227
        // select RA's from. Hence the 'rac.institution'.
228
        $this->authorizationRepositoryFilter->filter(
229
            $queryBuilder,
230
            $query->authorizationContext,
231
            'rac.institution',
232
            'iac'
233
        );
234
235
        return $queryBuilder->getQuery();
236
    }
237
238
    /**
239
     * @param string[] $sraaList
240
     * @return RaCandidate[]
241
     */
242
    public function findByNameIds(array $sraaList)
243
    {
244
        return $this->createQueryBuilder('rac')
245
            ->where('rac.nameId IN (:sraaList)')
246
            ->setParameter('sraaList', $sraaList)
247
            ->getQuery()
248
            ->getResult();
249
    }
250
251
    /**
252
     * @param string $identityId
253
     * @return RaCandidate[]
254
     * @throws \Doctrine\ORM\NonUniqueResultException
255
     */
256
    public function findByIdentityId($identityId)
257
    {
258
        return $this->createQueryBuilder('rac')
259
            ->where('rac.identityId = :identityId')
260
            ->setParameter('identityId', $identityId)
261
            ->getQuery()
262
            ->getResult();
263
    }
264
265
    /**
266
     * @param string $identityId
267
     * @param Institution $raInstitution
268
     * @return null|RaCandidate
269
     * @throws \Doctrine\ORM\NonUniqueResultException
270
     */
271
    public function findByIdentityIdAndRaInstitution($identityId, Institution $raInstitution)
272
    {
273
        return $this->createQueryBuilder('rac')
274
            ->where('rac.identityId = :identityId')
275
            ->andWhere('rac.raInstitution = :raInstitution')
276
            ->setParameter('identityId', $identityId)
277
            ->setParameter('raInstitution', $raInstitution)
278
            ->getQuery()
279
            ->getOneOrNullResult();
280
    }
281
282
    /**
283
     * @param string $identityId
284
     * @param InstitutionAuthorizationContextInterface $authorizationContext
285
     * @return RaCandidate[]
286
     */
287
    public function findAllRaasByIdentityId($identityId, InstitutionAuthorizationContextInterface $authorizationContext)
288
    {
289
        $queryBuilder = $this->createQueryBuilder('rac')
290
            ->where('rac.identityId = :identityId')
291
            ->setParameter('identityId', $identityId)
292
            ->orderBy('rac.raInstitution');
293
294
        // Modify query to filter on authorization:
295
        // For the RA candidates we want the identities that we could make RA. Because we then need to look at the
296
        // select_raa's we have to look at the institution of the candidate because that's the institution we could
297
        // select RA's from. Hence the 'rac.institution'.
298
        $this->authorizationRepositoryFilter->filter(
299
            $queryBuilder,
300
            $authorizationContext,
301
            'rac.institution',
302
            'iac'
303
        );
304
305
        return $queryBuilder->getQuery()->getResult();
306
    }
307
308
    /**
309
     * @param Institution $raInstitution
310
     * @return ArrayCollection|RaCandidate[]
0 ignored issues
show
Documentation introduced by
Should the return type not be array?

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.

Loading history...
311
     * @throws \Doctrine\ORM\NonUniqueResultException
312
     */
313
    public function findByRaInstitution(Institution $raInstitution)
314
    {
315
        return $this->createQueryBuilder('rac')
316
            ->where('rac.raInstitution = :raInstitution')
317
            ->setParameter('raInstitution', $raInstitution)
318
            ->getQuery()
319
            ->getResult();
320
    }
321
}
322