Completed
Push — feature/self-service-verified-... ( 7898a5...f60261 )
by Michiel
03:32
created

createSearchQuery()   B

Complexity

Conditions 7
Paths 32

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 8.2986
c 0
b 0
f 0
cc 7
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
use DateTime;
22
use Doctrine\ORM\EntityManager;
23
use Doctrine\ORM\EntityRepository;
24
use Doctrine\ORM\Mapping;
25
use Doctrine\ORM\Query;
26
use Surfnet\Stepup\Identity\Value\IdentityId;
27
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Filter\InstitutionAuthorizationRepositoryFilter;
28
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Value\InstitutionAuthorizationContextInterface;
29
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\VerifiedSecondFactor;
30
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\VerifiedSecondFactorOfIdentityQuery;
31
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\VerifiedSecondFactorQuery;
32
33
class VerifiedSecondFactorRepository extends EntityRepository
34
{
35
    /**
36
     * @var InstitutionAuthorizationRepositoryFilter
37
     */
38
    private $authorizationRepositoryFilter;
39
40
    /**
41
     * VerifiedSecondFactorRepository constructor.
42
     * @param EntityManager $em
43
     * @param Mapping\ClassMetadata $class
44
     * @param InstitutionAuthorizationRepositoryFilter $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
    /**
57
     * @param string $id
58
     * @return VerifiedSecondFactor|null
59
     */
60
    public function find($id)
61
    {
62
        /** @var VerifiedSecondFactor|null $secondFactor */
63
        $secondFactor = parent::find($id);
64
65
        return $secondFactor;
66
    }
67
68
    /**
69
     * @param DateTime $requestedAt
70
     * @return VerifiedSecondFactor[]
71
     */
72
    public function findByDate(DateTime $requestedAt)
73
    {
74
        $fromDate = clone $requestedAt;
75
        $fromDate->setTime(0, 0, 0);
76
77
        $toDate = clone $requestedAt;
78
        $toDate->setTime(23, 59, 59);
79
80
        return $this->createQueryBuilder('sf')
81
            ->where('sf.registrationRequestedAt <= :toDate')
82
            ->andWhere('sf.registrationRequestedAt >= :fromDate')
83
            ->setParameter('toDate', $toDate)
84
            ->setParameter('fromDate', $fromDate)
85
            ->getQuery()
86
            ->getResult();
87
    }
88
89
    /**
90
     * @param VerifiedSecondFactorQuery $query
91
     * @return Query
92
     */
93
    public function createSearchQuery(VerifiedSecondFactorQuery $query)
94
    {
95
        $queryBuilder = $this->createQueryBuilder('sf');
96
97
        // In certain situations, the FGA filtering is not applied.
98
        $applyFgaFilter = true;
99
100
        // The SRAA user does not adhere to the FGA filter rules when searching for a registration code.
101
        // This way the SRAA does not have to switch to a certain institution to start the vetting process.
102
        if ($query->authorizationContext->isActorSraa() && is_string($query->registrationCode)) {
103
            $applyFgaFilter = false;
104
        }
105
106
        if ($query->identityId) {
107
            $queryBuilder
108
                ->andWhere('sf.identityId = :identityId')
109
                ->setParameter('identityId', (string) $query->identityId);
110
        }
111
112
        if ($query->secondFactorId) {
113
            $queryBuilder
114
                ->andWhere('sf.id = :secondFactorId')
115
                ->setParameter('secondFactorId', (string) $query->secondFactorId);
116
        }
117
118
        if (is_string($query->registrationCode)) {
119
            $queryBuilder
120
                ->andWhere('sf.registrationCode = :registrationCode')
121
                ->setParameter('registrationCode', $query->registrationCode);
122
        }
123
124
        if ($applyFgaFilter) {
125
            // Modify query to filter on authorization
126
            $this->authorizationRepositoryFilter->filter(
127
                $queryBuilder,
128
                $query->authorizationContext,
129
                'sf.institution',
130
                'iac'
131
            );
132
        }
133
134
        return $queryBuilder->getQuery();
135
    }
136
137
    /**
138
     * @param VerifiedSecondFactorOfIdentityQuery $query
139
     * @return Query
140
     */
141 View Code Duplication
    public function createSearchForIdentityQuery(VerifiedSecondFactorOfIdentityQuery $query)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
    {
143
        $queryBuilder = $this->createQueryBuilder('sf');
144
145
        $queryBuilder
146
            ->andWhere('sf.identityId = :identityId')
147
            ->setParameter('identityId', (string) $query->identityId);
148
149
        return $queryBuilder->getQuery();
150
    }
151
152
    public function removeByIdentityId(IdentityId $identityId)
153
    {
154
        $this->getEntityManager()->createQueryBuilder()
155
            ->delete($this->_entityName, 'sf')
156
            ->where('sf.identityId = :identityId')
157
            ->setParameter('identityId', $identityId->getIdentityId())
158
            ->getQuery()
159
            ->execute();
160
    }
161
162
    /**
163
     * @param VerifiedSecondFactor $secondFactor
164
     */
165
    public function save(VerifiedSecondFactor $secondFactor)
166
    {
167
        $this->getEntityManager()->persist($secondFactor);
168
        $this->getEntityManager()->flush();
169
    }
170
171
    public function remove(VerifiedSecondFactor $secondFactor)
172
    {
173
        $this->getEntityManager()->remove($secondFactor);
174
        $this->getEntityManager()->flush();
175
    }
176
}
177