Completed
Pull Request — feature/fine-grained-authoriza... (#246)
by
unknown
40:56 queued 21:52
created

IdentityRepository::find()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
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 Surfnet\Stepup\Identity\Value\IdentityId;
26
use Surfnet\Stepup\Identity\Value\Institution;
27
use Surfnet\Stepup\Identity\Value\NameId;
28
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Filter\InstitutionAuthorizationRepositoryFilter;
29
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\Identity;
30
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\IdentityQuery;
31
32
class IdentityRepository extends EntityRepository
33
{
34
    /**
35
     * @var InstitutionAuthorizationRepositoryFilter
36
     */
37
    private $authorizationRepositoryFilter;
38
39
    public function __construct(
40
        EntityManager $em,
41
        Mapping\ClassMetadata $class,
42
        InstitutionAuthorizationRepositoryFilter $authorizationRepositoryFilter
43
    ) {
44
        parent::__construct($em, $class);
45
        $this->authorizationRepositoryFilter = $authorizationRepositoryFilter;
46
    }
47
48
    /**
49
     * @param string $id
50
     * @return Identity|null
51
     */
52
    public function find($id)
53
    {
54
        /** @var Identity|null $identity */
55
        $identity = parent::find($id);
56
57
        return $identity;
58
    }
59
60
    /**
61
     * @param Identity $identity
62
     */
63
    public function save(Identity $identity)
64
    {
65
        $entityManager = $this->getEntityManager();
66
        $entityManager->persist($identity);
67
        $entityManager->flush();
68
    }
69
70
    /**
71
     * @param IdentityQuery $query
72
     * @return \Doctrine\ORM\Query
73
     */
74 View Code Duplication
    public function createSearchQuery(
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...
75
        IdentityQuery $query
76
    ) {
77
        $queryBuilder = $this->createQueryBuilder('i');
78
79
        // Modify query to filter on authorization
80
        $this->authorizationRepositoryFilter->filter($queryBuilder, $query->authorizationContext, 'i.id', 'i.institution', 'iac');
81
82
        if ($query->nameId) {
83
            $queryBuilder
84
                ->andWhere('i.nameId = :nameId')
85
                ->setParameter('nameId', $query->nameId);
86
        }
87
88
        if ($query->email) {
89
            $queryBuilder
90
                ->andWhere('MATCH_AGAINST(i.email, :email) > 0')
91
                ->setParameter('email', $query->email);
92
        }
93
94
        if ($query->commonName) {
95
            $queryBuilder
96
                ->andWhere('MATCH_AGAINST(i.commonName, :commonName) > 0')
97
                ->setParameter('commonName', $query->commonName);
98
        }
99
100
        return $queryBuilder->getQuery();
101
    }
102
103
    /**
104
     * @param string[] $nameIds
105
     * @return Identity[] Indexed by NameID.
106
     */
107
    public function findByNameIdsIndexed(array $nameIds)
108
    {
109
        return $this->getEntityManager()->createQueryBuilder()
110
            ->select('i')
111
            ->from('Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\Identity', 'i', 'i.nameId')
112
            ->where('i.nameId IN (:nameIds)')
113
            ->setParameter('nameIds', $nameIds)
114
            ->getQuery()
115
            ->getResult();
116
    }
117
118
    /**
119
     * @param NameId      $nameId
120
     * @param Institution $institution
121
     *
122
     * @return bool
123
     */
124
    public function hasIdentityWithNameIdAndInstitution(NameId $nameId, Institution $institution)
125
    {
126
        $identityCount = $this->createQueryBuilder('i')
127
            ->select('COUNT(i.id)')
128
            ->where('i.nameId = :nameId')
129
            ->andWhere('i.institution = :institution')
130
            ->setParameter('nameId', $nameId->getNameId())
131
            ->setParameter('institution', $institution->getInstitution())
132
            ->getQuery()
133
            ->getSingleScalarResult();
134
135
        return $identityCount > 0;
136
    }
137
138
    /**
139
     * @param NameId      $nameId
140
     * @param Institution $institution
141
     * @return Identity
142
     */
143
    public function findOneByNameIdAndInstitution(NameId $nameId, Institution $institution)
144
    {
145
        return $this->createQueryBuilder('i')
146
                ->where('i.nameId = :nameId')
147
                ->setParameter('nameId', $nameId->getNameId())
148
                ->andWhere('i.institution = :institution')
149
                ->setParameter('institution', $institution->getInstitution())
150
                ->getQuery()
151
                ->getSingleResult();
152
    }
153
154 View Code Duplication
    public function removeByIdentityId(IdentityId $identityId)
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...
155
    {
156
        $this->getEntityManager()->createQueryBuilder()
157
            ->delete($this->_entityName, 'i')
158
            ->where('i.id = :identityId')
159
            ->setParameter('identityId', $identityId->getIdentityId())
160
            ->getQuery()
161
            ->execute();
162
    }
163
164
    /**
165
     * @param Institution $institution
166
     * @return ArrayCollection|Identity[]
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...
167
     */
168
    public function findByInstitution(Institution $institution)
169
    {
170
        return $this->createQueryBuilder('i')
171
            ->where('i.institution = :institution')
172
            ->setParameter('institution', $institution->getInstitution())
173
            ->getQuery()
174
            ->getResult();
175
    }
176
}
177