Passed
Push — dev ( a60d1b...ac21a3 )
by Chris
04:58 queued 11s
created

SettingsController::updateGovernment()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 12
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Http\Controllers\Controller;
6
use App\Models\User;
7
use App\Services\Validation\Rules\PasswordCorrectRule;
8
use App\Services\Validation\Rules\PasswordFormatRule;
9
use Facades\App\Services\WhichPortal;
10
use Illuminate\Http\Request;
11
use Illuminate\Support\Facades\Auth;
12
use Illuminate\Support\Facades\Hash;
13
use Illuminate\Support\Facades\Lang;
14
use Illuminate\Validation\Rule;
15
16
class SettingsController extends Controller
17
{
18
19
    /**
20
     * Show the form for editing the logged-in User's settings
21
     *
22
     * @param  \Illuminate\Http\Request $request Incoming request.
23
     * @return \Illuminate\Http\Response
24
     */
25
    public function editAuthenticated(Request $request)
26
    {
27
        return $this->edit($request, $request->user());
28
    }
29
30
31
    /**
32
     * Display the specified resource.
33
     *
34
     * @param  \Illuminate\Http\Request $request Incoming request.
35
     * @param  \App\Models\User    $user Incoming User.
36
     * @return \Illuminate\Http\Response
37
     */
38
    public function edit(Request $request, User $user)
39
    {
40
        $this->authorize('view', $user);
41
        $this->authorize('update', $user);
42
43
        $data = [
44
            // Localized strings.
45
            'settings' => Lang::get('common/settings'),
46
            // Applicant data.
47
            'user' => $user,
48
            // Update routes.
49
            'submit_personal' => route(WhichPortal::prefixRoute('settings.personal.update'), $user),
0 ignored issues
show
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
            'submit_personal' => /** @scrutinizer ignore-call */ route(WhichPortal::prefixRoute('settings.personal.update'), $user),
Loading history...
50
            'submit_password' => route(WhichPortal::prefixRoute('settings.password.update'), $user),
51
            'submit_government' => route(WhichPortal::prefixRoute('settings.government.update'), $user),
52
            'submit_delete' => route('settings.account.delete', $user),
53
            'activate_two_factor' => route(WhichPortal::prefixRoute('two_factor.activate')),
54
            'deactivate_two_factor' => route(WhichPortal::prefixRoute('two_factor.deactivate')),
55
            'forget_remembered_devices' => route(WhichPortal::prefixRoute('two_factor.forget')),
56
            'generate_recovery_codes' => route(WhichPortal::prefixRoute('recovery_codes.show'))
57
        ];
58
59
        return view(
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
        return /** @scrutinizer ignore-call */ view(
Loading history...
60
            'common/settings',
61
            $data
62
        );
63
    }
64
65
    /**
66
     * Update personal information
67
     *
68
     * @param  \Illuminate\Http\Request $request   Incoming request.
69
     * @param  \App\Models\User    $user Incoming User.
70
     * @return \Illuminate\Http\Response
71
     */
72
    public function updatePersonal(Request $request, User $user)
73
    {
74
        $validData = $request->validate([
75
            'first_name' => 'required|string|max:191',
76
            'last_name' => 'required|string|max:191',
77
            'email' => [
78
                'required',
79
                'email:rfc',
80
                'max:191',
81
                // Email may match existing email for this user, must be unique if changed.
82
                Rule::unique('users', 'email')->ignore($user->id)
83
            ]
84
        ]);
85
86
        if ($validData) {
87
            $user->update([
88
                'first_name' => $validData['first_name'],
89
                'last_name' => $validData['last_name'],
90
                'email' => $validData['email'],
91
            ]);
92
        }
93
94
        return redirect()->route(WhichPortal::prefixRoute('settings.edit'))->withSuccess(Lang::get('success.update_personal'));
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

94
        return /** @scrutinizer ignore-call */ redirect()->route(WhichPortal::prefixRoute('settings.edit'))->withSuccess(Lang::get('success.update_personal'));
Loading history...
95
    }
96
97
    /**
98
     * Update password.
99
     *
100
     * @param  \Illuminate\Http\Request $request   Incoming request.
101
     * @param  \App\Models\User    $user Incoming User.
102
     * @return \Illuminate\Http\Response
103
     */
104
    public function updatePassword(Request $request, User $user)
105
    {
106
        $validData = $request->validate([
107
            'current_password' => ['required', new PasswordCorrectRule],
108
            'new_password' => ['required', new PasswordFormatRule],
109
            'new_confirm_password' => ['required', 'same:new_password']
110
        ]);
111
112
        if ($validData) {
113
            $user->update(['password' => Hash::make($validData['new_password'])]);
114
        }
115
116
        return redirect()->route(WhichPortal::prefixRoute('settings.edit'))->withSuccess(Lang::get('success.update_password'));
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

116
        return /** @scrutinizer ignore-call */ redirect()->route(WhichPortal::prefixRoute('settings.edit'))->withSuccess(Lang::get('success.update_password'));
Loading history...
117
    }
118
119
    /**
120
     * Update government information.
121
     *
122
     * @param  \Illuminate\Http\Request $request   Incoming request.
123
     * @param  \App\Models\User    $user Incoming User.
124
     * @return \Illuminate\Http\Response
125
     */
126
    public function updateGovernment(Request $request, User $user)
127
    {
128
        $validData = $request->validate([
129
            'gov_email' => 'nullable|required_unless:department,0|email:rfc|max:191',
130
            Rule::unique('users', 'gov_email')->ignore($user->id)
131
        ]);
132
133
        if ($validData) {
134
            $user->update(['gov_email' => $validData['gov_email']]);
135
        }
136
137
        return redirect()->route(WhichPortal::prefixRoute('settings.edit'))->withSuccess(Lang::get('success.update_government'));
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

137
        return /** @scrutinizer ignore-call */ redirect()->route(WhichPortal::prefixRoute('settings.edit'))->withSuccess(Lang::get('success.update_government'));
Loading history...
138
    }
139
140
    /**
141
     * Delete (soft) applicant account.
142
     *
143
     * @param  \Illuminate\Http\Request $request   Incoming request.
144
     * @param  \App\Models\User    $user Incoming User.
145
     * @return \Illuminate\Http\Response
146
     */
147
    public function deleteAccount(Request $request, User $user)
148
    {
149
        $user = Auth::user();
150
151
        $validData = $request->validate([
152
            'confirm_delete' => ['required']
153
        ]);
154
155
        if ($validData) {
156
            Auth::logout();
157
158
            User::where('id', $user->id)->update([
159
                'first_name' => 'DELETED',
160
                'last_name' => 'DELETED',
161
                'email' => 'DELETED' . rand(7777777, 88888888),
162
            ]);
163
        }
164
165
        return redirect()->route('home')->withSuccess(Lang::get('success.delete_account'));
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

165
        return /** @scrutinizer ignore-call */ redirect()->route('home')->withSuccess(Lang::get('success.delete_account'));
Loading history...
166
    }
167
}
168