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