Passed
Push — dev5 ( 8481ee...a622eb )
by Ron
03:52 queued 01:34
created

InitializeUserController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
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 67
ccs 31
cts 31
cp 1
rs 10
wmc 6

3 Methods

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