This commit is contained in:
2024-08-27 21:34:18 +02:00
parent cab12fca92
commit c11eaaee45
13 changed files with 118 additions and 373 deletions
@@ -23,7 +23,6 @@ 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.*
@@ -42,7 +41,6 @@ data class RespondUserPermsPrefix(val prefix: RespondPrefix, val perms: List<Str
fun Route.configureUserPerms() {
route("/perms") {
install(SWPermissionCheck) {
allowMethod(HttpMethod.Get)
permission = UserPerm.MODERATION
}
get {
@@ -60,11 +58,6 @@ fun Route.configureUserPerms() {
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) {
@@ -86,38 +79,17 @@ fun Route.configureUserPerms() {
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 (user, prefix) = call.receivePermission("prefix") ?: 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 }!!)
UserPerm.addPerm(user, UserPerm.entries.find { it == 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
}
val (user, permission) = call.receivePermission() ?: return@put
if (!user.hasPerm(permission)) {
UserPerm.addPerm(user, permission)
@@ -127,20 +99,7 @@ fun Route.configureUserPerms() {
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
}
val (user, permission) = call.receivePermission() ?: return@delete
if (user.hasPerm(permission)) {
UserPerm.removePerm(user, permission)
@@ -151,4 +110,21 @@ fun Route.configureUserPerms() {
}
}
}
}
suspend fun ApplicationCall.receivePermission(fieldName: String = "perm", isPrefix: Boolean = false): Pair<SteamwarUser, UserPerm>? {
val user = request.getUser()
if (user == null) {
respond(HttpStatusCode.BadRequest)
return null
}
val perm = parameters[fieldName]
val permission = UserPerm.entries.find { it.name == perm }
if (perm == null || perm.startsWith("PREFIX_") == isPrefix || permission == null) {
respond(HttpStatusCode.BadRequest)
return null
}
return user to permission
}