aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/DatabaseController.kt
diff options
context:
space:
mode:
authorMarvin Borner2019-04-04 22:16:58 +0200
committerMarvin Borner2019-04-04 22:16:58 +0200
commit40191331c2fe5f14abcb319669808273c5125e8d (patch)
tree61bb70e14edd78f4a254bbd48c5bc8d81ba58f13 /src/main/kotlin/DatabaseController.kt
parent939834914e32464f66f0bfa30473263d96941715 (diff)
Added role and user feature
Diffstat (limited to 'src/main/kotlin/DatabaseController.kt')
-rw-r--r--src/main/kotlin/DatabaseController.kt25
1 files changed, 19 insertions, 6 deletions
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<Pair<String, Roles>> {
+ 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
}