1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* NOTICE OF LICENSE |
5
|
|
|
* |
6
|
|
|
* Part of the Rinvex Fort Package. |
7
|
|
|
* |
8
|
|
|
* This source file is subject to The MIT License (MIT) |
9
|
|
|
* that is bundled with this package in the LICENSE file. |
10
|
|
|
* |
11
|
|
|
* Package: Rinvex Fort Package |
12
|
|
|
* License: The MIT License (MIT) |
13
|
|
|
* Link: https://rinvex.com |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Rinvex\Fort\Models; |
17
|
|
|
|
18
|
|
|
use Rinvex\Fort\Traits\CanVerifyEmail; |
19
|
|
|
use Rinvex\Fort\Traits\Authenticatable; |
20
|
|
|
use Illuminate\Database\Eloquent\Model; |
21
|
|
|
use Illuminate\Notifications\Notifiable; |
22
|
|
|
use Rinvex\Fort\Traits\CanResetPassword; |
23
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
24
|
|
|
use Rinvex\Fort\Contracts\CanVerifyEmailContract; |
25
|
|
|
use Rinvex\Fort\Contracts\AuthenticatableContract; |
26
|
|
|
use Illuminate\Foundation\Auth\Access\Authorizable; |
27
|
|
|
use Rinvex\Fort\Contracts\CanResetPasswordContract; |
28
|
|
|
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; |
29
|
|
|
|
30
|
|
|
class User extends Model implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract, CanVerifyEmailContract |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
use Notifiable, Authenticatable, Authorizable, CanResetPassword, CanVerifyEmail, SoftDeletes; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
protected $dates = [ |
38
|
|
|
'email_verified_at', |
39
|
|
|
'deleted_at', |
40
|
|
|
'birthdate', |
41
|
|
|
'login_at', |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
protected $fillable = [ |
48
|
|
|
'username', |
49
|
|
|
'password', |
50
|
|
|
'two_factor', |
51
|
|
|
'email', |
52
|
|
|
'email_verified', |
53
|
|
|
'email_verified_at', |
54
|
|
|
'phone', |
55
|
|
|
'phone_verified', |
56
|
|
|
'phone_verified_at', |
57
|
|
|
'prefix', |
58
|
|
|
'first_name', |
59
|
|
|
'middle_name', |
60
|
|
|
'last_name', |
61
|
|
|
'sufix', |
62
|
|
|
'job_title', |
63
|
|
|
'country', |
64
|
|
|
'birthdate', |
65
|
|
|
'gender', |
66
|
|
|
'active', |
67
|
|
|
'login_at', |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
protected $hidden = [ |
74
|
|
|
'password', |
75
|
|
|
'two_factor', |
76
|
|
|
'remember_token', |
77
|
|
|
]; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
|
|
protected $with = ['abilities', 'roles']; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Create a new Eloquent model instance. |
86
|
|
|
* |
87
|
|
|
* @param array $attributes |
88
|
|
|
* |
89
|
|
|
* @return void |
|
|
|
|
90
|
|
|
*/ |
91
|
|
|
public function __construct(array $attributes = []) |
92
|
|
|
{ |
93
|
|
|
parent::__construct($attributes); |
94
|
|
|
|
95
|
|
|
$this->setTable(config('rinvex.fort.tables.users')); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* A user may have multiple direct abilities. |
100
|
|
|
* |
101
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
102
|
|
|
*/ |
103
|
|
|
public function abilities() |
104
|
|
|
{ |
105
|
|
|
return $this->belongsToMany(config('rinvex.fort.models.ability'), config('rinvex.fort.tables.ability_user')) |
106
|
|
|
->withTimestamps(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* A user may have multiple roles. |
111
|
|
|
* |
112
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
113
|
|
|
*/ |
114
|
|
|
public function roles() |
115
|
|
|
{ |
116
|
|
|
return $this->belongsToMany(config('rinvex.fort.models.role'), config('rinvex.fort.tables.role_user')) |
117
|
|
|
->withTimestamps(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* A user may have multiple persistences. |
122
|
|
|
* |
123
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
124
|
|
|
*/ |
125
|
|
|
public function persistences() |
126
|
|
|
{ |
127
|
|
|
return $this->hasMany(config('rinvex.fort.models.persistence')); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* A user may have multiple socialites. |
132
|
|
|
* |
133
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
134
|
|
|
*/ |
135
|
|
|
public function socialites() |
136
|
|
|
{ |
137
|
|
|
return $this->hasMany(config('rinvex.fort.models.socialite')); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get name attribute. |
142
|
|
|
* |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
|
|
public function getNameAttribute() |
146
|
|
|
{ |
147
|
|
|
$segments = [$this->prefix, $this->first_name, $this->middle_name, $this->last_name, $this->suffix]; |
|
|
|
|
148
|
|
|
|
149
|
|
|
return trim(implode(' ', $segments)); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get all abilities of the user. |
154
|
|
|
* |
155
|
|
|
* @return \Illuminate\Support\Collection |
156
|
|
|
*/ |
157
|
|
|
public function getAllAbilitiesAttribute() |
158
|
|
|
{ |
159
|
|
|
return $this->abilities->merge($this->roles->pluck('abilities')->collapse()); |
|
|
|
|
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Determine if the user is super admin. |
164
|
|
|
* |
165
|
|
|
* @return bool |
166
|
|
|
*/ |
167
|
|
|
public function isSuperadmin() |
168
|
|
|
{ |
169
|
|
|
return $this->getAllAbilitiesAttribute() |
170
|
|
|
->where('resource', 'global') |
171
|
|
|
->where('policy', null) |
172
|
|
|
->contains('action', 'superadmin'); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Determine if the user is protected. |
177
|
|
|
* |
178
|
|
|
* @return bool |
179
|
|
|
*/ |
180
|
|
|
public function isProtected() |
181
|
|
|
{ |
182
|
|
|
return in_array($this->id, config('rinvex.fort.protected.users')); |
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Set the user's password. |
187
|
|
|
* |
188
|
|
|
* @param string $value |
189
|
|
|
* |
190
|
|
|
* @return void |
191
|
|
|
*/ |
192
|
|
|
public function setPasswordAttribute($value) |
193
|
|
|
{ |
194
|
|
|
$this->attributes['password'] = bcrypt($value); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
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.