Passed
Push — task/laravel-breadcrumbs ( 3beccb...a96280 )
by Yonathan
10:46 queued 10s
created

SettingsController::updatePassword()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 13
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\Applicant;
7
use App\Models\User;
8
use App\Services\Validation\Rules\PasswordCorrectRule;
9
use App\Services\Validation\Rules\PasswordFormatRule;
10
use Facades\App\Services\WhichPortal;
11
use Illuminate\Http\Request;
12
use Illuminate\Support\Facades\Lang;
13
use Illuminate\Support\Facades\Hash;
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.
2 ignored issues
show
Coding Style introduced by
Expected 9 spaces after parameter type; 4 found
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
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),
50
            'submit_password' => route(WhichPortal::prefixRoute('settings.password.update'), $user),
51
            'submit_government' => route(WhichPortal::prefixRoute('settings.government.update'), $user),
52
            'activate_two_factor' => route(WhichPortal::prefixRoute('two_factor.activate')),
53
            'deactivate_two_factor' => route(WhichPortal::prefixRoute('two_factor.deactivate')),
54
            'forget_remembered_devices' => route(WhichPortal::prefixRoute('two_factor.forget')),
55
            'generate_recovery_codes' => route(WhichPortal::prefixRoute('recovery_codes.show'))
56
        ];
57
58
        return view(
59
            'common/settings',
60
            $data
61
        );
62
    }
63
64
    /**
65
     * Update personal information
66
     *
67
     * @param  \Illuminate\Http\Request $request   Incoming request.
1 ignored issue
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
68
     * @param  \App\Models\User    $user Incoming User.
2 ignored issues
show
Coding Style introduced by
Expected 9 spaces after parameter type; 4 found
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
69
     * @return \Illuminate\Http\Response
70
     */
71
    public function updatePersonal(Request $request, User $user)
72
    {
73
        $validData = $request->validate([
74
            'first_name' => 'required|string|max:191',
75
            'last_name' => 'required|string|max:191',
76
            'email' => [
77
                'required',
78
                'email:rfc',
79
                'max:191',
80
                // Email may match existing email for this user, must be unique if changed.
81
                Rule::unique('users', 'email')->ignore($user->id)
82
            ]
83
        ]);
84
85
        if ($validData) {
86
            $user->update([
87
                'first_name' => $validData['first_name'],
88
                'last_name' => $validData['last_name'],
89
                'email' => $validData['email'],
90
            ]);
91
        }
92
93
        return redirect()->route(WhichPortal::prefixRoute('settings.edit'))->withSuccess(Lang::get('success.update_personal'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route...cess.update_personal')) also could return the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
94
    }
95
96
    /**
97
     * Update password.
98
     *
99
     * @param  \Illuminate\Http\Request $request   Incoming request.
1 ignored issue
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
100
     * @param  \App\Models\User    $user Incoming User.
2 ignored issues
show
Coding Style introduced by
Expected 9 spaces after parameter type; 4 found
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
101
     * @return \Illuminate\Http\Response
102
     */
103
    public function updatePassword(Request $request, User $user)
104
    {
105
        $validData = $request->validate([
106
            'current_password' => ['required', new PasswordCorrectRule],
107
            'new_password' => ['required', new PasswordFormatRule],
108
            'new_confirm_password' => ['required', 'same:new_password']
109
        ]);
110
111
        if ($validData) {
112
            $user->update(['password'=> Hash::make($validData['new_password'])]);
113
        }
114
115
        return redirect()->route(WhichPortal::prefixRoute('settings.edit'))->withSuccess(Lang::get('success.update_password'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route...cess.update_password')) also could return the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
116
    }
117
118
    /**
119
     * Update government information.
120
     *
121
     * @param  \Illuminate\Http\Request $request   Incoming request.
1 ignored issue
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
122
     * @param  \App\Models\User    $user Incoming User.
2 ignored issues
show
Coding Style introduced by
Expected 9 spaces after parameter type; 4 found
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
123
     * @return \Illuminate\Http\Response
124
     */
125
    public function updateGovernment(Request $request, User $user)
126
    {
127
        $validData = $request->validate([
128
            'gov_email' => 'nullable|required_unless:department,0|email:rfc|max:191',
129
                            Rule::unique('users', 'gov_email')->ignore($user->id)
130
        ]);
131
132
        if ($validData) {
133
            $user->update(['gov_email' => $validData['gov_email']]);
134
        }
135
136
        return redirect()->route(WhichPortal::prefixRoute('settings.edit'))->withSuccess(Lang::get('success.update_government'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route...ss.update_government')) also could return the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
137
    }
138
}
139