Files
Gnuboard7/modules/_bundled/sirsoft-board/src/Traits/ChecksBoardPermission.php
T
HeuJung 7f8fb6870f fix(core,board,ecommerce): 조건부 검증 결함 패밀리 일괄 수정 및 실패 안내 노출
여러 화면에서 조건부(FormRequest) 검증이 우회되거나 과도하게 제한되던 결함을 한 계열로 묶어 수정한다.

- 검증 규칙 확장 필터 이름 10개를 나머지 92개와 동일 규칙으로 통일
- 설명 길이를 바이트가 아닌 글자 수로 계산(한글 오검출 해소)
- 비활성 언어팩 번역 보존한 채 수정 허용
- 알림 템플릿 제목 nullable 존중, 미지원 발송 수단 저장 차단
- 일정/실행 이력 목록 조회 검증 정합
- 게시판 첨부 미사용 시 빈 확장자 통과, new_display_hours/최소 길이 0 허용, 일괄 적용 override_values 범위 검증(SSoT)
- 이커머스 클레임 사유 조회 검증, 용어 정정
- 원문 i18n 키 노출 다수 해소, 저장/삭제 실패 시 사유 화면 노출
- 아이콘 크기/다크모드 대비 회귀 수정

번들 확장(모듈3·플러그인6·템플릿2·언어팩5) 버전 new-bump 및 CHANGELOG 반영, ja 언어팩 동기.
2026-07-23 10:54:17 +09:00

175 lines
6.2 KiB
PHP

<?php
namespace Modules\Sirsoft\Board\Traits;
use App\Enums\PermissionType;
use App\Http\Middleware\PermissionMiddleware;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Auth;
/**
* 게시판 권한 체크 Trait
*
* PermissionMiddleware를 내부적으로 호출하여 권한을 체크합니다.
* 미들웨어 로직 변경 시 자동으로 동일하게 동작합니다.
*/
trait ChecksBoardPermission
{
/**
* 게시판 권한을 확인합니다.
*
* @param string $slug 게시판 슬러그
* @param string $action 권한 액션 (예: 'admin.posts.read')
* @param PermissionType $type 권한 타입
*/
protected function checkBoardPermission(string $slug, string $action, PermissionType $type = PermissionType::Admin): bool
{
return $this->checkPermissionViaMiddleware("sirsoft-board.{$slug}.{$action}", $type);
}
/**
* 게시판 복수 권한을 확인합니다. (기본: OR 조건)
*
* @param string $slug 게시판 슬러그
* @param array $actions 권한 액션 배열
* @param PermissionType $type 권한 타입
* @param bool $requireAll 모든 권한 필요 여부
*/
protected function checkBoardPermissions(string $slug, array $actions, PermissionType $type = PermissionType::Admin, bool $requireAll = false): bool
{
$permissions = implode('|', array_map(fn ($action) => "sirsoft-board.{$slug}.{$action}", $actions));
return $this->checkPermissionViaMiddleware($permissions, $type, $requireAll);
}
/**
* 모듈 레벨 권한을 확인합니다.
*
* @param string $resource 리소스명 (예: 'boards')
* @param string $action 권한 액션 (예: 'create')
* @param PermissionType $type 권한 타입
*/
protected function checkModulePermission(string $resource, string $action, PermissionType $type = PermissionType::Admin): bool
{
return $this->checkPermissionViaMiddleware("sirsoft-board.{$resource}.{$action}", $type);
}
/**
* 게시판 권한 확인 후 실패 시 403 반환합니다.
*
* @param string $slug 게시판 슬러그
* @param string $action 권한 액션
* @param PermissionType $type 권한 타입
*/
protected function authorizeOrFail(string $slug, string $action, PermissionType $type = PermissionType::Admin): ?JsonResponse
{
if (! $this->checkBoardPermission($slug, $action, $type)) {
return $this->forbidden('sirsoft-board::messages.permission.denied');
}
return null;
}
/**
* 게시판 복수 권한 확인 후 실패 시 403 반환합니다.
*
* @param string $slug 게시판 슬러그
* @param array $actions 권한 액션 배열
* @param PermissionType $type 권한 타입
* @param bool $requireAll 모든 권한 필요 여부
*/
protected function authorizeAnyOrFail(string $slug, array $actions, PermissionType $type = PermissionType::Admin, bool $requireAll = false): ?JsonResponse
{
if (! $this->checkBoardPermissions($slug, $actions, $type, $requireAll)) {
return $this->forbidden('sirsoft-board::messages.permission.denied');
}
return null;
}
/**
* 모듈 레벨 권한 확인 후 실패 시 403 반환합니다.
*
* @param string $resource 리소스명
* @param string $action 권한 액션
* @param PermissionType $type 권한 타입
*/
protected function authorizeModuleOrFail(string $resource, string $action, PermissionType $type = PermissionType::Admin): ?JsonResponse
{
if (! $this->checkModulePermission($resource, $action, $type)) {
return $this->forbidden('sirsoft-board::messages.permission.denied');
}
return null;
}
/**
* 권한 식별자로 직접 권한을 확인합니다.
*
* Admin/User 페이지에 따라 자동으로 권한 타입을 결정합니다.
*
* @param string $identifier 권한 식별자 (예: 'sirsoft-board.notice.posts.read')
*/
protected function checkPermissionByIdentifier(string $identifier): bool
{
// 권한 타입 자동 결정: admin. 포함 시 Admin, 아니면 User
$type = str_contains($identifier, '.admin.') ? PermissionType::Admin : PermissionType::User;
return $this->checkPermissionViaMiddleware($identifier, $type);
}
/**
* 권한 캐시를 초기화합니다.
*
* 테스트 환경에서 동일 Request 인스턴스가 재사용되는 경우 호출합니다.
*/
public static function clearPermissionCache(): void
{
request()->attributes->remove('_board_permission_cache');
}
/**
* PermissionMiddleware를 통해 권한을 확인합니다.
*
* 동일 요청 내에서 같은 권한 조합에 대한 결과를 캐싱합니다.
* 캐시는 request()->instance()를 키로 사용하여 요청 단위로 자동 격리됩니다.
*
* @param string $permission 권한 식별자 (파이프로 복수 권한 구분)
* @param PermissionType $type 권한 타입
* @param bool $requireAll 모든 권한 필요 여부
*/
protected function checkPermissionViaMiddleware(string $permission, PermissionType $type, bool $requireAll = true): bool
{
$request = request();
$userId = Auth::id() ?? 'guest';
$cacheKey = $userId.'|'.$permission.'|'.$type->value.'|'.($requireAll ? '1' : '0');
// 요청 인스턴스에 권한 캐시를 저장하여 요청 단위로 자동 격리
$cache = $request->attributes->get('_board_permission_cache', []);
if (isset($cache[$cacheKey])) {
return $cache[$cacheKey];
}
$middleware = app(PermissionMiddleware::class);
$passed = false;
$middleware->handle(
$request,
function () use (&$passed) {
$passed = true;
return response('ok');
},
$type->value,
$permission,
$requireAll ? 'true' : 'false'
);
$cache[$cacheKey] = $passed;
$request->attributes->set('_board_permission_cache', $cache);
return $passed;
}
}