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