중첩 라우트의 상위 리소스 ID 를 조회 스코프에 반영하지 않아 A 의 경로로 B 의 하위 리소스를 읽거나 변경할 수 있었다. 방어 위치를 Repository where 절(SSoT)로 통일하고 컨트롤러 사후 비교에 의존하지 않게 했다. 같은 리소스의 형제 엔드포인트가 서로 다른 강도로 검증하던 부분도 맞췄다. 계층 리소스는 자기 자신·자손을 부모로 지정하는 순환을 검증 계층에서 차단하고, 검증을 우회하는 경로(시더/훅/오염 데이터)를 위해 path 재귀에 방문 ID 가드를 뒀다. 설정값이 정하는 깊이 상한을 Service 가 리터럴로 재클램프하던 부분을 제거해 게시판 설정이 실제로 적용되게 했다. 쓰기 경로는 validated 기준으로 좁혀 FormRequest 미정의 필드가 fillable 로 새지 않게 했다. 기존 오염 데이터는 업그레이드 스텝이 정리한다(삭제 없이 최상위로 이동). 재발 방지로 audit 룰 3종을 추가하고, 규정 문서와 트러블슈팅 사례를 함께 갱신했다. 공개 이슈: gnuboard/g7
106 lines
3.9 KiB
PHP
106 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Menu;
|
|
|
|
use App\Extension\HookManager;
|
|
use App\Models\Menu;
|
|
use App\Models\Role;
|
|
use App\Rules\LocaleRequiredTranslatable;
|
|
use App\Rules\NotCircularParent;
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class UpdateMenuRequest extends FormRequest
|
|
{
|
|
/**
|
|
* 사용자가 이 요청을 수행할 권한이 있는지 확인
|
|
*
|
|
* 권한 체크는 라우트의 permission 미들웨어에서 수행됩니다.
|
|
*
|
|
* @return bool 권한 검증 결과 (항상 true)
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 요청에 적용할 검증 규칙
|
|
*
|
|
* @return array<string, ValidationRule|array<mixed>|string>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
// Route Model Binding으로 Menu 모델을 받으므로 menu->id 사용
|
|
$menuId = $this->route('menu')?->id;
|
|
|
|
$rules = [
|
|
'name' => ['nullable', new LocaleRequiredTranslatable(maxLength: 255)],
|
|
'slug' => ['required', 'string', 'max:255', Rule::unique(Menu::class, 'slug')->ignore($menuId)],
|
|
'url' => 'nullable|string|max:500',
|
|
'icon' => 'nullable|string|max:100',
|
|
// 순서 변경 엔드포인트(UpdateMenuOrderRequest)와 동일하게 자손 전체를 검사한다.
|
|
// NotCircularParent 생성자는 int non-nullable 이므로 $menuId 가 있을 때만 부착한다.
|
|
'parent_id' => ['nullable', 'integer', Rule::exists(Menu::class, 'id')],
|
|
'order' => 'nullable|integer|min:0',
|
|
'is_active' => 'nullable|boolean',
|
|
'extension_type' => 'nullable|string|in:core,module,plugin',
|
|
'extension_identifier' => 'nullable|string|max:255',
|
|
'roles' => 'nullable|array',
|
|
'roles.*' => ['integer', Rule::exists(Role::class, 'id')],
|
|
];
|
|
|
|
if ($menuId !== null) {
|
|
$rules['parent_id'][] = new NotCircularParent((int) $menuId);
|
|
}
|
|
|
|
// 모듈/플러그인이 validation rules를 동적으로 추가할 수 있도록 훅 제공
|
|
return HookManager::applyFilters('core.menu.update_validation_rules', $rules, $this);
|
|
}
|
|
|
|
/**
|
|
* 검증 오류 메시지 커스터마이징
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
public function messages(): array
|
|
{
|
|
return [
|
|
'name.required' => __('validation.menu.name.required'),
|
|
'slug.required' => __('validation.menu.slug.required'),
|
|
'slug.unique' => __('validation.menu.slug.unique'),
|
|
'slug.max' => __('validation.menu.slug.max'),
|
|
'url.max' => __('validation.menu.url.max'),
|
|
'icon.max' => __('validation.menu.icon.max'),
|
|
'order.integer' => __('validation.menu.order.integer'),
|
|
'order.min' => __('validation.menu.order.min'),
|
|
'parent_id.exists' => __('validation.menu.parent_id.exists'),
|
|
'is_active.boolean' => __('validation.menu.is_active.boolean'),
|
|
'extension_type.in' => __('validation.menu.extension_type.in'),
|
|
'extension_identifier.max' => __('validation.menu.extension_identifier.max'),
|
|
'roles.array' => __('validation.menu.roles.array'),
|
|
'roles.*.integer' => __('validation.menu.roles.integer'),
|
|
'roles.*.exists' => __('validation.menu.roles.exists'),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 검증 전 데이터 전처리
|
|
*
|
|
* 역호환성을 위해 문자열로 입력된 name을 다국어 배열로 자동 변환합니다.
|
|
*/
|
|
protected function prepareForValidation(): void
|
|
{
|
|
$locales = config('app.translatable_locales', ['ko', 'en']);
|
|
|
|
if ($this->has('name') && is_string($this->name)) {
|
|
$nameArray = [];
|
|
foreach ($locales as $locale) {
|
|
$nameArray[$locale] = $this->name;
|
|
}
|
|
$this->merge(['name' => $nameArray]);
|
|
}
|
|
}
|
|
}
|