php-tenant/Rules/CheckUserPasswordRule.php

46 lines
1.3 KiB
PHP
Raw Normal View History

2026-01-26 21:08:59 +00:00
<?php
declare(strict_types=1);
namespace Core\Tenant\Rules;
2026-01-26 21:08:59 +00:00
use Closure;
use Core\Tenant\Models\User;
2026-01-26 21:08:59 +00:00
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Facades\Hash;
/**
* Validates that the provided value matches the user's current password.
*
* This is typically used for password confirmation flows where a user
* must provide their current password before changing it or performing
* sensitive operations.
*/
class CheckUserPasswordRule implements ValidationRule
{
/**
* Create a new rule instance.
*
* @param User $user The user whose password should be checked
* @param string|null $message Optional custom error message
*/
public function __construct(
protected User $user,
protected ?string $message = null
) {}
/**
* Run the validation rule.
*
* @param string $attribute The attribute being validated
* @param mixed $value The value being validated
* @param Closure $fail Closure to call if validation fails
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (! Hash::check($value, $this->user->password)) {
$fail($this->message ?: 'The password is incorrect.');
}
}
}