Test Failed
Push — dev5 ( e927b9...4863c0 )
by Ron
06:38
created

AccountController::initializeUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 9
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 16
ccs 0
cts 0
cp 0
crap 6
rs 9.9666
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Rules\ValidatePassword;
6
use App\User;
7
use Carbon\Carbon;
8
use App\UserSettings;
9
use Illuminate\Http\Request;
10
use Illuminate\Validation\Rule;
11
use Illuminate\Support\Facades\Log;
12
use Illuminate\Support\Facades\Auth;
13
use Illuminate\Support\Facades\Hash;
14
use Illuminate\Support\Facades\Route;
15
16
class AccountController extends Controller
17
{
18 26
    public function __construct()
19
    {
20 26
        $this->middleware('auth');
21 26
    }
22
23
    //  Index page is the change user settings form
24 2
    public function index()
25
    {
26 2
        $userData = User::find(Auth::user()->user_id);
27 2
        $userSett = UserSettings::where('user_id', Auth::user()->user_id)->first();
28
29 2
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
30 2
        return view('account.index', [
31 2
            'userData'     => $userData,
32 2
            'userSettings' => $userSett,
33 2
            'userID'       => Auth::user()->user_id
34
        ]);
35
    }
36
37
    //  Submit the new user settings
38 2
    public function submit(Request $request)
39
    {
40 2
        $request->validate([
41 2
            'username'   => 'required',
42 2
            'first_name' => 'required',
43 2
            'last_name'  => 'required',
44
            'email'      => [
45 2
                'required',
46 2
                Rule::unique('users')->ignore(Auth::user())
47
            ],
48
        ]);
49
50 2
        $userID = Auth::user()->user_id;
51 2
        User::find($userID)->update(
52
        [
53 2
            'first_name' => $request->first_name,
54 2
            'last_name'  => $request->last_name,
55 2
            'email'      => $request->email
56
        ]);
57
58 2
        session()->flash('success', 'User Settings Updated');
59
60 2
        Log::notice('User Settings Updated', ['user_id' => Auth::user()->user_id]);
61 2
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
62 2
        Log::debug('Submitted Data - ', $request->toArray());
63 2
        return redirect(route('account'));
64
    }
65
66
    //  Submit the user notification settings
67 2
    public function notifications(Request $request)
68
    {
69 2
        UserSettings::where('user_id', Auth::user()->user_id)->update(
70
        [
71 2
            'em_tech_tip'     => $request->em_tech_tip === 'on' ? true : false,
72 2
            'em_file_link'    => $request->em_file_link === 'on' ? true : false,
73 2
            'em_notification' => $request->em_notification === 'on' ? true : false,
74 2
            'auto_del_link'   => $request->auto_del_link === 'on' ? true : false,
75
        ]);
76
77 2
        session()->flash('success', 'User Notifications Updated');
78
79 2
        Log::notice('User Notifications Updated', ['user_id' => Auth::user()->user_id]);
80 2
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
81 2
        Log::debug('Submitted Data - ', $request->toArray());
82 2
        return redirect(route('account'));
83
    }
84
85
    //  Bring up the change password form
86 2
    public function changePassword()
87
    {
88 2
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
89 2
        return view('account.changePassword');
90
    }
91
92
    //  Submit the change password form
93 8
    public function submitPassword(Request $request)
94
    {
95
        //  Validate form data
96 8
        $request->validate([
97 8
            'oldPass' => ['required', new ValidatePassword],
98 8
            'newPass' => 'required|string|min:6|confirmed|different:oldPass'
99
        ]);
100
101
        //  Determine if there is a new password expire's date
102 2
        $newExpire = config('users.passExpires') != null ? Carbon::now()->addDays(config('users.passExpires')) : null;
103
104
        //  Change the password
105 2
        $user = Auth::user();
106 2
        $user->password = bcrypt($request->newPass);
107 2
        $user->password_expires = $newExpire;
108 2
        $user->save();
109
110 2
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
111 2
        Log::info('User Changed Password', ['user_id' => Auth::user()->user_id]);
112
113 2
        return redirect(route('account'))->with('success', 'Password Changed Successfully');
114
    }
115
116
117
118
119
120
121
122
123
124
125
126
//  TODO - use the initialize form to finish setting up an account
127
128
129
130
    //  Bring up the "Finish User Setup" form
131
    public function initializeUser($hash)
132
    {
133
        $this->middleware('guest');
134
135
        //  Validate the hash token
136
        $user = UserInitialize::where('token', $hash)->get();
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\UserInitialize was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
137
138
        if ($user->isEmpty()) {
139
            Log::debug('Route ' . Route::currentRouteName() . ' visited by User ID-' . Auth::user()->user_id);
140
            Log::warning('Visitor at IP Address ' . \Request::ip() . ' tried to access invalid initialize hash - ' . $hash);
141
            return abort(404);
1 ignored issue
show
Bug introduced by
Are you sure the usage of abort(404) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
142
        }
143
144
        Log::debug('Route ' . Route::currentRouteName() . ' visited.');
145
        Log::debug('Link Hash -' . $hash);
146
        return view('account.initializeUser', ['hash' => $hash]);
147
    }
148
149
    //  Submit the initialize user form
150
    public function submitInitializeUser(Request $request, $hash)
151
    {
152
        //  Verify that the link matches the assigned email address
153
        $valid = UserInitialize::where('token', $hash)->first();
154
        if (empty($valid)) {
155
            Log::debug('Route ' . Route::currentRouteName() . ' visited by User ID-' . Auth::user()->user_id);
156
            Log::warning('Visitor at IP Address ' . \Request::ip() . ' tried to submit an invalid User Initialization link - ' . $hash);
157
            return abort(404);
1 ignored issue
show
Bug introduced by
Are you sure the usage of abort(404) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
158
        }
159
160
        //  Validate the form
161
        $request->validate([
162
            'username' => [
163
                'required',
164
                Rule::in([$valid->username]),
165
            ],
166
            'newPass'  => 'required|string|min:6|confirmed'
167
        ]);
168
169
        //  Get the users information
170
        $userData = User::where('username', $valid->username)->first();
171
172
        $nextChange = config('users.passExpires') != null ? Carbon::now()->addDays(config('users.passExpires')) : null;
173
174
        //  Update the password
175
        User::find($userData->user_id)->update(
176
            [
177
                'password'         => bcrypt($request->newPass),
178
                'password_expires' => $nextChange
179
            ]
180
        );
181
182
        //  Remove the initialize instance
183
        UserInitialize::find($valid->id)->delete();
184
185
        //  Log in the user
186
        Auth::loginUsingID($userData->user_id);
187
188
        //  Redirect the user to the dashboard
189
        Log::debug('Route ' . Route::currentRouteName() . ' visited by User ID-' . Auth::user()->user_id);
190
        Log::debug('Initialize Data - ' . $request->toArray());
0 ignored issues
show
Bug introduced by
Are you sure $request->toArray() of type array can be used in concatenation? ( Ignorable by Annotation )

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

190
        Log::debug('Initialize Data - ' . /** @scrutinizer ignore-type */ $request->toArray());
Loading history...
191
        Log::notice('User has setup account', ['user_id' => $userData->user_id]);
192
        return redirect(route('dashboard'));
193
    }
194
}
195