|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use App\Entity\ActiveDirectoryUser; |
|
6
|
|
|
use App\Entity\User; |
|
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
8
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
9
|
|
|
use Doctrine\ORM\Tools\Pagination\Paginator; |
|
10
|
|
|
use Ramsey\Uuid\Uuid; |
|
11
|
|
|
|
|
12
|
|
|
class UserRepository implements UserRepositoryInterface { |
|
13
|
|
|
|
|
14
|
|
|
private $em; |
|
15
|
|
|
|
|
16
|
7 |
|
public function __construct(EntityManagerInterface $objectManager) { |
|
17
|
7 |
|
$this->em = $objectManager; |
|
18
|
7 |
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function findAll($offset = 0, $limit = null, bool $deleted = false) { |
|
21
|
|
|
$qb = $this->em |
|
22
|
|
|
->createQueryBuilder() |
|
23
|
|
|
->select('u') |
|
24
|
|
|
->from(User::class, 'u') |
|
25
|
|
|
->orderBy('u.username', 'asc') |
|
26
|
|
|
->setFirstResult($offset); |
|
27
|
|
|
|
|
28
|
|
|
if($deleted === true) { |
|
29
|
|
|
$qb->where($qb->expr()->isNotNull('u.deletedAt')); |
|
30
|
|
|
} else { |
|
31
|
|
|
$qb->where($qb->expr()->isNull('u.deletedAt')); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
if($limit !== null) { |
|
35
|
|
|
$qb->setMaxResults($limit); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return $qb->getQuery()->getResult(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function findUsersByUsernames(array $usernames) { |
|
42
|
|
|
$qb = $this->em->createQueryBuilder(); |
|
43
|
|
|
|
|
44
|
|
|
$qb->select(['u', 'a', 'r', 't']) |
|
45
|
|
|
->from(User::class, 'u') |
|
46
|
|
|
->leftJoin('u.attributes', 'a') |
|
47
|
|
|
->leftJoin('u.userRoles', 'r') |
|
48
|
|
|
->leftJoin('u.type', 't') |
|
49
|
|
|
->where('u.username IN (:usernames)') |
|
50
|
|
|
->setParameter('usernames', $usernames); |
|
51
|
|
|
|
|
52
|
|
|
return $qb->getQuery()->getResult(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function findUsersUpdatedAfter(\DateTime $dateTime, array $usernames = [ ]) { |
|
56
|
|
|
$qb = $this->em |
|
57
|
|
|
->createQueryBuilder(); |
|
58
|
|
|
|
|
59
|
|
|
$qb->select(['DISTINCT u.username']) |
|
60
|
|
|
->from(User::class, 'u') |
|
61
|
|
|
->leftJoin('u.attributes', 'a') |
|
62
|
|
|
->where( |
|
63
|
|
|
$qb->expr()->orX( |
|
64
|
|
|
$qb->expr()->andX( |
|
65
|
|
|
$qb->expr()->isNotNull('u.updatedAt'), |
|
66
|
|
|
$qb->expr()->gt('u.updatedAt', ':datetime') |
|
67
|
|
|
), |
|
68
|
|
|
$qb->expr()->andX( |
|
69
|
|
|
$qb->expr()->isNotNull('a.updatedAt'), |
|
70
|
|
|
$qb->expr()->gt('a.updatedAt', ':datetime') |
|
71
|
|
|
) |
|
72
|
|
|
) |
|
73
|
|
|
) |
|
74
|
|
|
->setParameter('datetime', $dateTime); |
|
75
|
|
|
|
|
76
|
|
|
if(count($usernames) > 0) { |
|
77
|
|
|
$qb->andWhere('u.username IN (:usernames)') |
|
78
|
|
|
->setParameter('usernames', $usernames); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$usernames = $qb->getQuery()->getScalarResult(); |
|
82
|
|
|
|
|
83
|
|
|
return $this->findUsersByUsernames($usernames); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
1 |
|
public function findOneByUsername(string $username): ?User { |
|
87
|
1 |
|
$qb = $this->em->createQueryBuilder(); |
|
88
|
|
|
|
|
89
|
1 |
|
$qb->select(['u', 'a', 'r', 't']) |
|
90
|
1 |
|
->from(User::class, 'u') |
|
91
|
1 |
|
->leftJoin('u.attributes', 'a') |
|
92
|
1 |
|
->leftJoin('u.userRoles', 'r') |
|
93
|
1 |
|
->leftJoin('u.type', 't') |
|
94
|
1 |
|
->where('u.username = :username') |
|
95
|
1 |
|
->setParameter('username', $username); |
|
96
|
|
|
|
|
97
|
1 |
|
$result = $qb->getQuery()->getResult(); |
|
98
|
|
|
|
|
99
|
1 |
|
if(count($result) === 0) { |
|
100
|
|
|
return null; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
1 |
|
return $result[0]; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
private function createDefaultQueryBuilder(): QueryBuilder { |
|
107
|
|
|
return $this->em |
|
108
|
|
|
->createQueryBuilder() |
|
109
|
|
|
->select(['u', 'a', 'r', 't']) |
|
110
|
|
|
->from(User::class, 'u') |
|
111
|
|
|
->leftJoin('u.attributes', 'a') |
|
112
|
|
|
->leftJoin('u.userRoles', 'r') |
|
113
|
|
|
->leftJoin('u.type', 't'); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @inheritDoc |
|
118
|
|
|
*/ |
|
119
|
|
|
public function findOneByEmail(string $email): ?User { |
|
120
|
|
|
return $this->createDefaultQueryBuilder() |
|
121
|
|
|
->where('u.email = :email') |
|
122
|
|
|
->setParameter('email', $email) |
|
123
|
|
|
->setMaxResults(1) |
|
124
|
|
|
->getQuery() |
|
125
|
|
|
->getOneOrNullResult(); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
1 |
|
public function persist(User $user) { |
|
129
|
1 |
|
$this->em->persist($user); |
|
130
|
1 |
|
$this->em->flush(); |
|
131
|
1 |
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function remove(User $user) { |
|
134
|
|
|
$this->em->remove($user); |
|
135
|
|
|
$this->em->flush(); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function getPaginatedUsers($itemsPerPage, &$page, $type = null, $query = null, bool $deleted = false): Paginator { |
|
139
|
|
|
$qb = $this->em |
|
140
|
|
|
->createQueryBuilder() |
|
141
|
|
|
->select('u') |
|
142
|
|
|
->from(User::class, 'u') |
|
143
|
|
|
->orderBy('u.username', 'asc'); |
|
144
|
|
|
|
|
145
|
|
|
if(!empty($query)) { |
|
146
|
|
|
$qb |
|
147
|
|
|
->andWhere( |
|
148
|
|
|
$qb->expr()->orX( |
|
149
|
|
|
'u.username LIKE :query', |
|
150
|
|
|
'u.firstname LIKE :query', |
|
151
|
|
|
'u.lastname LIKE :query', |
|
152
|
|
|
'u.email LIKE :query' |
|
153
|
|
|
) |
|
154
|
|
|
) |
|
155
|
|
|
->setParameter('query', '%' . $query . '%'); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
if(!empty($type)) { |
|
159
|
|
|
$qb |
|
160
|
|
|
->andWhere( |
|
161
|
|
|
'u.type = :type' |
|
162
|
|
|
) |
|
163
|
|
|
->setParameter('type', $type); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
if($deleted === true) { |
|
167
|
|
|
$qb->andWhere($qb->expr()->isNotNull('u.deletedAt')); |
|
168
|
|
|
} else { |
|
169
|
|
|
$qb->andWhere($qb->expr()->isNull('u.deletedAt')); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
if(!is_numeric($page) || $page < 1) { |
|
173
|
|
|
$page = 1; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
$offset = ($page - 1) * $itemsPerPage; |
|
177
|
|
|
|
|
178
|
|
|
$paginator = new Paginator($qb); |
|
179
|
|
|
$paginator->getQuery() |
|
180
|
|
|
->setMaxResults($itemsPerPage) |
|
181
|
|
|
->setFirstResult($offset); |
|
182
|
|
|
|
|
183
|
|
|
return $paginator; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @inheritDoc |
|
189
|
|
|
*/ |
|
190
|
|
|
public function findActiveDirectoryUserByObjectGuid(string $guid): ?ActiveDirectoryUser { |
|
191
|
|
|
return $this->em->getRepository(ActiveDirectoryUser::class) |
|
192
|
|
|
->findOneBy(['objectGuid' => $guid]); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @inheritDoc |
|
197
|
|
|
*/ |
|
198
|
|
|
public function findAllActiveDirectoryUsersObjectGuid(): array { |
|
199
|
|
|
return array_map(function(array $item) { |
|
200
|
|
|
return $item['objectGuid']; |
|
201
|
|
|
}, |
|
202
|
|
|
$this->em->createQueryBuilder() |
|
203
|
|
|
->select('u.objectGuid') |
|
204
|
|
|
->from(ActiveDirectoryUser::class, 'u') |
|
205
|
|
|
->getQuery() |
|
206
|
|
|
->getScalarResult() |
|
207
|
|
|
); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @inheritDoc |
|
212
|
|
|
*/ |
|
213
|
|
|
public function findAllUuids($offset = 0, $limit = null) { |
|
214
|
|
|
$qb = $this->em |
|
215
|
|
|
->createQueryBuilder() |
|
216
|
|
|
->select('u.uuid') |
|
217
|
|
|
->from(User::class, 'u') |
|
218
|
|
|
->orderBy('u.username', 'asc') |
|
219
|
|
|
->setFirstResult($offset); |
|
220
|
|
|
|
|
221
|
|
|
if($limit !== null) { |
|
222
|
|
|
$qb->setMaxResults($limit); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
return array_map(function(array $item) { |
|
226
|
|
|
return $item['uuid']; |
|
227
|
|
|
}, $qb->getQuery()->getScalarResult()); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
2 |
|
public function findOneByExternalId(string $externalId): ?User { |
|
231
|
2 |
|
return $this->em |
|
232
|
2 |
|
->getRepository(User::class) |
|
233
|
2 |
|
->findOneBy([ |
|
234
|
2 |
|
'externalId' => $externalId |
|
235
|
|
|
]); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
public function findOneByUuid(string $uuid): ?User { |
|
239
|
|
|
return $this->em |
|
240
|
|
|
->getRepository(User::class) |
|
241
|
|
|
->findOneBy([ |
|
242
|
|
|
'uuid' => $uuid |
|
243
|
|
|
]); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* @inheritDoc |
|
248
|
|
|
*/ |
|
249
|
|
|
public function findNextNonProvisionedUsers(int $limit): array { |
|
250
|
|
|
return $this->em |
|
251
|
|
|
->createQueryBuilder() |
|
252
|
|
|
->select('u') |
|
253
|
|
|
->from(User::class, 'u') |
|
254
|
|
|
->orderBy('u.createdAt', 'asc') |
|
255
|
|
|
->where('u.isProvisioned = false') |
|
256
|
|
|
->setMaxResults($limit) |
|
257
|
|
|
->getQuery() |
|
258
|
|
|
->getResult(); |
|
259
|
|
|
} |
|
260
|
|
|
} |