1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* (c) Lukasz D. Tulikowski <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace App\Entity; |
13
|
|
|
|
14
|
|
|
use App\EventSubscriber\UserSubscriber; |
15
|
|
|
use App\Traits\IdColumnTrait; |
16
|
|
|
use App\Traits\TimeAwareTrait; |
17
|
|
|
use Doctrine\ORM\Mapping as ORM; |
18
|
|
|
use JMS\Serializer\Annotation as JMS; |
19
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
20
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @JMS\ExclusionPolicy("ALL") |
24
|
|
|
*/ |
25
|
|
|
abstract class AbstractUser implements UserInterface, \Serializable |
26
|
|
|
{ |
27
|
|
|
use IdColumnTrait; |
28
|
|
|
use TimeAwareTrait; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
* |
33
|
|
|
* @ORM\Column(type="string") |
34
|
|
|
* @Assert\NotBlank |
35
|
|
|
* |
36
|
|
|
* @JMS\Expose |
37
|
|
|
*/ |
38
|
|
|
protected $fullName; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
* |
43
|
|
|
* @see UserSubscriber::onFlush |
44
|
|
|
* |
45
|
|
|
* @ORM\Column(type="string", unique=true) |
46
|
|
|
*/ |
47
|
|
|
protected $username; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string |
51
|
|
|
* |
52
|
|
|
* @ORM\Column(type="string", unique=true) |
53
|
|
|
* |
54
|
|
|
* @Assert\Email |
55
|
|
|
* @Assert\NotBlank |
56
|
|
|
* |
57
|
|
|
* @JMS\Expose |
58
|
|
|
*/ |
59
|
|
|
protected $email; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
protected $plainPassword; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var string |
68
|
|
|
* |
69
|
|
|
* @ORM\Column(type="string") |
70
|
|
|
*/ |
71
|
|
|
protected $password; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var array |
75
|
|
|
* |
76
|
|
|
* @ORM\Column(type="json") |
77
|
|
|
*/ |
78
|
|
|
protected $roles = []; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function __toString(): string |
84
|
|
|
{ |
85
|
|
|
return $this->username; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return int |
90
|
|
|
*/ |
91
|
|
|
public function getId(): int |
92
|
|
|
{ |
93
|
|
|
return $this->id; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $fullName |
98
|
|
|
*/ |
99
|
2 |
|
public function setFullName(string $fullName): void |
100
|
|
|
{ |
101
|
2 |
|
$this->fullName = $fullName; |
102
|
2 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
3 |
|
public function getFullName(): ?string |
108
|
|
|
{ |
109
|
3 |
|
return $this->fullName; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
public function getUsername(): ?string |
116
|
|
|
{ |
117
|
|
|
return $this->email; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $username |
122
|
|
|
*/ |
123
|
1 |
|
public function setUsername(string $username): void |
124
|
|
|
{ |
125
|
1 |
|
$this->username = $username; |
126
|
1 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
3 |
|
public function getEmail(): ?string |
132
|
|
|
{ |
133
|
3 |
|
return $this->email; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $email |
138
|
|
|
*/ |
139
|
3 |
|
public function setEmail(string $email): void |
140
|
|
|
{ |
141
|
3 |
|
$this->email = $email; |
142
|
3 |
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param string $plainPassword |
146
|
|
|
*/ |
147
|
2 |
|
public function setPlainPassword(string $plainPassword): void |
148
|
|
|
{ |
149
|
2 |
|
$this->plainPassword = $plainPassword; |
150
|
2 |
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return string|null |
154
|
|
|
*/ |
155
|
3 |
|
public function getPlainPassword(): ?string |
156
|
|
|
{ |
157
|
3 |
|
return $this->plainPassword; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return string|null |
162
|
|
|
*/ |
163
|
|
|
public function getPassword(): ?string |
164
|
|
|
{ |
165
|
|
|
return $this->password; |
166
|
|
|
} |
167
|
|
|
|
168
|
2 |
|
public function setPassword(string $password): void |
169
|
|
|
{ |
170
|
2 |
|
$this->password = $password; |
171
|
2 |
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Returns the roles or permissions granted to the user for security. |
175
|
|
|
*/ |
176
|
22 |
|
public function getRoles(): array |
177
|
|
|
{ |
178
|
22 |
|
$roles = $this->roles; |
179
|
|
|
|
180
|
|
|
// guarantees that a user always has at least one role for security |
181
|
22 |
|
if (empty($roles)) { |
182
|
|
|
$roles[] = 'ROLE_USER'; |
183
|
|
|
} |
184
|
|
|
|
185
|
22 |
|
return array_unique($roles); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function setRoles(array $roles): void |
189
|
|
|
{ |
190
|
|
|
$this->roles = $roles; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Returns the salt that was originally used to encode the password. |
195
|
|
|
* |
196
|
|
|
* {@inheritdoc} |
197
|
|
|
*/ |
198
|
2 |
|
public function getSalt(): ?string |
199
|
|
|
{ |
200
|
|
|
// See "Do you need to use a Salt?" at https://symfony.com/doc/current/cookbook/security/entity_provider.html |
201
|
|
|
// we're using bcrypt in security.yml to encode the password, so |
202
|
|
|
// the salt value is built-in and you don't have to generate one |
203
|
|
|
|
204
|
2 |
|
return null; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Removes sensitive data from the user. |
209
|
|
|
* |
210
|
|
|
* {@inheritdoc} |
211
|
|
|
*/ |
212
|
22 |
|
public function eraseCredentials(): void |
213
|
|
|
{ |
214
|
22 |
|
$this->plainPassword = null; |
215
|
22 |
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* {@inheritdoc} |
219
|
|
|
*/ |
220
|
|
|
public function serialize(): ?string |
221
|
|
|
{ |
222
|
|
|
return serialize([$this->id, $this->username, $this->password]); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* {@inheritdoc} |
227
|
|
|
*/ |
228
|
|
|
public function unserialize($serialized): void |
229
|
|
|
{ |
230
|
|
|
[$this->id, $this->username, $this->password] = unserialize($serialized, ['allowed_classes' => false]); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|