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\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; |
22
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
23
|
|
|
use Doctrine\ORM\Query; |
24
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
25
|
|
|
use Surfnet\Stepup\Identity\Value\CommonName; |
26
|
|
|
use Surfnet\Stepup\Identity\Value\DocumentNumber; |
27
|
|
|
use Surfnet\Stepup\Identity\Value\Email; |
28
|
|
|
use Surfnet\Stepup\Identity\Value\IdentityId; |
29
|
|
|
use Surfnet\Stepup\Identity\Value\Institution; |
30
|
|
|
use Surfnet\Stepup\Identity\Value\NameId; |
31
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\Identity; |
32
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\IdentityQuery; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @extends ServiceEntityRepository<Identity> |
36
|
|
|
*/ |
|
|
|
|
37
|
|
|
class IdentityRepository extends ServiceEntityRepository |
38
|
|
|
{ |
39
|
|
|
public function __construct( |
40
|
|
|
ManagerRegistry $registry, |
41
|
|
|
) { |
42
|
|
|
parent::__construct($registry, Identity::class); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function find(mixed $id, $lockMode = null, $lockVersion = null): ?Identity |
46
|
|
|
{ |
47
|
|
|
/** @var Identity|null $identity */ |
|
|
|
|
48
|
|
|
$identity = parent::find($id); |
49
|
|
|
|
50
|
|
|
return $identity; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function save(Identity $identity): void |
54
|
|
|
{ |
55
|
|
|
$entityManager = $this->getEntityManager(); |
56
|
|
|
$entityManager->persist($identity); |
57
|
|
|
$entityManager->flush(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
|
|
|
|
61
|
|
|
* @return Query |
62
|
|
|
*/ |
63
|
|
|
public function createSearchQuery( |
64
|
|
|
IdentityQuery $query, |
65
|
|
|
): Query { |
66
|
|
|
$queryBuilder = $this->createQueryBuilder('i'); |
67
|
|
|
|
68
|
|
|
if ($query->institution) { |
69
|
|
|
$queryBuilder |
70
|
|
|
->andWhere('i.institution = :institution') |
71
|
|
|
->setParameter('institution', $query->institution); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($query->nameId) { |
75
|
|
|
$queryBuilder |
76
|
|
|
->andWhere('i.nameId = :nameId') |
77
|
|
|
->setParameter('nameId', $query->nameId); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if ($query->email) { |
81
|
|
|
$queryBuilder |
82
|
|
|
->andWhere('i.email LIKE :email') |
83
|
|
|
->setParameter('email', sprintf('%%%s%%', $query->email)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ($query->commonName) { |
87
|
|
|
$queryBuilder |
88
|
|
|
->andWhere('i.commonName LIKE :commonName') |
89
|
|
|
->setParameter('commonName', sprintf('%%%s%%', $query->commonName)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $queryBuilder->getQuery(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string[] $nameIds |
|
|
|
|
97
|
|
|
* @return Identity[] Indexed by NameID. |
|
|
|
|
98
|
|
|
*/ |
99
|
|
|
public function findByNameIdsIndexed(array $nameIds): array |
100
|
|
|
{ |
101
|
|
|
return $this->getEntityManager()->createQueryBuilder() |
102
|
|
|
->select('i') |
103
|
|
|
->from(Identity::class, 'i', 'i.nameId') |
104
|
|
|
->where('i.nameId IN (:nameIds)') |
105
|
|
|
->setParameter('nameIds', $nameIds) |
106
|
|
|
->getQuery() |
107
|
|
|
->getResult(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
|
|
|
|
111
|
|
|
* |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
|
|
public function hasIdentityWithNameIdAndInstitution(NameId $nameId, Institution $institution): bool |
115
|
|
|
{ |
116
|
|
|
$identityCount = $this->createQueryBuilder('i') |
117
|
|
|
->select('COUNT(i.id)') |
118
|
|
|
->where('i.nameId = :nameId') |
119
|
|
|
->andWhere('i.institution = :institution') |
120
|
|
|
->setParameter('nameId', $nameId->getNameId()) |
121
|
|
|
->setParameter('institution', $institution->getInstitution()) |
122
|
|
|
->getQuery() |
123
|
|
|
->getSingleScalarResult(); |
124
|
|
|
|
125
|
|
|
return $identityCount > 0; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
|
|
|
|
129
|
|
|
* @return Identity |
130
|
|
|
*/ |
131
|
|
|
public function findOneByNameIdAndInstitution(NameId $nameId, Institution $institution): Identity |
132
|
|
|
{ |
133
|
|
|
return $this->createQueryBuilder('i') |
134
|
|
|
->where('i.nameId = :nameId') |
135
|
|
|
->setParameter('nameId', $nameId->getNameId()) |
136
|
|
|
->andWhere('i.institution = :institution') |
137
|
|
|
->setParameter('institution', $institution->getInstitution()) |
138
|
|
|
->getQuery() |
139
|
|
|
->getSingleResult(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function findOneByNameId(string $nameId): ?Identity |
143
|
|
|
{ |
144
|
|
|
return $this->findOneBy(['nameId' => $nameId]); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function updateStatusByIdentityIdToForgotten(IdentityId $identityId): void |
148
|
|
|
{ |
149
|
|
|
$this->getEntityManager()->createQueryBuilder() |
150
|
|
|
->update($this->getEntityName(), 'i') |
151
|
|
|
->set('i.commonName', ":name") |
152
|
|
|
->set('i.email', ":email") |
153
|
|
|
->where('i.id = :id') |
154
|
|
|
->setParameter('id', $identityId->getIdentityId()) |
155
|
|
|
->setParameter('name', CommonName::unknown()) |
156
|
|
|
->setParameter('email', Email::unknown()) |
157
|
|
|
->getQuery() |
158
|
|
|
->execute(); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|