mirror of
https://github.com/Bubka/2FAuth.git
synced 2024-11-22 00:03:09 +01:00
Add a regex validation rule for the restrictRule setting
This commit is contained in:
parent
6102a4b3f6
commit
012af3177b
@ -3,6 +3,7 @@
|
||||
namespace App\Api\v1\Requests;
|
||||
|
||||
use App\Rules\IsValidEmailList;
|
||||
use App\Rules\IsValidRegex;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
@ -25,14 +26,28 @@ public function authorize()
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
$rule = [
|
||||
'value' => [
|
||||
'required',
|
||||
],
|
||||
];
|
||||
$routeParam = $this->route()?->parameter('settingName');
|
||||
|
||||
if ($this->route()?->parameter('settingName') == 'restrictList') {
|
||||
$rule['value'][] = new IsValidEmailList;
|
||||
if ($routeParam == 'restrictList') {
|
||||
$rule = [
|
||||
'value' => [
|
||||
new IsValidEmailList,
|
||||
],
|
||||
];
|
||||
}
|
||||
else if ($routeParam == 'restrictRule') {
|
||||
$rule = [
|
||||
'value' => [
|
||||
new IsValidRegex,
|
||||
],
|
||||
];
|
||||
}
|
||||
else {
|
||||
$rule = [
|
||||
'value' => [
|
||||
'required',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
return $rule;
|
||||
|
26
app/Rules/IsValidRegex.php
Normal file
26
app/Rules/IsValidRegex.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Rules;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
|
||||
class IsValidRegex implements ValidationRule
|
||||
{
|
||||
/**
|
||||
* Run the validation rule.
|
||||
*/
|
||||
public function validate(string $attribute, mixed $value, Closure $fail) : void
|
||||
{
|
||||
try {
|
||||
preg_match('/' . $value . '/', '');
|
||||
|
||||
if (preg_last_error() !== PREG_NO_ERROR) {
|
||||
$fail('validation.IsValidRegex')->translate();
|
||||
}
|
||||
}
|
||||
catch (\Throwable $ex) {
|
||||
$fail('validation.IsValidRegex')->translate();
|
||||
}
|
||||
}
|
||||
}
|
@ -143,6 +143,7 @@
|
||||
|
||||
'single' => 'When using :attribute it must be the only parameter in this request body',
|
||||
'onlyCustomOtpWithUri' => 'The uri parameter must be provided alone or only in combination with the \'custom_otp\' parameter',
|
||||
'IsValidRegex' => 'The :attribute must be a valid regex pattern.',
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
@ -187,7 +188,7 @@
|
||||
],
|
||||
'ids' => [
|
||||
'regex' => 'IDs must be comma separated, without trailing comma.',
|
||||
]
|
||||
],
|
||||
],
|
||||
|
||||
/*
|
||||
|
Loading…
Reference in New Issue
Block a user