1 | <?php |
||
2 | |||
3 | namespace App\Http\Controllers; |
||
4 | |||
5 | use App\Models\User; |
||
6 | use Illuminate\Http\JsonResponse; |
||
7 | use Illuminate\Http\RedirectResponse; |
||
8 | use Illuminate\Http\Request; |
||
9 | use Illuminate\Support\Facades\Auth; |
||
10 | use Illuminate\Support\Facades\Hash; |
||
11 | |||
12 | /** |
||
13 | * Controller specifically for handling profile security operations |
||
14 | * like 2FA management with no dependencies on other profile functions |
||
15 | */ |
||
16 | class ProfileSecurityController extends BasePageController |
||
17 | { |
||
18 | /** |
||
19 | * Disable 2FA for the authenticated user from the profile page |
||
20 | * This is separate from the main profile edit functionality |
||
21 | * |
||
22 | * @return JsonResponse|RedirectResponse |
||
23 | */ |
||
24 | public function disable2fa(Request $request) |
||
25 | { |
||
26 | // Simple validation - only password is required |
||
27 | $validated = $request->validate([ |
||
28 | 'current_password' => 'required', |
||
29 | ]); |
||
30 | |||
31 | // Check if password is correct |
||
32 | if (! Hash::check($validated['current_password'], Auth::user()->password)) { |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
33 | if ($request->expectsJson() || $request->ajax()) { |
||
34 | return response()->json([ |
||
35 | 'success' => false, |
||
36 | 'message' => 'Your password does not match. Please try again.', |
||
37 | ]); |
||
38 | } |
||
39 | |||
40 | return redirect() |
||
41 | ->to('profileedit#security') |
||
42 | ->with('error_2fa', 'Your password does not match. Please try again.'); |
||
43 | } |
||
44 | |||
45 | // Get the user and disable 2FA |
||
46 | $user = Auth::user(); |
||
47 | if ($user->passwordSecurity) { |
||
0 ignored issues
–
show
|
|||
48 | $user->passwordSecurity->google2fa_enable = 0; |
||
49 | $user->passwordSecurity->save(); |
||
50 | |||
51 | if ($request->expectsJson() || $request->ajax()) { |
||
52 | return response()->json([ |
||
53 | 'success' => true, |
||
54 | 'message' => '2FA has been successfully disabled.', |
||
55 | ]); |
||
56 | } |
||
57 | |||
58 | return redirect() |
||
59 | ->to('profileedit#security') |
||
60 | ->with('success_2fa', '2FA has been successfully disabled.'); |
||
61 | } |
||
62 | |||
63 | if ($request->expectsJson() || $request->ajax()) { |
||
64 | return response()->json([ |
||
65 | 'success' => false, |
||
66 | 'message' => 'No 2FA configuration found for this user.', |
||
67 | ]); |
||
68 | } |
||
69 | |||
70 | return redirect() |
||
71 | ->to('profileedit#security') |
||
72 | ->with('error_2fa', 'No 2FA configuration found for this user.'); |
||
73 | } |
||
74 | } |
||
75 |