diff options
author | Marvin Borner | 2019-05-05 22:27:42 +0200 |
---|---|---|
committer | Marvin Borner | 2019-05-05 22:27:42 +0200 |
commit | bd89369b508f30fc987b27cf6b34a22fbc8e5d97 (patch) | |
tree | 8bb338a1112b4dae52d9d6d2919be4ce3d9544f9 | |
parent | 5081b240b07f08e21ad124da8bddf0e11471792d (diff) |
Added simple admin interface
Co-authored-by: LarsVomMars <lars@kroenner.eu>
-rw-r--r-- | src/main/kotlin/App.kt | 26 | ||||
-rw-r--r-- | src/main/kotlin/DatabaseController.kt | 15 | ||||
-rw-r--r-- | src/main/kotlin/UserHandler.kt | 28 | ||||
-rw-r--r-- | src/main/resources/views/admin.rocker.html | 19 | ||||
-rw-r--r-- | src/main/resources/views/index.rocker.html | 4 | ||||
-rw-r--r-- | src/main/resources/views/register.rocker.html | 4 |
6 files changed, 80 insertions, 16 deletions
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<String>) { 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<String>) { /** * 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 @@ -81,6 +81,13 @@ class UserHandler { } /** + * Renders the admin interface + */ + fun renderAdmin(ctx: Context) { + ctx.render("admin.rocker.html", model("message", "")) + } + + /** * Renders the setup page */ fun renderSetup(ctx: Context) { @@ -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) -> { +<div class="flex"> + <div> + <h1>Add new user</h1> + <form action="/user/add" method="post"> + <label for="username">Username:</label> + <input autocomplete="off" autofocus id="username" name="username" required type="text"/> + <button type="submit">Generate</button> + </form> + </div> + <div> + @if (message.length() > 0) { + <small>@message</small> + } + </div> +</div> +} 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 @@ <div> @if(username.length() > 0) { <a class="button" href="/user/logout">Logout</a> + @if((new UserHandler()).isAdmin(username)) { + <a class="button" href="/admin">Admin</a> + } } else if (!(new DatabaseController()).isSetup()) { <a class="button" href="/setup">Setup</a> } 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 @@ </div> <div> <label for="password">Password:</label> - <input id="password" name="password" required type="password"/> + <input autocomplete="off" autofocus id="password" name="password" required type="password"/> </div> <div> <label for="verifyPassword">Verify password:</label> - <input id="verifyPassword" name="verifyPassword" required type="password"/> + <input autocomplete="off" id="verifyPassword" name="verifyPassword" required type="password"/> </div> <div> |