Passed
Push — dev6 ( 6e2ff7...b01572 )
by Ron
18:29
created

ChangePasswordController::update()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 12
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
namespace App\Http\Controllers\User;
4
5
use Carbon\Carbon;
6
use Inertia\Inertia;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\Hash;
9
10
use App\Models\User;
11
use App\Events\UserPasswordChanged;
12
use App\Http\Controllers\Controller;
13
use App\Http\Requests\User\ChangePasswordRequest;
14
use App\Http\Requests\User\ChangeUserPasswordRequest;
15
16
class ChangePasswordController extends Controller
17
{
18
    /**
19
     * Page for a user to change their own password
20
     */
21
    public function index()
22
    {
23
        return Inertia::render('User/ChangePassword');
24
    }
25
26
    /**
27
     * Store a reset password for the logged in user
28
     */
29
    public function store(ChangePasswordRequest $request)
30
    {
31
        $user    = User::find($request->user()->user_id);
32
        $expires = config('auth.passwords.settings.expire') ? Carbon::now()->addDays(config('auth.passwords.settings.expire')) : null;
33
34
        $user->forceFill(['password' => Hash::make($request->password), 'password_expires' => $expires]);
35
        $user->save();
36
37
        event(new UserPasswordChanged($user));
1 ignored issue
show
Bug introduced by
It seems like $user can also be of type null; however, parameter $user of App\Events\UserPasswordChanged::__construct() does only seem to accept App\Models\User, maybe add an additional type check? ( Ignorable by Annotation )

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

37
        event(new UserPasswordChanged(/** @scrutinizer ignore-type */ $user));
Loading history...
38
        return redirect(route('dashboard'))->with([
39
            'message' => 'Password successfully updated',
40
            'type'    => 'success',
41
        ]);
42
    }
43
44
    /**
45
     * Update the password for a specific user
46
     */
47
    public function update(ChangeUserPasswordRequest $request, $id)
48
    {
49
        $expires = config('auth.passwords.settings.expire') ? Carbon::now()->addDays(config('auth.passwords.settings.expire')) : null;
50
        $user    = User::where('username', $id)->firstOrFail();
51
52
        $user->forceFill(['password' => Hash::make($request->password), 'password_expires' => $expires]);
53
        $user->save();
54
55
        event(new UserPasswordChanged($user));
56
        return back()->with([
57
            'message' => 'Password successfully updated',
58
            'type'    => 'success',
59
        ]);
60
    }
61
}
62