Rework admin panel
authorTheSaminator <thesaminator@users.noreply.github.com>
Wed, 6 Jul 2022 15:29:16 +0000 (11:29 -0400)
committerTheSaminator <thesaminator@users.noreply.github.com>
Wed, 6 Jul 2022 15:29:16 +0000 (11:29 -0400)
src/jvmMain/kotlin/net/starshipfights/admin/endpoints_admin.kt

index 7a124bd1ce9099c84bf64d1b5415bec30bec3d36..83b1dcf46072f5b3b5782d16954d91f5a54356b5 100644 (file)
@@ -9,10 +9,8 @@ import io.ktor.util.*
 import kotlinx.coroutines.Job
 import kotlinx.html.*
 import net.starshipfights.auth.getUser
-import net.starshipfights.auth.getUserAndSession
 import net.starshipfights.auth.receiveValidatedParameters
 import net.starshipfights.forbid
-import net.starshipfights.info.csrfToken
 import net.starshipfights.info.page
 import net.starshipfights.info.standardNavBar
 import net.starshipfights.redirect
@@ -21,14 +19,9 @@ private val shutDown = Job()
 
 fun Routing.installAdmin() {
        get("/admin") {
-               val (sess, user) = call.getUserAndSession()
-               
-               if (!user.isAdmin)
+               if (!call.getUser().isAdmin)
                        forbid()
                
-               sess ?: redirect("/login")
-               user ?: redirect("/login")
-               
                call.respondHtml(HttpStatusCode.OK, call.page("Admin Panel", call.standardNavBar()) {
                        section {
                                h1 { +"Admin Panel" }
@@ -40,24 +33,22 @@ fun Routing.installAdmin() {
                                                name = "announcement"
                                                required = true
                                        }
-                                       csrfToken(sess.id)
                                        submitInput {
                                                value = "Announce"
                                        }
                                }
                        }
+                       section {
+                               h2 { +"Server Shutdown" }
+                               form(action = "/admin/shutdown", method = FormMethod.post) {
+                                       submitInput(classes = "evil") {
+                                               value = "Shutdown the Server"
+                                       }
+                               }
+                       }
                })
        }
        
-       get("/admin/shutdown") {
-               if (!call.getUser().isAdmin)
-                       forbid()
-               
-               shutDown.complete()
-               
-               call.respond(HttpStatusCode.Gone)
-       }
-       
        post("/admin/announce") {
                val user = call.getUser()
                user ?: redirect("/login")
@@ -70,6 +61,15 @@ fun Routing.installAdmin() {
                sendAdminAnnouncement(announcement)
                redirect("/admin")
        }
+       
+       post("/admin/shutdown") {
+               if (!call.getUser().isAdmin)
+                       forbid()
+               
+               shutDown.complete()
+               
+               call.respond(HttpStatusCode.Gone)
+       }
 }
 
 suspend fun awaitShutDown() = shutDown.join()