ChangePasswordRequest::authorize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\Base\app\Http\Requests;
4
5
use Illuminate\Foundation\Http\FormRequest;
6
use Illuminate\Support\Facades\Hash;
7
8
class ChangePasswordRequest extends FormRequest
9
{
10
    /**
11
     * Determine if the user is authorized to make this request.
12
     *
13
     * @return bool
14
     */
15
    public function authorize()
16
    {
17
        // only allow updates if the user is logged in
18
        return backpack_auth()->check();
19
    }
20
21
    /**
22
     * Get the validation rules that apply to the request.
23
     *
24
     * @return array
25
     */
26
    public function rules()
27
    {
28
        return [
29
            'old_password'     => 'required',
30
            'new_password'     => 'required|min:6',
31
            'confirm_password' => 'required|same:new_password|min:6',
32
        ];
33
    }
34
35
    /**
36
     * Configure the validator instance.
37
     *
38
     * @param \Illuminate\Validation\Validator $validator
39
     *
40
     * @return void
41
     */
42
    public function withValidator($validator)
43
    {
44
        $validator->after(function ($validator) {
45
            // check old password matches
46
            if (!Hash::check($this->input('old_password'), backpack_auth()->user()->password)) {
47
                $validator->errors()->add('old_password', trans('backpack::base.old_password_incorrect'));
48
            }
49
        });
50
    }
51
}
52