Add Backend to Monorepo

This commit is contained in:
2024-08-18 11:15:54 +02:00
parent b8e50dc139
commit fd7fe8c305
37 changed files with 2703 additions and 26 deletions
@@ -0,0 +1,154 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2024 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.routes
import de.steamwar.plugins.SWPermissionCheck
import de.steamwar.plugins.getUser
import de.steamwar.sql.SteamwarUser
import de.steamwar.sql.UserPerm
import de.steamwar.sql.UserPerm.Prefix
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import kotlinx.serialization.Serializable
@Serializable
data class RespondPrefix(val name: String, val colorCode: String, val chatPrefix: String)
@Serializable
data class RespondUserPerms(val prefixes: Map<String, RespondPrefix>, val perms: List<String>)
@Serializable
data class RespondUserPermsPrefix(val prefix: RespondPrefix, val perms: List<String>)
fun Route.configureUserPerms() {
route("/perms") {
install(SWPermissionCheck) {
allowMethod(HttpMethod.Get)
permission = UserPerm.MODERATION
}
get {
val perms = mutableListOf<String>()
val prefixes = mutableMapOf<String, RespondPrefix>()
UserPerm.entries.forEach {
if (it.name.startsWith("PREFIX_")) {
val prefix = UserPerm.prefixes[it]!!
prefixes[it.name] = RespondPrefix(it.name, prefix.colorCode, prefix.chatPrefix)
} else {
perms.add(it.name)
}
}
call.respond(RespondUserPerms(prefixes, perms))
}
route("/user/{id}") {
install(SWPermissionCheck) {
allowMethod(HttpMethod.Get)
permission = UserPerm.MODERATION
mustAuth = true
}
get {
val user = call.request.getUser()
if (user == null) {
call.respond(HttpStatusCode.BadRequest)
return@get
}
val perms = mutableListOf<String>()
var prefix = UserPerm.PREFIX_NONE
user.perms().forEach {
if (it.name.startsWith("PREFIX_")) {
prefix = it
} else {
perms.add(it.name)
}
}
val prefixs = UserPerm.prefixes[prefix]!!
call.respond(RespondUserPermsPrefix(RespondPrefix(prefix.name, prefixs.colorCode, prefixs.chatPrefix), perms))
}
put("/prefix/{prefix}") {
val user = call.request.getUser()
if (user == null) {
call.respond(HttpStatusCode.BadRequest)
return@put
}
val prefix = call.parameters["prefix"]
if (prefix == null || UserPerm.values().find { it.name == prefix } == null) {
call.respond(HttpStatusCode.BadRequest)
return@put
}
user.perms().filter { it.name.startsWith("PREFIX_") }.forEach {
UserPerm.removePerm(user, it)
}
UserPerm.addPerm(user, UserPerm.values().find { it.name == prefix }!!)
call.respond(HttpStatusCode.Accepted)
}
put("/{perm}") {
val user = call.request.getUser()
if (user == null) {
call.respond(HttpStatusCode.BadRequest)
return@put
}
val perm = call.parameters["perm"]
val permission = UserPerm.values().find { it.name == perm }
if (perm == null || perm.startsWith("PREFIX_") || permission == null) {
call.respond(HttpStatusCode.BadRequest)
return@put
}
if (!user.hasPerm(permission)) {
UserPerm.addPerm(user, permission)
call.respond(HttpStatusCode.Accepted)
return@put
}
call.respond(HttpStatusCode.NoContent)
}
delete("/{perm}") {
val user = call.request.getUser()
if (user == null) {
call.respond(HttpStatusCode.BadRequest)
return@delete
}
val perm = call.parameters["perm"]
val permission = UserPerm.values().find { it.name == perm }
if (perm == null || perm.startsWith("PREFIX_") || permission == null) {
call.respond(HttpStatusCode.BadRequest)
return@delete
}
if (user.hasPerm(permission)) {
UserPerm.removePerm(user, permission)
call.respond(HttpStatusCode.Accepted)
return@delete
}
call.respond(HttpStatusCode.NoContent)
}
}
}
}