1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Rinvex\Fort\Models; |
6
|
|
|
|
7
|
|
|
use Rinvex\Fort\Traits\HasRoles; |
8
|
|
|
use Illuminate\Auth\Authenticatable; |
9
|
|
|
use Illuminate\Support\Facades\Hash; |
10
|
|
|
use Rinvex\Fort\Traits\CanVerifyEmail; |
11
|
|
|
use Rinvex\Fort\Traits\CanVerifyPhone; |
12
|
|
|
use Illuminate\Database\Eloquent\Model; |
13
|
|
|
use Rinvex\Cacheable\CacheableEloquent; |
14
|
|
|
use Rinvex\Fort\Contracts\UserContract; |
15
|
|
|
use Rinvex\Support\Traits\HasHashables; |
16
|
|
|
use Illuminate\Notifications\Notifiable; |
17
|
|
|
use Rinvex\Fort\Traits\CanResetPassword; |
18
|
|
|
use Rinvex\Support\Traits\ValidatingTrait; |
19
|
|
|
use Rinvex\Fort\Traits\AuthenticatableTwoFactor; |
20
|
|
|
use Rinvex\Fort\Contracts\CanVerifyEmailContract; |
21
|
|
|
use Rinvex\Fort\Contracts\CanVerifyPhoneContract; |
22
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
23
|
|
|
use Illuminate\Foundation\Auth\Access\Authorizable; |
24
|
|
|
use Rinvex\Fort\Contracts\CanResetPasswordContract; |
25
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
26
|
|
|
use Rinvex\Fort\Contracts\AuthenticatableTwoFactorContract; |
27
|
|
|
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; |
28
|
|
|
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Rinvex\Fort\Models\User. |
32
|
|
|
* |
33
|
|
|
* @property int $id |
|
|
|
|
34
|
|
|
* @property string $username |
|
|
|
|
35
|
|
|
* @property string $password |
|
|
|
|
36
|
|
|
* @property string|null $remember_token |
|
|
|
|
37
|
|
|
* @property string $email |
|
|
|
|
38
|
|
|
* @property bool $email_verified |
|
|
|
|
39
|
|
|
* @property \Carbon\Carbon $email_verified_at |
|
|
|
|
40
|
|
|
* @property string $phone |
|
|
|
|
41
|
|
|
* @property bool $phone_verified |
|
|
|
|
42
|
|
|
* @property \Carbon\Carbon $phone_verified_at |
|
|
|
|
43
|
|
|
* @property string $name_prefix |
|
|
|
|
44
|
|
|
* @property string $first_name |
|
|
|
|
45
|
|
|
* @property string $middle_name |
|
|
|
|
46
|
|
|
* @property string $last_name |
|
|
|
|
47
|
|
|
* @property string $name_suffix |
|
|
|
|
48
|
|
|
* @property string $job_title |
|
|
|
|
49
|
|
|
* @property string $country_code |
|
|
|
|
50
|
|
|
* @property string $language_code |
|
|
|
|
51
|
|
|
* @property array $two_factor |
|
|
|
|
52
|
|
|
* @property string $birthday |
|
|
|
|
53
|
|
|
* @property string $gender |
|
|
|
|
54
|
|
|
* @property bool $is_active |
|
|
|
|
55
|
|
|
* @property \Carbon\Carbon $last_activity |
|
|
|
|
56
|
|
|
* @property \Carbon\Carbon|null $created_at |
|
|
|
|
57
|
|
|
* @property \Carbon\Carbon|null $updated_at |
|
|
|
|
58
|
|
|
* @property \Carbon\Carbon|null $deleted_at |
|
|
|
|
59
|
|
|
* @property \Illuminate\Database\Eloquent\Collection|\Rinvex\Fort\Models\Ability[] $abilities |
|
|
|
|
60
|
|
|
* @property-read \Illuminate\Support\Collection $allAbilities |
|
|
|
|
61
|
|
|
* @property-read \Rinvex\Country\Country $country |
|
|
|
|
62
|
|
|
* @property-read \Rinvex\Language\Language $language |
|
|
|
|
63
|
|
|
* @property-read string $name |
|
|
|
|
64
|
|
|
* @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications |
|
|
|
|
65
|
|
|
* @property \Illuminate\Database\Eloquent\Collection|\Rinvex\Fort\Models\Role[] $roles |
|
|
|
|
66
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\Rinvex\Fort\Models\Session[] $sessions |
|
|
|
|
67
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\Rinvex\Fort\Models\Socialite[] $socialites |
|
|
|
|
68
|
|
|
* |
69
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User role($roles) |
70
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereBirthday($value) |
71
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereCountryCode($value) |
72
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereCreatedAt($value) |
73
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereDeletedAt($value) |
74
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereEmail($value) |
75
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereEmailVerified($value) |
76
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereEmailVerifiedAt($value) |
77
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereFirstName($value) |
78
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereGender($value) |
79
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereId($value) |
80
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereIsActive($value) |
81
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereJobTitle($value) |
82
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereLanguageCode($value) |
83
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereLastActivity($value) |
84
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereLastName($value) |
85
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereMiddleName($value) |
86
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereNamePrefix($value) |
87
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereNameSuffix($value) |
88
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User wherePassword($value) |
89
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User wherePhone($value) |
90
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User wherePhoneVerified($value) |
91
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User wherePhoneVerifiedAt($value) |
92
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereRememberToken($value) |
93
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereTwoFactor($value) |
94
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereUpdatedAt($value) |
95
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereUsername($value) |
96
|
|
|
* @mixin \Eloquent |
97
|
|
|
*/ |
98
|
|
|
class User extends Model implements UserContract, AuthenticatableContract, AuthenticatableTwoFactorContract, AuthorizableContract, CanResetPasswordContract, CanVerifyEmailContract, CanVerifyPhoneContract |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
use HasRoles; |
101
|
|
|
use Notifiable; |
102
|
|
|
use Authorizable; |
103
|
|
|
use HasHashables; |
104
|
|
|
use CanVerifyEmail; |
105
|
|
|
use CanVerifyPhone; |
106
|
|
|
use Authenticatable; |
107
|
|
|
use ValidatingTrait; |
108
|
|
|
use CanResetPassword; |
109
|
|
|
use CacheableEloquent; |
110
|
|
|
use AuthenticatableTwoFactor; |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
|
|
protected $fillable = [ |
116
|
|
|
'username', |
117
|
|
|
'password', |
118
|
|
|
'two_factor', |
119
|
|
|
'email', |
120
|
|
|
'email_verified', |
121
|
|
|
'email_verified_at', |
122
|
|
|
'phone', |
123
|
|
|
'phone_verified', |
124
|
|
|
'phone_verified_at', |
125
|
|
|
'name_prefix', |
126
|
|
|
'first_name', |
127
|
|
|
'middle_name', |
128
|
|
|
'last_name', |
129
|
|
|
'name_suffix', |
130
|
|
|
'job_title', |
131
|
|
|
'country_code', |
132
|
|
|
'language_code', |
133
|
|
|
'birthday', |
134
|
|
|
'gender', |
135
|
|
|
'is_active', |
136
|
|
|
'last_activity', |
137
|
|
|
'abilities', |
138
|
|
|
'roles', |
139
|
|
|
]; |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
*/ |
144
|
|
|
protected $casts = [ |
145
|
|
|
'username' => 'string', |
146
|
|
|
'password' => 'string', |
147
|
|
|
'two_factor' => 'json', |
148
|
|
|
'email' => 'string', |
149
|
|
|
'email_verified' => 'boolean', |
150
|
|
|
'email_verified_at' => 'datetime', |
151
|
|
|
'phone' => 'string', |
152
|
|
|
'phone_verified' => 'boolean', |
153
|
|
|
'phone_verified_at' => 'datetime', |
154
|
|
|
'name_prefix' => 'string', |
155
|
|
|
'first_name' => 'string', |
156
|
|
|
'middle_name' => 'string', |
157
|
|
|
'last_name' => 'string', |
158
|
|
|
'name_suffix' => 'string', |
159
|
|
|
'job_title' => 'string', |
160
|
|
|
'country_code' => 'string', |
161
|
|
|
'language_code' => 'string', |
162
|
|
|
'birthday' => 'string', |
163
|
|
|
'gender' => 'string', |
164
|
|
|
'is_active' => 'boolean', |
165
|
|
|
'last_activity' => 'datetime', |
166
|
|
|
'deleted_at' => 'datetime', |
167
|
|
|
]; |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* {@inheritdoc} |
171
|
|
|
*/ |
172
|
|
|
protected $hidden = [ |
173
|
|
|
'password', |
174
|
|
|
'two_factor', |
175
|
|
|
'remember_token', |
176
|
|
|
]; |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* {@inheritdoc} |
180
|
|
|
*/ |
181
|
|
|
protected $with = [ |
182
|
|
|
'abilities', |
183
|
|
|
'roles', |
184
|
|
|
]; |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* {@inheritdoc} |
188
|
|
|
*/ |
189
|
|
|
protected $observables = [ |
190
|
|
|
'validating', |
191
|
|
|
'validated', |
192
|
|
|
]; |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* The attributes to be encrypted before saving. |
196
|
|
|
* |
197
|
|
|
* @var array |
198
|
|
|
*/ |
199
|
|
|
protected $hashables = [ |
200
|
|
|
'password', |
201
|
|
|
]; |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* The default rules that the model will validate against. |
205
|
|
|
* |
206
|
|
|
* @var array |
207
|
|
|
*/ |
208
|
|
|
protected $rules = []; |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Whether the model should throw a |
212
|
|
|
* ValidationException if it fails validation. |
213
|
|
|
* |
214
|
|
|
* @var bool |
215
|
|
|
*/ |
216
|
|
|
protected $throwValidationExceptions = true; |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Create a new Eloquent model instance. |
220
|
|
|
* |
221
|
|
|
* @param array $attributes |
222
|
|
|
*/ |
223
|
|
|
public function __construct(array $attributes = []) |
224
|
|
|
{ |
225
|
|
|
parent::__construct($attributes); |
226
|
|
|
|
227
|
|
|
$this->setTable(config('rinvex.fort.tables.users')); |
228
|
|
|
$this->setRules([ |
229
|
|
|
'username' => 'required|alpha_dash|min:3|max:150|unique:'.config('rinvex.fort.tables.users').',username', |
230
|
|
|
'password' => 'sometimes|required|min:'.config('rinvex.fort.password_min_chars'), |
231
|
|
|
'two_factor' => 'nullable|array', |
232
|
|
|
'email' => 'required|email|min:3|max:150|unique:'.config('rinvex.fort.tables.users').',email', |
233
|
|
|
'email_verified' => 'sometimes|boolean', |
234
|
|
|
'email_verified_at' => 'nullable|date', |
235
|
|
|
'phone' => 'nullable|numeric|min:4', |
236
|
|
|
'phone_verified' => 'sometimes|boolean', |
237
|
|
|
'phone_verified_at' => 'nullable|date', |
238
|
|
|
'name_prefix' => 'nullable|string|max:150', |
239
|
|
|
'first_name' => 'nullable|string|max:150', |
240
|
|
|
'middle_name' => 'nullable|string|max:150', |
241
|
|
|
'last_name' => 'nullable|string|max:150', |
242
|
|
|
'name_suffix' => 'nullable|string|max:150', |
243
|
|
|
'job_title' => 'nullable|string|max:150', |
244
|
|
|
'country_code' => 'nullable|alpha|size:2|country', |
245
|
|
|
'language_code' => 'nullable|alpha|size:2|language', |
246
|
|
|
'birthday' => 'nullable|date_format:Y-m-d', |
247
|
|
|
'gender' => 'nullable|string|in:m,f', |
248
|
|
|
'is_active' => 'sometimes|boolean', |
249
|
|
|
'last_activity' => 'nullable|date', |
250
|
|
|
]); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* {@inheritdoc} |
255
|
|
|
*/ |
256
|
|
|
protected static function boot() |
257
|
|
|
{ |
258
|
|
|
parent::boot(); |
259
|
|
|
|
260
|
|
|
static::saving(function (self $user) { |
261
|
|
|
foreach (array_intersect($user->getHashables(), array_keys($user->getAttributes())) as $hashable) { |
262
|
|
|
if ($user->isDirty($hashable) && Hash::needsRehash($user->$hashable)) { |
263
|
|
|
$user->$hashable = Hash::make($user->$hashable); |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
}); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* A user may have multiple direct abilities. |
271
|
|
|
* |
272
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
273
|
|
|
*/ |
274
|
|
|
public function abilities() |
275
|
|
|
{ |
276
|
|
|
return $this->belongsToMany(config('rinvex.fort.models.ability'), config('rinvex.fort.tables.ability_user'), 'user_id', 'ability_id') |
|
|
|
|
277
|
|
|
->withTimestamps(); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Get all attached roles to the model. |
282
|
|
|
* |
283
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
284
|
|
|
*/ |
285
|
|
|
public function roles(): BelongsToMany |
286
|
|
|
{ |
287
|
|
|
return $this->belongsToMany(config('rinvex.fort.models.role'), config('rinvex.fort.tables.role_user'), 'user_id', 'role_id') |
|
|
|
|
288
|
|
|
->withTimestamps(); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* A user may have many sessions. |
293
|
|
|
* |
294
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
295
|
|
|
*/ |
296
|
|
|
public function sessions(): HasMany |
297
|
|
|
{ |
298
|
|
|
return $this->hasMany(config('rinvex.fort.models.session'), 'user_id', 'id'); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* A user may have multiple socialites. |
303
|
|
|
* |
304
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
305
|
|
|
*/ |
306
|
|
|
public function socialites(): HasMany |
307
|
|
|
{ |
308
|
|
|
return $this->hasMany(config('rinvex.fort.models.socialite'), 'user_id', 'id'); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Get name attribute. |
313
|
|
|
* |
314
|
|
|
* @return string |
315
|
|
|
*/ |
316
|
|
|
public function getNameAttribute() |
317
|
|
|
{ |
318
|
|
|
$name = trim(implode(' ', [$this->name_prefix, $this->first_name, $this->middle_name, $this->last_name, $this->name_suffix])); |
|
|
|
|
319
|
|
|
|
320
|
|
|
return $name ?: $this->username; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Get all abilities of the user. |
325
|
|
|
* |
326
|
|
|
* @return \Illuminate\Support\Collection |
327
|
|
|
*/ |
328
|
|
|
public function getAllAbilitiesAttribute() |
329
|
|
|
{ |
330
|
|
|
return $this->abilities->merge($this->roles->pluck('abilities')->collapse()); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Determine if the user is super admin. |
335
|
|
|
* |
336
|
|
|
* @return bool |
337
|
|
|
*/ |
338
|
|
|
public function isSuperadmin(): bool |
339
|
|
|
{ |
340
|
|
|
return $this->allAbilities->where('action', 'superadmin')->where('resource', 'global')->where('policy', null)->isNotEmpty(); |
|
|
|
|
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Determine if the user is protected. |
345
|
|
|
* |
346
|
|
|
* @return bool |
347
|
|
|
*/ |
348
|
|
|
public function isProtected(): bool |
349
|
|
|
{ |
350
|
|
|
return in_array($this->getKey(), config('rinvex.fort.protected.users')); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Route notifications for the authy channel. |
355
|
|
|
* |
356
|
|
|
* @return int |
357
|
|
|
*/ |
358
|
|
|
public function routeNotificationForAuthy() |
359
|
|
|
{ |
360
|
|
|
if (! ($authyId = array_get($this->getTwoFactor(), 'phone.authy_id')) && $this->getEmailForVerification() && $this->getPhoneForVerification() && $this->getCountryForVerification()) { |
|
|
|
|
361
|
|
|
$result = app('rinvex.authy.user')->register($this->getEmailForVerification(), preg_replace('/[^0-9]/', '', $this->getPhoneForVerification()), $this->getCountryForVerification()); |
|
|
|
|
362
|
|
|
$authyId = $result->get('user')['id']; |
363
|
|
|
|
364
|
|
|
// Prepare required variables |
365
|
|
|
$twoFactor = $this->getTwoFactor(); |
366
|
|
|
|
367
|
|
|
// Update user account |
368
|
|
|
array_set($twoFactor, 'phone.authy_id', $authyId); |
369
|
|
|
|
370
|
|
|
$this->fill(['two_factor' => $twoFactor])->forceSave(); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
return $authyId; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* Get the user's country. |
378
|
|
|
* |
379
|
|
|
* @return \Rinvex\Country\Country |
380
|
|
|
*/ |
381
|
|
|
public function getCountryAttribute() |
382
|
|
|
{ |
383
|
|
|
return country($this->country_code); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Get the user's language. |
388
|
|
|
* |
389
|
|
|
* @return \Rinvex\Language\Language |
390
|
|
|
*/ |
391
|
|
|
public function getLanguageAttribute() |
392
|
|
|
{ |
393
|
|
|
return language($this->language_code); |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Activate the user. |
398
|
|
|
* |
399
|
|
|
* @return $this |
400
|
|
|
*/ |
401
|
|
|
public function activate() |
402
|
|
|
{ |
403
|
|
|
$this->update(['is_active' => true]); |
404
|
|
|
|
405
|
|
|
return $this; |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* Deactivate the user. |
410
|
|
|
* |
411
|
|
|
* @return $this |
412
|
|
|
*/ |
413
|
|
|
public function deactivate() |
414
|
|
|
{ |
415
|
|
|
$this->update(['is_active' => false]); |
416
|
|
|
|
417
|
|
|
return $this; |
418
|
|
|
} |
419
|
|
|
} |
420
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.