From bd89369b508f30fc987b27cf6b34a22fbc8e5d97 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Sun, 5 May 2019 22:27:42 +0200 Subject: Added simple admin interface Co-authored-by: LarsVomMars --- src/main/kotlin/App.kt | 26 ++++++++++++++++++++----- src/main/kotlin/DatabaseController.kt | 15 ++++++++------ src/main/kotlin/UserHandler.kt | 28 ++++++++++++++++++++++++--- src/main/resources/views/admin.rocker.html | 19 ++++++++++++++++++ src/main/resources/views/index.rocker.html | 4 ++++ src/main/resources/views/register.rocker.html | 4 ++-- 6 files changed, 80 insertions(+), 16 deletions(-) create mode 100644 src/main/resources/views/admin.rocker.html (limited to 'src/main') diff --git a/src/main/kotlin/App.kt b/src/main/kotlin/App.kt index 5370f4b..c403b5a 100644 --- a/src/main/kotlin/App.kt +++ b/src/main/kotlin/App.kt @@ -44,20 +44,32 @@ fun main(args: Array) { get( "/css/*", { ctx -> ctx.contentType("text/css") - ctx.result(Thread.currentThread().contextClassLoader.getResourceAsStream("css/" + ctx.splat(0))) + try { + ctx.result(Thread.currentThread().contextClassLoader.getResourceAsStream("css/" + ctx.splat(0))) + } catch (_: Exception) { + ctx.status(404) + } }, roles(Roles.GUEST, Roles.USER) ) get( "/js/*", { ctx -> ctx.contentType("text/javascript") - ctx.result(Thread.currentThread().contextClassLoader.getResourceAsStream("js/" + ctx.splat(0))) + try { + ctx.result(Thread.currentThread().contextClassLoader.getResourceAsStream("js/" + ctx.splat(0))) + } catch (_: Exception) { + ctx.status(404) + } }, roles(Roles.GUEST, Roles.USER) ) get( "/fonts/*", { ctx -> - ctx.result(Thread.currentThread().contextClassLoader.getResourceAsStream("fonts/" + ctx.splat(0))) + try { + ctx.result(Thread.currentThread().contextClassLoader.getResourceAsStream("fonts/" + ctx.splat(0))) + } catch (_: Exception) { + ctx.status(404) + } }, roles(Roles.GUEST, Roles.USER) ) @@ -99,9 +111,13 @@ fun main(args: Array) { /** * Adds part of a new user (username) to database - * TODO: Create post request with admin interface */ - get("/user/add", databaseController::indexUserRegistration, roles(Roles.ADMIN)) + post("/user/add", databaseController::indexUserRegistration, roles(Roles.ADMIN)) + + /** + * Renders the admin interface + */ + get("/admin", userHandler::renderAdmin, roles(Roles.ADMIN)) /** * Renders the setup page (only on initial use) diff --git a/src/main/kotlin/DatabaseController.kt b/src/main/kotlin/DatabaseController.kt index 4b444ef..541bd06 100644 --- a/src/main/kotlin/DatabaseController.kt +++ b/src/main/kotlin/DatabaseController.kt @@ -2,6 +2,7 @@ package space.anity import at.favre.lib.crypto.bcrypt.* import io.javalin.* +import io.javalin.rendering.template.TemplateUtil.model import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.transactions.* import org.joda.time.* @@ -141,7 +142,7 @@ class DatabaseController(dbFileLocation: String = "main.db") { * Adds a user to the registration table */ fun indexUserRegistration(ctx: Context) { - val usernameString = ctx.queryParam("username", "").toString() + val usernameString = ctx.formParam("username", "").toString() val tokenString = generateRandomString() var error = false @@ -156,9 +157,11 @@ class DatabaseController(dbFileLocation: String = "main.db") { } } - if (error) ctx.result("User already exists") - else ctx.result( - "Registration url: " + "http://${ctx.host()}/user/register?username=$usernameString&token=$tokenString" + if (error) ctx.render("admin.rocker.html", model("message", "User already exists!")) + else ctx.render( + "admin.rocker.html", model( + "message", "http://${ctx.host()}/user/register?username=$usernameString&token=$tokenString" + ) ) } @@ -288,8 +291,8 @@ class DatabaseController(dbFileLocation: String = "main.db") { false } } catch (_: Exception) { - if (!isDirectoryBool) log.warning("File already exists!") - false + if (!isDirectoryBool) log.warning("Error during indexing of the file!") + true // Ugly solution } } } diff --git a/src/main/kotlin/UserHandler.kt b/src/main/kotlin/UserHandler.kt index 1a81812..42c4fcc 100644 --- a/src/main/kotlin/UserHandler.kt +++ b/src/main/kotlin/UserHandler.kt @@ -80,6 +80,13 @@ class UserHandler { ctx.redirect("/") } + /** + * Renders the admin interface + */ + fun renderAdmin(ctx: Context) { + ctx.render("admin.rocker.html", model("message", "")) + } + /** * Renders the setup page */ @@ -138,10 +145,16 @@ class UserHandler { databaseController.createUser(username, password, "USER") databaseController.removeRegistrationIndex(username) ctx.redirect("/user/login") - } else ctx.status(401).result("This user is not authorized to register.") - } else ctx.status(400).result("The passwords don't match!") + } else ctx.render( + "register.rocker.html", + model("username", username, "token", token, "message", "Not authorized!") + ) + } else ctx.render( + "register.rocker.html", + model("username", username, "token", token, "message", "The passwords don't match!") + ) } catch (_: Exception) { - ctx.status(400).result("An exception occured.") + ctx.status(400).result("An exception occurred.") } } @@ -154,4 +167,13 @@ class UserHandler { ) ctx.cookieStore("userId") else -1 } + + /** + * Checks whether a user has admin privileges + */ + fun isAdmin(usernameString: String): Boolean { + val userId = databaseController.getUserId(usernameString) + return if (userId > 0) databaseController.getRoles(userId).contains(Roles.ADMIN) + else false + } } diff --git a/src/main/resources/views/admin.rocker.html b/src/main/resources/views/admin.rocker.html new file mode 100644 index 0000000..26859eb --- /dev/null +++ b/src/main/resources/views/admin.rocker.html @@ -0,0 +1,19 @@ +@args (String message) + +@layout.template("Index", RockerContent.NONE, RockerContent.NONE) -> { +
+
+

Add new user

+
+ + + +
+
+
+ @if (message.length() > 0) { + @message + } +
+
+} diff --git a/src/main/resources/views/index.rocker.html b/src/main/resources/views/index.rocker.html index 98a659e..cc18d3b 100644 --- a/src/main/resources/views/index.rocker.html +++ b/src/main/resources/views/index.rocker.html @@ -1,4 +1,5 @@ @import space.anity.DatabaseController +@import space.anity.UserHandler @args (String username) @layout.template("Index", RockerContent.NONE, RockerContent.NONE) -> { @@ -8,6 +9,9 @@
@if(username.length() > 0) { Logout + @if((new UserHandler()).isAdmin(username)) { + Admin + } } else if (!(new DatabaseController()).isSetup()) { Setup } else { diff --git a/src/main/resources/views/register.rocker.html b/src/main/resources/views/register.rocker.html index af3d127..131f406 100644 --- a/src/main/resources/views/register.rocker.html +++ b/src/main/resources/views/register.rocker.html @@ -12,11 +12,11 @@
- +
- +
-- cgit v1.2.3