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