|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace App\Entity; |
|
5
|
|
|
|
|
6
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
7
|
|
|
use Doctrine\ORM\Mapping as ORM; |
|
8
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; |
|
9
|
|
|
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; |
|
10
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
11
|
|
|
use JMS\Serializer\Annotation\Exclude; |
|
12
|
|
|
use JMS\Serializer\Annotation\Expose; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @ORM\Entity(repositoryClass="App\Repository\UserRepository") |
|
16
|
|
|
* @ORM\Table(name="users") |
|
17
|
|
|
* @UniqueEntity(fields="email", message="Email already taken") |
|
18
|
|
|
* @UniqueEntity(fields="username", message="Username already taken") |
|
19
|
|
|
*/ |
|
20
|
|
|
class User implements UserInterface, \Serializable |
|
21
|
|
|
{ |
|
22
|
|
|
const ROLE_USER = 'ROLE_USER'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @ORM\Id() |
|
26
|
|
|
* @ORM\GeneratedValue() |
|
27
|
|
|
* @ORM\Column(type="integer") |
|
28
|
|
|
*/ |
|
29
|
|
|
private $id; |
|
30
|
|
|
|
|
31
|
|
|
#private $tokens; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var $profile UserProfile |
|
35
|
|
|
* @ORM\OneToOne(targetEntity="App\Entity\UserProfile", cascade={"persist", "remove"}) |
|
36
|
|
|
*/ |
|
37
|
|
|
private $profile; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @ORM\Column(type="string", length=255, unique=true) |
|
41
|
|
|
*/ |
|
42
|
|
|
public $email; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @ORM\Column(type="integer", length=1) |
|
46
|
|
|
*/ |
|
47
|
|
|
private $isEmailConfirmed; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @ORM\Column(type="string", length=255, unique=true) |
|
51
|
|
|
*/ |
|
52
|
|
|
public $username; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @ORM\Column(type="string", length=255, nullable=true) |
|
56
|
|
|
*/ |
|
57
|
|
|
private $roles; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @Exclude |
|
61
|
|
|
*/ |
|
62
|
|
|
private $plainPassword; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @Exclude |
|
66
|
|
|
* @ORM\Column(type="string", length=64) |
|
67
|
|
|
*/ |
|
68
|
|
|
private $password; |
|
69
|
|
|
|
|
70
|
31 |
|
public function __construct() |
|
71
|
|
|
{ |
|
72
|
31 |
|
$this->addRole(self::ROLE_USER); |
|
73
|
31 |
|
$this->profile = new UserProfile($this); |
|
74
|
31 |
|
$this->isEmailConfirmed = 0; |
|
75
|
31 |
|
} |
|
76
|
|
|
|
|
77
|
8 |
|
public function getProfile(): UserProfile |
|
78
|
|
|
{ |
|
79
|
8 |
|
return $this->profile; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
4 |
|
public function getId(): ?int |
|
83
|
|
|
{ |
|
84
|
4 |
|
return $this->id; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
31 |
|
public function addRole(string $role): self |
|
88
|
|
|
{ |
|
89
|
31 |
|
if (array_search($role, $this->getRoles()) === false) { |
|
90
|
3 |
|
return $this->setRoles( |
|
91
|
3 |
|
array_merge($this->getRoles(), [$role]) |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
31 |
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
1 |
|
public function removeRole(string $role): self |
|
99
|
|
|
{ |
|
100
|
1 |
|
$roles = $this->getRoles(); |
|
101
|
1 |
|
$foundedRoleKey = array_search($role, $roles); |
|
102
|
|
|
|
|
103
|
1 |
|
if ($foundedRoleKey !== false) { |
|
104
|
1 |
|
unset($roles[$foundedRoleKey]); |
|
105
|
1 |
|
return $this->setRoles($roles); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $this; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
3 |
|
private function setRoles(array $roles): self |
|
112
|
|
|
{ |
|
113
|
3 |
|
if (!count($roles)) { |
|
114
|
1 |
|
$this->roles = null; |
|
115
|
1 |
|
return $this; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
3 |
|
$this->roles = json_encode($roles); |
|
119
|
|
|
|
|
120
|
3 |
|
return $this; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
32 |
|
public function getRoles(): array |
|
124
|
|
|
{ |
|
125
|
32 |
|
if (!$this->roles) { |
|
126
|
32 |
|
return $this->getDefaultRoles(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
3 |
|
$roles = (array)json_decode($this->roles); |
|
130
|
|
|
|
|
131
|
3 |
|
if (!count($roles)) { |
|
132
|
|
|
return $this->getDefaultRoles(); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
3 |
|
return array_values($roles); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
32 |
|
private function getDefaultRoles() |
|
139
|
|
|
{ |
|
140
|
32 |
|
return [self::ROLE_USER]; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
5 |
|
public function setPlainPassword(string $plainPassword): self |
|
144
|
|
|
{ |
|
145
|
5 |
|
if (!empty($plainPassword)) { |
|
146
|
5 |
|
$this->plainPassword = $plainPassword; |
|
147
|
|
|
// Change some mapped values so preUpdate will get called. |
|
148
|
5 |
|
$this->password = ''; // just blank it out |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
5 |
|
return $this; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
7 |
|
public function getPlainPassword(): string |
|
155
|
|
|
{ |
|
156
|
7 |
|
return (string)$this->plainPassword; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
9 |
|
public function setPassword($password, UserPasswordEncoderInterface $passwordEncoder): self |
|
160
|
|
|
{ |
|
161
|
9 |
|
$this->password = $passwordEncoder->encodePassword($this, $password); |
|
162
|
|
|
|
|
163
|
9 |
|
return $this; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
6 |
|
public function isPasswordValid($password, UserPasswordEncoderInterface $passwordEncoder): bool |
|
167
|
|
|
{ |
|
168
|
6 |
|
return $passwordEncoder->isPasswordValid($this, $password); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
4 |
|
public function getPassword(): ?string |
|
172
|
|
|
{ |
|
173
|
4 |
|
return $this->password; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
11 |
|
public function getSalt(): ?string |
|
177
|
|
|
{ |
|
178
|
11 |
|
return null; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
3 |
|
public function getUsername(): ?string |
|
182
|
|
|
{ |
|
183
|
3 |
|
return $this->username; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
7 |
|
public function eraseCredentials(): self |
|
187
|
|
|
{ |
|
188
|
7 |
|
$this->plainPassword = null; |
|
189
|
|
|
|
|
190
|
7 |
|
return $this; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
1 |
|
public function serialize(): string |
|
194
|
|
|
{ |
|
195
|
1 |
|
return serialize([ |
|
196
|
1 |
|
$this->id, |
|
197
|
1 |
|
$this->email, |
|
198
|
1 |
|
$this->username, |
|
199
|
1 |
|
$this->roles, |
|
200
|
|
|
]); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
1 |
|
public function unserialize($serialized): self |
|
204
|
|
|
{ |
|
205
|
|
|
list ( |
|
206
|
1 |
|
$this->id, |
|
207
|
1 |
|
$this->email, |
|
208
|
1 |
|
$this->username, |
|
209
|
1 |
|
$this->roles, |
|
210
|
1 |
|
) = unserialize($serialized); |
|
211
|
|
|
|
|
212
|
1 |
|
return $this; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
1 |
|
public function confirmEmail() |
|
216
|
|
|
{ |
|
217
|
1 |
|
$this->isEmailConfirmed = 1; |
|
218
|
|
|
|
|
219
|
1 |
|
return $this; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
public function changeEmail($email) |
|
223
|
|
|
{ |
|
224
|
|
|
$this->email = $email; |
|
225
|
|
|
$this->isEmailConfirmed = 0; |
|
226
|
|
|
|
|
227
|
|
|
return $this; |
|
228
|
|
|
} |
|
229
|
|
|
} |