1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Components Bundle Project |
5
|
|
|
* |
6
|
|
|
* (c) Daniel West <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentsBundle\Entity\User; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Annotation\ApiProperty; |
17
|
|
|
use DateTime; |
18
|
|
|
use Lexik\Bundle\JWTAuthenticationBundle\Security\User\JWTUserInterface; |
19
|
|
|
use Ramsey\Uuid\Uuid; |
20
|
|
|
use Silverback\ApiComponentsBundle\Annotation as Silverback; |
21
|
|
|
use Silverback\ApiComponentsBundle\Entity\Utility\IdTrait; |
22
|
|
|
use Silverback\ApiComponentsBundle\Entity\Utility\TimestampedTrait; |
23
|
|
|
use Silverback\ApiComponentsBundle\Validator\Constraints as AcbAssert; |
24
|
|
|
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; |
25
|
|
|
use Symfony\Component\Security\Core\User\UserInterface as SymfonyUserInterface; |
26
|
|
|
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword; |
27
|
|
|
use Symfony\Component\Serializer\Annotation\Groups; |
28
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @author Daniel West <[email protected]> |
32
|
|
|
* |
33
|
|
|
* @Silverback\Timestamped |
34
|
|
|
* @UniqueEntity(fields={"username"}, errorPath="username", message="Sorry, that user already exists in the database.") |
35
|
|
|
* @UniqueEntity(fields={"emailAddress"}, errorPath="emailAddress", message="Sorry, that email address already exists in the database.") |
36
|
|
|
* @AcbAssert\NewEmailAddress(groups={"User:emailAddress", "Default"}) |
37
|
|
|
*/ |
38
|
|
|
abstract class AbstractUser implements SymfonyUserInterface, JWTUserInterface |
39
|
|
|
{ |
40
|
|
|
use IdTrait; |
41
|
|
|
use TimestampedTrait; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @Assert\NotBlank(groups={"Default"}, message="Please enter a username.") |
45
|
|
|
* @Groups({"User:superAdmin", "User:output", "Form:component:read"}) |
46
|
|
|
*/ |
47
|
|
|
protected ?string $username; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @Assert\NotBlank(groups={"Default"}) |
51
|
|
|
* @Assert\Email() |
52
|
|
|
* @Groups({"User:superAdmin", "User:output", "Form:component:read"}) |
53
|
|
|
*/ |
54
|
|
|
protected ?string $emailAddress; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @Groups({"User:superAdmin"}) |
58
|
|
|
*/ |
59
|
|
|
protected array $roles; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @Groups({"User:superAdmin"}) |
63
|
|
|
*/ |
64
|
|
|
protected bool $enabled; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @ApiProperty(readable=false, writable=false) |
68
|
|
|
*/ |
69
|
|
|
protected string $password; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @ApiProperty(readable=false) |
73
|
|
|
* @Assert\NotBlank(message="Please enter your desired password.", groups={"User:password:create"}) |
74
|
|
|
* @Assert\Length(max="4096", min="6", maxMessage="Your password cannot be over 4096 characters", minMessage="Your password must be more than 6 characters long.", groups={"User:password:create"}) |
75
|
|
|
* @Groups({"User:input"}) |
76
|
|
|
*/ |
77
|
|
|
protected ?string $plainPassword = null; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Random string sent to the user email address in order to verify it. |
81
|
|
|
* |
82
|
|
|
* @ApiProperty(readable=false, writable=false) |
83
|
|
|
*/ |
84
|
|
|
protected ?string $newPasswordConfirmationToken = null; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @ApiProperty(readable=false, writable=false) |
88
|
|
|
*/ |
89
|
|
|
public ?string $plainNewPasswordConfirmationToken = null; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @ApiProperty(readable=false, writable=false) |
93
|
|
|
*/ |
94
|
|
|
protected ?DateTime $passwordRequestedAt = null; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @ApiProperty(readable=false) |
98
|
|
|
* @UserPassword(message="You have not entered your current password correctly. Please try again.", groups={"User:password:change"}) |
99
|
|
|
* @Groups({"User:input"}) |
100
|
|
|
*/ |
101
|
|
|
protected ?string $oldPassword = null; |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @ApiProperty(readable=false, writable=false) |
105
|
|
|
*/ |
106
|
|
|
protected ?DateTime $passwordUpdatedAt = null; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @Assert\NotBlank(groups={"User:emailAddress", "Default"}, allowNull=true) |
110
|
|
|
* @Assert\Email() |
111
|
|
|
* @Groups({"User:input", "User:output", "User:emailAddress", "Form:component:read:role_user"}) |
112
|
|
|
*/ |
113
|
|
|
protected ?string $newEmailAddress = null; |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Random string sent to the user's new email address in order to verify it. |
117
|
|
|
* |
118
|
|
|
* @ApiProperty(readable=false, writable=false) |
119
|
|
|
*/ |
120
|
|
|
protected ?string $newEmailConfirmationToken = null; |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @ApiProperty(readable=false, writable=false) |
124
|
|
|
* @Groups({"User:output"}) |
125
|
|
|
*/ |
126
|
|
|
protected ?DateTime $newEmailAddressChangeRequestedAt = null; |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @ApiProperty(readable=false, writable=false) |
130
|
|
|
*/ |
131
|
|
|
public ?string $plainNewEmailConfirmationToken = null; |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @ApiProperty(readable=false, writable=false) |
135
|
|
|
*/ |
136
|
|
|
protected bool $emailAddressVerified = false; |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Random string sent to previous email address when email is changed to permit email restore and password change. |
140
|
|
|
* |
141
|
|
|
* @ApiProperty(readable=false, writable=false) |
142
|
|
|
*/ |
143
|
|
|
protected ?string $emailAddressVerifyToken = null; |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @ApiProperty(readable=false, writable=false) |
147
|
|
|
*/ |
148
|
|
|
public ?string $plainEmailAddressVerifyToken = null; |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @ApiProperty(readable=false, writable=false) |
152
|
|
|
*/ |
153
|
|
|
protected ?DateTime $emailLastUpdatedAt = null; |
154
|
|
|
|
155
|
58 |
|
public function __construct(string $username = '', string $emailAddress = '', bool $emailAddressVerified = false, array $roles = ['ROLE_USER'], string $password = '', bool $enabled = true) |
156
|
|
|
{ |
157
|
58 |
|
$this->username = $username; |
158
|
58 |
|
$this->emailAddress = $emailAddress; |
159
|
58 |
|
$this->emailAddressVerified = $emailAddressVerified; |
160
|
58 |
|
$this->roles = $roles; |
161
|
58 |
|
$this->password = $password; |
162
|
58 |
|
$this->enabled = $enabled; |
163
|
58 |
|
} |
164
|
|
|
|
165
|
23 |
|
public function getUsername(): ?string |
166
|
|
|
{ |
167
|
23 |
|
return $this->username; |
168
|
|
|
} |
169
|
|
|
|
170
|
25 |
|
public function setUsername(?string $username): self |
171
|
|
|
{ |
172
|
25 |
|
$this->username = $username; |
173
|
|
|
|
174
|
25 |
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
20 |
|
public function getEmailAddress(): ?string |
178
|
|
|
{ |
179
|
20 |
|
return $this->emailAddress; |
180
|
|
|
} |
181
|
|
|
|
182
|
27 |
|
public function setEmailAddress(?string $emailAddress): self |
183
|
|
|
{ |
184
|
27 |
|
$this->emailAddress = $emailAddress; |
185
|
27 |
|
if ($emailAddress) { |
186
|
27 |
|
$this->emailLastUpdatedAt = new \DateTime(); |
187
|
|
|
} |
188
|
|
|
|
189
|
27 |
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
6 |
|
public function getRoles(): array |
193
|
|
|
{ |
194
|
6 |
|
return $this->roles; |
195
|
|
|
} |
196
|
|
|
|
197
|
3 |
|
public function setRoles(array $roles): self |
198
|
|
|
{ |
199
|
3 |
|
$this->roles = $roles; |
200
|
|
|
|
201
|
3 |
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
8 |
|
public function isEnabled(): bool |
205
|
|
|
{ |
206
|
8 |
|
return $this->enabled; |
207
|
|
|
} |
208
|
|
|
|
209
|
6 |
|
public function setEnabled(bool $enabled): self |
210
|
|
|
{ |
211
|
6 |
|
$this->enabled = $enabled; |
212
|
|
|
|
213
|
6 |
|
return $this; |
214
|
|
|
} |
215
|
|
|
|
216
|
3 |
|
public function getPassword(): ?string |
217
|
|
|
{ |
218
|
3 |
|
return $this->password; |
219
|
|
|
} |
220
|
|
|
|
221
|
4 |
|
public function setPassword(string $password): self |
222
|
|
|
{ |
223
|
4 |
|
$this->password = $password; |
224
|
|
|
|
225
|
4 |
|
return $this; |
226
|
|
|
} |
227
|
|
|
|
228
|
1 |
|
public function getPlainPassword(): ?string |
229
|
|
|
{ |
230
|
1 |
|
return $this->plainPassword; |
231
|
|
|
} |
232
|
|
|
|
233
|
1 |
|
public function setPlainPassword(?string $plainPassword): self |
234
|
|
|
{ |
235
|
1 |
|
$this->plainPassword = $plainPassword; |
236
|
1 |
|
if ($plainPassword) { |
237
|
|
|
// Needs to update mapped field to trigger update event which will encode the plain password |
238
|
1 |
|
$this->passwordUpdatedAt = new \DateTime(); |
239
|
|
|
} |
240
|
|
|
|
241
|
1 |
|
return $this; |
242
|
|
|
} |
243
|
|
|
|
244
|
1 |
|
public function getNewPasswordConfirmationToken(): ?string |
245
|
|
|
{ |
246
|
1 |
|
return $this->newPasswordConfirmationToken; |
247
|
|
|
} |
248
|
|
|
|
249
|
3 |
|
public function setNewPasswordConfirmationToken(?string $newPasswordConfirmationToken): self |
250
|
|
|
{ |
251
|
3 |
|
$this->newPasswordConfirmationToken = $newPasswordConfirmationToken; |
252
|
|
|
|
253
|
3 |
|
return $this; |
254
|
|
|
} |
255
|
|
|
|
256
|
2 |
|
public function getPasswordRequestedAt(): ?DateTime |
257
|
|
|
{ |
258
|
2 |
|
return $this->passwordRequestedAt; |
259
|
|
|
} |
260
|
|
|
|
261
|
4 |
|
public function setPasswordRequestedAt(?DateTime $passwordRequestedAt): self |
262
|
|
|
{ |
263
|
4 |
|
$this->passwordRequestedAt = $passwordRequestedAt; |
264
|
|
|
|
265
|
4 |
|
return $this; |
266
|
|
|
} |
267
|
|
|
|
268
|
1 |
|
public function getOldPassword(): ?string |
269
|
|
|
{ |
270
|
1 |
|
return $this->oldPassword; |
271
|
|
|
} |
272
|
|
|
|
273
|
1 |
|
public function setOldPassword(?string $oldPassword): self |
274
|
|
|
{ |
275
|
1 |
|
$this->oldPassword = $oldPassword; |
276
|
|
|
|
277
|
1 |
|
return $this; |
278
|
|
|
} |
279
|
|
|
|
280
|
9 |
|
public function getNewEmailAddress(): ?string |
281
|
|
|
{ |
282
|
9 |
|
return $this->newEmailAddress; |
283
|
|
|
} |
284
|
|
|
|
285
|
7 |
|
public function setNewEmailAddress(?string $newEmailAddress): self |
286
|
|
|
{ |
287
|
7 |
|
$this->newEmailAddress = $newEmailAddress; |
288
|
7 |
|
if ($newEmailAddress) { |
289
|
7 |
|
$this->newEmailAddressChangeRequestedAt = new \DateTime(); |
290
|
|
|
} |
291
|
|
|
|
292
|
7 |
|
return $this; |
293
|
|
|
} |
294
|
|
|
|
295
|
1 |
|
public function getNewEmailConfirmationToken(): ?string |
296
|
|
|
{ |
297
|
1 |
|
return $this->newEmailConfirmationToken; |
298
|
|
|
} |
299
|
|
|
|
300
|
3 |
|
public function setNewEmailConfirmationToken(?string $newEmailConfirmationToken): self |
301
|
|
|
{ |
302
|
3 |
|
$this->newEmailConfirmationToken = $newEmailConfirmationToken; |
303
|
|
|
|
304
|
3 |
|
return $this; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
public function getNewEmailAddressChangeRequestedAt(): ?DateTime |
308
|
|
|
{ |
309
|
|
|
return $this->newEmailAddressChangeRequestedAt; |
310
|
|
|
} |
311
|
|
|
|
312
|
6 |
|
public function isEmailAddressVerified(): bool |
313
|
|
|
{ |
314
|
6 |
|
return $this->emailAddressVerified; |
315
|
|
|
} |
316
|
|
|
|
317
|
7 |
|
public function setEmailAddressVerified(bool $emailAddressVerified): self |
318
|
|
|
{ |
319
|
7 |
|
$this->emailAddressVerified = $emailAddressVerified; |
320
|
|
|
|
321
|
7 |
|
return $this; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
public function getEmailAddressVerifyToken(): ?string |
325
|
|
|
{ |
326
|
|
|
return $this->emailAddressVerifyToken; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
public function setEmailAddressVerifyToken(?string $emailAddressVerifyToken): void |
330
|
|
|
{ |
331
|
|
|
$this->emailAddressVerifyToken = $emailAddressVerifyToken; |
332
|
|
|
} |
333
|
|
|
|
334
|
1 |
|
public function isPasswordRequestLimitReached($ttl): bool |
335
|
|
|
{ |
336
|
1 |
|
$lastRequest = $this->getPasswordRequestedAt(); |
337
|
|
|
|
338
|
1 |
|
return $lastRequest instanceof DateTime && |
339
|
1 |
|
$lastRequest->getTimestamp() + $ttl > time(); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** @see \Serializable::serialize() */ |
343
|
1 |
|
public function serialize(): string |
344
|
|
|
{ |
345
|
1 |
|
return serialize( |
346
|
|
|
[ |
347
|
1 |
|
(string) $this->id, |
348
|
1 |
|
$this->username, |
349
|
1 |
|
$this->emailAddress, |
350
|
1 |
|
$this->password, |
351
|
1 |
|
$this->enabled, |
352
|
1 |
|
$this->roles, |
353
|
|
|
] |
354
|
|
|
); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* @see \Serializable::unserialize() |
359
|
|
|
*/ |
360
|
2 |
|
public function unserialize(string $serialized): self |
361
|
|
|
{ |
362
|
2 |
|
$id = null; |
|
|
|
|
363
|
|
|
[ |
364
|
|
|
$id, |
365
|
2 |
|
$this->username, |
366
|
2 |
|
$this->emailAddress, |
367
|
2 |
|
$this->password, |
368
|
2 |
|
$this->enabled, |
369
|
2 |
|
$this->roles, |
370
|
2 |
|
] = unserialize($serialized, ['allowed_classes' => false]); |
371
|
2 |
|
$this->id = Uuid::fromString($id); |
372
|
|
|
|
373
|
2 |
|
return $this; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* Not needed - we use bcrypt. |
378
|
|
|
* |
379
|
|
|
* @ApiProperty(readable=false, writable=false) |
380
|
|
|
*/ |
381
|
2 |
|
public function getSalt() |
382
|
|
|
{ |
383
|
2 |
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* Remove sensitive data - e.g. plain passwords etc. |
387
|
|
|
*/ |
388
|
1 |
|
public function eraseCredentials(): void |
389
|
|
|
{ |
390
|
1 |
|
$this->plainPassword = null; |
391
|
1 |
|
} |
392
|
|
|
|
393
|
2 |
|
public function __toString() |
394
|
|
|
{ |
395
|
2 |
|
return (string) $this->id; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
public static function createFromPayload($username, array $payload) |
399
|
|
|
{ |
400
|
|
|
$newUser = new static( |
401
|
|
|
$username, |
402
|
|
|
$payload['emailAddress'], |
403
|
|
|
$payload['emailAddressVerified'], |
404
|
|
|
$payload['roles'] |
405
|
|
|
); |
406
|
|
|
|
407
|
|
|
$newUser->setNewEmailAddress($payload['newEmailAddress']); |
408
|
|
|
|
409
|
|
|
$reflection = new \ReflectionClass(static::class); |
410
|
|
|
$idProperty = $reflection->getProperty('id'); |
411
|
|
|
$idProperty->setAccessible(true); |
412
|
|
|
$idProperty->setValue($newUser, Uuid::fromString($payload['id'])); |
413
|
|
|
|
414
|
|
|
return $newUser; |
415
|
|
|
} |
416
|
|
|
} |
417
|
|
|
|