refactor: [authz] 역할/권한 API 품질 개선

- Validator::make를 FormRequest로 분리 (6개 생성)
- 하드코딩 한글 문자열을 i18n 키로 교체
- RoleMenuPermission 데드코드 제거
- Role 모델 SpatieRole 상속으로 일원화
- 권한 변경 후 캐시 무효화 추가 (AccessService::bumpVersion)
- 미문서화 8개 Swagger 엔드포인트 추가
- 역할/권한 라우트에 perm.map+permission 미들웨어 추가
This commit is contained in:
김보곤
2026-02-20 21:59:26 +09:00
parent 555fd196f5
commit 1dd9057540
21 changed files with 1400 additions and 271 deletions

View File

@@ -4,8 +4,6 @@
use App\Models\Permissions\Role;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Spatie\Permission\PermissionRegistrar;
class RoleService
@@ -51,28 +49,13 @@ public static function store(array $params = [])
$tenantId = (int) app('tenant_id');
$userId = app('api_user');
$v = Validator::make($params, [
'name' => [
'required', 'string', 'max:100',
Rule::unique('roles', 'name')->where(fn ($q) => $q
->where('tenant_id', $tenantId)
->where('guard_name', self::$guard)),
],
'description' => 'nullable|string|max:255',
'is_hidden' => 'sometimes|boolean',
]);
if ($v->fails()) {
return ['error' => $v->errors()->first(), 'code' => 422];
}
// Spatie 팀(테넌트) 컨텍스트
app(PermissionRegistrar::class)->setPermissionsTeamId($tenantId);
$role = Role::create([
'tenant_id' => $tenantId,
'guard_name' => self::$guard,
'name' => $v->validated()['name'],
'name' => $params['name'],
'description' => $params['description'] ?? null,
'is_hidden' => $params['is_hidden'] ?? false,
'created_by' => $userId,
@@ -92,7 +75,7 @@ public static function show(int $id)
->find($id);
if (! $role) {
return ['error' => '역할을 찾을 수 없습니다.', 'code' => 404];
return ['error' => __('error.role.not_found'), 'code' => 404];
}
return $role;
@@ -109,25 +92,10 @@ public static function update(int $id, array $params = [])
->find($id);
if (! $role) {
return ['error' => '역할을 찾을 수 없습니다.', 'code' => 404];
return ['error' => __('error.role.not_found'), 'code' => 404];
}
$v = Validator::make($params, [
'name' => [
'sometimes', 'string', 'max:100',
Rule::unique('roles', 'name')
->where(fn ($q) => $q->where('tenant_id', $tenantId)->where('guard_name', self::$guard))
->ignore($role->id),
],
'description' => 'sometimes|nullable|string|max:255',
'is_hidden' => 'sometimes|boolean',
]);
if ($v->fails()) {
return ['error' => $v->errors()->first(), 'code' => 422];
}
$updateData = $v->validated();
$updateData = $params;
$updateData['updated_by'] = $userId;
$role->fill($updateData)->save();
@@ -146,7 +114,7 @@ public static function destroy(int $id)
->find($id);
if (! $role) {
return ['error' => '역할을 찾을 수 없습니다.', 'code' => 404];
return ['error' => __('error.role.not_found'), 'code' => 404];
}
DB::transaction(function () use ($role, $userId) {
@@ -158,6 +126,9 @@ public static function destroy(int $id)
$role->delete();
});
AccessService::bumpVersion($tenantId);
app(PermissionRegistrar::class)->forgetCachedPermissions();
return 'success';
}