1 | <?php |
||
2 | |||
3 | namespace ProtoneMedia\LaravelMixins\Rules; |
||
4 | |||
5 | use Illuminate\Contracts\Validation\Rule; |
||
6 | use Illuminate\Foundation\Auth\ThrottlesLogins; |
||
7 | use Illuminate\Http\Request; |
||
8 | use Illuminate\Support\Facades\Hash; |
||
9 | use Illuminate\Support\Str; |
||
10 | |||
11 | class CurrentPassword implements Rule |
||
12 | { |
||
13 | use ThrottlesLogins; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
14 | |||
15 | private bool $tooManyAttempts = false; |
||
16 | |||
17 | /** |
||
18 | * Get the throttle key for the given request. |
||
19 | * |
||
20 | * @param \Illuminate\Http\Request $request |
||
21 | * @return string |
||
22 | */ |
||
23 | protected function throttleKey(Request $request) |
||
24 | { |
||
25 | return Str::lower($request->user()->getAuthIdentifier() . '|' . $request->ip()); |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * Determine if the validation rule passes. |
||
30 | * |
||
31 | * @param string $attribute |
||
32 | * @param mixed $value |
||
33 | * @return bool |
||
34 | */ |
||
35 | public function passes($attribute, $value) |
||
36 | { |
||
37 | $request = request(); |
||
38 | |||
39 | if ($this->hasTooManyLoginAttempts($request)) { |
||
40 | $this->tooManyAttempts = true; |
||
41 | return false; |
||
42 | } |
||
43 | |||
44 | return tap( |
||
45 | Hash::check($value, $request->user()->getAuthPassword()), |
||
46 | fn ($verified) => $verified ? null : $this->incrementLoginAttempts($request) |
||
47 | ); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Get the validation error message. |
||
52 | * |
||
53 | * @return string |
||
54 | */ |
||
55 | public function message() |
||
56 | { |
||
57 | if ($this->tooManyAttempts) { |
||
58 | $seconds = $this->limiter()->availableIn( |
||
59 | $this->throttleKey(request()) |
||
60 | ); |
||
61 | |||
62 | return __('auth.throttle', [ |
||
63 | 'seconds' => $seconds, |
||
64 | 'minutes' => ceil($seconds / 60), |
||
65 | ]); |
||
66 | } |
||
67 | |||
68 | return __('validation.password'); |
||
69 | } |
||
70 | } |
||
71 |