1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Gate; |
6
|
|
|
use Illuminate\Notifications\Notifiable; |
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
8
|
|
|
use Illuminate\Foundation\Auth\User as Authenticatable; |
9
|
|
|
use App\Notifications\ResetPassword as ResetPasswordNotification; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @property int $id |
13
|
|
|
* @property string $first_name |
14
|
|
|
* @property string $last_name |
15
|
|
|
* @property string $email |
16
|
|
|
* @property \Carbon\Carbon|null $email_verified_at |
17
|
|
|
* @property string|null $password |
18
|
|
|
* @property bool $active |
19
|
|
|
* @property string|null $remember_token |
20
|
|
|
* @property string $locale |
21
|
|
|
* @property string $timezone |
22
|
|
|
* @property string $slug |
23
|
|
|
* @property \Carbon\Carbon|null $last_access_at |
24
|
|
|
* @property \Carbon\Carbon|null $created_at |
25
|
|
|
* @property \Carbon\Carbon|null $updated_at |
26
|
|
|
* @property mixed $avatar |
27
|
|
|
* @property mixed $can_delete |
28
|
|
|
* @property mixed $can_edit |
29
|
|
|
* @property mixed $is_super_admin |
30
|
|
|
* @property \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications |
31
|
|
|
* @property \Illuminate\Database\Eloquent\Collection|\App\Models\SocialLogin[] $providers |
32
|
|
|
* |
33
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User actives() |
34
|
|
|
* @mixin \Eloquent |
35
|
|
|
*/ |
36
|
|
|
class User extends Authenticatable |
37
|
|
|
{ |
38
|
|
|
use Notifiable; |
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The attributes that should be mutated to dates. |
42
|
|
|
* |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $dates = [ |
46
|
|
|
'last_access_at', |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The attributes that are mass assignable. |
51
|
|
|
* |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
protected $fillable = [ |
55
|
|
|
'first_name', |
56
|
|
|
'last_name', |
57
|
|
|
'email', |
58
|
|
|
'active', |
59
|
|
|
'locale', |
60
|
|
|
'timezone', |
61
|
|
|
]; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* The attributes that should be hidden for arrays. |
65
|
|
|
* |
66
|
|
|
* @var array |
67
|
|
|
*/ |
68
|
|
|
protected $hidden = [ |
69
|
|
|
'password', |
70
|
|
|
'remember_token', |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* The attributes that should be cast to native types. |
75
|
|
|
* |
76
|
|
|
* @var array |
77
|
|
|
*/ |
78
|
|
|
protected $casts = [ |
79
|
|
|
'active' => 'boolean', |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* The accessors to append to the model's array form. |
84
|
|
|
* |
85
|
|
|
* @var array |
86
|
|
|
*/ |
87
|
|
|
protected $appends = [ |
88
|
|
|
'avatar', |
89
|
|
|
'can_edit', |
90
|
|
|
'can_delete', |
91
|
|
|
]; |
92
|
|
|
|
93
|
|
|
public static function boot() |
94
|
|
|
{ |
95
|
|
|
parent::boot(); |
96
|
|
|
|
97
|
|
|
static::saving(function (self $model) { |
98
|
|
|
$model->slug = str_slug($model->name); |
99
|
|
|
}); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function getCanEditAttribute() |
103
|
|
|
{ |
104
|
|
|
return ! $this->is_super_admin || 1 === auth()->id(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function getCanDeleteAttribute() |
108
|
|
|
{ |
109
|
|
|
return ! $this->is_super_admin && $this->id !== auth()->id() && ( |
110
|
|
|
Gate::check('delete users') |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function getCanImpersonateAttribute() |
115
|
|
|
{ |
116
|
|
|
if (Gate::check('impersonate users')) { |
117
|
|
|
return ! $this->is_super_admin |
118
|
|
|
&& session()->get('admin_user_id') !== $this->id |
119
|
|
|
&& $this->id !== auth()->id(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return false; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function scopeActives(Builder $query) |
126
|
|
|
{ |
127
|
|
|
return $query->where('active', '=', true); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function getIsSuperAdminAttribute() |
131
|
|
|
{ |
132
|
|
|
return 1 === $this->id; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Send the password reset notification. |
137
|
|
|
* |
138
|
|
|
* @param string $token |
139
|
|
|
*/ |
140
|
|
|
public function sendPasswordResetNotification($token) |
141
|
|
|
{ |
142
|
|
|
$this->notify(new ResetPasswordNotification($token)); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param $provider |
147
|
|
|
* |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
public function getProvider($provider) |
151
|
|
|
{ |
152
|
|
|
return $this->providers->first(function (SocialLogin $item) use ($provider) { |
153
|
|
|
return $item->provider === $provider; |
154
|
|
|
}); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return mixed |
159
|
|
|
*/ |
160
|
|
|
public function providers() |
161
|
|
|
{ |
162
|
|
|
return $this->hasMany(SocialLogin::class); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Get user avatar from gravatar. |
167
|
|
|
*/ |
168
|
|
|
public function getAvatarAttribute() |
169
|
|
|
{ |
170
|
|
|
$hash = md5($this->email); |
171
|
|
|
|
172
|
|
|
return "https://secure.gravatar.com/avatar/{$hash}?size=100&d=mm&r=g"; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function posts() |
176
|
|
|
{ |
177
|
|
|
return $this->hasMany(Post::class); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @return string |
182
|
|
|
*/ |
183
|
|
|
public function __toString() |
184
|
|
|
{ |
185
|
|
|
return $this->name; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** @deprecated Created for backward compatibility with old code that we are going to remove */ |
189
|
|
|
public function isAdmin(): bool |
190
|
|
|
{ |
191
|
|
|
return true; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function getNameAttribute(): string |
195
|
|
|
{ |
196
|
|
|
return "$this->first_name $this->last_name"; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|