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 Silverback\ApiComponentsBundle\Annotation as Silverback; |
20
|
|
|
use Silverback\ApiComponentsBundle\Entity\Utility\IdTrait; |
21
|
|
|
use Silverback\ApiComponentsBundle\Entity\Utility\TimestampedTrait; |
22
|
|
|
use Silverback\ApiComponentsBundle\Validator\Constraints as APIAssert; |
23
|
|
|
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; |
24
|
|
|
use Symfony\Component\Security\Core\User\UserInterface as SymfonyUserInterface; |
25
|
|
|
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword; |
26
|
|
|
use Symfony\Component\Serializer\Annotation\Groups; |
27
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @author Daniel West <[email protected]> |
31
|
|
|
* |
32
|
|
|
* @Silverback\Timestamped |
33
|
|
|
* @UniqueEntity(fields={"username"}, errorPath="username", message="Sorry, that user already exists in the database.") |
34
|
|
|
* @UniqueEntity(fields={"emailAddress"}, errorPath="emailAddress", message="Sorry, that email address already exists in the database.") |
35
|
|
|
* @APIAssert\NewEmailAddress(groups={"new_email_address", "Default"}) |
36
|
|
|
*/ |
37
|
|
|
abstract class AbstractUser implements SymfonyUserInterface, JWTUserInterface |
38
|
|
|
{ |
39
|
|
|
use IdTrait; |
40
|
|
|
use TimestampedTrait; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @Assert\NotBlank(groups={"Default"}) |
44
|
|
|
* @Groups({"User:super_admin", "User:output"}) |
45
|
|
|
*/ |
46
|
|
|
protected ?string $username; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @Assert\NotBlank(groups={"Default"}) |
50
|
|
|
* @Assert\Email() |
51
|
|
|
* @Groups({"User:super_admin", "User:output"}) |
52
|
|
|
*/ |
53
|
|
|
protected ?string $emailAddress; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @Groups({"User:super_admin"}) |
57
|
|
|
*/ |
58
|
|
|
protected array $roles; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @Groups({"User:super_admin"}) |
62
|
|
|
*/ |
63
|
|
|
protected bool $enabled; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @ApiProperty(readable=false, writable=false) |
67
|
|
|
*/ |
68
|
|
|
protected string $password; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @ApiProperty(readable=false) |
72
|
|
|
* @Assert\NotBlank(message="Please enter your desired password", groups={"password_reset", "change_password"}) |
73
|
|
|
* @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={"Default", "password_reset", "change_password"}) |
74
|
|
|
* @Groups({"User:input"}) |
75
|
|
|
*/ |
76
|
|
|
protected ?string $plainPassword = null; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Random string sent to the user email address in order to verify it. |
80
|
|
|
* |
81
|
|
|
* @ApiProperty(readable=false, writable=false) |
82
|
|
|
*/ |
83
|
|
|
protected ?string $newPasswordConfirmationToken = null; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @ApiProperty(readable=false, writable=false) |
87
|
|
|
*/ |
88
|
|
|
protected ?DateTime $passwordRequestedAt = null; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @ApiProperty(readable=false) |
92
|
|
|
* @UserPassword(message="You have not entered your current password correctly. Please try again.", groups={"change_password"}) |
93
|
|
|
* @Groups({"User:input"}) |
94
|
|
|
*/ |
95
|
|
|
protected ?string $oldPassword = null; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @ApiProperty(readable=false, writable=false) |
99
|
|
|
*/ |
100
|
|
|
protected ?DateTime $passwordLastUpdated = null; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @Assert\NotBlank(groups={"new_email_address"}) |
104
|
|
|
* @Groups({"User:input", "User:output", "new_email_address"}) |
105
|
|
|
*/ |
106
|
|
|
protected ?string $newEmailAddress = null; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Random string sent to the user's new email address in order to verify it. |
110
|
|
|
* |
111
|
|
|
* @ApiProperty(readable=false, writable=false) |
112
|
|
|
*/ |
113
|
|
|
protected ?string $newEmailVerificationToken = null; |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @ApiProperty(readable=false, writable=false) |
117
|
|
|
*/ |
118
|
|
|
protected bool $emailAddressVerified = false; |
119
|
|
|
|
120
|
56 |
|
public function __construct( |
121
|
|
|
string $username = '', |
122
|
|
|
string $emailAddress = '', |
123
|
|
|
bool $emailAddressVerified = false, |
124
|
|
|
array $roles = ['ROLE_USER'], |
125
|
|
|
string $password = '', |
126
|
|
|
bool $enabled = true |
127
|
|
|
) { |
128
|
56 |
|
$this->username = $username; |
129
|
56 |
|
$this->emailAddress = $emailAddress; |
130
|
56 |
|
$this->emailAddressVerified = $emailAddressVerified; |
131
|
56 |
|
$this->roles = $roles; |
132
|
56 |
|
$this->password = $password; |
133
|
56 |
|
$this->enabled = $enabled; |
134
|
56 |
|
$this->setId(); |
135
|
56 |
|
} |
136
|
|
|
|
137
|
28 |
|
public function getUsername(): ?string |
138
|
|
|
{ |
139
|
28 |
|
return $this->username; |
140
|
|
|
} |
141
|
|
|
|
142
|
21 |
|
public function setUsername(?string $username): self |
143
|
|
|
{ |
144
|
21 |
|
$this->username = $username; |
145
|
|
|
|
146
|
21 |
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
25 |
|
public function getEmailAddress(): ?string |
150
|
|
|
{ |
151
|
25 |
|
return $this->emailAddress; |
152
|
|
|
} |
153
|
|
|
|
154
|
23 |
|
public function setEmailAddress(?string $emailAddress): self |
155
|
|
|
{ |
156
|
23 |
|
$this->emailAddress = $emailAddress; |
157
|
|
|
|
158
|
23 |
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
4 |
|
public function getRoles(): array |
162
|
|
|
{ |
163
|
4 |
|
return $this->roles; |
164
|
|
|
} |
165
|
|
|
|
166
|
1 |
|
public function setRoles(?array $roles): self |
167
|
|
|
{ |
168
|
1 |
|
$this->roles = $roles; |
169
|
|
|
|
170
|
1 |
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
6 |
|
public function isEnabled(): bool |
174
|
|
|
{ |
175
|
6 |
|
return $this->enabled; |
176
|
|
|
} |
177
|
|
|
|
178
|
4 |
|
public function setEnabled(bool $enabled): self |
179
|
|
|
{ |
180
|
4 |
|
$this->enabled = $enabled; |
181
|
|
|
|
182
|
4 |
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
3 |
|
public function getPassword(): ?string |
186
|
|
|
{ |
187
|
3 |
|
return $this->password; |
188
|
|
|
} |
189
|
|
|
|
190
|
2 |
|
public function setPassword(string $password): self |
191
|
|
|
{ |
192
|
2 |
|
$this->password = $password; |
193
|
|
|
|
194
|
2 |
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
6 |
|
public function getPlainPassword(): ?string |
198
|
|
|
{ |
199
|
6 |
|
return $this->plainPassword; |
200
|
|
|
} |
201
|
|
|
|
202
|
1 |
|
public function setPlainPassword(?string $plainPassword): self |
203
|
|
|
{ |
204
|
1 |
|
$this->plainPassword = $plainPassword; |
205
|
1 |
|
if ($plainPassword) { |
206
|
|
|
// Needs to update mapped field to trigger update event which will encode the plain password |
207
|
1 |
|
$this->passwordLastUpdated = new \DateTime(); |
208
|
|
|
} |
209
|
|
|
|
210
|
1 |
|
return $this; |
211
|
|
|
} |
212
|
|
|
|
213
|
3 |
|
public function getNewPasswordConfirmationToken(): ?string |
214
|
|
|
{ |
215
|
3 |
|
return $this->newPasswordConfirmationToken; |
216
|
|
|
} |
217
|
|
|
|
218
|
4 |
|
public function setNewPasswordConfirmationToken(?string $newPasswordConfirmationToken): self |
219
|
|
|
{ |
220
|
4 |
|
$this->newPasswordConfirmationToken = $newPasswordConfirmationToken; |
221
|
|
|
|
222
|
4 |
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
2 |
|
public function getPasswordRequestedAt(): ?DateTime |
226
|
|
|
{ |
227
|
2 |
|
return $this->passwordRequestedAt; |
228
|
|
|
} |
229
|
|
|
|
230
|
4 |
|
public function setPasswordRequestedAt(?DateTime $passwordRequestedAt): self |
231
|
|
|
{ |
232
|
4 |
|
$this->passwordRequestedAt = $passwordRequestedAt; |
233
|
|
|
|
234
|
4 |
|
return $this; |
235
|
|
|
} |
236
|
|
|
|
237
|
1 |
|
public function getOldPassword(): ?string |
238
|
|
|
{ |
239
|
1 |
|
return $this->oldPassword; |
240
|
|
|
} |
241
|
|
|
|
242
|
1 |
|
public function setOldPassword(?string $oldPassword): self |
243
|
|
|
{ |
244
|
1 |
|
$this->oldPassword = $oldPassword; |
245
|
|
|
|
246
|
1 |
|
return $this; |
247
|
|
|
} |
248
|
|
|
|
249
|
5 |
|
public function getNewEmailAddress(): ?string |
250
|
|
|
{ |
251
|
5 |
|
return $this->newEmailAddress; |
252
|
|
|
} |
253
|
|
|
|
254
|
9 |
|
public function setNewEmailAddress(?string $newEmailAddress): self |
255
|
|
|
{ |
256
|
9 |
|
$this->newEmailAddress = $newEmailAddress; |
257
|
|
|
|
258
|
9 |
|
return $this; |
259
|
|
|
} |
260
|
|
|
|
261
|
10 |
|
public function getNewEmailVerificationToken(): ?string |
262
|
|
|
{ |
263
|
10 |
|
return $this->newEmailVerificationToken; |
264
|
|
|
} |
265
|
|
|
|
266
|
4 |
|
public function setNewEmailVerificationToken(?string $newEmailVerificationToken): self |
267
|
|
|
{ |
268
|
4 |
|
$this->newEmailVerificationToken = $newEmailVerificationToken; |
269
|
|
|
|
270
|
4 |
|
return $this; |
271
|
|
|
} |
272
|
|
|
|
273
|
3 |
|
public function isEmailAddressVerified(): bool |
274
|
|
|
{ |
275
|
3 |
|
return $this->emailAddressVerified; |
276
|
|
|
} |
277
|
|
|
|
278
|
9 |
|
public function setEmailAddressVerified(bool $emailAddressVerified): self |
279
|
|
|
{ |
280
|
9 |
|
$this->emailAddressVerified = $emailAddressVerified; |
281
|
|
|
|
282
|
9 |
|
return $this; |
283
|
|
|
} |
284
|
|
|
|
285
|
1 |
|
public function isPasswordRequestLimitReached($ttl): bool |
286
|
|
|
{ |
287
|
1 |
|
$lastRequest = $this->getPasswordRequestedAt(); |
288
|
|
|
|
289
|
1 |
|
return $lastRequest instanceof DateTime && |
290
|
1 |
|
$lastRequest->getTimestamp() + $ttl > time(); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** @see \Serializable::serialize() */ |
294
|
1 |
|
public function serialize(): string |
295
|
|
|
{ |
296
|
1 |
|
return serialize([ |
297
|
1 |
|
$this->id, |
298
|
1 |
|
$this->username, |
299
|
1 |
|
$this->emailAddress, |
300
|
1 |
|
$this->password, |
301
|
1 |
|
$this->enabled, |
302
|
1 |
|
$this->roles, |
303
|
|
|
]); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @see \Serializable::unserialize() |
308
|
|
|
*/ |
309
|
2 |
|
public function unserialize(string $serialized): self |
310
|
|
|
{ |
311
|
|
|
[ |
312
|
2 |
|
$this->id, |
313
|
2 |
|
$this->username, |
314
|
2 |
|
$this->emailAddress, |
315
|
2 |
|
$this->password, |
316
|
2 |
|
$this->enabled, |
317
|
2 |
|
$this->roles, |
318
|
2 |
|
] = unserialize($serialized, ['allowed_classes' => false]); |
319
|
|
|
|
320
|
2 |
|
return $this; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Not needed - we use bcrypt. |
325
|
|
|
* |
326
|
|
|
* @ApiProperty(readable=false, writable=false) |
327
|
|
|
*/ |
328
|
|
|
public function getSalt() |
329
|
|
|
{ |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Remove sensitive data - e.g. plain passwords etc. |
334
|
|
|
*/ |
335
|
1 |
|
public function eraseCredentials(): void |
336
|
|
|
{ |
337
|
1 |
|
$this->plainPassword = null; |
338
|
1 |
|
} |
339
|
|
|
|
340
|
1 |
|
public function __toString() |
341
|
|
|
{ |
342
|
1 |
|
return $this->id; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
public static function createFromPayload($username, array $payload) |
346
|
|
|
{ |
347
|
|
|
return new static( |
348
|
|
|
$username, |
349
|
|
|
$payload['roles'] |
350
|
|
|
); |
351
|
|
|
} |
352
|
|
|
} |
353
|
|
|
|