Completed
Pull Request — feature/fine-grained-authoriza... (#242)
by
unknown
05:24 queued 02:46
created

IdentityRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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