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