Merge pull request #272 from usbharu/bugfix/get-loginuser

Bugfix/get loginuser
This commit is contained in:
usbharu 2024-02-01 16:00:22 +09:00 committed by GitHub
commit f011e43379
4 changed files with 29 additions and 1 deletions

View File

@ -424,6 +424,23 @@ class AccountApiTest {
.andExpect { status { isUnauthorized() } } .andExpect { status { isUnauthorized() } }
} }
@Test
fun `apiV1AccountsIdStatusesGet read権限で取得できる`() {
mockMvc
.get("/api/v1/accounts/1/statuses")
.asyncDispatch()
.andExpect { status { isOk() } }
}
@Test
@WithAnonymousUser
fun `apiV1AccountsIdStatusesGet 匿名でもpublic投稿を取得できる`() {
mockMvc
.get("/api/v1/accounts/1/statuses")
.asyncDispatch()
.andExpect { status { isOk() } }
}
companion object { companion object {
@JvmStatic @JvmStatic
@AfterAll @AfterAll

View File

@ -2,4 +2,6 @@ package dev.usbharu.hideout.core.infrastructure.springframework.security
interface LoginUserContextHolder { interface LoginUserContextHolder {
fun getLoginUserId(): Long fun getLoginUserId(): Long
fun getLoginUserIdOrNull(): Long?
} }

View File

@ -11,4 +11,13 @@ class OAuth2JwtLoginUserContextHolder : LoginUserContextHolder {
return principal.getClaim<String>("uid").toLong() return principal.getClaim<String>("uid").toLong()
} }
override fun getLoginUserIdOrNull(): Long? {
val principal = SecurityContextHolder.getContext()?.authentication?.principal
if (principal !is Jwt) {
return null
}
return principal.getClaim<String>("uid").toLongOrNull()
}
} }

View File

@ -71,7 +71,7 @@ class MastodonAccountApiController(
pinned: Boolean, pinned: Boolean,
tagged: String? tagged: String?
): ResponseEntity<Flow<Status>> = runBlocking { ): ResponseEntity<Flow<Status>> = runBlocking {
val userid = loginUserContextHolder.getLoginUserId() val userid = loginUserContextHolder.getLoginUserIdOrNull()
val statuses = accountApiService.accountsStatuses( val statuses = accountApiService.accountsStatuses(
userid = id.toLong(), userid = id.toLong(),
onlyMedia = onlyMedia, onlyMedia = onlyMedia,