Completed
Pull Request — feature/fine-grained-authoriza... (#246)
by
unknown
56:03 queued 45:22
created

RaCandidateRepository::createSearchQuery()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
c 0
b 0
f 0
rs 8.6577
cc 6
nc 32
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
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
        $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($queryBuilder, $query->authorizationContext, 'rac.identityId', 'rac.institution', 'iac');
168
169
        if ($query->actorInstitution) {
170
            $queryBuilder
171
                ->andWhere('rac.raInstitution = :raInstitution')
172
                ->setParameter('raInstitution', $query->institution);
173
        }
174
175
        if ($query->institution) {
176
            $queryBuilder
177
                ->andWhere('rac.institution = :institution')
178
                ->setParameter('institution', $query->institution);
179
        }
180
181
        if ($query->commonName) {
182
            $queryBuilder
183
                ->andWhere('MATCH_AGAINST(rac.commonName, :commonName) > 0')
184
                ->setParameter('commonName', $query->commonName);
185
        }
186
187
        if ($query->email) {
188
            $queryBuilder
189
                ->andWhere('MATCH_AGAINST(rac.email, :email) > 0')
190
                ->setParameter('email', $query->email);
191
        }
192
193
        if (!empty($query->secondFactorTypes)) {
194
            $queryBuilder
195
                ->innerJoin(VettedSecondFactor::class, 'vsf', Join::WITH, 'rac.identityId = vsf.identityId')
196
                ->andWhere('vsf.type IN (:secondFactorTypes)')
197
                ->setParameter('secondFactorTypes', $query->secondFactorTypes);
198
        }
199
200
        return $queryBuilder->getQuery();
201
    }
202
203
    /**
204
     * @param string[] $sraaList
205
     * @return RaCandidate[]
206
     */
207
    public function findByNameIds(array $sraaList)
208
    {
209
        return $this->createQueryBuilder('rac')
210
            ->where('rac.nameId IN (:sraaList)')
211
            ->setParameter('sraaList', $sraaList)
212
            ->getQuery()
213
            ->getResult();
214
    }
215
216
    /**
217
     * @param string $identityId
218
     * @return null|RaCandidate
219
     * @throws \Doctrine\ORM\NonUniqueResultException
220
     */
221
    public function findByIdentityId($identityId)
222
    {
223
        return $this->createQueryBuilder('rac')
224
            ->where('rac.identityId = :identityId')
225
            ->setParameter('identityId', $identityId)
226
            ->getQuery()
227
            ->getOneOrNullResult();
228
    }
229
230
    /**
231
     * @param string $identityId
232
     * @param Institution $raInstitution
233
     * @return null|RaCandidate
234
     * @throws \Doctrine\ORM\NonUniqueResultException
235
     */
236
    public function findByIdentityIdAndRaInstitution($identityId, Institution $raInstitution)
237
    {
238
        return $this->createQueryBuilder('rac')
239
            ->where('rac.identityId = :identityId')
240
            ->andWhere('rac.raInstitution = :raInstitution')
241
            ->setParameter('identityId', $identityId)
242
            ->setParameter('raInstitution', $raInstitution)
243
            ->getQuery()
244
            ->getOneOrNullResult();
245
    }
246
247
    /**
248
     * @param Institution $raInstitution
249
     * @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...
250
     * @throws \Doctrine\ORM\NonUniqueResultException
251
     */
252
    public function findByRaInstitution(Institution $raInstitution)
253
    {
254
        return $this->createQueryBuilder('rac')
255
            ->where('rac.raInstitution = :raInstitution')
256
            ->setParameter('raInstitution', $raInstitution)
257
            ->getQuery()
258
            ->getResult();
259
    }
260
}
261