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\Http\Controllers; |
17
|
|
|
|
18
|
|
|
use Carbon\Carbon; |
19
|
|
|
use Rinvex\Country\Models\Country; |
20
|
|
|
use Illuminate\Support\MessageBag; |
21
|
|
|
use Illuminate\Support\Facades\Auth; |
22
|
|
|
use Illuminate\Support\ViewErrorBag; |
23
|
|
|
use Illuminate\Support\Facades\Lang; |
24
|
|
|
use Rinvex\Fort\Http\Requests\AccountUpdate; |
25
|
|
|
use Rinvex\Fort\Http\Requests\TwoFactorTotp; |
26
|
|
|
use Rinvex\Fort\Http\Requests\TwoFactorPhone; |
27
|
|
|
use Rinvex\Fort\Services\TwoFactorTotpProvider; |
28
|
|
|
use Rinvex\Fort\Contracts\UserRepositoryContract; |
29
|
|
|
|
30
|
|
|
class AccountController extends FoundationController |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Whitelisted methods. |
34
|
|
|
* |
35
|
|
|
* Array of whitelisted methods which do not need to be authorized. |
36
|
|
|
* |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
protected $authWhitelist = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The users repository instance. |
43
|
|
|
* |
44
|
|
|
* @var \Rinvex\Fort\Contracts\UserRepositoryContract |
45
|
|
|
*/ |
46
|
|
|
protected $users; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Create a new account controller instance. |
50
|
|
|
* |
51
|
|
|
* @param \Rinvex\Fort\Contracts\UserRepositoryContract $users |
52
|
|
|
* |
53
|
|
|
* @return void |
|
|
|
|
54
|
|
|
*/ |
55
|
|
|
public function __construct(UserRepositoryContract $users) |
56
|
|
|
{ |
57
|
|
|
$this->users = $users; |
58
|
|
|
|
59
|
|
|
$this->middleware($this->getAuthMiddleware(), ['except' => $this->authWhitelist]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Show the account update form. |
64
|
|
|
* |
65
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
66
|
|
|
*/ |
67
|
|
|
public function showAccountUpdate(Country $country) |
68
|
|
|
{ |
69
|
|
|
$twoFactor = $this->currentUser()->getTwoFactor(); |
70
|
|
|
$countries = $country->findAll()->pluck('name.common', 'iso_3166_1_alpha2'); |
71
|
|
|
|
72
|
|
|
return view('rinvex.fort::account.page', compact('twoFactor', 'countries')); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Process the account update form. |
77
|
|
|
* |
78
|
|
|
* @param \Rinvex\Fort\Http\Requests\AccountUpdate $request |
79
|
|
|
* |
80
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
81
|
|
|
*/ |
82
|
|
|
public function processAccountUpdate(AccountUpdate $request) |
83
|
|
|
{ |
84
|
|
|
$currentUser = $this->currentUser(); |
85
|
|
|
$data = $request->except(['_token', 'id']); |
86
|
|
|
$twoFactor = $currentUser->getTwoFactor(); |
87
|
|
|
|
88
|
|
|
if (isset($data['password'])) { |
89
|
|
|
$data['password'] = bcrypt($data['password']); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$emailVerification = $data['email'] != $currentUser->email ? [ |
|
|
|
|
93
|
|
|
'email_verified' => false, |
94
|
|
|
'email_verified_at' => null, |
95
|
|
|
] : []; |
96
|
|
|
|
97
|
|
|
$phoneVerification = $data['phone'] != $currentUser->phone ? [ |
|
|
|
|
98
|
|
|
'phone_verified' => false, |
99
|
|
|
'phone_verified_at' => null, |
100
|
|
|
] : []; |
101
|
|
|
|
102
|
|
|
$countryVerification = $data['country'] !== $currentUser->country; |
|
|
|
|
103
|
|
|
|
104
|
|
|
if ($phoneVerification || $countryVerification) { |
|
|
|
|
105
|
|
|
array_set($twoFactor, 'phone.enabled', false); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$this->users->update($request->get('id'), $data + $emailVerification + $phoneVerification + $twoFactor); |
109
|
|
|
|
110
|
|
|
return intend([ |
111
|
|
|
'back' => true, |
112
|
|
|
'with' => [ |
113
|
|
|
'rinvex.fort.alert.success' => Lang::get('rinvex.fort::message.account.'.(! empty($emailVerification) ? 'reverify' : 'updated')), |
|
|
|
|
114
|
|
|
] + ($twoFactor !== $currentUser->getTwoFactor() ? ['rinvex.fort.alert.warning' => Lang::get('rinvex.fort::message.verification.twofactor.phone.auto_disabled')] : []), |
|
|
|
|
115
|
|
|
]); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Show the account sessions. |
120
|
|
|
* |
121
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
122
|
|
|
*/ |
123
|
|
|
public function showAccountSessions() |
124
|
|
|
{ |
125
|
|
|
return view('rinvex.fort::account.sessions'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Flush the given session. |
130
|
|
|
* |
131
|
|
|
* @param string $token |
|
|
|
|
132
|
|
|
* |
133
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
134
|
|
|
*/ |
135
|
|
|
public function processSessionFlush($token = null) |
136
|
|
|
{ |
137
|
|
|
$status = ''; |
138
|
|
|
|
139
|
|
|
if ($token) { |
|
|
|
|
140
|
|
|
app('rinvex.fort.persistence')->delete($token); |
141
|
|
|
$status = Lang::get('rinvex.fort::message.auth.session.flushed'); |
142
|
|
|
} elseif (request()->get('confirm')) { |
143
|
|
|
app('rinvex.fort.persistence')->deleteByUser($this->currentUser()->id); |
|
|
|
|
144
|
|
|
$status = Lang::get('rinvex.fort::message.auth.session.flushedall'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return intend([ |
148
|
|
|
'back' => true, |
149
|
|
|
'with' => ['rinvex.fort.alert.warning' => $status], |
150
|
|
|
]); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Show the Two-Factor TOTP enable form. |
155
|
|
|
* |
156
|
|
|
* @param \Rinvex\Fort\Http\Requests\TwoFactorTotp $request |
157
|
|
|
* @param \Rinvex\Fort\Services\TwoFactorTotpProvider $totpProvider |
158
|
|
|
* |
159
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
160
|
|
|
*/ |
161
|
|
|
public function showTwoFactorTotpEnable(TwoFactorTotp $request, TwoFactorTotpProvider $totpProvider) |
|
|
|
|
162
|
|
|
{ |
163
|
|
|
$currentUser = $this->currentUser(); |
164
|
|
|
$settings = $currentUser->getTwoFactor(); |
165
|
|
|
|
166
|
|
|
if (array_get($settings, 'totp.enabled') && ! session()->get('rinvex.fort.alert.success') && ! session()->get('errors')) { |
|
|
|
|
167
|
|
|
$messageBag = new MessageBag([Lang::get('rinvex.fort::message.verification.twofactor.totp.already')]); |
168
|
|
|
$errors = (new ViewErrorBag())->put('default', $messageBag); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
if (! $secret = array_get($settings, 'totp.secret')) { |
172
|
|
|
array_set($settings, 'totp.enabled', false); |
173
|
|
|
array_set($settings, 'totp.secret', $secret = $totpProvider->generateSecretKey()); |
174
|
|
|
|
175
|
|
|
$this->users->update($currentUser->id, [ |
|
|
|
|
176
|
|
|
'two_factor' => $settings, |
177
|
|
|
]); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$qrCode = $totpProvider->getQRCodeInline(config('rinvex.fort.twofactor.issuer'), $currentUser->email, $secret); |
|
|
|
|
181
|
|
|
|
182
|
|
|
return view('rinvex.fort::account.twofactor', compact('secret', 'qrCode', 'settings', 'errors')); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Process the Two-Factor TOTP enable form. |
187
|
|
|
* |
188
|
|
|
* @param \Rinvex\Fort\Http\Requests\TwoFactorTotp $request |
189
|
|
|
* @param \Rinvex\Fort\Services\TwoFactorTotpProvider $totpProvider |
190
|
|
|
* |
191
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
192
|
|
|
*/ |
193
|
|
|
public function processTwoFactorTotpEnable(TwoFactorTotp $request, TwoFactorTotpProvider $totpProvider) |
194
|
|
|
{ |
195
|
|
|
$currentUser = $this->currentUser(); |
196
|
|
|
$settings = $currentUser->getTwoFactor(); |
197
|
|
|
$secret = array_get($settings, 'totp.secret'); |
198
|
|
|
$backup = array_get($settings, 'totp.backup'); |
199
|
|
|
$backupAt = array_get($settings, 'totp.backup_at'); |
200
|
|
|
|
201
|
|
|
if ($totpProvider->verifyKey($secret, $request->get('token'))) { |
202
|
|
|
array_set($settings, 'totp.enabled', true); |
203
|
|
|
array_set($settings, 'totp.secret', $secret); |
204
|
|
|
array_set($settings, 'totp.backup', $backup ?: $this->generateTwoFactorTotpBackups()); |
205
|
|
|
array_set($settings, 'totp.backup_at', $backupAt ?: (new Carbon())->toDateTimeString()); |
206
|
|
|
|
207
|
|
|
// Update Two-Factor settings |
208
|
|
|
$this->users->update($currentUser->id, [ |
|
|
|
|
209
|
|
|
'two_factor' => $settings, |
210
|
|
|
]); |
211
|
|
|
|
212
|
|
|
return intend([ |
213
|
|
|
'back' => true, |
214
|
|
|
'with' => ['rinvex.fort.alert.success' => Lang::get('rinvex.fort::message.verification.twofactor.totp.enabled')], |
|
|
|
|
215
|
|
|
]); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return intend([ |
219
|
|
|
'back' => true, |
220
|
|
|
'withErrors' => ['token' => Lang::get('rinvex.fort::message.verification.twofactor.totp.invalid_token')], |
221
|
|
|
]); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Process the Two-Factor TOTP disable. |
226
|
|
|
* |
227
|
|
|
* @param \Rinvex\Fort\Http\Requests\TwoFactorTotp $request |
228
|
|
|
* |
229
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
230
|
|
|
*/ |
231
|
|
|
public function processTwoFactorTotpDisable(TwoFactorTotp $request) |
|
|
|
|
232
|
|
|
{ |
233
|
|
|
$currentUser = $this->currentUser(); |
234
|
|
|
$settings = $currentUser->getTwoFactor(); |
235
|
|
|
|
236
|
|
|
array_set($settings, 'totp', []); |
237
|
|
|
|
238
|
|
|
$this->users->update($currentUser->id, [ |
|
|
|
|
239
|
|
|
'two_factor' => $settings, |
240
|
|
|
]); |
241
|
|
|
|
242
|
|
|
return intend([ |
243
|
|
|
'intended' => route('rinvex.fort.account.page'), |
244
|
|
|
'with' => ['rinvex.fort.alert.success' => Lang::get('rinvex.fort::message.verification.twofactor.totp.disabled')], |
|
|
|
|
245
|
|
|
]); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Process the Two-Factor Phone enable. |
250
|
|
|
* |
251
|
|
|
* @param \Rinvex\Fort\Http\Requests\TwoFactorPhone $request |
252
|
|
|
* |
253
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
254
|
|
|
*/ |
255
|
|
|
public function processTwoFactorPhoneEnable(TwoFactorPhone $request) |
|
|
|
|
256
|
|
|
{ |
257
|
|
|
$currentUser = $this->currentUser(); |
258
|
|
|
|
259
|
|
|
if (! $currentUser->phone || ! $currentUser->phone_verified) { |
|
|
|
|
260
|
|
|
return intend([ |
261
|
|
|
'intended' => route('rinvex.fort.account.page'), |
262
|
|
|
'withErrors' => ['phone' => Lang::get('rinvex.fort::message.account.phone_required')], |
263
|
|
|
]); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
$settings = $this->currentUser()->getTwoFactor(); |
267
|
|
|
|
268
|
|
|
array_set($settings, 'phone.enabled', true); |
269
|
|
|
|
270
|
|
|
$this->users->update($currentUser->id, [ |
|
|
|
|
271
|
|
|
'two_factor' => $settings, |
272
|
|
|
]); |
273
|
|
|
|
274
|
|
|
return intend([ |
275
|
|
|
'intended' => route('rinvex.fort.account.page'), |
276
|
|
|
'with' => ['rinvex.fort.alert.success' => Lang::get('rinvex.fort::message.verification.twofactor.phone.enabled')], |
|
|
|
|
277
|
|
|
]); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Process the Two-Factor Phone disable. |
282
|
|
|
* |
283
|
|
|
* @param \Rinvex\Fort\Http\Requests\TwoFactorPhone $request |
284
|
|
|
* |
285
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
286
|
|
|
*/ |
287
|
|
|
public function processTwoFactorPhoneDisable(TwoFactorPhone $request) |
|
|
|
|
288
|
|
|
{ |
289
|
|
|
$currentUser = $this->currentUser(); |
290
|
|
|
$settings = $currentUser->getTwoFactor(); |
291
|
|
|
|
292
|
|
|
array_set($settings, 'phone.enabled', false); |
293
|
|
|
|
294
|
|
|
$this->users->update($currentUser->id, [ |
|
|
|
|
295
|
|
|
'two_factor' => $settings, |
296
|
|
|
]); |
297
|
|
|
|
298
|
|
|
return intend([ |
299
|
|
|
'intended' => route('rinvex.fort.account.page'), |
300
|
|
|
'with' => ['rinvex.fort.alert.success' => Lang::get('rinvex.fort::message.verification.twofactor.phone.disabled')], |
|
|
|
|
301
|
|
|
]); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Process the Two-Factor OTP backup. |
306
|
|
|
* |
307
|
|
|
* @param \Rinvex\Fort\Http\Requests\TwoFactorTotp $request |
308
|
|
|
* |
309
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
310
|
|
|
*/ |
311
|
|
|
public function processTwoFactorTotpBackup(TwoFactorTotp $request) |
|
|
|
|
312
|
|
|
{ |
313
|
|
|
$currentUser = $this->currentUser(); |
314
|
|
|
$settings = $currentUser->getTwoFactor(); |
315
|
|
|
|
316
|
|
|
array_set($settings, 'totp.backup', $this->generateTwoFactorTotpBackups()); |
317
|
|
|
array_set($settings, 'totp.backup_at', (new Carbon())->toDateTimeString()); |
318
|
|
|
|
319
|
|
|
$this->users->update($currentUser->id, [ |
|
|
|
|
320
|
|
|
'two_factor' => $settings, |
321
|
|
|
]); |
322
|
|
|
|
323
|
|
|
return intend([ |
324
|
|
|
'back' => true, |
325
|
|
|
'with' => ['rinvex.fort.alert.success' => Lang::get('rinvex.fort::message.verification.twofactor.totp.rebackup')], |
|
|
|
|
326
|
|
|
]); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Generate Two-Factor OTP backup codes. |
331
|
|
|
* |
332
|
|
|
* @return array |
333
|
|
|
*/ |
334
|
|
|
protected function generateTwoFactorTotpBackups() |
335
|
|
|
{ |
336
|
|
|
$backup = []; |
337
|
|
|
|
338
|
|
|
for ($x = 0; $x <= 9; $x++) { |
339
|
|
|
$backup[] = str_pad(random_int(0, 9999999999), 10, 0, STR_PAD_BOTH); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
return $backup; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Get current user. |
347
|
|
|
* |
348
|
|
|
* @return \Rinvex\Fort\Contracts\AuthenticatableContract |
349
|
|
|
*/ |
350
|
|
|
protected function currentUser() |
351
|
|
|
{ |
352
|
|
|
return Auth::guard($this->getGuard())->user(); |
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.