Skip to content

Commit

Permalink
fix: group 변경 반영 (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
leeeryboy authored Sep 28, 2023
1 parent 193f410 commit 7dc9cbb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class UserEntity(
val studentId: String,

@Enumerated(EnumType.STRING)
val role: Role?
var role: Role?

) : BaseTimeEntity()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,34 @@ class CustomOidcUserService(
private val restTemplate: RestTemplate
) : OAuth2UserService<OidcUserRequest, OidcUser> {

@Transactional
override fun loadUser(userRequest: OidcUserRequest): OidcUser {
val oidcUser = DefaultOidcUser(
userRequest.clientRegistration.scopes.map { SimpleGrantedAuthority("SCOPE_$it") },
userRequest.idToken
)

val username = oidcUser.idToken.getClaim<String>("username")
val user = userRepository.findByUsername(username)
var user = userRepository.findByUsername(username)

if (user == null) {
val userInfoAttributes = fetchUserInfo(userRequest)
createUser(username, userInfoAttributes)
user = createUser(username, userInfoAttributes)
}

val groups = oidcUser.idToken.getClaim<List<String>>("groups")
val role = if ("staff" in groups) {
Role.ROLE_STAFF
} else if ("professor" in groups) {
Role.ROLE_PROFESSOR
} else if ("graduate" in groups) {
Role.ROLE_GRADUATE
} else {
null
}

user.role = role

return oidcUser
}

Expand Down Expand Up @@ -67,31 +81,21 @@ class CustomOidcUserService(
return userInfoResponse.body ?: emptyMap()
}

@Transactional
fun createUser(username: String, userInfo: Map<String, Any>) {
private fun createUser(username: String, userInfo: Map<String, Any>): UserEntity {
val name = userInfo["name"] as String
val email = userInfo["email"] as String
val studentId = userInfo["student_id"] as String

val groups = userInfo["groups"] as List<String>
val role = if ("staff" in groups) {
Role.ROLE_STAFF
} else if ("professor" in groups) {
Role.ROLE_PROFESSOR
} else if ("graduate" in groups) {
Role.ROLE_GRADUATE
} else {
null
}

val newUser = UserEntity(
username = username,
name = name,
email = email,
studentId = studentId,
role = role
role = null
)

userRepository.save(newUser)

return newUser
}
}

0 comments on commit 7dc9cbb

Please sign in to comment.