-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathUser.php
More file actions
302 lines (252 loc) · 8.14 KB
/
User.php
File metadata and controls
302 lines (252 loc) · 8.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter Shield.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Shield\Entities;
use CodeIgniter\Database\Exceptions\DataException;
use CodeIgniter\Entity\Entity;
use CodeIgniter\I18n\Time;
use CodeIgniter\Shield\Authentication\Authenticators\Session;
use CodeIgniter\Shield\Authentication\Traits\HasAccessTokens;
use CodeIgniter\Shield\Authentication\Traits\HasHmacTokens;
use CodeIgniter\Shield\Authorization\Traits\Authorizable;
use CodeIgniter\Shield\Models\LoginModel;
use CodeIgniter\Shield\Models\UserIdentityModel;
use CodeIgniter\Shield\Traits\Activatable;
use CodeIgniter\Shield\Traits\Bannable;
use CodeIgniter\Shield\Traits\Resettable;
/**
* @property string|null $email
* @property int|string|null $id
* @property list<UserIdentity>|null $identities
* @property Time|null $last_active
* @property string|null $password
* @property string|null $password_hash
* @property string|null $username
*/
class User extends Entity
{
use Authorizable;
use HasAccessTokens;
use HasHmacTokens;
use Resettable;
use Activatable;
use Bannable;
/**
* @var list<UserIdentity>|null
*/
private ?array $identities = null;
private ?string $email = null;
private ?string $password = null;
private ?string $password_hash = null;
/**
* @var list<string>
*/
protected $dates = [
'created_at',
'updated_at',
'deleted_at',
'last_active',
];
/**
* @var array<string, string>
*/
protected $casts = [
'id' => '?integer',
'active' => 'int-bool',
'permissions' => 'array',
'groups' => 'array',
];
/**
* Returns the first identity of the given $type for this user.
*
* @param string $type See const ID_TYPE_* in Authenticator.
* 'email_2fa'|'email_activate'|'email_password'|'magic-link'|'access_token'
*/
public function getIdentity(string $type): ?UserIdentity
{
$identities = $this->getIdentities($type);
return count($identities) ? array_shift($identities) : null;
}
/**
* ensures that all of the user's identities are loaded
* into the instance for faster access later.
*/
private function populateIdentities(): void
{
if ($this->identities === null) {
/** @var UserIdentityModel $identityModel */
$identityModel = model(UserIdentityModel::class);
$this->identities = $identityModel->getIdentities($this);
}
}
/**
* Accessor method for this user's UserIdentity objects.
* Will populate if they don't exist.
*
* @param string $type 'all' returns all identities.
*
* @return list<UserIdentity>
*/
public function getIdentities(string $type = 'all'): array
{
$this->populateIdentities();
if ($type === 'all') {
return $this->identities;
}
$identities = [];
foreach ($this->identities as $identity) {
if ($identity->type === $type) {
$identities[] = $identity;
}
}
return $identities;
}
public function setIdentities(array $identities): void
{
$this->identities = $identities;
}
/**
* Creates a new identity for this user with an email/password
* combination.
*
* @phpstan-param array{email: string, password: string} $credentials
*/
public function createEmailIdentity(array $credentials): void
{
/** @var UserIdentityModel $identityModel */
$identityModel = model(UserIdentityModel::class);
$identityModel->createEmailIdentity($this, $credentials);
// Ensure we will reload all identities
$this->identities = null;
}
/**
* Returns the user's Email/Password identity.
*/
public function getEmailIdentity(): ?UserIdentity
{
return $this->getIdentity(Session::ID_TYPE_EMAIL_PASSWORD);
}
/**
* If $email, $password, or $password_hash have been updated,
* will update the user's email identity record with the
* correct values.
*/
public function saveEmailIdentity(): bool
{
if (($this->email === null || $this->email === '') && ($this->password === null || $this->password === '') && ($this->password_hash === null || $this->password_hash === '')) {
return true;
}
$identity = $this->getEmailIdentity();
if ($identity === null) {
// Ensure we reload all identities
$this->identities = null;
$this->createEmailIdentity([
'email' => $this->email,
'password' => '',
]);
$identity = $this->getEmailIdentity();
}
if ($this->email !== null && $this->email !== '') {
$identity->secret = $this->email;
}
if ($this->password !== null && $this->password !== '') {
$identity->secret2 = service('passwords')->hash($this->password);
}
if ($this->password_hash !== null && $this->password_hash !== '' && ($this->password === null || $this->password === '')) {
$identity->secret2 = $this->password_hash;
}
/** @var UserIdentityModel $identityModel */
$identityModel = model(UserIdentityModel::class);
try {
/** @throws DataException */
$identityModel->save($identity);
} catch (DataException $e) {
// There may be no data to update.
$messages = [
lang('Database.emptyDataset', ['insert']),
lang('Database.emptyDataset', ['update']),
];
if (in_array($e->getMessage(), $messages, true)) {
return true;
}
throw $e;
}
return true;
}
/**
* Update the last used at date for an identity record.
*/
public function touchIdentity(UserIdentity $identity): void
{
/** @var UserIdentityModel $identityModel */
$identityModel = model(UserIdentityModel::class);
$identityModel->touchIdentity($identity);
}
/**
* Accessor method to grab the user's email address.
* Will cache it in $this->email, since it has
* to hit the database the first time to get it, most likely.
*/
public function getEmail(): ?string
{
if ($this->email === null) {
$this->email = $this->getEmailIdentity()->secret ?? null;
}
return $this->email;
}
public function setEmail(string $email): void
{
$this->email = $email;
}
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(string $password): User
{
$this->password = $password;
return $this;
}
public function setPasswordHash(string $hash): User
{
$this->password_hash = $hash;
return $this;
}
/**
* Accessor method to grab the user's password hash.
* Will cache it in $this->attributes, since it has
* to hit the database the first time to get it, most likely.
*/
public function getPasswordHash(): ?string
{
if ($this->password_hash === null) {
$this->password_hash = $this->getEmailIdentity()->secret2 ?? null;
}
return $this->password_hash;
}
/**
* Returns the previous login information for this user
*/
public function previousLogin(): ?Login
{
/** @var LoginModel $logins */
$logins = model(LoginModel::class);
return $logins->previousLogin($this);
}
/**
* Returns the last login information for this user as
*/
public function lastLogin(): ?Login
{
/** @var LoginModel $logins */
$logins = model(LoginModel::class);
return $logins->lastLogin($this);
}
}