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; |
9
|
|
|
use SumoCoders\FrameworkMultiUserBundle\ValueObject\Status; |
10
|
|
|
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @ORM\Entity(repositoryClass="SumoCoders\FrameworkMultiUserBundle\BaseUser\DoctrineBaseUserRepository") |
14
|
|
|
* @ORM\InheritanceType("JOINED") |
15
|
|
|
* @ORM\DiscriminatorColumn(name="discr", type="string") |
16
|
|
|
*/ |
17
|
|
|
class BaseUser implements User |
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
|
|
|
* @var string |
79
|
|
|
*/ |
80
|
|
|
protected $plainPassword; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $username |
84
|
|
|
* @param string $plainPassword |
85
|
|
|
* @param string $displayName |
86
|
|
|
* @param string $email |
87
|
|
|
* @param int $id |
88
|
|
|
* @param PasswordResetToken $token |
89
|
|
|
*/ |
90
|
|
|
public function __construct( |
91
|
|
|
$username, |
92
|
|
|
$plainPassword, |
93
|
|
|
$displayName, |
94
|
|
|
$email, |
95
|
|
|
$id = null, |
96
|
|
|
PasswordResetToken $token = null |
97
|
|
|
) { |
98
|
|
|
$this->username = $username; |
99
|
|
|
$this->plainPassword = $plainPassword; |
100
|
|
|
$this->displayName = $displayName; |
101
|
|
|
$this->email = $email; |
102
|
|
|
$this->id = $id; |
103
|
|
|
|
104
|
|
|
// set the default status to active |
105
|
|
|
$this->status = Status::active(); |
106
|
|
|
|
107
|
|
|
if ($token) { |
108
|
|
|
$this->passwordResetToken = $token; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function getRoles() |
113
|
|
|
{ |
114
|
|
|
return ['ROLE_USER']; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function getPassword() |
118
|
|
|
{ |
119
|
|
|
return $this->password; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function getSalt() |
123
|
|
|
{ |
124
|
|
|
return $this->salt; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function encodePassword(PasswordEncoderInterface $encoder) |
128
|
|
|
{ |
129
|
|
|
if (empty($this->plainPassword)) { |
130
|
|
|
return; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if (empty($this->salt)) { |
134
|
|
|
$this->salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$this->password = $encoder->encodePassword($this->plainPassword, $this->salt); |
138
|
|
|
$this->eraseCredentials(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function getUsername() |
142
|
|
|
{ |
143
|
|
|
return $this->username; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function eraseCredentials() |
147
|
|
|
{ |
148
|
|
|
$this->plainPassword = null; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function getDisplayName() |
152
|
|
|
{ |
153
|
|
|
return $this->displayName; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function __toString() |
157
|
|
|
{ |
158
|
|
|
return $this->getDisplayName(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function clearPasswordResetToken() |
162
|
|
|
{ |
163
|
|
|
$this->passwordResetToken = null; |
164
|
|
|
|
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function generatePasswordResetToken() |
169
|
|
|
{ |
170
|
|
|
$this->passwordResetToken = PasswordResetToken::generate(); |
171
|
|
|
|
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function getPasswordResetToken() |
176
|
|
|
{ |
177
|
|
|
return $this->passwordResetToken; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function getEmail() |
181
|
|
|
{ |
182
|
|
|
return $this->email; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function setPassword($password) |
186
|
|
|
{ |
187
|
|
|
$this->plainPassword = $password; |
188
|
|
|
|
189
|
|
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function getId() |
193
|
|
|
{ |
194
|
|
|
return $this->id; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function hasPlainPassword() |
198
|
|
|
{ |
199
|
|
|
return !empty($this->plainPassword); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function getPlainPassword() |
203
|
|
|
{ |
204
|
|
|
return $this->plainPassword; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function change(UserDataTransferObject $data) |
208
|
|
|
{ |
209
|
|
|
$this->username = $data->getUserName(); |
210
|
|
|
$this->plainPassword = $data->getPlainPassword(); |
211
|
|
|
$this->displayName = $data->getDisplayName(); |
212
|
|
|
$this->email = $data->getEmail(); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function toggleBlock() |
216
|
|
|
{ |
217
|
|
|
if ((string) $this->status === Status::BLOCKED) { |
218
|
|
|
$this->status = Status::active(); |
219
|
|
|
|
220
|
|
|
return; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
$this->status = Status::blocked(); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public function isBlocked() |
227
|
|
|
{ |
228
|
|
|
return $this->status->isBlocked(); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|