small refactor
This commit is contained in:
1
backend/.gitignore
vendored
1
backend/.gitignore
vendored
@@ -6,4 +6,5 @@
|
||||
/gradle.properties
|
||||
|
||||
# Kotlin
|
||||
/.kotlin/
|
||||
/build/
|
||||
|
||||
@@ -13,10 +13,10 @@ fun Application.module() {
|
||||
|
||||
configureDatabase()
|
||||
|
||||
configureSecurity()
|
||||
configureRouting()
|
||||
|
||||
configureSerialization()
|
||||
configureValidation()
|
||||
configureException()
|
||||
|
||||
configureSecurity()
|
||||
configureRouting()
|
||||
}
|
||||
|
||||
@@ -8,12 +8,6 @@ enum class Role(
|
||||
children = listOf(USER),
|
||||
);
|
||||
|
||||
fun hasChild(
|
||||
role: Role,
|
||||
): Boolean {
|
||||
return children.contains(role)
|
||||
}
|
||||
|
||||
companion object {
|
||||
val ALL = entries.toList()
|
||||
}
|
||||
|
||||
@@ -26,8 +26,7 @@ import com.infendro.account.model.repository.SessionRepository
|
||||
import com.infendro.account.util.Hasher
|
||||
import com.infendro.account.util.OTP
|
||||
import com.infendro.account.util.SecureHasher
|
||||
import io.ktor.server.sessions.CurrentSession
|
||||
import io.ktor.server.sessions.clear
|
||||
import io.ktor.server.sessions.*
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
||||
|
||||
class AccountService(
|
||||
@@ -161,7 +160,7 @@ class AccountService(
|
||||
.singleOrNull { AccountTable.id eq id }
|
||||
?: throw NotFoundException()
|
||||
|
||||
if (!principal.role.hasChild(account.role))
|
||||
if (account.role !in principal.role.children)
|
||||
throw ForbiddenException()
|
||||
|
||||
accountRepository.delete(account)
|
||||
@@ -176,10 +175,10 @@ class AccountService(
|
||||
.singleOrNull { AccountTable.id eq id }
|
||||
?: throw NotFoundException()
|
||||
|
||||
if (!principal.role.hasChild(account.role))
|
||||
if (account.role !in principal.role.children)
|
||||
throw ForbiddenException()
|
||||
|
||||
if (!principal.role.hasChild(request.role))
|
||||
if (request.role !in principal.role.children)
|
||||
throw ForbiddenException()
|
||||
|
||||
accountRepository.update(account) {
|
||||
|
||||
@@ -20,8 +20,7 @@ object SecureHasher {
|
||||
}
|
||||
|
||||
fun generateSalt(): String {
|
||||
return ByteArray(16)
|
||||
.also { random.nextBytes(it) }
|
||||
return random.nextBytes(16)
|
||||
.let { Hex.encode(it).decodeToString() }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,8 +7,7 @@ object TokenGenerator {
|
||||
private val random = PRNG.HMAC
|
||||
|
||||
fun generate(): String {
|
||||
val bytes = ByteArray(32)
|
||||
.also { random.nextBytes(it) }
|
||||
return Hex.encode(bytes).toString()
|
||||
return random.nextBytes(32)
|
||||
.let { Hex.encode(it).decodeToString() }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user