small refactor

This commit is contained in:
2025-08-17 09:52:46 +02:00
parent 9c363d4f4c
commit dcb490ae31
6 changed files with 11 additions and 19 deletions

1
backend/.gitignore vendored
View File

@@ -6,4 +6,5 @@
/gradle.properties
# Kotlin
/.kotlin/
/build/

View File

@@ -13,10 +13,10 @@ fun Application.module() {
configureDatabase()
configureSecurity()
configureRouting()
configureSerialization()
configureValidation()
configureException()
configureSecurity()
configureRouting()
}

View File

@@ -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()
}

View File

@@ -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) {

View File

@@ -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() }
}
}

View File

@@ -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() }
}
}