feat(admin): 콘텐츠 관리자 읽기 권한을 확장한다
This commit is contained in:
@@ -6,6 +6,10 @@
|
||||
- [x] 관리자와 콘텐츠 관리자만 로그인 가능한 관리자 전용 로그인 API를 구현한다.
|
||||
- [x] 응답을 `token`, `role`만 포함하도록 구현한다.
|
||||
- [x] focused test, 관련 테스트, 스타일 검사를 실행하고 결과를 기록한다.
|
||||
- [x] 콘텐츠 관리자가 `GET /menu`를 호출할 수 있도록 메뉴 조회 권한을 확장한다.
|
||||
- [x] 콘텐츠 관리자가 관리자 콘텐츠 목록/조회 보조 API를 호출할 수 있도록 읽기 권한만 확장한다.
|
||||
- [x] 콘텐츠 관리자가 관리자 콘텐츠 수정 API를 호출할 수 없도록 수정 권한은 관리자 전용으로 유지한다.
|
||||
- [x] focused security test, 관련 테스트, 스타일 검사를 실행하고 결과를 기록한다.
|
||||
|
||||
## 설계
|
||||
- 새 권한은 기존 `MemberRole` enum에 `CONTENT_MANAGER`로 추가한다.
|
||||
@@ -13,6 +17,21 @@
|
||||
- 서비스는 기존 이메일/비밀번호 인증 흐름과 JWT 생성 방식을 재사용하되, `ADMIN`, `CONTENT_MANAGER` 외 역할은 `common.error.bad_credentials` 예외로 거부한다.
|
||||
- 응답 DTO는 관리자 로그인 전용으로 분리해 `token`, `role`만 노출한다.
|
||||
|
||||
## 후속 설계: 콘텐츠 관리자 메뉴 및 콘텐츠 읽기 권한
|
||||
- `CONTENT_MANAGER`는 이미 관리자 로그인 API로 토큰을 받을 수 있으므로 새 역할이나 새 권한 타입을 추가하지 않는다.
|
||||
- 메뉴 조회는 기존 `GET /menu`와 `MenuRepository.getMenu(member.role)` 구조를 그대로 사용한다. 컨트롤러의 `@PreAuthorize`에 `CONTENT_MANAGER`만 추가해 콘텐츠 관리자가 자신의 역할에 매핑된 메뉴를 받을 수 있게 한다.
|
||||
- `/content/list`는 서버 코드 상수가 아니라 `Menu.route` DB 값으로 내려가는 구조이므로, 서버에서는 별도 라우트 상수를 추가하지 않는다. 실제 메뉴 노출은 `roles = CONTENT_MANAGER`, `route = /content/list`, `isActive = true` 데이터가 존재할 때 가능하다.
|
||||
- 관리자 콘텐츠 API는 기존 `hasRole('ADMIN')` 클래스 권한을 메서드 단위로 분리한다. `GET /admin/audio-content/list`, `GET /admin/audio-content/search`, `GET /admin/audio-content/main/tab`은 `ADMIN`과 `CONTENT_MANAGER`를 허용하고, `PUT /admin/audio-content`는 `ADMIN`만 허용한다.
|
||||
- 콘텐츠 재생은 기존 사용자 콘텐츠 API의 `GET /audio-content/{id}/generate-url` 흐름을 변경하지 않는다. 이 API는 로그인 사용자와 구매/접근 조건으로 재생 URL을 제어하므로 콘텐츠 관리자 전용 우회 권한은 추가하지 않는다.
|
||||
|
||||
## 구현 계획
|
||||
- [x] `MenuController` 보안 테스트를 추가해 `CONTENT_MANAGER`가 `GET /menu`에 접근 가능하고 일반 사용자는 거부되는지 확인한다.
|
||||
- [x] `AdminContentController` 보안 테스트를 추가해 `CONTENT_MANAGER`는 목록/검색/메인탭 조회가 가능하고 수정은 거부되는지 확인한다.
|
||||
- [x] 테스트가 실패하는 것을 확인한다.
|
||||
- [x] `MenuController`의 `@PreAuthorize`에 `CONTENT_MANAGER`를 추가한다.
|
||||
- [x] `AdminContentController`의 클래스 단위 `@PreAuthorize`를 제거하고 각 메서드에 읽기/수정 권한을 분리한다.
|
||||
- [x] focused test, 관련 test, ktlint를 실행해 검증한다.
|
||||
|
||||
## 검증 기록
|
||||
- 2026-05-07: RED 확인: `./gradlew test --tests 'kr.co.vividnext.sodalive.admin.member.AdminMemberLoginServiceTest' --tests 'kr.co.vividnext.sodalive.admin.member.AdminMemberLoginControllerTest'` 실행 시 `AdminMemberLoginService`, `AdminMemberLoginController`, `AdminMemberLoginResponse`, `CONTENT_MANAGER`, `findByEmail` 미구현으로 `compileTestKotlin`이 실패함을 확인했다.
|
||||
- 2026-05-07: GREEN 확인: 동일 focused test가 `BUILD SUCCESSFUL`로 통과해 관리자/콘텐츠 관리자 로그인 허용 및 일반 사용자 거부를 확인했다.
|
||||
@@ -20,3 +39,8 @@
|
||||
- 2026-05-07: 회귀 확인: `./gradlew test --tests 'kr.co.vividnext.sodalive.admin.member.*'`가 `BUILD SUCCESSFUL`로 통과했다.
|
||||
- 2026-05-07: 스타일 확인: `./gradlew ktlintCheck`가 `BUILD SUCCESSFUL`로 통과했다.
|
||||
- 2026-05-07: Kotlin LSP는 현재 환경에 `.kt` 서버가 설정되어 있지 않아 `lsp_diagnostics` 실행이 불가했다. 대신 Gradle 컴파일 포함 focused/관련 test와 ktlint로 검증했다.
|
||||
- 2026-05-07: RED 확인: `./gradlew test --tests 'kr.co.vividnext.sodalive.menu.MenuControllerSecurityTest' --tests 'kr.co.vividnext.sodalive.admin.content.AdminContentControllerSecurityTest'` 실행 시 `CONTENT_MANAGER`의 `GET /menu`, `GET /admin/audio-content/list`, `GET /admin/audio-content/search`, `GET /admin/audio-content/main/tab` 허용 기대 테스트 4건이 실패함을 확인했다.
|
||||
- 2026-05-07: GREEN 확인: 동일 focused security test가 `BUILD SUCCESSFUL`로 통과해 콘텐츠 관리자 메뉴 조회, 관리자 콘텐츠 목록/검색/메인탭 조회 허용과 관리자 콘텐츠 수정 거부를 확인했다.
|
||||
- 2026-05-07: 관련 테스트 확인: `./gradlew test --tests 'kr.co.vividnext.sodalive.admin.content.AdminContentServiceTest' --tests 'kr.co.vividnext.sodalive.admin.member.AdminMemberLoginServiceTest' --tests 'kr.co.vividnext.sodalive.admin.member.AdminMemberLoginControllerTest'`가 `BUILD SUCCESSFUL`로 통과했다.
|
||||
- 2026-05-07: 스타일 확인: `./gradlew ktlintCheck`가 `BUILD SUCCESSFUL`로 통과했다.
|
||||
- 2026-05-07: Kotlin LSP는 현재 환경에 `.kt` 서버가 설정되어 있지 않아 `lsp_diagnostics` 실행이 불가했다. 대신 Gradle 컴파일 포함 focused/관련 test와 ktlint로 검증했다.
|
||||
|
||||
@@ -13,10 +13,10 @@ import org.springframework.web.bind.annotation.RestController
|
||||
import org.springframework.web.multipart.MultipartFile
|
||||
|
||||
@RestController
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
@RequestMapping("/admin/audio-content")
|
||||
class AdminContentController(private val service: AdminContentService) {
|
||||
@GetMapping("/list")
|
||||
@PreAuthorize("hasAnyRole('ADMIN', 'CONTENT_MANAGER')")
|
||||
fun getAudioContentList(
|
||||
@RequestParam(value = "status", required = false) status: ContentReleaseStatus?,
|
||||
pageable: Pageable
|
||||
@@ -28,6 +28,7 @@ class AdminContentController(private val service: AdminContentService) {
|
||||
)
|
||||
|
||||
@GetMapping("/search")
|
||||
@PreAuthorize("hasAnyRole('ADMIN', 'CONTENT_MANAGER')")
|
||||
fun searchAudioContent(
|
||||
@RequestParam(value = "status", required = false) status: ContentReleaseStatus?,
|
||||
@RequestParam(value = "search_word") searchWord: String,
|
||||
@@ -41,12 +42,14 @@ class AdminContentController(private val service: AdminContentService) {
|
||||
)
|
||||
|
||||
@PutMapping(consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
fun modifyAudioContent(
|
||||
@RequestPart("request") requestString: String,
|
||||
@RequestPart("coverImage", required = false) coverImage: MultipartFile? = null
|
||||
) = ApiResponse.ok(service.updateAudioContent(coverImage, requestString))
|
||||
|
||||
@GetMapping("/main/tab")
|
||||
@PreAuthorize("hasAnyRole('ADMIN', 'CONTENT_MANAGER')")
|
||||
fun getContentMainTabList() = ApiResponse.ok(service.getContentMainTabList())
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ import org.springframework.web.bind.annotation.RestController
|
||||
@RequestMapping("/menu")
|
||||
class MenuController(private val service: MenuService) {
|
||||
@GetMapping
|
||||
@PreAuthorize("hasAnyRole('AGENT', 'ADMIN', 'CREATOR')")
|
||||
@PreAuthorize("hasAnyRole('AGENT', 'ADMIN', 'CREATOR', 'CONTENT_MANAGER')")
|
||||
fun getMenus(
|
||||
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member?
|
||||
) = run {
|
||||
|
||||
@@ -0,0 +1,184 @@
|
||||
package kr.co.vividnext.sodalive.admin.content
|
||||
|
||||
import kr.co.vividnext.sodalive.common.CountryContext
|
||||
import kr.co.vividnext.sodalive.content.main.tab.GetContentMainTabItem
|
||||
import kr.co.vividnext.sodalive.i18n.LangContext
|
||||
import kr.co.vividnext.sodalive.i18n.SodaMessageSource
|
||||
import org.junit.jupiter.api.DisplayName
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.mockito.Mockito
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
|
||||
import org.springframework.boot.test.context.TestConfiguration
|
||||
import org.springframework.boot.test.mock.mockito.MockBean
|
||||
import org.springframework.context.annotation.Bean
|
||||
import org.springframework.context.annotation.Import
|
||||
import org.springframework.data.domain.PageRequest
|
||||
import org.springframework.http.HttpStatus
|
||||
import org.springframework.http.MediaType
|
||||
import org.springframework.mock.web.MockMultipartFile
|
||||
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity
|
||||
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.anonymous
|
||||
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user
|
||||
import org.springframework.security.web.SecurityFilterChain
|
||||
import org.springframework.security.web.authentication.HttpStatusEntryPoint
|
||||
import org.springframework.test.web.servlet.MockMvc
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
|
||||
import javax.servlet.http.HttpServletResponse
|
||||
|
||||
@WebMvcTest(AdminContentController::class)
|
||||
@Import(AdminContentControllerSecurityTest.TestSecurityConfig::class)
|
||||
class AdminContentControllerSecurityTest @Autowired constructor(
|
||||
private val mockMvc: MockMvc
|
||||
) {
|
||||
@MockBean
|
||||
private lateinit var service: AdminContentService
|
||||
|
||||
@MockBean
|
||||
private lateinit var countryContext: CountryContext
|
||||
|
||||
@MockBean
|
||||
private lateinit var langContext: LangContext
|
||||
|
||||
@MockBean
|
||||
private lateinit var sodaMessageSource: SodaMessageSource
|
||||
|
||||
@TestConfiguration
|
||||
@EnableGlobalMethodSecurity(prePostEnabled = true)
|
||||
class TestSecurityConfig {
|
||||
@Bean
|
||||
fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
|
||||
return http
|
||||
.csrf().disable()
|
||||
.authorizeRequests()
|
||||
.anyRequest().authenticated()
|
||||
.and()
|
||||
.exceptionHandling()
|
||||
.authenticationEntryPoint(HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED))
|
||||
.accessDeniedHandler { _, response, _ -> response.sendError(HttpServletResponse.SC_FORBIDDEN) }
|
||||
.and()
|
||||
.build()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("콘텐츠 관리자 권한이면 관리자 콘텐츠 목록 조회에 성공한다")
|
||||
fun shouldAllowContentManagerRoleForContentList() {
|
||||
Mockito.`when`(
|
||||
service.getAudioContentList(
|
||||
status = ContentReleaseStatus.OPEN,
|
||||
pageable = PageRequest.of(0, 20)
|
||||
)
|
||||
).thenReturn(GetAdminContentListResponse(totalCount = 0, items = emptyList()))
|
||||
|
||||
mockMvc.perform(
|
||||
get("/admin/audio-content/list")
|
||||
.param("page", "0")
|
||||
.param("size", "20")
|
||||
.with(user("content-manager").roles("CONTENT_MANAGER"))
|
||||
)
|
||||
.andExpect(status().isOk)
|
||||
.andExpect(jsonPath("$.success").value(true))
|
||||
.andExpect(jsonPath("$.data.totalCount").value(0))
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("콘텐츠 관리자 권한이면 관리자 콘텐츠 검색에 성공한다")
|
||||
fun shouldAllowContentManagerRoleForContentSearch() {
|
||||
Mockito.`when`(
|
||||
service.searchAudioContent(
|
||||
status = ContentReleaseStatus.OPEN,
|
||||
searchWord = "title",
|
||||
pageable = PageRequest.of(0, 20)
|
||||
)
|
||||
).thenReturn(GetAdminContentListResponse(totalCount = 0, items = emptyList()))
|
||||
|
||||
mockMvc.perform(
|
||||
get("/admin/audio-content/search")
|
||||
.param("search_word", "title")
|
||||
.param("page", "0")
|
||||
.param("size", "20")
|
||||
.with(user("content-manager").roles("CONTENT_MANAGER"))
|
||||
)
|
||||
.andExpect(status().isOk)
|
||||
.andExpect(jsonPath("$.success").value(true))
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("콘텐츠 관리자 권한이면 관리자 콘텐츠 메인 탭 조회에 성공한다")
|
||||
fun shouldAllowContentManagerRoleForContentMainTab() {
|
||||
Mockito.`when`(service.getContentMainTabList()).thenReturn(
|
||||
listOf(GetContentMainTabItem(tabId = 1L, title = "홈"))
|
||||
)
|
||||
|
||||
mockMvc.perform(
|
||||
get("/admin/audio-content/main/tab")
|
||||
.with(user("content-manager").roles("CONTENT_MANAGER"))
|
||||
)
|
||||
.andExpect(status().isOk)
|
||||
.andExpect(jsonPath("$.success").value(true))
|
||||
.andExpect(jsonPath("$.data[0].tabId").value(1L))
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("콘텐츠 관리자 권한이면 관리자 콘텐츠 수정에 접근할 수 없다")
|
||||
fun shouldRejectContentManagerRoleForContentUpdate() {
|
||||
val requestPart = MockMultipartFile(
|
||||
"request",
|
||||
"request.json",
|
||||
MediaType.APPLICATION_JSON_VALUE,
|
||||
"{\"id\":1,\"isDefaultCoverImage\":false}".toByteArray()
|
||||
)
|
||||
|
||||
mockMvc.perform(
|
||||
multipart("/admin/audio-content")
|
||||
.file(requestPart)
|
||||
.with { request ->
|
||||
request.method = "PUT"
|
||||
request
|
||||
}
|
||||
.with(user("content-manager").roles("CONTENT_MANAGER"))
|
||||
)
|
||||
.andExpect(status().isOk)
|
||||
.andExpect(jsonPath("$.success").value(false))
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("관리자 권한이면 관리자 콘텐츠 수정에 접근할 수 있다")
|
||||
fun shouldAllowAdminRoleForContentUpdate() {
|
||||
val requestPart = MockMultipartFile(
|
||||
"request",
|
||||
"request.json",
|
||||
MediaType.APPLICATION_JSON_VALUE,
|
||||
"{\"id\":1,\"isDefaultCoverImage\":false}".toByteArray()
|
||||
)
|
||||
|
||||
mockMvc.perform(
|
||||
multipart("/admin/audio-content")
|
||||
.file(requestPart)
|
||||
.with { request ->
|
||||
request.method = "PUT"
|
||||
request
|
||||
}
|
||||
.with(user("admin").roles("ADMIN"))
|
||||
)
|
||||
.andExpect(status().isOk)
|
||||
.andExpect(jsonPath("$.success").value(true))
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("익명 사용자는 관리자 콘텐츠 목록 조회에 접근할 수 없다")
|
||||
fun shouldRejectAnonymousUserForContentList() {
|
||||
mockMvc.perform(
|
||||
get("/admin/audio-content/list")
|
||||
.param("page", "0")
|
||||
.param("size", "20")
|
||||
.with(anonymous())
|
||||
)
|
||||
.andExpect(status().isUnauthorized)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package kr.co.vividnext.sodalive.menu
|
||||
|
||||
import kr.co.vividnext.sodalive.common.CountryContext
|
||||
import kr.co.vividnext.sodalive.i18n.LangContext
|
||||
import kr.co.vividnext.sodalive.i18n.SodaMessageSource
|
||||
import kr.co.vividnext.sodalive.member.Member
|
||||
import kr.co.vividnext.sodalive.member.MemberAdapter
|
||||
import kr.co.vividnext.sodalive.member.MemberRole
|
||||
import org.junit.jupiter.api.DisplayName
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.mockito.Mockito
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
|
||||
import org.springframework.boot.test.context.TestConfiguration
|
||||
import org.springframework.boot.test.mock.mockito.MockBean
|
||||
import org.springframework.context.annotation.Bean
|
||||
import org.springframework.context.annotation.Import
|
||||
import org.springframework.http.HttpStatus
|
||||
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity
|
||||
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.anonymous
|
||||
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user
|
||||
import org.springframework.security.web.SecurityFilterChain
|
||||
import org.springframework.security.web.authentication.HttpStatusEntryPoint
|
||||
import org.springframework.test.web.servlet.MockMvc
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
|
||||
import javax.servlet.http.HttpServletResponse
|
||||
|
||||
@WebMvcTest(MenuController::class)
|
||||
@Import(MenuControllerSecurityTest.TestSecurityConfig::class)
|
||||
class MenuControllerSecurityTest @Autowired constructor(
|
||||
private val mockMvc: MockMvc
|
||||
) {
|
||||
@MockBean
|
||||
private lateinit var service: MenuService
|
||||
|
||||
@MockBean
|
||||
private lateinit var countryContext: CountryContext
|
||||
|
||||
@MockBean
|
||||
private lateinit var langContext: LangContext
|
||||
|
||||
@MockBean
|
||||
private lateinit var sodaMessageSource: SodaMessageSource
|
||||
|
||||
@TestConfiguration
|
||||
@EnableGlobalMethodSecurity(prePostEnabled = true)
|
||||
class TestSecurityConfig {
|
||||
@Bean
|
||||
fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
|
||||
return http
|
||||
.csrf().disable()
|
||||
.authorizeRequests()
|
||||
.anyRequest().authenticated()
|
||||
.and()
|
||||
.exceptionHandling()
|
||||
.authenticationEntryPoint(HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED))
|
||||
.accessDeniedHandler { _, response, _ -> response.sendError(HttpServletResponse.SC_FORBIDDEN) }
|
||||
.and()
|
||||
.build()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("콘텐츠 관리자 권한이면 메뉴 조회에 성공한다")
|
||||
fun shouldAllowContentManagerRole() {
|
||||
val member = createMember(role = MemberRole.CONTENT_MANAGER)
|
||||
Mockito.`when`(service.getMenus(member)).thenReturn(
|
||||
listOf(GetMenuResponse(title = "콘텐츠 리스트", route = "/content/list"))
|
||||
)
|
||||
|
||||
mockMvc.perform(
|
||||
get("/menu")
|
||||
.with(user(MemberAdapter(member)))
|
||||
)
|
||||
.andExpect(status().isOk)
|
||||
.andExpect(jsonPath("$.success").value(true))
|
||||
.andExpect(jsonPath("$.data[0].route").value("/content/list"))
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("일반 사용자 권한이면 메뉴 조회에 접근할 수 없다")
|
||||
fun shouldRejectUserRole() {
|
||||
val member = createMember(role = MemberRole.USER)
|
||||
|
||||
mockMvc.perform(
|
||||
get("/menu")
|
||||
.with(user(MemberAdapter(member)))
|
||||
)
|
||||
.andExpect(status().isOk)
|
||||
.andExpect(jsonPath("$.success").value(false))
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("익명 사용자는 메뉴 조회에 접근할 수 없다")
|
||||
fun shouldRejectAnonymousUser() {
|
||||
mockMvc.perform(
|
||||
get("/menu")
|
||||
.with(anonymous())
|
||||
)
|
||||
.andExpect(status().isUnauthorized)
|
||||
}
|
||||
|
||||
private fun createMember(role: MemberRole): Member {
|
||||
return Member(
|
||||
email = "${role.name.lowercase()}@test.com",
|
||||
password = "password",
|
||||
nickname = role.name.lowercase(),
|
||||
role = role
|
||||
).apply {
|
||||
id = role.ordinal.toLong() + 1
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user