make auth work
This commit is contained in:
@@ -6,27 +6,26 @@ import com.fasterxml.jackson.databind.SerializationFeature
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule
|
||||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
||||
import com.restapi.config.AppConfig.Companion.appConfig
|
||||
import com.restapi.config.AuthUser
|
||||
import io.ebean.Database
|
||||
import io.ebean.DatabaseFactory
|
||||
import io.ebean.config.CurrentTenantProvider
|
||||
import io.ebean.config.CurrentUserProvider
|
||||
import io.ebean.config.DatabaseConfig
|
||||
import io.ebean.config.TenantMode
|
||||
import redis.clients.jedis.JedisPooled
|
||||
import java.util.*
|
||||
|
||||
data class CurrentUser(
|
||||
val anon: Boolean = true,
|
||||
val userId: String = "",
|
||||
val tenantId: String = ""
|
||||
)
|
||||
import kotlin.jvm.optionals.getOrDefault
|
||||
|
||||
|
||||
object Session {
|
||||
private val currentUser = object : ThreadLocal<CurrentUser>() {
|
||||
override fun initialValue(): CurrentUser {
|
||||
return CurrentUser()
|
||||
private val currentUser = object : ThreadLocal<AuthUser>() {
|
||||
override fun initialValue(): AuthUser {
|
||||
return AuthUser("", "", emptyList<String>())
|
||||
}
|
||||
}
|
||||
fun setAuthorizedUser(a: AuthUser) = currentUser.set(a)
|
||||
|
||||
private val sc = DatabaseConfig().apply {
|
||||
loadFromProperties(Properties().apply {
|
||||
setProperty("datasource.db.username", appConfig.dbUser())
|
||||
@@ -35,8 +34,8 @@ object Session {
|
||||
setProperty("ebean.migration.run", appConfig.dbRunMigration().toString())
|
||||
})
|
||||
tenantMode = TenantMode.PARTITION
|
||||
currentTenantProvider = CurrentTenantProvider { currentUser.get().tenantId }
|
||||
currentUserProvider = CurrentUserProvider { currentUser.get().userId }
|
||||
currentTenantProvider = CurrentTenantProvider { currentUser.get().tenant }
|
||||
currentUserProvider = CurrentUserProvider { currentUser.get().userName }
|
||||
}
|
||||
|
||||
val database: Database = DatabaseFactory.create(sc)
|
||||
@@ -46,7 +45,7 @@ object Session {
|
||||
findAndRegisterModules()
|
||||
}
|
||||
|
||||
fun currentUser() = currentUser.get().userId
|
||||
fun currentUser() = currentUser.get().userName
|
||||
|
||||
fun Database.findByEntityAndId(entity: String, id: String): DataModel {
|
||||
return find(DataModel::class.java)
|
||||
@@ -68,6 +67,8 @@ object Session {
|
||||
return String.format("%s-%s", entity, "$s".padStart(10, '0'))
|
||||
}
|
||||
|
||||
val redis = JedisPooled(appConfig.redisUri().getOrDefault("redis://localhost:6739/0"))
|
||||
|
||||
}
|
||||
|
||||
object DataNotFoundException : Exception() {
|
||||
|
||||
Reference in New Issue
Block a user