|
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"}) |
|
46
|
|
|
*/ |
|
47
|
|
|
protected ?string $username; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @Assert\NotBlank(groups={"Default"}) |
|
51
|
|
|
* @Assert\Email() |
|
52
|
|
|
* @Groups({"User:superAdmin", "User:output"}) |
|
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"}) |
|
110
|
|
|
* @Groups({"User:input", "User:output", "User:emailAddress"}) |
|
111
|
|
|
*/ |
|
112
|
|
|
protected ?string $newEmailAddress = null; |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Random string sent to the user's new email address in order to verify it. |
|
116
|
|
|
* |
|
117
|
|
|
* @ApiProperty(readable=false, writable=false) |
|
118
|
|
|
*/ |
|
119
|
|
|
protected ?string $newEmailVerificationToken = null; |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @ApiProperty(readable=false, writable=false) |
|
123
|
|
|
*/ |
|
124
|
|
|
public ?string $plainNewEmailVerificationToken = null; |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @ApiProperty(readable=false, writable=false) |
|
128
|
|
|
*/ |
|
129
|
|
|
protected bool $emailAddressVerified = false; |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @ApiProperty(readable=false, writable=false) |
|
133
|
|
|
*/ |
|
134
|
|
|
protected ?string $previousEmailAddress = null; |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Random string sent to previous email address when email is changed to permit email restore and password change. |
|
138
|
|
|
* |
|
139
|
|
|
* @ApiProperty(readable=false, writable=false) |
|
140
|
|
|
*/ |
|
141
|
|
|
protected ?string $restoreAccessToken = null; |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @ApiProperty(readable=false, writable=false) |
|
145
|
|
|
*/ |
|
146
|
|
|
protected ?string $plainRestoreAccessToken = null; |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @ApiProperty(readable=false, writable=false) |
|
150
|
|
|
*/ |
|
151
|
|
|
protected ?DateTime $emailLastUpdatedAt = null; |
|
152
|
|
|
|
|
153
|
56 |
|
public function __construct(string $username = '', string $emailAddress = '', bool $emailAddressVerified = false, array $roles = ['ROLE_USER'], string $password = '', bool $enabled = true) |
|
154
|
|
|
{ |
|
155
|
56 |
|
$this->username = $username; |
|
156
|
56 |
|
$this->emailAddress = $emailAddress; |
|
157
|
56 |
|
$this->emailAddressVerified = $emailAddressVerified; |
|
158
|
56 |
|
$this->roles = $roles; |
|
159
|
56 |
|
$this->password = $password; |
|
160
|
56 |
|
$this->enabled = $enabled; |
|
161
|
56 |
|
} |
|
162
|
|
|
|
|
163
|
23 |
|
public function getUsername(): ?string |
|
164
|
|
|
{ |
|
165
|
23 |
|
return $this->username; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
22 |
|
public function setUsername(?string $username): self |
|
169
|
|
|
{ |
|
170
|
22 |
|
$this->username = $username; |
|
171
|
|
|
|
|
172
|
22 |
|
return $this; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
18 |
|
public function getEmailAddress(): ?string |
|
176
|
|
|
{ |
|
177
|
18 |
|
return $this->emailAddress; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
24 |
|
public function setEmailAddress(?string $emailAddress): self |
|
181
|
|
|
{ |
|
182
|
24 |
|
$this->emailAddress = $emailAddress; |
|
183
|
24 |
|
if ($emailAddress) { |
|
184
|
24 |
|
$this->emailLastUpdatedAt = new \DateTime(); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
24 |
|
return $this; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
4 |
|
public function getRoles(): array |
|
191
|
|
|
{ |
|
192
|
4 |
|
return $this->roles; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
1 |
|
public function setRoles(array $roles): self |
|
196
|
|
|
{ |
|
197
|
1 |
|
$this->roles = $roles; |
|
198
|
|
|
|
|
199
|
1 |
|
return $this; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
6 |
|
public function isEnabled(): bool |
|
203
|
|
|
{ |
|
204
|
6 |
|
return $this->enabled; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
4 |
|
public function setEnabled(bool $enabled): self |
|
208
|
|
|
{ |
|
209
|
4 |
|
$this->enabled = $enabled; |
|
210
|
|
|
|
|
211
|
4 |
|
return $this; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
3 |
|
public function getPassword(): ?string |
|
215
|
|
|
{ |
|
216
|
3 |
|
return $this->password; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
2 |
|
public function setPassword(string $password): self |
|
220
|
|
|
{ |
|
221
|
2 |
|
$this->password = $password; |
|
222
|
|
|
|
|
223
|
2 |
|
return $this; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
1 |
|
public function getPlainPassword(): ?string |
|
227
|
|
|
{ |
|
228
|
1 |
|
return $this->plainPassword; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
1 |
|
public function setPlainPassword(?string $plainPassword): self |
|
232
|
|
|
{ |
|
233
|
1 |
|
$this->plainPassword = $plainPassword; |
|
234
|
1 |
|
if ($plainPassword) { |
|
235
|
|
|
// Needs to update mapped field to trigger update event which will encode the plain password |
|
236
|
1 |
|
$this->passwordUpdatedAt = new \DateTime(); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
1 |
|
return $this; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
1 |
|
public function getNewPasswordConfirmationToken(): ?string |
|
243
|
|
|
{ |
|
244
|
1 |
|
return $this->newPasswordConfirmationToken; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
3 |
|
public function setNewPasswordConfirmationToken(?string $newPasswordConfirmationToken): self |
|
248
|
|
|
{ |
|
249
|
3 |
|
$this->newPasswordConfirmationToken = $newPasswordConfirmationToken; |
|
250
|
|
|
|
|
251
|
3 |
|
return $this; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
2 |
|
public function getPasswordRequestedAt(): ?DateTime |
|
255
|
|
|
{ |
|
256
|
2 |
|
return $this->passwordRequestedAt; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
4 |
|
public function setPasswordRequestedAt(?DateTime $passwordRequestedAt): self |
|
260
|
|
|
{ |
|
261
|
4 |
|
$this->passwordRequestedAt = $passwordRequestedAt; |
|
262
|
|
|
|
|
263
|
4 |
|
return $this; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
1 |
|
public function getOldPassword(): ?string |
|
267
|
|
|
{ |
|
268
|
1 |
|
return $this->oldPassword; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
1 |
|
public function setOldPassword(?string $oldPassword): self |
|
272
|
|
|
{ |
|
273
|
1 |
|
$this->oldPassword = $oldPassword; |
|
274
|
|
|
|
|
275
|
1 |
|
return $this; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
7 |
|
public function getNewEmailAddress(): ?string |
|
279
|
|
|
{ |
|
280
|
7 |
|
return $this->newEmailAddress; |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
6 |
|
public function setNewEmailAddress(?string $newEmailAddress): self |
|
284
|
|
|
{ |
|
285
|
6 |
|
$this->newEmailAddress = $newEmailAddress; |
|
286
|
|
|
|
|
287
|
6 |
|
return $this; |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
1 |
|
public function getNewEmailVerificationToken(): ?string |
|
291
|
|
|
{ |
|
292
|
1 |
|
return $this->newEmailVerificationToken; |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
2 |
|
public function setNewEmailVerificationToken(?string $newEmailVerificationToken): self |
|
296
|
|
|
{ |
|
297
|
2 |
|
$this->newEmailVerificationToken = $newEmailVerificationToken; |
|
298
|
|
|
|
|
299
|
2 |
|
return $this; |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
6 |
|
public function isEmailAddressVerified(): bool |
|
303
|
|
|
{ |
|
304
|
6 |
|
return $this->emailAddressVerified; |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
5 |
|
public function setEmailAddressVerified(bool $emailAddressVerified): self |
|
308
|
|
|
{ |
|
309
|
5 |
|
$this->emailAddressVerified = $emailAddressVerified; |
|
310
|
|
|
|
|
311
|
5 |
|
return $this; |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
public function getPreviousEmailAddress(): ?string |
|
315
|
|
|
{ |
|
316
|
|
|
return $this->previousEmailAddress; |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
public function setPreviousEmailAddress(?string $previousEmailAddress): void |
|
320
|
|
|
{ |
|
321
|
|
|
$this->previousEmailAddress = $previousEmailAddress; |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
public function getRestoreAccessToken(): ?string |
|
325
|
|
|
{ |
|
326
|
|
|
return $this->restoreAccessToken; |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
public function setRestoreAccessToken(?string $restoreAccessToken): void |
|
330
|
|
|
{ |
|
331
|
|
|
$this->restoreAccessToken = $restoreAccessToken; |
|
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
|
1 |
|
(string) $this->id, |
|
347
|
1 |
|
$this->username, |
|
348
|
1 |
|
$this->emailAddress, |
|
349
|
1 |
|
$this->password, |
|
350
|
1 |
|
$this->enabled, |
|
351
|
1 |
|
$this->roles, |
|
352
|
|
|
]); |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
/** |
|
356
|
|
|
* @see \Serializable::unserialize() |
|
357
|
|
|
*/ |
|
358
|
2 |
|
public function unserialize(string $serialized): self |
|
359
|
|
|
{ |
|
360
|
2 |
|
$id = null; |
|
|
|
|
|
|
361
|
|
|
[ |
|
362
|
|
|
$id, |
|
363
|
2 |
|
$this->username, |
|
364
|
2 |
|
$this->emailAddress, |
|
365
|
2 |
|
$this->password, |
|
366
|
2 |
|
$this->enabled, |
|
367
|
2 |
|
$this->roles, |
|
368
|
2 |
|
] = unserialize($serialized, ['allowed_classes' => false]); |
|
369
|
2 |
|
$this->id = Uuid::fromString($id); |
|
370
|
|
|
|
|
371
|
2 |
|
return $this; |
|
372
|
|
|
} |
|
373
|
|
|
|
|
374
|
|
|
/** |
|
375
|
|
|
* Not needed - we use bcrypt. |
|
376
|
|
|
* |
|
377
|
|
|
* @ApiProperty(readable=false, writable=false) |
|
378
|
|
|
*/ |
|
379
|
|
|
public function getSalt() |
|
380
|
|
|
{ |
|
381
|
|
|
} |
|
382
|
|
|
|
|
383
|
|
|
/** |
|
384
|
|
|
* Remove sensitive data - e.g. plain passwords etc. |
|
385
|
|
|
*/ |
|
386
|
1 |
|
public function eraseCredentials(): void |
|
387
|
|
|
{ |
|
388
|
1 |
|
$this->plainPassword = null; |
|
389
|
1 |
|
} |
|
390
|
|
|
|
|
391
|
2 |
|
public function __toString() |
|
392
|
|
|
{ |
|
393
|
2 |
|
return (string) $this->id; |
|
394
|
|
|
} |
|
395
|
|
|
|
|
396
|
|
|
public static function createFromPayload($username, array $payload) |
|
397
|
|
|
{ |
|
398
|
|
|
return new static( |
|
399
|
|
|
$username, |
|
400
|
|
|
$payload['roles'] |
|
401
|
|
|
); |
|
402
|
|
|
} |
|
403
|
|
|
} |
|
404
|
|
|
|