-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/main/kotlin/com/wafflestudio/csereal/core/user/api/UserController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/com/wafflestudio/csereal/core/user/dto/StaffAuthResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
24 changes: 24 additions & 0 deletions
24
src/main/kotlin/com/wafflestudio/csereal/core/user/service/UserService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |