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