收紧输入渲染与后台配置权限
This commit is contained in:
@@ -13,10 +13,14 @@ namespace App\Http\Requests;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
/**
|
||||
* 新建聊天室请求验证器
|
||||
* 负责限制建房权限并拦截危险的房间名称输入。
|
||||
*/
|
||||
class StoreRoomRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
* 判断当前用户是否具备自建房间权限。
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
@@ -26,24 +30,42 @@ class StoreRoomRequest extends FormRequest
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
* 返回建房请求的校验规则。
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => ['required', 'string', 'max:50', 'unique:rooms,room_name'],
|
||||
'name' => ['required', 'string', 'max:50', 'regex:/^[^<>]+$/u', 'unique:rooms,room_name'],
|
||||
'description' => ['nullable', 'string', 'max:255'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 在校验前整理房间输入,避免空白与危险字符绕过前端限制。
|
||||
*/
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
$name = $this->input('name');
|
||||
$description = $this->input('description');
|
||||
|
||||
$this->merge([
|
||||
'name' => is_string($name) ? trim($name) : $name,
|
||||
'description' => is_string($description) ? trim($description) : $description,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回建房失败时的中文提示。
|
||||
*/
|
||||
public function messages(): array
|
||||
{
|
||||
return [
|
||||
'name.required' => '必须填写房间名称。',
|
||||
'name.unique' => '该房间名称已被占用。',
|
||||
'name.max' => '房间名称最多 50 个字符。',
|
||||
'name.regex' => '房间名称不能包含尖括号。',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user