Test Setup Failed
Push — dev6 ( 81193f...01d104 )
by Ron
22:34
created

UserInitializeController::update()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 24
rs 9.8666
cc 3
nc 4
nop 2
1
<?php
2
3
namespace App\Http\Controllers\User;
4
5
use Carbon\Carbon;
6
use Inertia\Inertia;
7
8
use App\Models\User;
9
use App\Models\UserInitialize;
10
use App\Http\Controllers\Controller;
11
use App\Http\Requests\User\InitializeUserRequest;
12
13
use Illuminate\Support\Facades\Log;
14
use Illuminate\Support\Facades\Auth;
15
use Illuminate\Support\Facades\Hash;
16
use Illuminate\Auth\Events\PasswordReset;
17
18
class UserInitializeController extends Controller
19
{
20
    /**
21
     *  Show the finish setup form
22
     */
23
    public function show($id)
24
    {
25
        $link = UserInitialize::where('token', $id)->first();
26
27
        if(!$link)
28
        {
29
            abort(404, 'The Setup Link you are looking for cannot be found');
30
        }
31
32
        return Inertia::render('User/initialize', [
33
            'token' => $id,
34
            'name'  => $link->username,
35
        ]);
36
    }
37
38
    /**
39
     *  Finish setting up the new user
40
     */
41
    public function update(InitializeUserRequest $request, $id)
42
    {
43
        //  Validate the user
44
        $link = UserInitialize::where('token', $id)->firstOrFail();
45
46
        $user = User::where('email', $request->email)->first();
47
48
        if($link->username !== $user->username)
49
        {
50
            abort(403);
51
        }
52
53
        //  Determine the new expiration date
54
        $expires = config('auth.passwords.settings.expire') ? Carbon::now()->addDays(config('auth.passwords.settings.expire')) : null;
55
56
        $user->forceFill(['password' => Hash::make($request->password), 'password_expires' => $expires]);
57
        $user->save();
58
59
        event(new PasswordReset($user));
60
        $link->delete();
61
        Auth::login($user);
62
63
        Log::stack(['auth', 'user'])->notice('User '.$user->username.' has completed setting up their account');
64
        return redirect(route('dashboard'))->with(['message' => 'Your account is setup', 'type' => 'success']);
65
    }
66
}
67