Skip to content

Commit

Permalink
feat: 행정실 권한 체크 API (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
leeeryboy authored Sep 2, 2023
1 parent 068a38b commit d546a9f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.wafflestudio.csereal.core.user.api

import com.wafflestudio.csereal.common.CserealException
import com.wafflestudio.csereal.core.user.dto.StaffAuthResponse
import com.wafflestudio.csereal.core.user.service.UserService
import org.springframework.http.ResponseEntity
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.security.oauth2.core.oidc.user.OidcUser
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RequestMapping("/api/v1/user")
@RestController
class UserController(
private val userService: UserService
) {

@GetMapping("/is-staff")
fun isStaff(@AuthenticationPrincipal oidcUser: OidcUser?): ResponseEntity<StaffAuthResponse> {
if (oidcUser == null) {
throw CserealException.Csereal401("로그인이 필요합니다.")
}
val username = oidcUser.idToken.getClaim<String>("username")
if (userService.checkStaffAuth(username)) {
return ResponseEntity.ok(StaffAuthResponse(true))
} else {
return ResponseEntity.ok(StaffAuthResponse(false))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.wafflestudio.csereal.core.user.dto

data class StaffAuthResponse(
val isStaff: Boolean
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.wafflestudio.csereal.core.user.service

import com.wafflestudio.csereal.common.CserealException
import com.wafflestudio.csereal.core.user.database.Role
import com.wafflestudio.csereal.core.user.database.UserRepository
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

interface UserService {
fun checkStaffAuth(username: String): Boolean
}

@Service
@Transactional
class UserServiceImpl(
private val userRepository: UserRepository
) : UserService {

@Transactional(readOnly = true)
override fun checkStaffAuth(username: String): Boolean {
val user = userRepository.findByUsername(username) ?: throw CserealException.Csereal404("재로그인이 필요합니다.")
return user.role == Role.ROLE_STAFF
}
}

0 comments on commit d546a9f

Please sign in to comment.