mirror of
https://github.com/BrenBroZAYT/gameyfin.git
synced 2026-06-15 08:15:37 +00:00
Replaced hardcoded mapping with reflection (yay reflection)
This commit is contained in:
@@ -17,7 +17,7 @@ abstract class TokenService<T : TokenType>(
|
||||
)
|
||||
|
||||
tokenRepository.findByUserAndType(user, type)?.let {
|
||||
log.warn { "Deleting existing '${it.type}' token for user '${user.username}'" }
|
||||
log.warn { "Deleting existing ${it.type.key} token for user '${user.username}'" }
|
||||
delete(it)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,20 +1,13 @@
|
||||
package de.grimsi.gameyfin.shared.token
|
||||
|
||||
import java.io.Serializable
|
||||
import kotlin.time.Duration
|
||||
import kotlin.time.Duration.Companion.minutes
|
||||
|
||||
sealed class TokenType(
|
||||
val key: String,
|
||||
val expiration: Duration
|
||||
) : Serializable {
|
||||
) {
|
||||
data object PasswordReset : TokenType("password-reset", 15.minutes)
|
||||
data object EmailVerification : TokenType("email-verification", Duration.INFINITE)
|
||||
data object Invitation : TokenType("invitation", Duration.INFINITE)
|
||||
|
||||
fun readResolve(): Any = when (this) {
|
||||
PasswordReset -> PasswordReset
|
||||
EmailVerification -> EmailVerification
|
||||
Invitation -> Invitation
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import java.io.Serializable
|
||||
import java.sql.PreparedStatement
|
||||
import java.sql.ResultSet
|
||||
import java.sql.Types
|
||||
import kotlin.reflect.full.createInstance
|
||||
|
||||
class TokenTypeUserType : UserType<TokenType> {
|
||||
|
||||
@@ -24,12 +25,12 @@ class TokenTypeUserType : UserType<TokenType> {
|
||||
owner: Any?
|
||||
): TokenType? {
|
||||
val key = rs.getString(position) ?: return null
|
||||
return when (key) {
|
||||
TokenType.PasswordReset.key -> TokenType.PasswordReset
|
||||
TokenType.EmailVerification.key -> TokenType.EmailVerification
|
||||
TokenType.Invitation.key -> TokenType.Invitation
|
||||
else -> throw IllegalArgumentException("Unknown TokenType key: $key")
|
||||
}
|
||||
val tokenTypeClass = TokenType::class
|
||||
|
||||
return tokenTypeClass.sealedSubclasses
|
||||
.map { it.objectInstance ?: it.createInstance() }
|
||||
.firstOrNull { it.key == key }
|
||||
?: throw IllegalArgumentException("Unknown TokenType: $key")
|
||||
}
|
||||
|
||||
override fun nullSafeSet(
|
||||
|
||||
Reference in New Issue
Block a user