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