Passed
Push — master ( 1d6d9a...2a90e7 )
by Ron
02:47 queued 12s
created

InitializeUserController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A initializeUser() 0 13 2
A submitInitializeUser() 0 41 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 by IP Address .'.\Request::ip());
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 6
        Log::debug('Route '.Route::currentRouteName().' visited by IP Address -'.\Request::ip().'. Submitted Data - ', $request->toArray());
42 6
        Log::debug('Initialize Data - ', $request->toArray());
43
44
        //  Verify that the link matches the assigned email address
45 6
        $valid = UserInitialize::where('token', $hash)->first();
46 6
        if(empty($valid)) {
47 2
            Log::warning('Visitor at IP Address '.\Request::ip().' tried to submit an invalid User Initialization link - '.$hash);
48 2
            return abort(404);
49
        }
50
51
        //  Validate the form
52 4
        $request->validate([
53
            'username' => [
54 4
                'required',
55 4
                Rule::in([$valid->username]),
56
            ],
57 4
            'newPass'  => 'required|string|min:6|confirmed'
58
        ]);
59
60
        //  Get the users information
61 2
        $userData   = User::where('username', $valid->username)->first();
62 2
        $nextChange = config('auth.passwords.settings.expire') != null ? Carbon::now()->addDays(config('auth.passwords.settings.expire')) : null;
63
64
        //  Update the password
65 2
        User::find($userData->user_id)->update(
66
            [
67 2
                'password'         => bcrypt($request->newPass),
68 2
                'password_expires' => $nextChange
69
            ]
70
        );
71
72
        //  Remove the initialize instance
73 2
        UserInitialize::find($valid->id)->delete();
74
        //  Log in the user
75 2
        Auth::loginUsingID($userData->user_id);
76
77
        //  Redirect the user to the dashboard
78 2
        Log::info('New user '.$request->username.' has finished setting their account.');
79 2
        return redirect(route('dashboard'));
80
    }
81
}
82