Passed
Push — dev5 ( 0d97ef...978ab8 )
by Ron
08:29
created

InitializeUserController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
dl 0
loc 66
ccs 31
cts 31
cp 1
rs 10
c 1
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A initializeUser() 0 13 2
A submitInitializeUser() 0 42 3
1
<?php
2
3
namespace App\Http\Controllers\Auth;
4
5
use App\User;
6
use Carbon\Carbon;
7
use App\UserInitialize;
8
use Illuminate\Http\Request;
9
use Illuminate\Validation\Rule;
10
use Illuminate\Support\Facades\Log;
11
use App\Http\Controllers\Controller;
12
use Illuminate\Support\Facades\Auth;
13
use Illuminate\Support\Facades\Route;
14
15
class InitializeUserController extends Controller
16
{
17 10
    public function __construct()
18
    {
19 10
        $this->middleware('guest');
20 10
    }
21
22
    //  Bring up the "Finish User Setup" form
23 4
    public function initializeUser($hash)
24
    {
25
        //  Validate the hash token
26 4
        $user = UserInitialize::where('token', $hash)->get();
27
28 4
        if ($user->isEmpty()) {
29 2
            Log::warning('Visitor at IP Address ' . \Request::ip() . ' tried to access invalid initialize hash - ' . $hash);
30 2
            return abort(404);
31
        }
32
33 2
        Log::debug('Route ' . Route::currentRouteName() . ' visited.');
34 2
        Log::debug('Link Hash -' . $hash);
35 2
        return view('account.initializeUser', ['hash' => $hash]);
36
    }
37
38
    //  Submit the initialize user form
39 6
    public function submitInitializeUser(Request $request, $hash)
40
    {
41
        //  Verify that the link matches the assigned email address
42 6
        $valid = UserInitialize::where('token', $hash)->first();
43 6
        if (empty($valid)) {
44 2
            Log::warning('Visitor at IP Address ' . \Request::ip() . ' tried to submit an invalid User Initialization link - ' . $hash);
45 2
            return abort(404);
46
        }
47
48
        //  Validate the form
49 4
        $request->validate([
50
            'username' => [
51 4
                'required',
52 4
                Rule::in([$valid->username]),
53
            ],
54 4
            'newPass'  => 'required|string|min:6|confirmed'
55
        ]);
56
57
        //  Get the users information
58 2
        $userData = User::where('username', $valid->username)->first();
59
60 2
        $nextChange = config('auth.passwords.settings.expire') != null ? Carbon::now()->addDays(config('auth.passwords.settings.expire')) : null;
61
62
        //  Update the password
63 2
        User::find($userData->user_id)->update(
64
            [
65 2
                'password'         => bcrypt($request->newPass),
66 2
                'password_expires' => $nextChange
67
            ]
68
        );
69
70
        //  Remove the initialize instance
71 2
        UserInitialize::find($valid->id)->delete();
72
73
        //  Log in the user
74 2
        Auth::loginUsingID($userData->user_id);
75
76
        //  Redirect the user to the dashboard
77 2
        Log::debug('Route ' . Route::currentRouteName() . ' visited by User ID-' . Auth::user()->user_id);
78 2
        Log::debug('Initialize Data - ', $request->toArray());
79 2
        Log::notice('User has setup account', ['user_id' => $userData->user_id]);
80 2
        return redirect(route('dashboard'));
81
    }
82
}
83