-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathUser.php
236 lines (196 loc) · 5.84 KB
/
User.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Entity;
use App\Repository\UserRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
use Doctrine\ORM\Mapping\PrePersist;
use Doctrine\ORM\Mapping\PreUpdate;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Defines the properties of the User entity to represent the application users.
* See https://symfony.com/doc/current/doctrine.html#creating-an-entity-class.
*
* Tip: if you have an existing database, you can generate these entity class automatically.
* See https://symfony.com/doc/current/doctrine/reverse_engineering.html
*
* @author Ryan Weaver <[email protected]>
* @author Javier Eguiluz <[email protected]>
* @author Mecanik <[email protected]>
*/
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\Table(name: 'symfony_demo_user')]
#[HasLifecycleCallbacks]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
// We can use constants for roles to find usages all over the application rather
// than doing a full-text search on the "ROLE_" string.
// It also prevents from making typo errors.
final public const ROLE_USER = 'ROLE_USER';
final public const ROLE_ADMIN = 'ROLE_ADMIN';
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: Types::INTEGER)]
private ?int $id = null;
#[ORM\Column(type: Types::STRING)]
#[Assert\NotBlank]
private ?string $fullName = null;
#[ORM\Column(type: Types::STRING, unique: true)]
#[Assert\NotBlank]
#[Assert\Length(min: 2, max: 50)]
private ?string $username = null;
#[ORM\Column(type: Types::STRING, unique: true)]
#[Assert\Email]
private ?string $email = null;
#[ORM\Column(type: Types::STRING)]
private ?string $password = null;
#[ORM\Column(type: Types::DATETIMET_IMMUTABLE)]
private $createdAt;
#[ORM\Column(type: Types::DATETIMET_IMMUTABLE)]
private $updatedAt;
/**
* @var string[]
*/
#[ORM\Column(type: Types::JSON)]
private array $roles = [];
public function getId(): ?int
{
return $this->id;
}
public function setFullName(string $fullName): void
{
$this->fullName = $fullName;
}
public function getFullName(): ?string
{
return $this->fullName;
}
public function getUserIdentifier(): string
{
return (string) $this->username;
}
public function getUsername(): string
{
return $this->getUserIdentifier();
}
public function setUsername(string $username): void
{
$this->username = $username;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): void
{
$this->email = $email;
}
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(string $password): void
{
$this->password = $password;
}
/**
* Returns the roles or permissions granted to the user for security.
*/
public function getRoles(): array
{
$roles = $this->roles;
// guarantees that a user always has at least one role for security
if (empty($roles)) {
$roles[] = self::ROLE_USER;
}
return array_unique($roles);
}
/**
* @param string[] $roles
*/
public function setRoles(array $roles): void
{
$this->roles = $roles;
}
/**
* Returns the salt that was originally used to encode the password.
*
* {@inheritdoc}
*/
public function getSalt(): ?string
{
// We're using bcrypt in security.yaml to encode the password, so
// the salt value is built-in and you don't have to generate one
// See https://en.wikipedia.org/wiki/Bcrypt
return null;
}
/**
* Removes sensitive data from the user.
*
* {@inheritdoc}
*/
public function eraseCredentials(): void
{
// if you had a plainPassword property, you'd nullify it here
// $this->plainPassword = null;
}
/**
* @return array{int|null, string|null, string|null}
*/
public function __serialize(): array
{
// add $this->salt too if you don't use Bcrypt or Argon2i
return [$this->id, $this->username, $this->password];
}
/**
* @param array{int|null, string, string} $data
*/
public function __unserialize(array $data): void
{
// add $this->salt too if you don't use Bcrypt or Argon2i
[$this->id, $this->username, $this->password] = $data;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeImmutable
{
return $this->updatedAt;
}
public function setUpdatedAt(\DateTimeImmutable $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
#[PrePersist]
public function onPrePersist()
{
if (null === $this->getCreatedAt()) {
$this->setCreatedAt(new \DateTimeImmutable('now'));
}
if ($this->getUpdatedAt() == null) {
$this->setUpdatedAt(new \DateTimeImmutable('now'));
}
}
#[PreUpdate]
public function onPreUpdate()
{
$this->setUpdatedAt(new \DateTimeImmutable('now'));
}
}