aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/DatabaseController.kt
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin/DatabaseController.kt')
-rw-r--r--src/main/kotlin/DatabaseController.kt44
1 files changed, 44 insertions, 0 deletions
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.*
@@ -43,6 +44,14 @@ class DatabaseController(dbFileLocation: String = "main.db") {
}
/**
+ * 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
*/
object RolesData : Table() {
@@ -78,6 +87,7 @@ class DatabaseController(dbFileLocation: String = "main.db") {
FileLocation,
UserData,
UserRoles,
+ UserRegistration,
RolesData,
LoginAttempts,
General
@@ -110,6 +120,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
*/
fun checkUser(usernameString: String, passwordString: String): Boolean {