diff options
-rw-r--r-- | src/main/kotlin/App.kt | 40 | ||||
-rw-r--r-- | src/main/kotlin/DatabaseController.kt | 11 | ||||
-rw-r--r-- | src/main/kotlin/UserHandler.kt | 69 |
3 files changed, 59 insertions, 61 deletions
diff --git a/src/main/kotlin/App.kt b/src/main/kotlin/App.kt index a8c1b03..95dfdca 100644 --- a/src/main/kotlin/App.kt +++ b/src/main/kotlin/App.kt @@ -65,27 +65,17 @@ fun main(args: Array<String>) { /** * Main page */ - get( - "/", - { ctx -> - ctx.render( - "index.rocker.html", - model("username", databaseController.getUsername(userHandler.getVerifiedUserId(ctx))) - ) - }, - roles(Roles.GUEST) - ) + get("/", { ctx -> + ctx.render( + "index.rocker.html", + model("username", databaseController.getUsername(userHandler.getVerifiedUserId(ctx))) + ) + }, roles(Roles.GUEST)) /** * Renders the login page */ - get("/user/login", { ctx -> - if (userHandler.getVerifiedUserId(ctx) > 0 || !databaseController.isSetup()) ctx.redirect("/") - else ctx.render( - "login.rocker.html", - model("message", "", "counter", 0) - ) - }, roles(Roles.GUEST)) + get("/user/login", userHandler::renderLogin, roles(Roles.GUEST)) /** * Endpoint for user authentication @@ -100,7 +90,7 @@ fun main(args: Array<String>) { /** * Renders the registration page */ - get("/user/register", userHandler::renderRegistration, roles(Roles.GUEST)) // use setup page with additional parameter? + get("/user/register", userHandler::renderRegistration, roles(Roles.GUEST)) /** * Registers new user @@ -110,18 +100,16 @@ fun main(args: Array<String>) { /** * Adds part of a new user (username) to database */ - get("/user/add", databaseController::indexUserRegistration, roles(Roles.ADMIN)) // TODO: Create post request with admin interface + get( + "/user/add", + databaseController::indexUserRegistration, + roles(Roles.ADMIN) + ) // TODO: Create post request with admin interface /** * Renders the setup page (only on initial use) */ - get("/setup", { ctx -> - if (databaseController.isSetup()) ctx.redirect("/user/login") - else ctx.render( - "setup.rocker.html", - model("message", "") - ) - }, roles(Roles.GUEST)) + get("/setup", userHandler::renderSetup, roles(Roles.GUEST)) /** * Endpoint for setup (only on initial use) diff --git a/src/main/kotlin/DatabaseController.kt b/src/main/kotlin/DatabaseController.kt index d68bea0..8c0550a 100644 --- a/src/main/kotlin/DatabaseController.kt +++ b/src/main/kotlin/DatabaseController.kt @@ -127,8 +127,7 @@ class DatabaseController(dbFileLocation: String = "main.db") { return transaction { try { if (UserData.select { UserData.username eq usernameString }.empty()) { - val username = UserRegistration.select { UserRegistration.username eq usernameString }.map { it[UserRegistration.username] }[0] - username == usernameString + usernameString == UserRegistration.select { UserRegistration.username eq usernameString }.map { it[UserRegistration.username] }[0] } else false } catch (_: Exception) { false @@ -317,9 +316,13 @@ class DatabaseController(dbFileLocation: String = "main.db") { return transaction { try { val fileData = - FileLocation.select { FileLocation.accessId eq accessId }.map { it[FileLocation.path] to it[FileLocation.userId] to it[FileLocation.isShared] }[0] + FileLocation.select { + FileLocation.accessId eq accessId + }.map { it[FileLocation.path] to it[FileLocation.userId] to it[FileLocation.isShared] }[0] if (fileData.second) - FileLocation.select { (FileLocation.path eq "${fileData.first.first}${filename.substring(1)}") and (FileLocation.userId eq fileData.first.second) }.map { it[FileLocation.accessId] }[0] + FileLocation.select { + (FileLocation.path eq "${fileData.first.first}${filename.substring(1)}") and (FileLocation.userId eq fileData.first.second) + }.map { it[FileLocation.accessId] }[0] else "" } catch (_: Exception) { "" diff --git a/src/main/kotlin/UserHandler.kt b/src/main/kotlin/UserHandler.kt index 3811fbe..a950860 100644 --- a/src/main/kotlin/UserHandler.kt +++ b/src/main/kotlin/UserHandler.kt @@ -8,6 +8,15 @@ import kotlin.math.* class UserHandler { private val log = Logger.getLogger(this.javaClass.name) + + /** + * Renders the login page + */ + fun renderLogin(ctx: Context) { + if (userHandler.getVerifiedUserId(ctx) > 0 || !databaseController.isSetup()) ctx.redirect("/") + else ctx.render("login.rocker.html", model("message", "", "counter", 0)) + } + /** * Checks and verifies users credentials and logs the user in */ @@ -20,10 +29,11 @@ class UserHandler { val loginAttempts = databaseController.getLoginAttempts(requestIp) val lastAttemptDifference = - if (loginAttempts.isEmpty()) - -1 - else Interval(loginAttempts[loginAttempts.indexOfLast { true }].first.toInstant(), Instant()).toDuration() - .standardSeconds.toInt() + if (loginAttempts.isEmpty()) -1 + else Interval( + loginAttempts[loginAttempts.indexOfLast { true }].first.toInstant(), + Instant() + ).toDuration().standardSeconds.toInt() var lastHourAttempts = 0 loginAttempts.forEach { @@ -71,6 +81,14 @@ class UserHandler { } /** + * Renders the setup page + */ + fun renderSetup(ctx: Context) { + if (databaseController.isSetup()) ctx.redirect("/user/login") + else ctx.render("setup.rocker.html", model("message", "")) + } + + /** * Sets up the general settings and admin credentials */ fun setup(ctx: Context) { @@ -82,44 +100,23 @@ class UserHandler { if (databaseController.createUser(username, password, "ADMIN")) { databaseController.toggleSetup() ctx.redirect("/user/login") - } else ctx.status(400).render( - "setup.rocker.html", - model("message", "User already exists!") - ) - } else ctx.status(400).render( - "setup.rocker.html", - model("message", "Passwords do not match!") - ) + } else ctx.status(400).render("setup.rocker.html", model("message", "User already exists!")) + } else ctx.status(400).render("setup.rocker.html", model("message", "Passwords do not match!")) } catch (_: Exception) { ctx.status(400).render("setup.rocker.html", model("message", "An error occurred!")) } } /** - * Gets the username and verifies its identity - */ - fun getVerifiedUserId(ctx: Context): Int { - return if (databaseController.getUserIdByVerificationId(ctx.cookieStore("verification") ?: "verification") - == ctx.cookieStore("userId") ?: "userId" - ) ctx.cookieStore("userId") - else -1 - } - - /** * Renders the registration page */ fun renderRegistration(ctx: Context) { val username = ctx.queryParam("username", "") - if (username.isNullOrEmpty()) - ctx.status(403).result("Please provide a valid username!") + if (username.isNullOrEmpty()) ctx.status(403).result("Please provide a valid username!") else { - if (databaseController.isUserRegistrationValid(username)) ctx.render( - "register.rocker.html", - model( - "username", username, - "message", "" - ) - ) else ctx.redirect("/user/login") + if (databaseController.isUserRegistrationValid(username)) + ctx.render("register.rocker.html", model("username", username, "message", "")) + else ctx.redirect("/user/login") } } @@ -143,4 +140,14 @@ class UserHandler { ctx.status(400).result("An exception occured.") } } + + /** + * Gets the username and verifies its identity + */ + fun getVerifiedUserId(ctx: Context): Int { + return if (databaseController.getUserIdByVerificationId(ctx.cookieStore("verification") ?: "verification") + == ctx.cookieStore("userId") ?: "userId" + ) ctx.cookieStore("userId") + else -1 + } } |