-
-
Notifications
You must be signed in to change notification settings - Fork 205
/
Copy pathUser.php
297 lines (261 loc) · 7.2 KB
/
User.php
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
<?php
namespace Myth\Auth\Entities;
use CodeIgniter\Entity\Entity;
use CodeIgniter\I18n\Time;
use Exception;
use Myth\Auth\Authorization\GroupModel;
use Myth\Auth\Authorization\PermissionModel;
use Myth\Auth\Password;
use RuntimeException;
/**
* @property array<int, string> $permissions
* @property Time|null $reset_expires
*/
class User extends Entity
{
/**
* Maps names used in sets and gets against unique
* names within the class, allowing independence from
* database column names.
*
* Example:
* $datamap = [
* 'db_name' => 'class_name'
* ];
*/
protected $datamap = [];
/**
* Define properties that are automatically converted to Time instances.
*/
protected $dates = ['reset_at', 'reset_expires', 'created_at', 'updated_at', 'deleted_at'];
/**
* Array of field names and the type of value to cast them as
* when they are accessed.
*/
protected $casts = [
'username' => 'string',
'email' => 'string',
'active' => 'boolean',
'force_pass_reset' => 'boolean',
];
/**
* Per-user permissions cache
*
* @var array<int, string>
*/
protected $permissions = [];
/**
* Per-user roles cache
*
* @var array
*/
protected $roles = [];
/**
* Automatically hashes the password when set.
*
* @see https://paragonie.com/blog/2015/04/secure-authentication-php-with-long-term-persistence
*/
public function setPassword(string $password)
{
$this->attributes['password_hash'] = Password::hash($password);
/*
Set these vars to null in case a reset password was asked.
Scenario:
user (a *dumb* one with short memory) requests a
reset-token and then does nothing => asks the
administrator to reset his password.
User would have a new password but still anyone with the
reset-token would be able to change the password.
*/
$this->attributes['reset_hash'] = null;
$this->attributes['reset_at'] = null;
$this->attributes['reset_expires'] = null;
}
/**
* Explicitly convert false and true to 0 and 1
*
* Some BDD (PostgreSQL for example) can be picky about data types and
* if 'active' or 'force_pass_reset' are set to (bool)true/false, the method
* CodeIgniter\Database\Postgre\Connection::escape() will translate it
* to a literal TRUE/FALSE. Since the database fields are defined as integer,
* the BDD will throw an error about mismatched type.
*
* @param bool|int $active
*/
public function setActive($active)
{
$this->attributes['active'] = $active ? 1 : 0;
}
/**
* Explicitly convert false and true to 0 and 1
*
* @see setActive() Explanation about strict typing at database level
*
* @param bool|int $force_pass_reset
*/
public function setForcePassReset($force_pass_reset)
{
$this->attributes['force_pass_reset'] = $force_pass_reset ? 1 : 0;
}
/**
* Force a user to reset their password on next page refresh
* or login. Checked in the LocalAuthenticator's check() method.
*
* @return $this
*
* @throws Exception
*/
public function forcePasswordReset()
{
$this->generateResetHash();
$this->attributes['force_pass_reset'] = 1;
return $this;
}
/**
* Generates a secure hash to use for password reset purposes,
* saves it to the instance.
*
* @return $this
*
* @throws Exception
*/
public function generateResetHash()
{
$this->attributes['reset_hash'] = bin2hex(random_bytes(16));
$this->attributes['reset_expires'] = date('Y-m-d H:i:s', time() + config('Auth')->resetTime);
return $this;
}
/**
* Generates a secure random hash to use for account activation.
*
* @return $this
*
* @throws Exception
*/
public function generateActivateHash()
{
$this->attributes['activate_hash'] = bin2hex(random_bytes(16));
return $this;
}
/**
* Activate user.
*
* @return $this
*/
public function activate()
{
$this->attributes['active'] = 1;
$this->attributes['activate_hash'] = null;
return $this;
}
/**
* Unactivate user.
*
* @return $this
*/
public function deactivate()
{
$this->attributes['active'] = 0;
return $this;
}
/**
* Checks to see if a user is active.
*/
public function isActivated(): bool
{
return $this->active;
}
/**
* Bans a user.
*
* @return $this
*/
public function ban(string $reason)
{
$this->attributes['status'] = 'banned';
$this->attributes['status_message'] = $reason;
return $this;
}
/**
* Removes a ban from a user.
*
* @return $this
*/
public function unBan()
{
$this->attributes['status'] = $this->status_message = '';
return $this;
}
/**
* Checks to see if a user has been banned.
*/
public function isBanned(): bool
{
return isset($this->attributes['status']) && $this->attributes['status'] === 'banned';
}
/**
* Determines whether the user has the appropriate permission,
* either directly, or through one of it's groups.
*
* @return bool
*/
public function can(string $permission)
{
return in_array(strtolower($permission), $this->getPermissions(), true);
}
/**
* Returns the user's permissions, formatted for simple checking:
*
* [
* id => name,
* id=> name,
* ]
*
* @return array<int, string>
*/
public function getPermissions()
{
if (empty($this->id)) {
throw new RuntimeException('Users must be created before getting permissions.');
}
if (empty($this->permissions)) {
$this->permissions = model(PermissionModel::class)->getPermissionsForUser($this->id);
}
return $this->permissions;
}
/**
* Returns the user's roles, formatted for simple checking:
*
* [
* id => name,
* id => name,
* ]
*
* @return array|mixed
*/
public function getRoles()
{
if (empty($this->id)) {
throw new RuntimeException('Users must be created before getting roles.');
}
if (empty($this->roles)) {
$groups = model(GroupModel::class)->getGroupsForUser($this->id);
foreach ($groups as $group) {
$this->roles[$group['group_id']] = strtolower($group['name']);
}
}
return $this->roles;
}
/**
* Warns the developer it won't work, so they don't spend
* hours tracking stuff down.
*
* @param array $permissions
*
* @return $this
*/
public function setPermissions(?array $permissions = null)
{
throw new RuntimeException('User entity does not support saving permissions directly.');
}
}