43 lines
1.1 KiB
PHP
43 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Rules;
|
|
|
|
use Encore\Admin\Auth\Database\Administrator;
|
|
use Illuminate\Contracts\Validation\Rule;
|
|
|
|
class AdminG2FARule implements Rule
|
|
{
|
|
protected array $formData;
|
|
|
|
protected string $errorMessage = '身份校验码 校验失败';
|
|
|
|
public function __construct(array $data)
|
|
{
|
|
$this->formData = $data;
|
|
}
|
|
|
|
public function passes($attribute, $value): bool
|
|
{
|
|
$user = Administrator::where('username', $this->formData['username'])->first();
|
|
|
|
if ($user && $user->use_g2fa) {
|
|
if (blank($value)) {
|
|
$this->errorMessage = '身份校验码 必须填写';
|
|
return false;
|
|
}
|
|
if (strlen($value) != 6) {
|
|
$this->errorMessage = '身份校验码 必须是6位';
|
|
return false;
|
|
}
|
|
|
|
$google2fa = app('pragmarx.google2fa');
|
|
return $google2fa->verifyGoogle2FA($user->g2fa_secret, $value);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public function message(): string
|
|
{
|
|
return $this->errorMessage;
|
|
}
|
|
} |