diff options
author | Marvin Borner | 2019-04-07 13:01:13 +0200 |
---|---|---|
committer | Marvin Borner | 2019-04-07 13:01:13 +0200 |
commit | ea6d0b7176fb2133f170421666f410dfce20ca31 (patch) | |
tree | 34ef84d06ef6f1687c6f09b3cd7bb187120c3579 | |
parent | 05f73150bc060dc9dc52e34da6323a38c433e118 (diff) |
Updated documentation
-rw-r--r-- | src/main/kotlin/App.kt | 11 | ||||
-rw-r--r-- | src/main/kotlin/DatabaseController.kt | 41 |
2 files changed, 23 insertions, 29 deletions
diff --git a/src/main/kotlin/App.kt b/src/main/kotlin/App.kt index f080357..34bdc4b 100644 --- a/src/main/kotlin/App.kt +++ b/src/main/kotlin/App.kt @@ -16,7 +16,7 @@ import java.util.logging.* const val fileHome = "files" val databaseController = DatabaseController() -val LOG = Logger.getLogger("App.kt") +private val log = Logger.getLogger("App.kt") fun main() { val app = Javalin.create() @@ -30,8 +30,7 @@ fun main() { FileRenderer { filepath, model -> Rocker.template(filepath).bind(model).render().toString() }, ".rocker.html" ) - - // db test + // Only for testing purposes databaseController.createUser("melvin", "supersecure", "ADMIN") app.routes { @@ -54,6 +53,9 @@ fun main() { } } +/** + * Sets up the roles with the database and declares the handling of roles + */ fun setupRoles(handler: Handler, ctx: Context, permittedRoles: Set<Role>) { val userRole = databaseController.getUser("melvin")[0].second when { @@ -106,6 +108,9 @@ fun upload(ctx: Context) { } } +/** + * Declares the roles in which a user can be in + */ enum class Roles : Role { ADMIN, USER, GUEST } diff --git a/src/main/kotlin/DatabaseController.kt b/src/main/kotlin/DatabaseController.kt index 32e33ef..86d20a8 100644 --- a/src/main/kotlin/DatabaseController.kt +++ b/src/main/kotlin/DatabaseController.kt @@ -6,8 +6,8 @@ import java.sql.* import java.util.logging.* class DatabaseController(dbFileLocation: String = "main.db") { - val db: Database - val LOG = Logger.getLogger(this.javaClass.name) + val db: Database = Database.connect("jdbc:sqlite:$dbFileLocation", "org.sqlite.JDBC") + private val log = Logger.getLogger(this.javaClass.name) /** * Database table for the file location indexing @@ -21,9 +21,8 @@ class DatabaseController(dbFileLocation: String = "main.db") { * Database table to index the users with their regarding passwords */ object UserData : Table() { - // only for multiple users: - // val id = integer("id").autoIncrement().primaryKey() - val username = varchar("username", 24).primaryKey() // remove .primaryKey(), if id column is used + val id = integer("id").autoIncrement().primaryKey() + val username = varchar("username", 24) val password = varchar("password", 64) val role = varchar("role", 64).default("USER") } @@ -32,20 +31,22 @@ class DatabaseController(dbFileLocation: String = "main.db") { * Database table storing general data/states */ object General : Table() { - val initialUse = integer("initialUse").default(1).primaryKey() // boolean -> 0:1 + val initialUse = integer("initialUse").default(1).primaryKey() } init { - // create connection - this.db = Database.connect("jdbc:sqlite:$dbFileLocation", "org.sqlite.JDBC") + // Create connection TransactionManager.manager.defaultIsolationLevel = Connection.TRANSACTION_SERIALIZABLE - // add tables + // Add tables transaction { SchemaUtils.createMissingTablesAndColumns(FileLocation, UserData, General) } } + /** + * Creates the user in the database using username, password and the role + */ fun createUser(usernameString: String, passwordHash: String, roleString: String) { transaction { try { @@ -55,12 +56,15 @@ class DatabaseController(dbFileLocation: String = "main.db") { it[role] = roleString } } catch (_: org.jetbrains.exposed.exceptions.ExposedSQLException) { - LOG.warning("User already exists!") + log.warning("User already exists!") } } } + /** + * Returns a list of the username paired with the corresponding role using [usernameString] + */ fun getUser(usernameString: String): List<Pair<String, Roles>> { return transaction { return@transaction UserData.select { UserData.username eq usernameString }.map { @@ -69,20 +73,5 @@ class DatabaseController(dbFileLocation: String = "main.db") { } } - /* - fun selectUser(uname: String) :String { - var pwd :String - - transaction { - UserData.select{UserData.username eq uname}.forEach { - pwd = it[UserData.password] - } - } - - // return pwd - } - - */ - - // TODO add functions for database usage + // TODO: Add more functions for database interaction } |