1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gbere\SimpleAuth\Entity; |
6
|
|
|
|
7
|
|
|
use DateTime; |
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\UserInterface; |
13
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @ORM\Table(name="gbere_auth_user") |
17
|
|
|
* @ORM\HasLifecycleCallbacks() |
18
|
|
|
* @ORM\Entity(repositoryClass="Gbere\SimpleAuth\Repository\UserRepository") |
19
|
|
|
* @UniqueEntity("email") |
20
|
|
|
*/ |
21
|
|
|
class User implements UserInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var int |
25
|
|
|
* @ORM\Id() |
26
|
|
|
* @ORM\GeneratedValue() |
27
|
|
|
* @ORM\Column(type="integer") |
28
|
|
|
*/ |
29
|
|
|
private $id; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
* @ORM\Column(type="string", length=180, unique=true) |
34
|
|
|
* @Assert\NotBlank() |
35
|
|
|
* @Assert\Email() |
36
|
|
|
*/ |
37
|
|
|
private $email; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
* @ORM\Column(type="string", length=80) |
42
|
|
|
* @Assert\NotBlank() |
43
|
|
|
*/ |
44
|
|
|
private $name; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var Collection |
48
|
|
|
* @ORM\ManyToMany(targetEntity="Gbere\SimpleAuth\Entity\Role", inversedBy="users") |
49
|
|
|
* @ORM\JoinTable(name="gbere_auth_user_role") |
50
|
|
|
*/ |
51
|
|
|
private $roles; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string The hashed password |
55
|
|
|
* @ORM\Column(type="string") |
56
|
|
|
* @Assert\NotBlank() |
57
|
|
|
*/ |
58
|
|
|
private $password; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var DateTime |
62
|
|
|
* @ORM\Column(type="datetime") |
63
|
|
|
*/ |
64
|
|
|
private $createdAt; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var bool |
68
|
|
|
* @ORM\Column(type="boolean") |
69
|
|
|
*/ |
70
|
|
|
private $enabled; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var string|null |
74
|
|
|
* @ORM\Column(type="string", length=100, nullable=true, unique=true) |
75
|
|
|
*/ |
76
|
|
|
private $confirmationToken; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @var DateTime|null |
80
|
|
|
* @ORM\Column(type="datetime", nullable=true) |
81
|
|
|
*/ |
82
|
|
|
private $passwordRequestAt; |
83
|
|
|
|
84
|
|
|
public function __construct() |
85
|
|
|
{ |
86
|
|
|
$this->enabled = false; |
87
|
|
|
$this->roles = new ArrayCollection(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getId(): ?int |
91
|
|
|
{ |
92
|
|
|
return $this->id; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getEmail(): ?string |
96
|
|
|
{ |
97
|
|
|
return $this->email; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function setEmail(string $email): self |
101
|
|
|
{ |
102
|
|
|
$this->email = $email; |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* A visual identifier that represents this user. |
109
|
|
|
* |
110
|
|
|
* @see UserInterface |
111
|
|
|
*/ |
112
|
|
|
public function getUsername(): string |
113
|
|
|
{ |
114
|
|
|
return $this->email; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function getName(): ?string |
118
|
|
|
{ |
119
|
|
|
return $this->name; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function setName(string $name): self |
123
|
|
|
{ |
124
|
|
|
$this->name = $name; |
125
|
|
|
|
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return array<Role|string> |
131
|
|
|
*/ |
132
|
|
|
public function getRoles(): array |
133
|
|
|
{ |
134
|
|
|
$roles = []; |
135
|
|
|
/** @var Role $role */ |
136
|
|
|
foreach ($this->roles as $role) { |
137
|
|
|
$roles[] = $role->getName(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $roles; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function getRolesCollection(): Collection |
144
|
|
|
{ |
145
|
|
|
return $this->roles; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function addRoleEntity(Role $role): self |
149
|
|
|
{ |
150
|
|
|
if (false === $this->roles->contains($role)) { |
151
|
|
|
$this->roles[] = $role; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function removeRoleEntity(Role $role): self |
158
|
|
|
{ |
159
|
|
|
if ($this->roles->contains($role)) { |
160
|
|
|
$this->roles->removeElement($role); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @see UserInterface |
168
|
|
|
*/ |
169
|
|
|
public function getPassword(): ?string |
170
|
|
|
{ |
171
|
|
|
return $this->password; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function setPassword(string $password): self |
175
|
|
|
{ |
176
|
|
|
$this->password = $password; |
177
|
|
|
|
178
|
|
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function getCreatedAt(): ?DateTime |
182
|
|
|
{ |
183
|
|
|
return $this->createdAt; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function setCreatedAt(DateTime $createdAt): self |
187
|
|
|
{ |
188
|
|
|
$this->createdAt = $createdAt; |
189
|
|
|
|
190
|
|
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @ORM\PrePersist |
195
|
|
|
*/ |
196
|
|
|
public function setCreatedAtValue(): void |
197
|
|
|
{ |
198
|
|
|
$this->createdAt = new DateTime(); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @see UserInterface |
203
|
|
|
*/ |
204
|
|
|
public function getSalt(): ?string |
205
|
|
|
{ |
206
|
|
|
// not needed when using the "bcrypt" algorithm in security.yaml |
207
|
|
|
return null; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @see UserInterface |
212
|
|
|
*/ |
213
|
|
|
public function eraseCredentials(): void |
214
|
|
|
{ |
215
|
|
|
// If you store any temporary, sensitive data on the user, clear it here |
216
|
|
|
// $this->plainPassword = null; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function isEnabled(): ?bool |
220
|
|
|
{ |
221
|
|
|
return $this->enabled; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public function hasEnabled(bool $enabled): self |
225
|
|
|
{ |
226
|
|
|
$this->enabled = $enabled; |
227
|
|
|
|
228
|
|
|
return $this; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
public function getConfirmationToken(): ?string |
232
|
|
|
{ |
233
|
|
|
return $this->confirmationToken; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
public function setConfirmationToken(?string $confirmationToken): self |
237
|
|
|
{ |
238
|
|
|
$this->confirmationToken = $confirmationToken; |
239
|
|
|
|
240
|
|
|
return $this; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @throws \Exception |
245
|
|
|
*/ |
246
|
|
|
public function generateToken(): self |
247
|
|
|
{ |
248
|
|
|
$this->setConfirmationToken(bin2hex(random_bytes(50))); |
249
|
|
|
|
250
|
|
|
return $this; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
public function getPasswordRequestAt(): ?DateTime |
254
|
|
|
{ |
255
|
|
|
return $this->passwordRequestAt; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public function setPasswordRequestAt(?DateTime $passwordRequestAt): self |
259
|
|
|
{ |
260
|
|
|
$this->passwordRequestAt = $passwordRequestAt; |
261
|
|
|
|
262
|
|
|
return $this; |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|