You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
4.2 KiB
119 lines
4.2 KiB
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use App\Models\Config;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class ConfigRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
$id = $this->input('id');
|
|
$configId = $this->route('id') ?? $id;
|
|
|
|
return [
|
|
'name' => 'required|string|max:200',
|
|
'key' => [
|
|
'required',
|
|
'string',
|
|
'max:100',
|
|
'regex:/^[a-zA-Z_][a-zA-Z0-9_]*$/',
|
|
Rule::unique('configs')->ignore($configId),
|
|
],
|
|
'value' => 'required',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get custom messages for validator errors.
|
|
*/
|
|
public function messages(): array
|
|
{
|
|
return [
|
|
'name.required' => __('validation.required', ['attribute' => __('config.name')]),
|
|
'name.max' => __('validation.max.string', ['attribute' => __('config.name'), 'max' => 200]),
|
|
'key.required' => __('validation.required', ['attribute' => __('config.key')]),
|
|
'key.max' => __('validation.max.string', ['attribute' => __('config.key'), 'max' => 100]),
|
|
'key.regex' => __('validation.regex', ['attribute' => __('config.key')]),
|
|
'key.unique' => __('validation.unique', ['attribute' => __('config.key')]),
|
|
'value.required' => __('validation.required', ['attribute' => __('config.value')]),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Configure the validator instance.
|
|
*/
|
|
public function withValidator($validator): void
|
|
{
|
|
$validator->after(function ($validator) {
|
|
// 检查配置键是否为系统保留键
|
|
if ($this->has('key')) {
|
|
$key = $this->input('key');
|
|
$reservedKeys = [
|
|
Config::KEY_SYSTEM_NAME,
|
|
Config::KEY_SYSTEM_LOGO,
|
|
Config::KEY_VISIT_ADVANCE_DAYS,
|
|
Config::KEY_VISIT_MAX_DAYS,
|
|
Config::KEY_STUDY_REQUIRED,
|
|
Config::KEY_STUDY_EXPIRE_DAYS,
|
|
Config::KEY_AUDIT_AUTO_APPROVE,
|
|
Config::KEY_BLACKLIST_CHECK,
|
|
Config::KEY_SMS_ENABLED,
|
|
Config::KEY_EMAIL_ENABLED,
|
|
Config::KEY_WECHAT_ENABLED,
|
|
Config::KEY_GATE_QR_EXPIRE,
|
|
Config::KEY_VISIT_REMINDER_HOURS,
|
|
Config::KEY_SYSTEM_MAINTENANCE,
|
|
];
|
|
|
|
if (in_array($key, $reservedKeys)) {
|
|
$validator->errors()->add('key', __('config.reserved_key'));
|
|
}
|
|
}
|
|
|
|
// 验证配置值的格式
|
|
if ($this->has('key') && $this->has('value')) {
|
|
$key = $this->input('key');
|
|
$value = $this->input('value');
|
|
|
|
// 根据配置键验证值的格式
|
|
switch ($key) {
|
|
case Config::KEY_VISIT_ADVANCE_DAYS:
|
|
case Config::KEY_VISIT_MAX_DAYS:
|
|
case Config::KEY_STUDY_EXPIRE_DAYS:
|
|
case Config::KEY_GATE_QR_EXPIRE:
|
|
case Config::KEY_VISIT_REMINDER_HOURS:
|
|
if (!is_numeric($value) || $value < 0) {
|
|
$validator->errors()->add('value', __('config.numeric_value_required'));
|
|
}
|
|
break;
|
|
case Config::KEY_STUDY_REQUIRED:
|
|
case Config::KEY_AUDIT_AUTO_APPROVE:
|
|
case Config::KEY_BLACKLIST_CHECK:
|
|
case Config::KEY_SMS_ENABLED:
|
|
case Config::KEY_EMAIL_ENABLED:
|
|
case Config::KEY_WECHAT_ENABLED:
|
|
case Config::KEY_SYSTEM_MAINTENANCE:
|
|
if (!in_array($value, ['0', '1', 'true', 'false'])) {
|
|
$validator->errors()->add('value', __('config.boolean_value_required'));
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|