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
|
|
|
public function __construct() |
81
|
|
|
{ |
82
|
|
|
$this->properties = new ArrayCollection(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getUsername(): ?string |
86
|
|
|
{ |
87
|
|
|
return $this->username; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getUserIdentifier(): string |
91
|
|
|
{ |
92
|
|
|
return $this->username; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function setUsername(string $username): void |
96
|
|
|
{ |
97
|
|
|
$this->username = $username; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getEmail(): ?string |
101
|
|
|
{ |
102
|
|
|
return $this->email; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function setEmail(string $email): void |
106
|
|
|
{ |
107
|
|
|
$this->email = $email; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function getPassword(): ?string |
111
|
|
|
{ |
112
|
|
|
return $this->password; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function setPassword(string $password): void |
116
|
|
|
{ |
117
|
|
|
$this->password = $password; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns the roles or permissions granted to the user for security. |
122
|
|
|
*/ |
123
|
|
|
public function getRoles(): array |
124
|
|
|
{ |
125
|
|
|
$roles = $this->roles; |
126
|
|
|
// guarantees that a user always has at least one role for security |
127
|
|
|
if (empty($roles)) { |
128
|
|
|
$roles[] = 'ROLE_USER'; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return array_unique($roles); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function setRoles(array $roles): void |
135
|
|
|
{ |
136
|
|
|
$this->roles = $roles; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Returns the salt that was originally used to encode the password. |
141
|
|
|
* |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
*/ |
144
|
|
|
public function getSalt(): ?string |
145
|
|
|
{ |
146
|
|
|
// See "Do you need to use a Salt?" at https://symfony.com/doc/current/cookbook/security/entity_provider.html |
147
|
|
|
// we're using bcrypt in security.yml to encode the password, so |
148
|
|
|
// the salt value is built-in and you don't have to generate one |
149
|
|
|
return null; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Removes sensitive data from the user. |
154
|
|
|
* |
155
|
|
|
* {@inheritdoc} |
156
|
|
|
*/ |
157
|
|
|
public function eraseCredentials(): void |
158
|
|
|
{ |
159
|
|
|
// if you had a plainPassword property, you'd nullify it here |
160
|
|
|
// $this->plainPassword = null; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* {@inheritdoc} |
165
|
|
|
*/ |
166
|
|
|
public function __serialize(): array |
167
|
|
|
{ |
168
|
|
|
// add $this->salt too if you don't use Bcrypt or Argon2i |
169
|
|
|
return [$this->id, $this->username, $this->password]; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* {@inheritdoc} |
174
|
|
|
*/ |
175
|
|
|
public function __unserialize(array $data): void |
176
|
|
|
{ |
177
|
|
|
// add $this->salt too if you don't use Bcrypt or Argon2i |
178
|
|
|
[$this->id, $this->username, $this->password] = $data; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function getProperties(): Collection |
182
|
|
|
{ |
183
|
|
|
return $this->properties; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function addProperty(Property $property): self |
187
|
|
|
{ |
188
|
|
|
if (!$this->properties->contains($property)) { |
189
|
|
|
$this->properties[] = $property; |
190
|
|
|
$property->setAuthor($this); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function removeProperty(Property $property): self |
197
|
|
|
{ |
198
|
|
|
if ($this->properties->contains($property)) { |
199
|
|
|
$this->properties->removeElement($property); |
200
|
|
|
// set the owning side to null (unless already changed) |
201
|
|
|
if ($property->getAuthor() === $this) { |
202
|
|
|
$property->setAuthor(null); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function getConfirmationToken(): ?string |
210
|
|
|
{ |
211
|
|
|
return $this->confirmation_token; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function setConfirmationToken(?string $confirmation_token): self |
215
|
|
|
{ |
216
|
|
|
$this->confirmation_token = $confirmation_token; |
217
|
|
|
|
218
|
|
|
return $this; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function getPasswordRequestedAt(): ?\DateTimeInterface |
222
|
|
|
{ |
223
|
|
|
return $this->password_requested_at; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public function setPasswordRequestedAt(?\DateTimeInterface $password_requested_at): self |
227
|
|
|
{ |
228
|
|
|
$this->password_requested_at = $password_requested_at; |
229
|
|
|
|
230
|
|
|
return $this; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Checks whether the password reset request has expired. |
235
|
|
|
*/ |
236
|
|
|
public function isPasswordRequestNonExpired(int $ttl): bool |
237
|
|
|
{ |
238
|
|
|
return $this->getPasswordRequestedAt() instanceof \DateTime && |
239
|
|
|
$this->getPasswordRequestedAt()->getTimestamp() + $ttl > time(); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
public function getProfile(): ?Profile |
243
|
|
|
{ |
244
|
|
|
return $this->profile; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
public function setProfile(Profile $profile): self |
248
|
|
|
{ |
249
|
|
|
// set the owning side of the relation if necessary |
250
|
|
|
if ($profile->getUser() !== $this) { |
251
|
|
|
$profile->setUser($this); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
$this->profile = $profile; |
255
|
|
|
|
256
|
|
|
return $this; |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|