|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace A17\Twill\Models; |
|
4
|
|
|
|
|
5
|
|
|
use A17\Twill\Models\Behaviors\HasMedias; |
|
6
|
|
|
use A17\Twill\Models\Behaviors\HasPresenter; |
|
7
|
|
|
use A17\Twill\Models\Behaviors\HasOauth; |
|
8
|
|
|
use A17\Twill\Models\Enums\UserRole; |
|
9
|
|
|
use A17\Twill\Models\Behaviors\IsTranslatable; |
|
10
|
|
|
use A17\Twill\Notifications\Reset as ResetNotification; |
|
11
|
|
|
use A17\Twill\Notifications\Welcome as WelcomeNotification; |
|
12
|
|
|
use Illuminate\Auth\Authenticatable; |
|
13
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
|
14
|
|
|
use Illuminate\Foundation\Auth\Access\Authorizable; |
|
15
|
|
|
use Illuminate\Foundation\Auth\User as AuthenticatableContract; |
|
16
|
|
|
use Illuminate\Notifications\Notifiable; |
|
17
|
|
|
use Illuminate\Support\Facades\Session; |
|
18
|
|
|
use PragmaRX\Google2FAQRCode\Google2FA; |
|
19
|
|
|
|
|
20
|
|
|
class User extends AuthenticatableContract |
|
21
|
|
|
{ |
|
22
|
|
|
use Authenticatable, Authorizable, HasMedias, Notifiable, HasPresenter, HasOauth, SoftDeletes, IsTranslatable; |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
public $timestamps = true; |
|
25
|
|
|
|
|
26
|
|
|
protected $fillable = [ |
|
27
|
|
|
'email', |
|
28
|
|
|
'name', |
|
29
|
|
|
'role', |
|
30
|
|
|
'published', |
|
31
|
|
|
'title', |
|
32
|
|
|
'description', |
|
33
|
|
|
'google_2fa_enabled', |
|
34
|
|
|
'google_2fa_secret', |
|
35
|
|
|
'language' |
|
36
|
|
|
]; |
|
37
|
|
|
|
|
38
|
|
|
protected $dates = [ |
|
39
|
|
|
'deleted_at', |
|
40
|
|
|
]; |
|
41
|
|
|
|
|
42
|
|
|
protected $hidden = ['password', 'remember_token', 'google_2fa_secret']; |
|
43
|
|
|
public $checkboxes = ['published']; |
|
44
|
|
|
|
|
45
|
|
|
public $mediasParams = [ |
|
46
|
|
|
'profile' => [ |
|
47
|
|
|
'default' => [ |
|
48
|
|
|
[ |
|
49
|
|
|
'name' => 'default', |
|
50
|
|
|
'ratio' => 1, |
|
51
|
|
|
], |
|
52
|
|
|
], |
|
53
|
|
|
], |
|
54
|
|
|
]; |
|
55
|
|
|
|
|
56
|
|
|
protected $casts = ['published' => 'boolean']; |
|
57
|
|
|
|
|
58
|
69 |
|
public function __construct(array $attributes = []) |
|
59
|
|
|
{ |
|
60
|
69 |
|
$this->table = config('twill.users_table', 'twill_users'); |
|
61
|
|
|
|
|
62
|
69 |
|
parent::__construct($attributes); |
|
63
|
69 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function getTitleInBrowserAttribute() |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->name; |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function getRoleValueAttribute() |
|
71
|
|
|
{ |
|
72
|
|
|
if (!empty($this->role)) { |
|
|
|
|
|
|
73
|
|
|
if ($this->role == 'SUPERADMIN') { |
|
74
|
|
|
return "SUPERADMIN"; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return UserRole::{$this->role}()->getValue(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return null; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function getCanDeleteAttribute() |
|
84
|
|
|
{ |
|
85
|
|
|
return auth('twill_users')->user()->id !== $this->id; |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
public function scopePublished($query) |
|
89
|
|
|
{ |
|
90
|
1 |
|
return $query->wherePublished(true); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
1 |
|
public function scopeDraft($query) |
|
94
|
|
|
{ |
|
95
|
1 |
|
return $query->wherePublished(false); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function scopeOnlyTrashed($query) |
|
99
|
|
|
{ |
|
100
|
|
|
return $query->whereNotNull('deleted_at'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
2 |
|
public function setImpersonating($id) |
|
104
|
|
|
{ |
|
105
|
2 |
|
Session::put('impersonate', $id); |
|
106
|
2 |
|
} |
|
107
|
|
|
|
|
108
|
1 |
|
public function stopImpersonating() |
|
109
|
|
|
{ |
|
110
|
1 |
|
Session::forget('impersonate'); |
|
111
|
1 |
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function isImpersonating() |
|
114
|
|
|
{ |
|
115
|
|
|
return Session::has('impersonate'); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
2 |
|
public function notifyWithCustomMarkdownTheme($instance) |
|
119
|
|
|
{ |
|
120
|
2 |
|
$hostAppMailConfig = config('mail.markdown.paths'); |
|
121
|
|
|
|
|
122
|
2 |
|
config([ |
|
123
|
|
|
'mail.markdown.paths' => array_merge( |
|
124
|
2 |
|
[__DIR__ . '/../../views/emails'], |
|
125
|
|
|
$hostAppMailConfig |
|
126
|
|
|
), |
|
127
|
|
|
]); |
|
128
|
|
|
|
|
129
|
2 |
|
$this->notify($instance); |
|
130
|
|
|
|
|
131
|
2 |
|
config([ |
|
132
|
2 |
|
'mail.markdown.paths' => $hostAppMailConfig, |
|
133
|
|
|
]); |
|
134
|
|
|
|
|
135
|
2 |
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function sendWelcomeNotification($token) |
|
138
|
|
|
{ |
|
139
|
|
|
$this->notifyWithCustomMarkdownTheme(new WelcomeNotification($token)); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
2 |
|
public function sendPasswordResetNotification($token) |
|
143
|
|
|
{ |
|
144
|
2 |
|
$this->notifyWithCustomMarkdownTheme(new ResetNotification($token)); |
|
145
|
2 |
|
} |
|
146
|
|
|
|
|
147
|
49 |
|
public function isSuperAdmin() |
|
148
|
|
|
{ |
|
149
|
49 |
|
return $this->role === 'SUPERADMIN'; |
|
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
49 |
|
public function isPublished() |
|
153
|
|
|
{ |
|
154
|
49 |
|
return (bool) $this->published; |
|
|
|
|
|
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
2 |
|
public function setGoogle2faSecretAttribute($secret) |
|
158
|
|
|
{ |
|
159
|
2 |
|
$this->attributes['google_2fa_secret'] = filled($secret) ? \Crypt::encrypt($secret) : null; |
|
160
|
2 |
|
} |
|
161
|
|
|
|
|
162
|
49 |
|
public function getGoogle2faSecretAttribute($secret) |
|
163
|
|
|
{ |
|
164
|
49 |
|
return filled($secret) ? \Crypt::decrypt($secret) : null; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
2 |
|
public function generate2faSecretKey() |
|
168
|
|
|
{ |
|
169
|
2 |
|
if (is_null($this->google_2fa_secret)) { |
|
170
|
2 |
|
$secret = (new Google2FA())->generateSecretKey(); |
|
171
|
|
|
|
|
172
|
2 |
|
$this->google_2fa_secret = $secret; |
|
|
|
|
|
|
173
|
|
|
|
|
174
|
2 |
|
$this->save(); |
|
175
|
|
|
} |
|
176
|
2 |
|
} |
|
177
|
|
|
|
|
178
|
1 |
|
public function get2faQrCode() |
|
179
|
|
|
{ |
|
180
|
1 |
|
return (new Google2FA())->getQRCodeInline( |
|
181
|
1 |
|
config('app.name'), |
|
182
|
1 |
|
$this->email, |
|
|
|
|
|
|
183
|
1 |
|
$this->google_2fa_secret, |
|
184
|
1 |
|
200 |
|
185
|
|
|
); |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|