|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Entity; |
|
6
|
|
|
|
|
7
|
|
|
use App\Entity\Traits\EntityIdTrait; |
|
8
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
9
|
|
|
use Doctrine\Common\Collections\Collection; |
|
10
|
|
|
use Doctrine\ORM\Mapping as ORM; |
|
11
|
|
|
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; |
|
12
|
|
|
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; |
|
13
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
14
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @ORM\Entity(repositoryClass="App\Repository\UserRepository") |
|
18
|
|
|
* @ORM\Table(name="users") |
|
19
|
|
|
* @UniqueEntity("email") |
|
20
|
|
|
*/ |
|
21
|
|
|
class User implements UserInterface, PasswordAuthenticatedUserInterface |
|
22
|
|
|
{ |
|
23
|
|
|
use EntityIdTrait; |
|
24
|
|
|
/** |
|
25
|
|
|
* Requests older than this many seconds will be considered expired. |
|
26
|
|
|
*/ |
|
27
|
|
|
public const RETRY_TTL = 3600; |
|
28
|
|
|
/** |
|
29
|
|
|
* Maximum time that the confirmation token will be valid. |
|
30
|
|
|
*/ |
|
31
|
|
|
public const TOKEN_TTL = 43200; |
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
* |
|
35
|
|
|
* @ORM\Column(type="string", unique=true) |
|
36
|
|
|
* @Assert\NotBlank() |
|
37
|
|
|
* @Assert\Length(min=2, max=50) |
|
38
|
|
|
*/ |
|
39
|
|
|
private $username; |
|
40
|
|
|
/** |
|
41
|
|
|
* @var string |
|
42
|
|
|
* |
|
43
|
|
|
* @ORM\Column(type="string", unique=true) |
|
44
|
|
|
* @Assert\Email() |
|
45
|
|
|
*/ |
|
46
|
|
|
private $email; |
|
47
|
|
|
/** |
|
48
|
|
|
* @var string |
|
49
|
|
|
* |
|
50
|
|
|
* @ORM\Column(type="string") |
|
51
|
|
|
*/ |
|
52
|
|
|
private $password; |
|
53
|
|
|
/** |
|
54
|
|
|
* @var array |
|
55
|
|
|
* |
|
56
|
|
|
* @ORM\Column(type="json") |
|
57
|
|
|
*/ |
|
58
|
|
|
private $roles = []; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @ORM\OneToMany(targetEntity="App\Entity\Property", mappedBy="author") |
|
62
|
|
|
*/ |
|
63
|
|
|
private $properties; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @ORM\Column(type="string", length=255, nullable=true) |
|
67
|
|
|
*/ |
|
68
|
|
|
private $confirmation_token; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @ORM\Column(type="datetime", nullable=true) |
|
72
|
|
|
*/ |
|
73
|
|
|
private $password_requested_at; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @ORM\OneToOne(targetEntity=Profile::class, mappedBy="user", cascade={"persist", "remove"}) |
|
77
|
|
|
*/ |
|
78
|
|
|
private $profile; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @ORM\Column(type="datetime", nullable=true) |
|
82
|
|
|
*/ |
|
83
|
|
|
private $emailVerifiedAt = null; |
|
84
|
|
|
|
|
85
|
|
|
public function __construct() |
|
86
|
|
|
{ |
|
87
|
|
|
$this->properties = new ArrayCollection(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function getUsername(): ?string |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->username; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function getUserIdentifier(): string |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->username; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function setUsername(string $username): void |
|
101
|
|
|
{ |
|
102
|
|
|
$this->username = $username; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function getEmail(): ?string |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->email; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function setEmail(string $email): void |
|
111
|
|
|
{ |
|
112
|
|
|
$this->email = $email; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function getPassword(): ?string |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->password; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function setPassword(string $password): void |
|
121
|
|
|
{ |
|
122
|
|
|
$this->password = $password; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Returns the roles or permissions granted to the user for security. |
|
127
|
|
|
*/ |
|
128
|
|
|
public function getRoles(): array |
|
129
|
|
|
{ |
|
130
|
|
|
$roles = $this->roles; |
|
131
|
|
|
// guarantees that a user always has at least one role for security |
|
132
|
|
|
if (empty($roles)) { |
|
133
|
|
|
$roles[] = 'ROLE_USER'; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return array_unique($roles); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function setRoles(array $roles): void |
|
140
|
|
|
{ |
|
141
|
|
|
$this->roles = $roles; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Returns the salt that was originally used to encode the password. |
|
146
|
|
|
* |
|
147
|
|
|
* {@inheritdoc} |
|
148
|
|
|
*/ |
|
149
|
|
|
public function getSalt(): ?string |
|
150
|
|
|
{ |
|
151
|
|
|
// See "Do you need to use a Salt?" at https://symfony.com/doc/current/cookbook/security/entity_provider.html |
|
152
|
|
|
// we're using bcrypt in security.yml to encode the password, so |
|
153
|
|
|
// the salt value is built-in and you don't have to generate one |
|
154
|
|
|
return null; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Removes sensitive data from the user. |
|
159
|
|
|
* |
|
160
|
|
|
* {@inheritdoc} |
|
161
|
|
|
*/ |
|
162
|
|
|
public function eraseCredentials(): void |
|
163
|
|
|
{ |
|
164
|
|
|
// if you had a plainPassword property, you'd nullify it here |
|
165
|
|
|
// $this->plainPassword = null; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* {@inheritdoc} |
|
170
|
|
|
*/ |
|
171
|
|
|
public function __serialize(): array |
|
172
|
|
|
{ |
|
173
|
|
|
// add $this->salt too if you don't use Bcrypt or Argon2i |
|
174
|
|
|
return [$this->id, $this->username, $this->password]; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* {@inheritdoc} |
|
179
|
|
|
*/ |
|
180
|
|
|
public function __unserialize(array $data): void |
|
181
|
|
|
{ |
|
182
|
|
|
// add $this->salt too if you don't use Bcrypt or Argon2i |
|
183
|
|
|
[$this->id, $this->username, $this->password] = $data; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function getProperties(): Collection |
|
187
|
|
|
{ |
|
188
|
|
|
return $this->properties; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
public function addProperty(Property $property): self |
|
192
|
|
|
{ |
|
193
|
|
|
if (!$this->properties->contains($property)) { |
|
194
|
|
|
$this->properties[] = $property; |
|
195
|
|
|
$property->setAuthor($this); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
return $this; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
public function removeProperty(Property $property): self |
|
202
|
|
|
{ |
|
203
|
|
|
if ($this->properties->contains($property)) { |
|
204
|
|
|
$this->properties->removeElement($property); |
|
205
|
|
|
// set the owning side to null (unless already changed) |
|
206
|
|
|
if ($property->getAuthor() === $this) { |
|
207
|
|
|
$property->setAuthor(null); |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
return $this; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
public function getConfirmationToken(): ?string |
|
215
|
|
|
{ |
|
216
|
|
|
return $this->confirmation_token; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
public function setConfirmationToken(?string $confirmation_token): self |
|
220
|
|
|
{ |
|
221
|
|
|
$this->confirmation_token = $confirmation_token; |
|
222
|
|
|
|
|
223
|
|
|
return $this; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
public function getPasswordRequestedAt(): ?\DateTimeInterface |
|
227
|
|
|
{ |
|
228
|
|
|
return $this->password_requested_at; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
public function setPasswordRequestedAt(?\DateTimeInterface $password_requested_at): self |
|
232
|
|
|
{ |
|
233
|
|
|
$this->password_requested_at = $password_requested_at; |
|
234
|
|
|
|
|
235
|
|
|
return $this; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Checks whether the password reset request has expired. |
|
240
|
|
|
*/ |
|
241
|
|
|
public function isPasswordRequestNonExpired(int $ttl): bool |
|
242
|
|
|
{ |
|
243
|
|
|
return $this->getPasswordRequestedAt() instanceof \DateTime && |
|
244
|
|
|
$this->getPasswordRequestedAt()->getTimestamp() + $ttl > time(); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
public function getProfile(): ?Profile |
|
248
|
|
|
{ |
|
249
|
|
|
return $this->profile; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
public function setProfile(Profile $profile): self |
|
253
|
|
|
{ |
|
254
|
|
|
// set the owning side of the relation if necessary |
|
255
|
|
|
if ($profile->getUser() !== $this) { |
|
256
|
|
|
$profile->setUser($this); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
$this->profile = $profile; |
|
260
|
|
|
|
|
261
|
|
|
return $this; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
public function isVerified(): bool |
|
265
|
|
|
{ |
|
266
|
|
|
return null !== $this->emailVerifiedAt; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
public function getEmailVerifiedAt(): ?\DateTime |
|
270
|
|
|
{ |
|
271
|
|
|
return $this->emailVerifiedAt; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
public function setEmailVerifiedAt(?\DateTime $dateTime): self |
|
275
|
|
|
{ |
|
276
|
|
|
$this->emailVerifiedAt = $dateTime; |
|
277
|
|
|
|
|
278
|
|
|
return $this; |
|
279
|
|
|
} |
|
280
|
|
|
} |
|
281
|
|
|
|