32 lines
1.3 KiB
Kotlin
32 lines
1.3 KiB
Kotlin
package net.halfbinary.scavengerhuntapi.service
|
|
|
|
import net.halfbinary.scavengerhuntapi.repository.HunterRepository
|
|
import org.springframework.security.core.authority.SimpleGrantedAuthority
|
|
import org.springframework.security.core.userdetails.User
|
|
import org.springframework.security.core.userdetails.UserDetails
|
|
import org.springframework.security.core.userdetails.UserDetailsService
|
|
import org.springframework.security.core.userdetails.UsernameNotFoundException
|
|
import org.springframework.stereotype.Service
|
|
import java.util.*
|
|
|
|
|
|
@Service
|
|
class HunterDetailsService(private val hunterRepository: HunterRepository): UserDetailsService {
|
|
override fun loadUserByUsername(username: String): UserDetails {
|
|
hunterRepository.findByEmail(username)
|
|
?.let { hunter ->
|
|
val hunterAuthorities =
|
|
if (hunter.isAdmin) {
|
|
SimpleGrantedAuthority("ROLE_ADMIN")
|
|
} else {
|
|
SimpleGrantedAuthority("ROLE_USER")
|
|
}
|
|
return User(
|
|
hunter.email,
|
|
hunter.password,
|
|
Collections.singleton(hunterAuthorities)
|
|
)
|
|
}
|
|
throw UsernameNotFoundException("User Not Found with username: $username")
|
|
}
|
|
} |