1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sinergi\Users\User; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Sinergi\Users\Utils\Token; |
7
|
|
|
|
8
|
|
|
trait UserEntityTrait |
9
|
|
|
{ |
10
|
|
|
protected $id; |
11
|
|
|
protected $status = UserEntityInterface::STATUS_ACTIVE; |
12
|
|
|
protected $isAdmin; |
13
|
|
|
protected $email = null; |
14
|
|
|
protected $pendingEmail = null; |
15
|
|
|
protected $deletedEmail = null; |
16
|
|
|
protected $isEmailConfirmed = false; |
17
|
|
|
protected $emailConfirmationToken; |
18
|
|
|
protected $emailConfirmationTokenExpirationDatetime; |
19
|
|
|
protected $lastEmailTokenGeneratedDatetime; |
20
|
|
|
protected $password; |
21
|
|
|
protected $passwordResetToken; |
22
|
|
|
protected $passwordResetTokenExpirationDatetime; |
23
|
|
|
protected $lastPasswordResetTokenGeneratedDatetime; |
24
|
|
|
protected $creationDatetime; |
25
|
|
|
protected $modificationDatetime; |
26
|
|
|
|
27
|
|
|
public function __construct() |
28
|
|
|
{ |
29
|
|
|
$this->setCreationDatetime(new DateTime()); |
30
|
|
|
$this->setModificationDatetime(new DateTime()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function getId(): int |
34
|
|
|
{ |
35
|
|
|
return $this->id; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function setId(int $id): UserEntityInterface |
39
|
|
|
{ |
40
|
|
|
$this->id = $id; |
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getStatus(): string |
45
|
|
|
{ |
46
|
|
|
return $this->status; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function setStatus(string $status): UserEntityInterface |
50
|
|
|
{ |
51
|
|
|
$this->status = $status; |
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function isAdmin(): boolean |
56
|
|
|
{ |
57
|
|
|
return $this->isAdmin; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function setIsAdmin(boolean $isAdmin): UserEntityInterface |
61
|
|
|
{ |
62
|
|
|
$this->isAdmin = $isAdmin; |
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function isActive(): boolean |
67
|
|
|
{ |
68
|
|
|
return $this->getStatus() === UserEntityInterface::STATUS_ACTIVE; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getEmail(): string |
72
|
|
|
{ |
73
|
|
|
return $this->email; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function setEmail(string $email): UserEntityInterface |
77
|
|
|
{ |
78
|
|
|
$this->email = $email; |
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function getPendingEmail(): string |
83
|
|
|
{ |
84
|
|
|
return $this->pendingEmail; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function setPendingEmail(string $pendingEmail): UserEntityInterface |
88
|
|
|
{ |
89
|
|
|
$this->pendingEmail = $pendingEmail; |
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function isEmailConfirmed(): boolean |
94
|
|
|
{ |
95
|
|
|
return $this->isEmailConfirmed; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function setIsEmailConfirmed(boolean $isEmailConfirmed): UserEntityInterface |
99
|
|
|
{ |
100
|
|
|
$this->isEmailConfirmed = $isEmailConfirmed; |
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function getEmailConfirmationToken(): string |
105
|
|
|
{ |
106
|
|
|
return $this->emailConfirmationToken; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function setEmailConfirmationToken(string $emailConfirmationToken): UserEntityInterface |
110
|
|
|
{ |
111
|
|
|
$this->emailConfirmationToken = $emailConfirmationToken; |
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function getEmailConfirmationTokenExpirationDatetime(): DateTime |
116
|
|
|
{ |
117
|
|
|
return $this->emailConfirmationTokenExpirationDatetime; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function setEmailConfirmationTokenExpirationDatetime( |
121
|
|
|
DateTime $emailConfirmationTokenExpirationDatetime |
122
|
|
|
): UserEntityInterface { |
123
|
|
|
$this->emailConfirmationTokenExpirationDatetime = $emailConfirmationTokenExpirationDatetime; |
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function getLastEmailTokenGeneratedDatetime(): DateTime |
128
|
|
|
{ |
129
|
|
|
return $this->lastEmailTokenGeneratedDatetime; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function setLastEmailTokenGeneratedDatetime( |
133
|
|
|
DateTime $lastEmailTokenGeneratedDatetime |
134
|
|
|
): UserEntityInterface { |
135
|
|
|
$this->lastEmailTokenGeneratedDatetime = $lastEmailTokenGeneratedDatetime; |
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
View Code Duplication |
public function canGenerateNewEmailConfirmationToken(): boolean |
|
|
|
|
140
|
|
|
{ |
141
|
|
|
$lastGenerated = $this->getLastEmailTokenGeneratedDatetime(); |
142
|
|
|
return ( |
143
|
|
|
empty($lastGenerated) || |
144
|
|
|
(new DateTime())->getTimestamp() - $lastGenerated->getTimestamp() > UserEntityInterface::EMAIL_COOLDOWN |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function generateEmailConfirmationToken(): UserEntityInterface |
149
|
|
|
{ |
150
|
|
|
if ($this->canGenerateNewEmailConfirmationToken()) { |
151
|
|
|
$this->setEmailConfirmationToken(Token::generate(40)); |
152
|
|
|
$this->setLastEmailTokenGeneratedDatetime(new DateTime()); |
153
|
|
|
} |
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function getDeletedEmail(): string |
158
|
|
|
{ |
159
|
|
|
return $this->deletedEmail; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function setDeletedEmail(string $deletedEmail): UserEntityInterface |
163
|
|
|
{ |
164
|
|
|
$this->deletedEmail = $deletedEmail; |
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function getPassword(): string |
169
|
|
|
{ |
170
|
|
|
return $this->password; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function setPassword(string $password): UserEntityInterface |
174
|
|
|
{ |
175
|
|
|
$this->password = $password; |
176
|
|
|
$this->hashPassword(); |
177
|
|
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function getPasswordResetToken(): string |
181
|
|
|
{ |
182
|
|
|
return $this->passwordResetToken; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function setPasswordResetToken(string $passwordResetToken): UserEntityInterface |
186
|
|
|
{ |
187
|
|
|
$this->passwordResetToken = $passwordResetToken; |
188
|
|
|
return $this; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function getPasswordResetTokenExpirationDatetime(): DateTime |
192
|
|
|
{ |
193
|
|
|
return $this->passwordResetTokenExpirationDatetime; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function setPasswordResetTokenExpirationDatetime( |
197
|
|
|
DateTime $passwordResetTokenExpirationDatetime |
198
|
|
|
): UserEntityInterface { |
199
|
|
|
$this->passwordResetTokenExpirationDatetime = $passwordResetTokenExpirationDatetime; |
200
|
|
|
return $this; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function getLastPasswordResetTokenGeneratedDatetime(): DateTime |
204
|
|
|
{ |
205
|
|
|
return $this->lastPasswordResetTokenGeneratedDatetime; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function setLastPasswordResetTokenGeneratedDatetime( |
209
|
|
|
DateTime $lastPasswordResetTokenGeneratedDatetime |
210
|
|
|
): UserEntityInterface { |
211
|
|
|
$this->lastPasswordResetTokenGeneratedDatetime = $lastPasswordResetTokenGeneratedDatetime; |
212
|
|
|
return $this; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
View Code Duplication |
public function canGenerateNewResetPasswordToken(): boolean |
|
|
|
|
216
|
|
|
{ |
217
|
|
|
$lastGenerated = $this->getLastPasswordResetTokenGeneratedDatetime(); |
218
|
|
|
return ( |
219
|
|
|
empty($lastGenerated) || |
220
|
|
|
(new DateTime())->getTimestamp() - $lastGenerated->getTimestamp() > UserEntityInterface::EMAIL_COOLDOWN |
221
|
|
|
); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public function generatePasswordResetToken(): UserEntityInterface |
225
|
|
|
{ |
226
|
|
|
if ($this->canGenerateNewResetPasswordToken()) { |
227
|
|
|
$this->setPasswordResetToken(Token::generate(40)); |
228
|
|
|
$this->setLastPasswordResetTokenGeneratedDatetime(new DateTime()); |
229
|
|
|
} |
230
|
|
|
return $this; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
protected function hashPassword(): UserEntityInterface |
234
|
|
|
{ |
235
|
|
|
$this->password = password_hash( |
236
|
|
|
(string) $this->password, |
237
|
|
|
PASSWORD_DEFAULT |
238
|
|
|
); |
239
|
|
|
return $this; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
public function testPassword(string $password): boolean |
243
|
|
|
{ |
244
|
|
|
return password_verify($password, $this->password); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
public function getCreationDatetime(): DateTime |
248
|
|
|
{ |
249
|
|
|
return $this->creationDatetime; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
public function setCreationDatetime(DateTime $creationDatetime): UserEntityInterface |
253
|
|
|
{ |
254
|
|
|
$this->creationDatetime = $creationDatetime; |
255
|
|
|
return $this; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public function getModificationDatetime(): DateTime |
259
|
|
|
{ |
260
|
|
|
return $this->modificationDatetime; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
public function setModificationDatetime(DateTime $modificationDatetime): UserEntityInterface |
264
|
|
|
{ |
265
|
|
|
$this->modificationDatetime = $modificationDatetime; |
266
|
|
|
return $this; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
public function toArray(): array |
270
|
|
|
{ |
271
|
|
|
return [ |
272
|
|
|
'id' => $this->getId(), |
273
|
|
|
'status' => $this->getStatus(), |
274
|
|
|
'email' => $this->getEmail(), |
275
|
|
|
'pendingEmail' => $this->getPendingEmail(), |
276
|
|
|
'deletedEmail' => $this->getDeletedEmail(), |
277
|
|
|
'isEmailConfirmed' => $this->isEmailConfirmed(), |
278
|
|
|
'emailConfirmationToken' => $this->getEmailConfirmationToken(), |
279
|
|
|
'emailConfirmationTokenExpirationDatetime' => $this->getEmailConfirmationTokenExpirationDatetime() |
280
|
|
|
->format('Y-m-d H:i:s'), |
281
|
|
|
'lastEmailTokenGeneratedDatetime' => $this->getEmailConfirmationTokenExpirationDatetime() |
282
|
|
|
->format('Y-m-d H:i:s'), |
283
|
|
|
'password' => $this->getPassword(), |
284
|
|
|
'passwordResetToken' => $this->getPasswordResetToken(), |
285
|
|
|
'passwordResetTokenExpirationDatetime' => $this->getPasswordResetTokenExpirationDatetime() |
286
|
|
|
->format('Y-m-d H:i:s'), |
287
|
|
|
'lastPasswordResetTokenGeneratedDatetime' => $this->getLastPasswordResetTokenGeneratedDatetime() |
288
|
|
|
->format('Y-m-d H:i:s'), |
289
|
|
|
'isAdmin' => $this->isAdmin(), |
290
|
|
|
'creationDatetime' => $this->getCreationDatetime()->format('Y-m-d H:i:s'), |
291
|
|
|
'modificationDatetime' => $this->getModificationDatetime()->format('Y-m-d H:i:s'), |
292
|
|
|
]; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
public function jsonSerialize(): array |
296
|
|
|
{ |
297
|
|
|
return $this->toArray(); |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
|
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..