1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
5
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace eZ\Publish\Core\Persistence\Legacy\User\Gateway; |
10
|
|
|
|
11
|
|
|
use Doctrine\DBAL\Connection; |
12
|
|
|
use Doctrine\DBAL\FetchMode; |
13
|
|
|
use Doctrine\DBAL\ParameterType; |
14
|
|
|
use Doctrine\DBAL\Query\QueryBuilder; |
15
|
|
|
use eZ\Publish\Core\Persistence\Legacy\User\Gateway; |
16
|
|
|
use eZ\Publish\SPI\Persistence\User\UserTokenUpdateStruct; |
17
|
|
|
use function time; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* User gateway implementation using the Doctrine database. |
21
|
|
|
* |
22
|
|
|
* @internal Gateway implementation is considered internal. Use Persistence User Handler instead. |
23
|
|
|
* |
24
|
|
|
* @see \eZ\Publish\SPI\Persistence\User\Handler |
25
|
|
|
*/ |
26
|
|
|
final class DoctrineDatabase extends Gateway |
27
|
|
|
{ |
28
|
|
|
/** @var \Doctrine\DBAL\Connection */ |
29
|
|
|
private $connection; |
30
|
|
|
|
31
|
|
|
/** @var \Doctrine\DBAL\Platforms\AbstractPlatform */ |
32
|
|
|
private $dbPlatform; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @throws \Doctrine\DBAL\DBALException |
36
|
|
|
*/ |
37
|
|
|
public function __construct(Connection $connection) |
38
|
|
|
{ |
39
|
|
|
$this->connection = $connection; |
40
|
|
|
$this->dbPlatform = $this->connection->getDatabasePlatform(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
View Code Duplication |
public function load(int $userId): array |
44
|
|
|
{ |
45
|
|
|
$query = $this->getLoadUserQueryBuilder(); |
46
|
|
|
$query |
47
|
|
|
->where( |
48
|
|
|
$query->expr()->eq( |
49
|
|
|
'u.contentobject_id', |
50
|
|
|
$query->createPositionalParameter($userId, ParameterType::INTEGER) |
51
|
|
|
) |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
$statement = $query->execute(); |
55
|
|
|
|
56
|
|
|
return $statement->fetchAll(FetchMode::ASSOCIATIVE); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function loadByLogin(string $login): array |
60
|
|
|
{ |
61
|
|
|
$query = $this->getLoadUserQueryBuilder(); |
62
|
|
|
$expr = $query->expr(); |
63
|
|
|
$query |
64
|
|
|
->where( |
65
|
|
|
$expr->eq( |
66
|
|
|
$this->dbPlatform->getLowerExpression('u.login'), |
67
|
|
|
// Index is case in-sensitive, on some db's lowercase, so we lowercase $login |
68
|
|
|
$query->createPositionalParameter( |
69
|
|
|
mb_strtolower($login, 'UTF-8'), |
70
|
|
|
ParameterType::STRING |
71
|
|
|
) |
72
|
|
|
) |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
return $query->execute()->fetchAll(FetchMode::ASSOCIATIVE); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
View Code Duplication |
public function loadByEmail(string $email): array |
79
|
|
|
{ |
80
|
|
|
$query = $this->getLoadUserQueryBuilder(); |
81
|
|
|
$query->where( |
82
|
|
|
$query->expr()->eq( |
83
|
|
|
'u.email', |
84
|
|
|
$query->createPositionalParameter($email, ParameterType::STRING) |
85
|
|
|
) |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$statement = $query->execute(); |
89
|
|
|
|
90
|
|
|
return $statement->fetchAll(FetchMode::ASSOCIATIVE); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function loadUserByToken(string $hash): array |
94
|
|
|
{ |
95
|
|
|
$query = $this->getLoadUserQueryBuilder(); |
96
|
|
|
$query |
97
|
|
|
->leftJoin( |
98
|
|
|
'u', |
99
|
|
|
'ezuser_accountkey', |
100
|
|
|
'token', |
101
|
|
|
$query->expr()->eq( |
102
|
|
|
'token.user_id', |
103
|
|
|
'u.contentobject_id' |
104
|
|
|
) |
105
|
|
|
) |
106
|
|
|
->where( |
107
|
|
|
$query->expr()->eq( |
108
|
|
|
'token.hash_key', |
109
|
|
|
$query->createPositionalParameter($hash, ParameterType::STRING) |
110
|
|
|
) |
111
|
|
|
) |
112
|
|
|
->andWhere( |
113
|
|
|
$query->expr()->gte( |
114
|
|
|
'token.time', |
115
|
|
|
$query->createPositionalParameter(time(), ParameterType::INTEGER) |
116
|
|
|
) |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
$statement = $query->execute(); |
120
|
|
|
|
121
|
|
|
return $statement->fetchAll(FetchMode::ASSOCIATIVE); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function updateUserToken(UserTokenUpdateStruct $userTokenUpdateStruct): void |
125
|
|
|
{ |
126
|
|
|
$query = $this->connection->createQueryBuilder(); |
127
|
|
|
if (false === $this->userHasToken($userTokenUpdateStruct->userId)) { |
128
|
|
|
$query |
129
|
|
|
->insert('ezuser_accountkey') |
130
|
|
|
->values( |
131
|
|
|
[ |
132
|
|
|
'hash_key' => ':hash_key', |
133
|
|
|
'time' => ':time', |
134
|
|
|
'user_id' => ':user_id', |
135
|
|
|
] |
136
|
|
|
); |
137
|
|
|
} else { |
138
|
|
|
$query |
139
|
|
|
->update('ezuser_accountkey') |
140
|
|
|
->set('hash_key', ':hash_key') |
141
|
|
|
->set('time', ':time') |
142
|
|
|
->where('user_id = :user_id'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$query->setParameter('hash_key', $userTokenUpdateStruct->hashKey, ParameterType::STRING); |
146
|
|
|
$query->setParameter('time', $userTokenUpdateStruct->time, ParameterType::INTEGER); |
147
|
|
|
$query->setParameter('user_id', $userTokenUpdateStruct->userId, ParameterType::INTEGER); |
148
|
|
|
|
149
|
|
|
$query->execute(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function expireUserToken(string $hash): void |
153
|
|
|
{ |
154
|
|
|
$query = $this->connection->createQueryBuilder(); |
155
|
|
|
$query |
156
|
|
|
->update('ezuser_accountkey') |
157
|
|
|
->set( |
158
|
|
|
'time', |
159
|
|
|
$query->createPositionalParameter(0, ParameterType::INTEGER) |
160
|
|
|
)->where( |
161
|
|
|
$query->expr()->eq( |
162
|
|
|
'hash_key', |
163
|
|
|
$query->createPositionalParameter($hash, ParameterType::STRING) |
164
|
|
|
) |
165
|
|
|
); |
166
|
|
|
$query->execute(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function assignRole(int $contentId, int $roleId, array $limitation): void |
170
|
|
|
{ |
171
|
|
|
foreach ($limitation as $identifier => $values) { |
172
|
|
|
foreach ($values as $value) { |
173
|
|
|
$query = $this->connection->createQueryBuilder(); |
174
|
|
|
$query |
175
|
|
|
->insert('ezuser_role') |
176
|
|
|
->values( |
177
|
|
|
[ |
178
|
|
|
'contentobject_id' => $query->createPositionalParameter( |
179
|
|
|
$contentId, |
180
|
|
|
ParameterType::INTEGER |
181
|
|
|
), |
182
|
|
|
'role_id' => $query->createPositionalParameter( |
183
|
|
|
$roleId, |
184
|
|
|
ParameterType::INTEGER |
185
|
|
|
), |
186
|
|
|
'limit_identifier' => $query->createPositionalParameter( |
187
|
|
|
$identifier, |
188
|
|
|
ParameterType::STRING |
189
|
|
|
), |
190
|
|
|
'limit_value' => $query->createPositionalParameter( |
191
|
|
|
$value, |
192
|
|
|
ParameterType::STRING |
193
|
|
|
), |
194
|
|
|
] |
195
|
|
|
); |
196
|
|
|
$query->execute(); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
View Code Duplication |
public function removeRole(int $contentId, int $roleId): void |
202
|
|
|
{ |
203
|
|
|
$query = $this->connection->createQueryBuilder(); |
204
|
|
|
$expr = $query->expr(); |
205
|
|
|
$query |
206
|
|
|
->delete('ezuser_role') |
207
|
|
|
->where( |
208
|
|
|
$expr->eq( |
209
|
|
|
'contentobject_id', |
210
|
|
|
$query->createPositionalParameter($contentId, ParameterType::INTEGER) |
211
|
|
|
) |
212
|
|
|
) |
213
|
|
|
->andWhere( |
214
|
|
|
$expr->eq( |
215
|
|
|
'role_id', |
216
|
|
|
$query->createPositionalParameter($roleId, ParameterType::INTEGER) |
217
|
|
|
) |
218
|
|
|
); |
219
|
|
|
$query->execute(); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
View Code Duplication |
public function removeRoleAssignmentById(int $roleAssignmentId): void |
223
|
|
|
{ |
224
|
|
|
$query = $this->connection->createQueryBuilder(); |
225
|
|
|
$query |
226
|
|
|
->delete('ezuser_role') |
227
|
|
|
->where( |
228
|
|
|
$query->expr()->eq( |
229
|
|
|
'id', |
230
|
|
|
$query->createPositionalParameter($roleAssignmentId, ParameterType::INTEGER) |
231
|
|
|
) |
232
|
|
|
); |
233
|
|
|
$query->execute(); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
private function getLoadUserQueryBuilder(): QueryBuilder |
237
|
|
|
{ |
238
|
|
|
$query = $this->connection->createQueryBuilder(); |
239
|
|
|
$expr = $query->expr(); |
240
|
|
|
$query |
241
|
|
|
->select( |
242
|
|
|
'u.contentobject_id', |
243
|
|
|
'u.login', |
244
|
|
|
'u.email', |
245
|
|
|
'u.password_hash', |
246
|
|
|
'u.password_hash_type', |
247
|
|
|
'u.password_updated_at', |
248
|
|
|
's.is_enabled', |
249
|
|
|
's.max_login' |
250
|
|
|
) |
251
|
|
|
->from('ezuser', 'u') |
252
|
|
|
->leftJoin( |
253
|
|
|
'u', |
254
|
|
|
'ezuser_setting', |
255
|
|
|
's', |
256
|
|
|
$expr->eq( |
257
|
|
|
's.user_id', |
258
|
|
|
'u.contentobject_id' |
259
|
|
|
) |
260
|
|
|
); |
261
|
|
|
|
262
|
|
|
return $query; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
View Code Duplication |
private function userHasToken(int $userId): bool |
266
|
|
|
{ |
267
|
|
|
$query = $this->connection->createQueryBuilder(); |
268
|
|
|
$expr = $query->expr(); |
269
|
|
|
$query |
270
|
|
|
->select('token.id') |
271
|
|
|
->from('ezuser_accountkey', 'token') |
272
|
|
|
->where( |
273
|
|
|
$expr->eq( |
274
|
|
|
'token.user_id', |
275
|
|
|
$query->createPositionalParameter( |
276
|
|
|
$userId, |
277
|
|
|
ParameterType::INTEGER |
278
|
|
|
) |
279
|
|
|
) |
280
|
|
|
); |
281
|
|
|
|
282
|
|
|
return !empty($query->execute()->fetch(FetchMode::ASSOCIATIVE)); |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|