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