diff options
author | Marvin Borner | 2019-04-18 22:08:26 +0200 |
---|---|---|
committer | Marvin Borner | 2019-04-18 22:08:26 +0200 |
commit | 551868000825ebbe037b1be494679b24c2f0fcac (patch) | |
tree | d280dcd86b3417a7bf3fcfc6b74d5c44484b4f77 | |
parent | 8c85eef4dd56b6a2d4a2563c5634dc2c60a35370 (diff) |
Added better setup process redirection
Co-authored-by: LarsVomMars <lars@kroenner.eu>
-rw-r--r-- | src/main/kotlin/App.kt | 4 | ||||
-rw-r--r-- | src/main/kotlin/DatabaseController.kt | 8 | ||||
-rw-r--r-- | src/main/kotlin/UserHandler.kt | 37 | ||||
-rw-r--r-- | src/main/resources/views/index.rocker.html | 3 |
4 files changed, 25 insertions, 27 deletions
diff --git a/src/main/kotlin/App.kt b/src/main/kotlin/App.kt index 3967d80..f081bea 100644 --- a/src/main/kotlin/App.kt +++ b/src/main/kotlin/App.kt @@ -56,7 +56,7 @@ fun main() { * Renders the login page */ get("/login", { ctx -> - if (userHandler.getVerifiedUserId(ctx) > 0) ctx.redirect("/") + if (userHandler.getVerifiedUserId(ctx) > 0 || !databaseController.isSetup()) ctx.redirect("/") else ctx.render( "login.rocker.html", model("message", "", "counter", 0) @@ -77,7 +77,7 @@ fun main() { * Renders the setup page (only on initial use) */ get("/setup", { ctx -> - if (databaseController.isSetup()) ctx.redirect("/") + if (databaseController.isSetup()) ctx.redirect("/login") else ctx.render( "setup.rocker.html", model("message", "") diff --git a/src/main/kotlin/DatabaseController.kt b/src/main/kotlin/DatabaseController.kt index c21b175..51179e8 100644 --- a/src/main/kotlin/DatabaseController.kt +++ b/src/main/kotlin/DatabaseController.kt @@ -185,15 +185,15 @@ class DatabaseController(dbFileLocation: String = "main.db") { val userRoles = mutableListOf<Roles>() RolesData.select { RolesData.id eq userRoleId }.map { it[RolesData.role] }.forEach { - when (it) { - "GUEST" -> { + when (Roles.valueOf(it)) { + Roles.GUEST -> { userRoles.add(Roles.GUEST) } - "USER" -> { + Roles.USER -> { userRoles.add(Roles.GUEST) userRoles.add(Roles.USER) } - "ADMIN" -> { + Roles.ADMIN -> { userRoles.add(Roles.GUEST) userRoles.add(Roles.USER) userRoles.add(Roles.ADMIN) diff --git a/src/main/kotlin/UserHandler.kt b/src/main/kotlin/UserHandler.kt index 8197427..cc9a768 100644 --- a/src/main/kotlin/UserHandler.kt +++ b/src/main/kotlin/UserHandler.kt @@ -10,7 +10,7 @@ class UserHandler { * Checks and verifies users credentials and logs the user in */ fun login(ctx: Context) { - if (getVerifiedUserId(ctx) > 0) ctx.redirect("/") + if (getVerifiedUserId(ctx) > 0 || !databaseController.isSetup()) ctx.redirect("/") val username = ctx.formParam("username").toString() val password = ctx.formParam("password").toString() @@ -71,29 +71,24 @@ class UserHandler { * Sets up the general settings and admin credentials */ fun setup(ctx: Context) { - if (databaseController.isSetup()) ctx.render( - "setup.rocker.html", - TemplateUtil.model("message", "Setup process already finished!") - ) else { - try { - val username = ctx.formParam("username").toString() - val password = ctx.formParam("password").toString() - val verifyPassword = ctx.formParam("verifyPassword").toString() - if (password == verifyPassword) { - if (databaseController.createUser(username, password, "ADMIN")) { - databaseController.toggleSetup() - ctx.render("setup.rocker.html", TemplateUtil.model("message", "Setup succeeded!")) - } else ctx.status(400).render( - "setup.rocker.html", - TemplateUtil.model("message", "User already exists!") - ) + try { + val username = ctx.formParam("username").toString() + val password = ctx.formParam("password").toString() + val verifyPassword = ctx.formParam("verifyPassword").toString() + if (password == verifyPassword) { + if (databaseController.createUser(username, password, "ADMIN")) { + databaseController.toggleSetup() + ctx.redirect("/login") } else ctx.status(400).render( "setup.rocker.html", - TemplateUtil.model("message", "Passwords do not match!") + TemplateUtil.model("message", "User already exists!") ) - } catch (_: Exception) { - ctx.status(400).render("setup.rocker.html", TemplateUtil.model("message", "An error occurred!")) - } + } else ctx.status(400).render( + "setup.rocker.html", + TemplateUtil.model("message", "Passwords do not match!") + ) + } catch (_: Exception) { + ctx.status(400).render("setup.rocker.html", TemplateUtil.model("message", "An error occurred!")) } } diff --git a/src/main/resources/views/index.rocker.html b/src/main/resources/views/index.rocker.html index 74e8e0b..cab6712 100644 --- a/src/main/resources/views/index.rocker.html +++ b/src/main/resources/views/index.rocker.html @@ -1,3 +1,4 @@ +@import space.anity.DatabaseController @args (String username) @layout.template("Index", RockerContent.NONE, RockerContent.NONE) -> { @@ -7,6 +8,8 @@ <div> @if(username.length() > 0) { <a class="button" href="/logout">Logout</a> + } else if (!(new DatabaseController()).isSetup()) { + <a class="button" href="/setup">Setup</a> } else { <a class="button" href="/login">Login</a> } |