Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: group 변경 반영 #167

Merged
merged 2 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
Loading