From a6fc766d6eb808584c7cebea216f683b53f8e99b Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Thu, 2 May 2019 22:37:13 +0200 Subject: Added user registration via admin Co-authored-by: LarsVomMars --- src/main/kotlin/DatabaseController.kt | 44 +++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'src/main/kotlin/DatabaseController.kt') diff --git a/src/main/kotlin/DatabaseController.kt b/src/main/kotlin/DatabaseController.kt index cce9601..5ab0743 100644 --- a/src/main/kotlin/DatabaseController.kt +++ b/src/main/kotlin/DatabaseController.kt @@ -1,6 +1,7 @@ package space.anity import at.favre.lib.crypto.bcrypt.* +import io.javalin.* import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.transactions.* import org.joda.time.* @@ -42,6 +43,14 @@ class DatabaseController(dbFileLocation: String = "main.db") { val roleId = integer("role").references(RolesData.id) } + /** + * Database table indexing the soon-to-be registered users by username + */ + object UserRegistration : Table() { + val id = integer("id").autoIncrement().primaryKey() + val username = varchar("username", 24).uniqueIndex() + } + /** * Database table declaring available roles */ @@ -78,6 +87,7 @@ class DatabaseController(dbFileLocation: String = "main.db") { FileLocation, UserData, UserRoles, + UserRegistration, RolesData, LoginAttempts, General @@ -109,6 +119,40 @@ class DatabaseController(dbFileLocation: String = "main.db") { } } + /** + * Checks whether the user is allowed to register + * TODO: Verify registration via token + */ + fun isUserRegistrationValid(usernameString: String): Boolean { + 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 + } else false + } catch (err: Exception) { + false + } + } + } + + /** + * Adds a user to the registration table + */ + fun indexUserRegistration(ctx: Context) { + transaction { + UserRegistration.insert { + it[username] = ctx.queryParam("username", "").toString() + } + } + } + + fun removeRegistrationIndex(usernameString: String) { + transaction { + UserRegistration.deleteWhere { UserRegistration.username eq usernameString } + } + } + /** * Tests whether the password [passwordString] of the user [usernameString] is correct */ -- cgit v1.2.3