fix : 메뉴 모델 및 일부 서비스파일 response 오류 수정

This commit is contained in:
2025-08-16 04:16:34 +09:00
parent 73d06e03b0
commit 6f1842181e
5 changed files with 312 additions and 211 deletions

View File

@@ -54,18 +54,14 @@ protected static function resolveRoleNames(int $tenantId, array $params): array
// 정제
$names = array_values(array_unique(array_filter($names)));
// 존재하지 않는 이름이 섞였는지 확인(실패 시 422로 안내하고 싶다면 여기서 검사)
// 존재 확인(필요시 에러 처리 확장 가능)
if (!empty($names)) {
$count = Role::query()
->where('tenant_id', $tenantId)
->where('guard_name', self::$guard)
->whereIn('name', $names)
->count();
if ($count !== count($names)) {
// 존재하지 않는 역할 이름이 포함됨
// 필요하면 어떤 이름이 없는지 찾아서 에러 반환하도록 개선 가능
}
// if ($count !== count($names)) { ... 필요시 상세 에러 반환 }
}
return $names;
@@ -84,13 +80,13 @@ public static function list(int $userId)
self::setTeam($tenantId);
// 현재 테넌트의 역할만
$roles = $user->roles()
$builder = $user->roles()
->where('roles.tenant_id', $tenantId)
->where('roles.guard_name', self::$guard)
->orderBy('roles.id', 'desc')
->get(['roles.id','roles.tenant_id','roles.name','roles.description','roles.guard_name','roles.created_at','roles.updated_at']);
->select(['roles.id','roles.tenant_id','roles.name','roles.description','roles.guard_name','roles.created_at','roles.updated_at'])
->orderBy('roles.id', 'desc');
return ApiResponse::response('result', $roles);
return ApiResponse::response('get', $builder);
}
/** 부여 (중복 무시) */
@@ -104,10 +100,10 @@ public static function grant(int $userId, array $params = [])
}
$v = Validator::make($params, [
'role_names' => 'sometimes|array',
'role_names' => 'sometimes|array',
'role_names.*' => 'string|min:1',
'role_ids' => 'sometimes|array',
'role_ids.*' => 'integer|min:1',
'role_ids' => 'sometimes|array',
'role_ids.*' => 'integer|min:1',
]);
if ($v->fails()) {
return ApiResponse::error($v->errors()->first(), 422);
@@ -123,9 +119,7 @@ public static function grant(int $userId, array $params = [])
return ApiResponse::error('유효한 역할이 없습니다.', 422);
}
// Spatie: 이름 배열로 부여 (teams 컨텍스트 적용)
$user->assignRole($names);
$user->assignRole($names); // teams 컨텍스트 적용
return ApiResponse::response('success');
}
@@ -140,10 +134,10 @@ public static function revoke(int $userId, array $params = [])
}
$v = Validator::make($params, [
'role_names' => 'sometimes|array',
'role_names' => 'sometimes|array',
'role_names.*' => 'string|min:1',
'role_ids' => 'sometimes|array',
'role_ids.*' => 'integer|min:1',
'role_ids' => 'sometimes|array',
'role_ids.*' => 'integer|min:1',
]);
if ($v->fails()) {
return ApiResponse::error($v->errors()->first(), 422);
@@ -160,7 +154,6 @@ public static function revoke(int $userId, array $params = [])
}
$user->removeRole($names); // 배열 허용
return ApiResponse::response('success');
}
@@ -175,10 +168,10 @@ public static function sync(int $userId, array $params = [])
}
$v = Validator::make($params, [
'role_names' => 'sometimes|array',
'role_names' => 'sometimes|array',
'role_names.*' => 'string|min:1',
'role_ids' => 'sometimes|array',
'role_ids.*' => 'integer|min:1',
'role_ids' => 'sometimes|array',
'role_ids.*' => 'integer|min:1',
]);
if ($v->fails()) {
return ApiResponse::error($v->errors()->first(), 422);
@@ -191,13 +184,11 @@ public static function sync(int $userId, array $params = [])
$names = self::resolveRoleNames($tenantId, $params);
if (empty($names)) {
// 빈 목록으로 sync = 모두 제거 의도라면 허용할 수도 있음.
// 정책에 맞춰 처리: 여기서는 빈 목록이면 실패 처리
// 정책상 빈 목록 sync 허용 시: $user->syncRoles([]) 로 전부 제거 가능
return ApiResponse::error('유효한 역할이 없습니다.', 422);
}
$user->syncRoles($names); // 교체
$user->syncRoles($names);
return ApiResponse::response('success');
}
}