1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Silverback\ApiComponentBundle\Entity\User; |
4
|
|
|
|
5
|
|
|
use Silverback\ApiComponentBundle\Validator\Constraints as APIAssert; |
6
|
|
|
use Doctrine\ORM\Mapping as ORM; |
7
|
|
|
use Serializable; |
8
|
|
|
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; |
9
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
10
|
|
|
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword; |
11
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @ORM\Entity(repositoryClass="Silverback\ApiComponentBundle\Repository\User\UserRepository") |
15
|
|
|
* @UniqueEntity(fields={"username"}, message="A user is already registered with that email address as their username") |
16
|
|
|
* @APIAssert\NewUsername(groups={"new_username", "Default"}) |
17
|
|
|
*/ |
18
|
|
|
class User implements Serializable, UserInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @ORM\Id() |
22
|
|
|
* @ORM\GeneratedValue() |
23
|
|
|
* @ORM\Column(type="integer") |
24
|
|
|
* @var integer |
25
|
|
|
*/ |
26
|
|
|
protected $id; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @ORM\Column(type="string", length=255, unique=true) |
30
|
|
|
* @Assert\NotBlank(groups={"Default"}) |
31
|
|
|
* @Assert\Email(groups={"Default"}) |
32
|
|
|
* @var string|null |
33
|
|
|
*/ |
34
|
|
|
private $username; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @ORM\Column(type="string", length=64) |
38
|
|
|
*/ |
39
|
|
|
private $password; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @ORM\Column(type="boolean") |
43
|
|
|
*/ |
44
|
|
|
private $enabled; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @ORM\Column(type="array") |
48
|
|
|
*/ |
49
|
|
|
private $roles; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @Assert\NotBlank(message="Please enter your desired password", groups={"Default", "password_reset", "change_password"}) |
53
|
|
|
* @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"}) |
54
|
|
|
* @var string|null |
55
|
|
|
*/ |
56
|
|
|
private $plainPassword; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Random string sent to the user email address in order to verify it. |
60
|
|
|
* @ORM\Column(nullable=true) |
61
|
|
|
* @var string|null |
62
|
|
|
*/ |
63
|
|
|
private $passwordResetConfirmationToken; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @ORM\Column(type="datetime", nullable=true) |
67
|
|
|
* @var \DateTime|null |
68
|
|
|
*/ |
69
|
|
|
private $passwordRequestedAt; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @ORM\Column(type="string", length=255, nullable=true) |
73
|
|
|
* @Assert\NotBlank(groups={"new_username"}) |
74
|
|
|
* @Assert\Email(groups={"new_username"}) |
75
|
|
|
* @var string|null |
76
|
|
|
*/ |
77
|
|
|
private $newUsername; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Random string sent to the user's new email address in order to verify it. |
81
|
|
|
* @ORM\Column(nullable=true) |
82
|
|
|
* @var string|null |
83
|
|
|
*/ |
84
|
|
|
private $usernameConfirmationToken; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @UserPassword(message="You have not entered your current password correctly. Please try again.", groups={"change_password"}) |
88
|
|
|
* @var string|null |
89
|
|
|
*/ |
90
|
|
|
private $oldPassword; |
91
|
|
|
|
92
|
|
|
public function __construct( |
93
|
|
|
string $username = '', |
94
|
|
|
array $roles = [ 'ROLE_USER' ], |
95
|
|
|
string $password = '', |
96
|
|
|
bool $enabled = true |
97
|
|
|
) { |
98
|
|
|
$this->username = $username; |
99
|
|
|
$this->roles = $roles; |
100
|
|
|
$this->password = $password; |
101
|
|
|
$this->enabled = $enabled; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function getId(): ?int |
105
|
|
|
{ |
106
|
|
|
return $this->id; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function getUsername(): ?string |
110
|
|
|
{ |
111
|
|
|
return $this->username; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function setUsername(?string $username): self |
115
|
|
|
{ |
116
|
|
|
$this->username = $username; |
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function getPassword(): string |
121
|
|
|
{ |
122
|
|
|
return $this->password; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function setPassword(string $password): self |
126
|
|
|
{ |
127
|
|
|
$this->password = $password; |
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function getRoles(): array |
132
|
|
|
{ |
133
|
|
|
return $this->roles; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function setRoles(?array $roles): self |
137
|
|
|
{ |
138
|
|
|
$this->roles = $roles; |
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function isEnabled(): bool |
143
|
|
|
{ |
144
|
|
|
return $this->enabled; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return null|string |
149
|
|
|
*/ |
150
|
|
|
public function getPlainPassword(): ?string |
151
|
|
|
{ |
152
|
|
|
return $this->plainPassword; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param null|string $plainPassword |
157
|
|
|
* @return User |
158
|
|
|
*/ |
159
|
|
|
public function setPlainPassword(?string $plainPassword): self |
160
|
|
|
{ |
161
|
|
|
$this->plainPassword = $plainPassword; |
162
|
|
|
|
163
|
|
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return null|string |
168
|
|
|
*/ |
169
|
|
|
public function getPasswordResetConfirmationToken(): ?string |
170
|
|
|
{ |
171
|
|
|
return $this->passwordResetConfirmationToken; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param null|string $passwordResetConfirmationToken |
176
|
|
|
* @return User |
177
|
|
|
*/ |
178
|
|
|
public function setPasswordResetConfirmationToken(?string $passwordResetConfirmationToken): self |
179
|
|
|
{ |
180
|
|
|
$this->passwordResetConfirmationToken = $passwordResetConfirmationToken; |
181
|
|
|
|
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return \DateTime|null |
187
|
|
|
*/ |
188
|
|
|
public function getPasswordRequestedAt(): ?\DateTime |
189
|
|
|
{ |
190
|
|
|
return $this->passwordRequestedAt; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @param \DateTime|null $passwordRequestedAt |
195
|
|
|
* @return User |
196
|
|
|
*/ |
197
|
|
|
public function setPasswordRequestedAt(?\DateTime $passwordRequestedAt): self |
198
|
|
|
{ |
199
|
|
|
$this->passwordRequestedAt = $passwordRequestedAt; |
200
|
|
|
|
201
|
|
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function isPasswordRequestLimitReached($ttl) |
205
|
|
|
{ |
206
|
|
|
$lastRequest = $this->getPasswordRequestedAt(); |
207
|
|
|
return $lastRequest instanceof \DateTime && |
208
|
|
|
$lastRequest->getTimestamp() + $ttl > time(); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @return null|string |
213
|
|
|
*/ |
214
|
|
|
public function getNewUsername(): ?string |
215
|
|
|
{ |
216
|
|
|
return $this->newUsername; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @param null|string $newUsername |
221
|
|
|
* @return User |
222
|
|
|
*/ |
223
|
|
|
public function setNewUsername(?string $newUsername): self |
224
|
|
|
{ |
225
|
|
|
$this->newUsername = $newUsername; |
226
|
|
|
return $this; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @return null|string |
231
|
|
|
*/ |
232
|
|
|
public function getUsernameConfirmationToken(): ?string |
233
|
|
|
{ |
234
|
|
|
return $this->usernameConfirmationToken; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @param null|string $usernameConfirmationToken |
239
|
|
|
* @return User |
240
|
|
|
*/ |
241
|
|
|
public function setUsernameConfirmationToken(?string $usernameConfirmationToken): self |
242
|
|
|
{ |
243
|
|
|
$this->usernameConfirmationToken = $usernameConfirmationToken; |
244
|
|
|
return $this; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @return null|string |
249
|
|
|
*/ |
250
|
|
|
public function getOldPassword(): ?string |
251
|
|
|
{ |
252
|
|
|
return $this->oldPassword; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @param null|string $oldPassword |
257
|
|
|
*/ |
258
|
|
|
public function setOldPassword(?string $oldPassword): void |
259
|
|
|
{ |
260
|
|
|
$this->oldPassword = $oldPassword; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** @see \Serializable::serialize() */ |
264
|
|
|
public function serialize(): string |
265
|
|
|
{ |
266
|
|
|
return serialize([ |
267
|
|
|
$this->id, |
268
|
|
|
$this->username, |
269
|
|
|
$this->password, |
270
|
|
|
$this->enabled |
271
|
|
|
]); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @see \Serializable::unserialize() |
276
|
|
|
* @param string $serialized |
277
|
|
|
*/ |
278
|
|
|
public function unserialize($serialized): void |
279
|
|
|
{ |
280
|
|
|
[ |
281
|
|
|
$this->id, |
282
|
|
|
$this->username, |
283
|
|
|
$this->password, |
284
|
|
|
$this->enabled |
285
|
|
|
] = unserialize($serialized, ['allowed_classes' => false]); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
// Not needed - we use bcrypt |
289
|
|
|
public function getSalt() |
290
|
|
|
{ |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
// remove sensitive data - e.g. plain passwords etc. |
294
|
|
|
public function eraseCredentials(): void |
295
|
|
|
{ |
296
|
|
|
$this->plainPassword = null; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
public function __toString() |
300
|
|
|
{ |
301
|
|
|
return (string) $this->id; |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
|