1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SumoCoders\FrameworkMultiUserBundle\Entity; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Mapping as ORM; |
6
|
|
|
use SumoCoders\FrameworkMultiUserBundle\DataTransferObject\Interfaces\UserDataTransferObject; |
7
|
|
|
use SumoCoders\FrameworkMultiUserBundle\Security\PasswordResetToken; |
8
|
|
|
use SumoCoders\FrameworkMultiUserBundle\User\Interfaces\User as UserInterface; |
9
|
|
|
use SumoCoders\FrameworkMultiUserBundle\ValueObject\Status; |
10
|
|
|
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @ORM\Entity |
14
|
|
|
* @ORM\InheritanceType("JOINED") |
15
|
|
|
* @ORM\DiscriminatorColumn(name="discr", type="string") |
16
|
|
|
*/ |
17
|
|
|
class User implements UserInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var int |
21
|
|
|
* |
22
|
|
|
* @ORM\Column(type="integer") |
23
|
|
|
* @ORM\Id |
24
|
|
|
* @ORM\GeneratedValue |
25
|
|
|
*/ |
26
|
|
|
protected $id; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
* |
31
|
|
|
* @ORM\Column(type="string") |
32
|
|
|
*/ |
33
|
|
|
protected $username; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
* |
38
|
|
|
* @ORM\Column(type="string") |
39
|
|
|
*/ |
40
|
|
|
protected $password; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
* |
45
|
|
|
* @ORM\Column(type="string") |
46
|
|
|
*/ |
47
|
|
|
protected $salt; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string |
51
|
|
|
* |
52
|
|
|
* @ORM\Column(type="string") |
53
|
|
|
*/ |
54
|
|
|
protected $displayName; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var PasswordResetToken |
58
|
|
|
* |
59
|
|
|
* @ORM\Column(type="string", nullable=true) |
60
|
|
|
*/ |
61
|
|
|
protected $passwordResetToken; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var string |
65
|
|
|
* |
66
|
|
|
* @ORM\Column(type="string") |
67
|
|
|
*/ |
68
|
|
|
protected $email; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var Status |
72
|
|
|
* |
73
|
|
|
* @ORM\Column(type="user_status") |
74
|
|
|
*/ |
75
|
|
|
protected $status; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $username |
79
|
|
|
* @param string $plainPassword |
80
|
|
|
* @param string $displayName |
81
|
|
|
* @param string $email |
82
|
|
|
* @param int $id |
83
|
|
|
* @param PasswordResetToken $token |
84
|
|
|
*/ |
85
|
|
|
public function __construct( |
86
|
|
|
$username, |
87
|
|
|
$plainPassword, |
88
|
|
|
$displayName, |
89
|
|
|
$email, |
90
|
|
|
$id = null, |
91
|
|
|
PasswordResetToken $token = null |
92
|
|
|
) { |
93
|
|
|
$this->username = $username; |
94
|
|
|
$this->plainPassword = $plainPassword; |
|
|
|
|
95
|
|
|
$this->displayName = $displayName; |
96
|
|
|
$this->email = $email; |
97
|
|
|
$this->id = $id; |
98
|
|
|
|
99
|
|
|
if ($token) { |
100
|
|
|
$this->passwordResetToken = $token; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function getRoles() |
105
|
|
|
{ |
106
|
|
|
return ['ROLE_USER']; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function getPassword() |
110
|
|
|
{ |
111
|
|
|
return $this->password; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function getSalt() |
115
|
|
|
{ |
116
|
|
|
return $this->salt; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function encodePassword(PasswordEncoderInterface $encoder) |
120
|
|
|
{ |
121
|
|
|
if (empty($this->plainPassword)) { |
|
|
|
|
122
|
|
|
return; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if (empty($this->salt)) { |
126
|
|
|
$this->salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$this->password = $encoder->encodePassword($this->plainPassword, $this->salt); |
|
|
|
|
130
|
|
|
$this->eraseCredentials(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function getUsername() |
134
|
|
|
{ |
135
|
|
|
return $this->username; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function eraseCredentials() |
139
|
|
|
{ |
140
|
|
|
$this->plainPassword = null; |
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function getDisplayName() |
144
|
|
|
{ |
145
|
|
|
return $this->displayName; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function __toString() |
149
|
|
|
{ |
150
|
|
|
return $this->getDisplayName(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function clearPasswordResetToken() |
154
|
|
|
{ |
155
|
|
|
$this->passwordResetToken = null; |
156
|
|
|
|
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function generatePasswordResetToken() |
161
|
|
|
{ |
162
|
|
|
$this->passwordResetToken = PasswordResetToken::generate(); |
163
|
|
|
|
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function getPasswordResetToken() |
168
|
|
|
{ |
169
|
|
|
return $this->passwordResetToken; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function getEmail() |
173
|
|
|
{ |
174
|
|
|
return $this->email; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function setPassword($password) |
178
|
|
|
{ |
179
|
|
|
$this->plainPassword = $password; |
|
|
|
|
180
|
|
|
|
181
|
|
|
return $this; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function getId() |
185
|
|
|
{ |
186
|
|
|
return $this->id; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function hasPlainPassword() |
190
|
|
|
{ |
191
|
|
|
return !empty($this->plainPassword); |
|
|
|
|
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function getPlainPassword() |
195
|
|
|
{ |
196
|
|
|
return $this->plainPassword; |
|
|
|
|
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function change(UserDataTransferObject $data) |
200
|
|
|
{ |
201
|
|
|
$this->username = $data->getUserName(); |
202
|
|
|
$this->plainPassword = $data->getPlainPassword(); |
|
|
|
|
203
|
|
|
$this->displayName = $data->getDisplayName(); |
204
|
|
|
$this->email = $data->getEmail(); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function toggleBlock() |
208
|
|
|
{ |
209
|
|
|
if ((string) $this->status === Status::BLOCKED) { |
210
|
|
|
$this->status = Status::active(); |
211
|
|
|
|
212
|
|
|
return; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
$this->status = Status::blocked(); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function isBlocked() |
219
|
|
|
{ |
220
|
|
|
return $this->status->isBlocked(); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.