1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Entity; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use DateTimeImmutable; |
7
|
|
|
use DateTimeInterface; |
8
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
9
|
|
|
use Doctrine\Common\Collections\Collection; |
10
|
|
|
use Doctrine\ORM\Mapping as ORM; |
11
|
|
|
use Exception; |
12
|
|
|
use Serializable; |
13
|
|
|
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; |
14
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
15
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
16
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
17
|
|
|
use Gedmo\Mapping\Annotation as Gedmo; |
18
|
|
|
use Symfony\Component\HttpFoundation\File\File; |
19
|
|
|
use Vich\UploaderBundle\Mapping\Annotation as Vich; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @ORM\Entity(repositoryClass="App\Repository\UserRepository") |
23
|
|
|
* @UniqueEntity(fields={"email"}, message="There is already an account with this email") |
24
|
|
|
* @UniqueEntity(fields={"userName"}, message="There is already an account with this username") |
25
|
|
|
* @UniqueEntity(fields={"verifiedHash"}, message="Hash already exists") |
26
|
|
|
* @Vich\Uploadable |
27
|
|
|
*/ |
28
|
|
|
class User extends AbstractAppEntity implements UserInterface, Serializable |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
const HASH_VALIDATION_TIME_LIMIT = 1; //number of days that the validation link is active |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @ORM\Id() |
35
|
|
|
* @ORM\GeneratedValue() |
36
|
|
|
* @ORM\Column(type="integer") |
37
|
|
|
*/ |
38
|
|
|
private $id; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @ORM\Column(type="string", length=180, unique=true) |
42
|
|
|
*/ |
43
|
|
|
private $email; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @ORM\Column(type="json") |
47
|
|
|
*/ |
48
|
|
|
private $roles = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string The hashed password |
52
|
|
|
* @ORM\Column(type="string") |
53
|
|
|
*/ |
54
|
|
|
private $password; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @ORM\Column(type="string", length=255, unique=true) |
58
|
|
|
* @Assert\Type(type="alnum") |
59
|
|
|
*/ |
60
|
|
|
private $userName; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @ORM\Column(type="string", length=255) |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
private $image = ""; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @Assert\Image() |
70
|
|
|
* @Vich\UploadableField(mapping="user_images", fileNameProperty="image") |
71
|
|
|
* @Assert\File(maxSize="1M") |
72
|
|
|
* @var File |
73
|
|
|
*/ |
74
|
|
|
private $imageFile; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @ORM\Column(type="boolean", options={"default":"0"}) |
78
|
|
|
*/ |
79
|
|
|
private $verified = false; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @ORM\Column(type="string", length=255, unique=true) |
83
|
|
|
*/ |
84
|
|
|
private $verifiedHash; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @ORM\Column(type="datetime") |
88
|
|
|
* @Gedmo\Timestampable(on="create") |
89
|
|
|
*/ |
90
|
|
|
private $verifiedDateTime; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @ORM\Column(type="datetime") |
94
|
|
|
* @Gedmo\Timestampable(on="update") |
95
|
|
|
*/ |
96
|
|
|
private $updatedAt; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @ORM\OneToMany(targetEntity="App\Entity\Comment", mappedBy="user", cascade={"remove", "persist"}, orphanRemoval=true) |
100
|
|
|
*/ |
101
|
|
|
private $comments; |
102
|
|
|
|
103
|
|
|
public function __construct() |
104
|
|
|
{ |
105
|
|
|
$this->comments = new ArrayCollection(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function getId(): ?int |
109
|
|
|
{ |
110
|
|
|
return $this->id; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function getEmail(): ?string |
114
|
|
|
{ |
115
|
|
|
return $this->email; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function setEmail(string $email): self |
119
|
|
|
{ |
120
|
|
|
$this->email = $email; |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* A visual identifier that represents this user. |
127
|
|
|
* |
128
|
|
|
* @see UserInterface |
129
|
|
|
*/ |
130
|
|
|
public function getUsername(): string |
131
|
|
|
{ |
132
|
|
|
return (string)$this->userName; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @see UserInterface |
137
|
|
|
*/ |
138
|
|
|
public function getRoles(): array |
139
|
|
|
{ |
140
|
|
|
$roles = $this->roles; |
141
|
|
|
// guarantee every user at least has ROLE_USER |
142
|
|
|
$roles[] = 'ROLE_USER'; |
143
|
|
|
|
144
|
|
|
return array_unique($roles); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function setRoles(array $roles): self |
148
|
|
|
{ |
149
|
|
|
$this->roles = $roles; |
150
|
|
|
|
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @see UserInterface |
156
|
|
|
*/ |
157
|
|
|
public function getPassword(): string |
158
|
|
|
{ |
159
|
|
|
return (string)$this->password; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function setPassword(string $password): self |
163
|
|
|
{ |
164
|
|
|
$this->password = $password; |
165
|
|
|
|
166
|
|
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @see UserInterface |
171
|
|
|
*/ |
172
|
|
|
public function getSalt() |
173
|
|
|
{ |
174
|
|
|
// not needed when using the "bcrypt" algorithm in security.yaml |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @see UserInterface |
179
|
|
|
*/ |
180
|
|
|
public function eraseCredentials() |
181
|
|
|
{ |
182
|
|
|
// If you store any temporary, sensitive data on the user, clear it here |
183
|
|
|
// $this->plainPassword = null; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function setUserName(string $userName): self |
187
|
|
|
{ |
188
|
|
|
$this->userName = $userName; |
189
|
|
|
|
190
|
|
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function getImage() |
194
|
|
|
{ |
195
|
|
|
if ($this->image === "") { |
196
|
|
|
return ""; |
197
|
|
|
} |
198
|
|
|
return $this->image; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function setImage($image): self |
202
|
|
|
{ |
203
|
|
|
$this->image = $image; |
204
|
|
|
|
205
|
|
|
return $this; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @return File|null |
210
|
|
|
*/ |
211
|
|
|
public function getImageFile(): ?File |
212
|
|
|
{ |
213
|
|
|
return $this->imageFile; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param File|UploadedFile $imageFile |
218
|
|
|
* @return User |
219
|
|
|
* @throws Exception |
220
|
|
|
*/ |
221
|
|
|
public function setImageFile(File $imageFile = null): self |
222
|
|
|
{ |
223
|
|
|
$this->imageFile = $imageFile; |
224
|
|
|
|
225
|
|
|
// VERY IMPORTANT: |
226
|
|
|
// It is required that at least one field changes if you are using Doctrine, |
227
|
|
|
// otherwise the event listeners won't be called and the file is lost |
228
|
|
|
if ($imageFile) { |
229
|
|
|
$this->updatedAt = new DateTimeImmutable(); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
return $this; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @return DateTimeInterface|null |
237
|
|
|
*/ |
238
|
|
|
public function getUpdatedAt(): ?DateTimeInterface |
239
|
|
|
{ |
240
|
|
|
return $this->updatedAt; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @param mixed $updatedAt |
245
|
|
|
* @return User |
246
|
|
|
*/ |
247
|
|
|
public function setUpdatedAt($updatedAt): self |
248
|
|
|
{ |
249
|
|
|
$this->updatedAt = $updatedAt; |
250
|
|
|
|
251
|
|
|
return $this; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
public function getVerified(): ?bool |
255
|
|
|
{ |
256
|
|
|
return $this->verified; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
public function setVerified(bool $verified): self |
260
|
|
|
{ |
261
|
|
|
$this->verified = $verified; |
262
|
|
|
|
263
|
|
|
return $this; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
public function getVerifiedHash(): ?string |
267
|
|
|
{ |
268
|
|
|
return $this->verifiedHash; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
public function setVerifiedHash(string $verifiedHash): self |
272
|
|
|
{ |
273
|
|
|
$this->verifiedHash = $verifiedHash; |
274
|
|
|
|
275
|
|
|
return $this; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
public function getVerifiedDateTime(): ?DateTimeInterface |
279
|
|
|
{ |
280
|
|
|
return $this->verifiedDateTime; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
public function setVerifiedDateTime(DateTimeInterface $verifiedDateTime): self |
284
|
|
|
{ |
285
|
|
|
$this->verifiedDateTime = $verifiedDateTime; |
286
|
|
|
|
287
|
|
|
return $this; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @return bool |
292
|
|
|
* @throws Exception |
293
|
|
|
*/ |
294
|
|
|
public function isVerifiedDateTimeValid(): bool |
295
|
|
|
{ |
296
|
|
|
$now = new DateTime(); |
297
|
|
|
return $now->getTimestamp() - $this->getVerifiedDateTime()->getTimestamp() <= self::HASH_VALIDATION_TIME_LIMIT * 60 * 60 * 24; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* @param string $hash |
302
|
|
|
* @return bool |
303
|
|
|
*/ |
304
|
|
|
public function isHashValid(string $hash): bool |
305
|
|
|
{ |
306
|
|
|
return $this->getVerifiedHash() === $hash; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* String representation of object |
312
|
|
|
* @link https://php.net/manual/en/serializable.serialize.php |
313
|
|
|
* @return string the string representation of the object or null |
314
|
|
|
* @since 5.1.0 |
315
|
|
|
*/ |
316
|
|
|
public function serialize() |
317
|
|
|
{ |
318
|
|
|
return serialize([ |
319
|
|
|
$this->id, |
320
|
|
|
$this->email, |
321
|
|
|
$this->roles, |
322
|
|
|
$this->password, |
323
|
|
|
$this->userName, |
324
|
|
|
$this->image, |
325
|
|
|
$this->verified, |
326
|
|
|
$this->verifiedDateTime, |
327
|
|
|
$this->updatedAt, |
328
|
|
|
]); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Constructs the object |
333
|
|
|
* @link https://php.net/manual/en/serializable.unserialize.php |
334
|
|
|
* @param string $serialized <p> |
335
|
|
|
* The string representation of the object. |
336
|
|
|
* </p> |
337
|
|
|
* @return void |
338
|
|
|
* @since 5.1.0 |
339
|
|
|
*/ |
340
|
|
|
public function unserialize($serialized) |
341
|
|
|
{ |
342
|
|
|
list( |
343
|
|
|
$this->id, |
344
|
|
|
$this->email, |
345
|
|
|
$this->roles, |
346
|
|
|
$this->password, |
347
|
|
|
$this->userName, |
348
|
|
|
$this->image, |
349
|
|
|
$this->verified, |
350
|
|
|
$this->verifiedDateTime, |
351
|
|
|
$this->updatedAt, |
352
|
|
|
) = unserialize($serialized, ['allowed_classes' => false]); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* @return Collection|Comment[] |
357
|
|
|
*/ |
358
|
|
|
public function getComments(): Collection |
359
|
|
|
{ |
360
|
|
|
return $this->comments; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
public function addComment(Comment $comment): self |
364
|
|
|
{ |
365
|
|
|
if (!$this->comments->contains($comment)) { |
366
|
|
|
$this->comments[] = $comment; |
367
|
|
|
$comment->setUser($this); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
return $this; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
public function removeComment(Comment $comment): self |
374
|
|
|
{ |
375
|
|
|
if ($this->comments->contains($comment)) { |
376
|
|
|
$this->comments->removeElement($comment); |
377
|
|
|
// set the owning side to null (unless already changed) |
378
|
|
|
if ($comment->getUser() === $this) { |
379
|
|
|
$comment->setUser(null); |
380
|
|
|
} |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
return $this; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
public function __toString() |
387
|
|
|
{ |
388
|
|
|
return $this->getUsername() . ' / ' . $this->getEmail(); |
389
|
|
|
} |
390
|
|
|
} |
391
|
|
|
|