From 40191331c2fe5f14abcb319669808273c5125e8d Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Thu, 4 Apr 2019 22:16:58 +0200 Subject: Added role and user feature --- src/main/kotlin/DatabaseController.kt | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'src/main/kotlin/DatabaseController.kt') diff --git a/src/main/kotlin/DatabaseController.kt b/src/main/kotlin/DatabaseController.kt index d118b97..64acc6c 100644 --- a/src/main/kotlin/DatabaseController.kt +++ b/src/main/kotlin/DatabaseController.kt @@ -23,6 +23,7 @@ class DatabaseController(dbFileLocation: String = "main.db") { // val id = integer("id").autoIncrement().primaryKey() val username = varchar("username", 24).primaryKey() // remove .primaryKey(), if id column is used val password = varchar("password", 64) + val role = varchar("role", 64).default("USER") } /** @@ -43,11 +44,25 @@ class DatabaseController(dbFileLocation: String = "main.db") { } } - fun createUser(uname :String, passwordHash :String) { + fun createUser(usernameString: String, passwordHash: String, roleString: String) { transaction { - UserData.insert { - it[username] = uname - it[password] = passwordHash + try { + UserData.insert { + it[username] = usernameString + it[password] = passwordHash + it[role] = roleString + } + } catch (_: org.jetbrains.exposed.exceptions.ExposedSQLException) { + println("User already exists") + } + + } + } + + fun getUser(usernameString: String): List> { + return transaction { + return@transaction UserData.select { UserData.username eq usernameString }.map { + it[UserData.username] to (if (it[UserData.role] == "ADMIN") Roles.ADMIN else Roles.USER) } } } @@ -67,7 +82,5 @@ class DatabaseController(dbFileLocation: String = "main.db") { */ - - // TODO add functions for database usage } -- cgit v1.2.3