ikechukwukalu /
sanctumauthstarter
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Ikechukwukalu\Sanctumauthstarter\Rules; |
||
| 4 | |||
| 5 | use Ikechukwukalu\Sanctumauthstarter\Models\OldPassword; |
||
| 6 | use Illuminate\Contracts\Validation\Rule; |
||
| 7 | use Illuminate\Database\Eloquent\Collection as EloquentCollection; |
||
| 8 | use Illuminate\Support\Facades\Auth; |
||
| 9 | use Illuminate\Support\Facades\Hash; |
||
| 10 | |||
| 11 | class DisallowOldPassword implements Rule |
||
|
0 ignored issues
–
show
|
|||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Create a new rule instance. |
||
| 15 | * |
||
| 16 | * @return void |
||
| 17 | */ |
||
| 18 | |||
| 19 | private int|bool $checkAll; |
||
| 20 | private int $number; |
||
| 21 | private $user; |
||
| 22 | |||
| 23 | public function __construct($checkAll = true, $number = 4) |
||
| 24 | { |
||
| 25 | // |
||
| 26 | $this->checkAll = $checkAll; |
||
| 27 | $this->number = $number; |
||
| 28 | |||
| 29 | if (is_int($this->checkAll) && !empty($this->checkAll)) { |
||
| 30 | $this->number = $checkAll; |
||
| 31 | } |
||
| 32 | |||
| 33 | $this->user = Auth::user(); |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Determine if the validation rule passes. |
||
| 38 | * |
||
| 39 | * @param string $attribute |
||
| 40 | * @param mixed $value |
||
| 41 | * @return bool |
||
| 42 | */ |
||
| 43 | public function passes($attribute, $value) |
||
| 44 | { |
||
| 45 | $oldpasswords = $this->getOldPasswords(); |
||
| 46 | |||
| 47 | if ($oldpasswords->count() === 0) { |
||
| 48 | return !Hash::check($value, $this->user->password); |
||
|
0 ignored issues
–
show
|
|||
| 49 | } |
||
| 50 | |||
| 51 | foreach ($oldpasswords as $oldpassword) { |
||
| 52 | if (Hash::check($value, $oldpassword->password)) { |
||
| 53 | return false; |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | return true; |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Get the validation error message. |
||
| 62 | * |
||
| 63 | * @return string |
||
| 64 | */ |
||
| 65 | public function message() |
||
| 66 | { |
||
| 67 | return trans_choice('sanctumauthstarter::passwords.exists', intval(is_int($this->checkAll)), ['number' => $this->number]); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Get OldPin Model. |
||
| 72 | * |
||
| 73 | * @return \Illuminate\Database\Eloquent\Collection |
||
| 74 | */ |
||
| 75 | private function getOldPasswords(): EloquentCollection |
||
| 76 | { |
||
| 77 | if ($this->checkAll === true) { |
||
| 78 | return OldPassword::where('user_id', $this->user->id) |
||
|
0 ignored issues
–
show
The expression
return Ikechukwukalu\San...ted_at', 'desc')->get() could return the type Illuminate\Database\Eloq...Relations\HasOneThrough which is incompatible with the type-hinted return Illuminate\Database\Eloquent\Collection. Consider adding an additional type-check to rule them out.
Loading history...
|
|||
| 79 | ->orderBy('created_at', 'desc') |
||
| 80 | ->get(); |
||
| 81 | } |
||
| 82 | |||
| 83 | return OldPassword::where('user_id', $this->user->id) |
||
|
0 ignored issues
–
show
The expression
return Ikechukwukalu\San...e($this->number)->get() could return the type Illuminate\Database\Eloq...Relations\HasOneThrough which is incompatible with the type-hinted return Illuminate\Database\Eloquent\Collection. Consider adding an additional type-check to rule them out.
Loading history...
|
|||
| 84 | ->orderBy('created_at', 'desc') |
||
| 85 | ->take($this->number) |
||
| 86 | ->get(); |
||
| 87 | } |
||
| 88 | } |
||
| 89 |
This interface has been deprecated. The supplier of the interface has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.