Skip to content

Commit

Permalink
#26 Add superadmin check
Browse files Browse the repository at this point in the history
  • Loading branch information
akselsf committed Sep 19, 2024
1 parent 1cb1287 commit ea71749
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ package com.example.autobank.data.authentication;
class AuthenticatedUserResponse(
val success: Boolean,
val isadmin: Boolean,
val issuperadmin: Boolean
)
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class AuthenticationService {
@Value("\${environment}")
private val environment: String = ""

@Value("\${superadmin.emails}")
private val superadminEmails: String = ""

fun getAuth0User(token: String): Auth0User {
return Auth0User("sub", "email", "name")
}
Expand Down Expand Up @@ -121,6 +124,10 @@ class AuthenticationService {
return userCommittees.contains(adminCommitteeNameLong)
}

fun checkSuperAdmin(): Boolean {
return superadminEmails.split(",").contains(getUserDetails().email)
}

data class Result(
val name_long: String = ""
)
Expand Down
25 changes: 6 additions & 19 deletions src/main/kotlin/com/example/autobank/service/OnlineUserService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,14 @@ class OnlineUserService(
}

fun checkUser(): AuthenticatedUserResponse {

val storedUser = onlineUserRepository.findByOnlineId(authenticationService.getUserSub())
return if (storedUser != null) {
AuthenticatedUserResponse(success = true, authenticationService.checkBankomMembership())
} else {
return createOnlineUser()
var storedUser = onlineUserRepository.findByOnlineId(authenticationService.getUserSub())
if (storedUser == null) {
storedUser = createOnlineUser()
}
return AuthenticatedUserResponse(success = true, authenticationService.checkBankomMembership(), authenticationService.checkSuperAdmin())
}

fun createOnlineUser(): AuthenticatedUserResponse {
try {
fun createOnlineUser(): OnlineUser {
val userinfo: Auth0User = authenticationService.getUserDetails()
val onlineUser = OnlineUser(
id = 0,
Expand All @@ -46,17 +43,7 @@ class OnlineUserService(
fullname = userinfo.name,
)

if (onlineUser.onlineId.isEmpty() || onlineUser.email.isEmpty() || onlineUser.fullname.isEmpty()) {
return AuthenticatedUserResponse(success = false, false)
}

onlineUserRepository.save(onlineUser)

return AuthenticatedUserResponse(success = true, authenticationService.checkBankomMembership())
} catch (e: Exception) {
println(e)
return AuthenticatedUserResponse(success = false, false)
}
return onlineUserRepository.save(onlineUser)
}

}

0 comments on commit ea71749

Please sign in to comment.