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