1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SumoCoders\FrameworkMultiUserBundle\Entity; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Mapping as ORM; |
6
|
|
|
use Serializable; |
7
|
|
|
use SumoCoders\FrameworkMultiUserBundle\DataTransferObject\Interfaces\UserDataTransferObject; |
8
|
|
|
use SumoCoders\FrameworkMultiUserBundle\Security\PasswordResetToken; |
9
|
|
|
use SumoCoders\FrameworkMultiUserBundle\User\Interfaces\User; |
10
|
|
|
use SumoCoders\FrameworkMultiUserBundle\ValueObject\Status; |
11
|
|
|
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface; |
12
|
|
|
use Symfony\Component\Security\Core\User\EquatableInterface; |
13
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @ORM\Entity(repositoryClass="SumoCoders\FrameworkMultiUserBundle\User\DoctrineBaseUserRepository") |
17
|
|
|
* @ORM\InheritanceType("JOINED") |
18
|
|
|
* @ORM\DiscriminatorColumn(name="discr", type="string") |
19
|
|
|
*/ |
20
|
|
|
class BaseUser implements User, Serializable, EquatableInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var int |
24
|
|
|
* |
25
|
|
|
* @ORM\Column(type="integer") |
26
|
|
|
* @ORM\Id |
27
|
|
|
* @ORM\GeneratedValue |
28
|
|
|
*/ |
29
|
|
|
protected $id; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
* |
34
|
|
|
* @ORM\Column(type="string") |
35
|
|
|
*/ |
36
|
|
|
protected $username; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
* |
41
|
|
|
* @ORM\Column(type="string") |
42
|
|
|
*/ |
43
|
|
|
protected $password; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
* |
48
|
|
|
* @ORM\Column(type="string") |
49
|
|
|
*/ |
50
|
|
|
protected $salt; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var string |
54
|
|
|
* |
55
|
|
|
* @ORM\Column(type="string") |
56
|
|
|
*/ |
57
|
|
|
protected $displayName; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var PasswordResetToken |
61
|
|
|
* |
62
|
|
|
* @ORM\Column(type="string", nullable=true) |
63
|
|
|
*/ |
64
|
|
|
protected $passwordResetToken; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var string |
68
|
|
|
* |
69
|
|
|
* @ORM\Column(type="string") |
70
|
|
|
*/ |
71
|
|
|
protected $email; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var Status |
75
|
|
|
* |
76
|
|
|
* @ORM\Column(type="user_status") |
77
|
|
|
*/ |
78
|
|
|
protected $status; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @var string |
82
|
|
|
*/ |
83
|
|
|
protected $plainPassword; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $username |
87
|
|
|
* @param string $plainPassword |
88
|
|
|
* @param string $displayName |
89
|
|
|
* @param string $email |
90
|
|
|
* @param int $id |
91
|
|
|
* @param PasswordResetToken $token |
92
|
|
|
*/ |
93
|
|
|
public function __construct( |
94
|
|
|
string $username, |
95
|
|
|
string $plainPassword, |
96
|
|
|
string $displayName, |
97
|
|
|
string $email, |
98
|
|
|
int $id = null, |
99
|
|
|
PasswordResetToken $token = null |
100
|
|
|
) { |
101
|
|
|
$this->username = $username; |
102
|
|
|
$this->plainPassword = $plainPassword; |
103
|
|
|
$this->displayName = $displayName; |
104
|
|
|
$this->email = $email; |
105
|
|
|
$this->id = $id; |
106
|
|
|
|
107
|
|
|
// set the default status to active |
108
|
|
|
$this->status = Status::active(); |
|
|
|
|
109
|
|
|
|
110
|
|
|
if ($token) { |
111
|
|
|
$this->passwordResetToken = $token; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function getRoles(): array |
116
|
|
|
{ |
117
|
|
|
return ['ROLE_USER']; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function getPassword(): string |
121
|
|
|
{ |
122
|
|
|
return $this->password; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function getSalt(): string |
126
|
|
|
{ |
127
|
|
|
return $this->salt; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function encodePassword(PasswordEncoderInterface $encoder): void |
131
|
|
|
{ |
132
|
|
|
if (empty($this->plainPassword)) { |
133
|
|
|
return; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if (empty($this->salt)) { |
137
|
|
|
$this->salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$this->password = $encoder->encodePassword($this->plainPassword, $this->salt); |
141
|
|
|
$this->eraseCredentials(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function getUsername(): string |
145
|
|
|
{ |
146
|
|
|
return $this->username; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function eraseCredentials(): void |
150
|
|
|
{ |
151
|
|
|
$this->plainPassword = null; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function getDisplayName(): string |
155
|
|
|
{ |
156
|
|
|
return $this->displayName; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function __toString(): string |
160
|
|
|
{ |
161
|
|
|
return $this->getDisplayName(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function clearPasswordResetToken(): self |
165
|
|
|
{ |
166
|
|
|
$this->passwordResetToken = null; |
167
|
|
|
|
168
|
|
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function generatePasswordResetToken(): self |
172
|
|
|
{ |
173
|
|
|
$this->passwordResetToken = PasswordResetToken::generate(); |
174
|
|
|
|
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function getPasswordResetToken(): ?PasswordResetToken |
179
|
|
|
{ |
180
|
|
|
return $this->passwordResetToken; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function getEmail(): string |
184
|
|
|
{ |
185
|
|
|
return $this->email; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function setPassword(string $password): self |
189
|
|
|
{ |
190
|
|
|
$this->plainPassword = $password; |
191
|
|
|
|
192
|
|
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function getId(): int |
196
|
|
|
{ |
197
|
|
|
return $this->id; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function hasPlainPassword(): bool |
201
|
|
|
{ |
202
|
|
|
return !empty($this->plainPassword); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function getPlainPassword(): string |
206
|
|
|
{ |
207
|
|
|
return $this->plainPassword; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function change(UserDataTransferObject $data): void |
211
|
|
|
{ |
212
|
|
|
$this->username = $data->getUserName(); |
213
|
|
|
$this->plainPassword = $data->getPlainPassword(); |
214
|
|
|
$this->displayName = $data->getDisplayName(); |
215
|
|
|
$this->email = $data->getEmail(); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function toggleBlock(): void |
219
|
|
|
{ |
220
|
|
|
if ((string) $this->status === Status::BLOCKED) { |
221
|
|
|
$this->status = Status::active(); |
|
|
|
|
222
|
|
|
|
223
|
|
|
return; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
$this->status = Status::blocked(); |
|
|
|
|
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
public function isBlocked(): bool |
230
|
|
|
{ |
231
|
|
|
return $this->status->isBlocked(); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function canSwitchTo(BaseUser $user): bool |
|
|
|
|
235
|
|
|
{ |
236
|
|
|
return false; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function serialize(): string |
240
|
|
|
{ |
241
|
|
|
return serialize( |
242
|
|
|
[ |
243
|
|
|
$this->id, |
244
|
|
|
$this->username, |
245
|
|
|
$this->password, |
246
|
|
|
$this->salt, |
247
|
|
|
] |
248
|
|
|
); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
public function unserialize($serialized): void |
252
|
|
|
{ |
253
|
|
|
[$this->id, $this->username, $this->password, $this->salt] = unserialize($serialized); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
public function isEqualTo(UserInterface $user): bool |
257
|
|
|
{ |
258
|
|
|
return $user->getUsername() === $this->getUsername(); |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..