core/features/roles/server/src/jvmMain/kotlin/dev/inmo/postssystem/features/roles/server/RolesStorageWriteServerRoutesConfigurator.kt

54 lines
1.9 KiB
Kotlin
Raw Normal View History

2021-11-24 07:52:27 +00:00
package dev.inmo.postssystem.features.roles.server
import dev.inmo.postssystem.features.roles.common.*
import dev.inmo.micro_utils.ktor.server.*
import dev.inmo.micro_utils.ktor.server.configurators.ApplicationRoutingConfigurator
2022-05-07 14:48:17 +00:00
import io.ktor.server.auth.authenticate
import io.ktor.server.routing.*
2021-11-24 07:52:27 +00:00
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.serializer
2021-11-27 19:11:04 +00:00
class RolesStorageWriteServerRoutesConfigurator<T : Role>(
private val storage: WriteRolesStorage<T>,
2021-11-24 07:52:27 +00:00
private val serializer: KSerializer<T>,
private val includeAuthKey: String,
2021-11-24 14:25:05 +00:00
private val excludeAuthKey: String = includeAuthKey,
private val unifiedRouter: UnifiedRouter
2021-11-24 07:52:27 +00:00
) : ApplicationRoutingConfigurator.Element {
override fun Route.invoke() {
2021-11-27 19:11:04 +00:00
route(usersRolesRootPathPart) {
val wrapperSerializer = RolesStorageIncludeExcludeWrapper.serializer(
serializer
)
unifiedRouter.apply {
2021-11-24 14:25:05 +00:00
authenticate(includeAuthKey) {
post(usersRolesIncludePathPart) {
val wrapper = uniload(wrapperSerializer)
2021-11-24 07:52:27 +00:00
2021-11-24 14:25:05 +00:00
unianswer(
Boolean.serializer(),
storage.include(
2021-11-27 19:11:04 +00:00
wrapper.subject,
2021-11-24 14:25:05 +00:00
wrapper.userRole
)
2021-11-24 07:52:27 +00:00
)
2021-11-24 14:25:05 +00:00
}
2021-11-24 07:52:27 +00:00
}
2021-11-24 14:25:05 +00:00
authenticate(excludeAuthKey) {
post(usersRolesExcludePathPart) {
val wrapper = uniload(wrapperSerializer)
2021-11-24 07:52:27 +00:00
2021-11-24 14:25:05 +00:00
unianswer(
Boolean.serializer(),
storage.exclude(
2021-11-27 19:11:04 +00:00
wrapper.subject,
2021-11-24 14:25:05 +00:00
wrapper.userRole
)
2021-11-24 07:52:27 +00:00
)
2021-11-24 14:25:05 +00:00
}
2021-11-24 07:52:27 +00:00
}
}
}
}
}