Update to UserPerms

Dieser Commit ist enthalten in:
Chaoscaot 2023-08-30 00:56:27 +02:00
Ursprung 05d677e539
Commit eaff459715
Signiert von: Chaoscaot
GPG-Schlüssel-ID: BDF8FADD7D5EDB7A
4 geänderte Dateien mit 174 neuen und 1 gelöschten Zeilen

@ -1 +1 @@
Subproject commit 02581ec36b29663c80a90dc2d40ff56bc47cf4c8
Subproject commit 66fc131803d8fad84dbc51e60999301e4a162190

Datei anzeigen

@ -0,0 +1,28 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.plugins
import de.steamwar.sql.SteamwarUser
import io.ktor.server.request.*
fun ApplicationRequest.getUser(): SteamwarUser? {
SteamwarUser.clear()
return SteamwarUser.get(call.parameters["id"]?.toIntOrNull() ?: return null)
}

Datei anzeigen

@ -28,5 +28,6 @@ fun Application.configureRoutes() {
configureDataRoutes()
configureEventFightRoutes()
configureModRoutes()
configureUserPerms()
}
}

Datei anzeigen

@ -0,0 +1,144 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.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 Routing.configureUserPerms() {
route("/perms") {
get {
val perms = mutableListOf<String>()
val prefixes = mutableMapOf<String, RespondPrefix>()
UserPerm.values().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}") {
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)
}
}
}
}