/* * 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 . */ 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, val perms: List) @Serializable data class RespondUserPermsPrefix(val prefix: RespondPrefix, val perms: List) fun Route.configureUserPerms() { route("/perms") { install(SWPermissionCheck) { allowMethod(HttpMethod.Get) permission = UserPerm.MODERATION } get { val perms = mutableListOf() val prefixes = mutableMapOf() 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() 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) } } } }