diff options
author | Marvin Borner | 2019-04-13 19:59:48 +0200 |
---|---|---|
committer | Marvin Borner | 2019-04-13 19:59:48 +0200 |
commit | 9e91d7e8cd3a3cdfee7f4d10347f48980eeaae93 (patch) | |
tree | 3b6e129153a83a88b9f1f85ae35bfe711bced3c5 /src/main/kotlin/DatabaseController.kt | |
parent | 407cd889cada0154faaa06ff4372e237cf260cf7 (diff) |
Added support for multiple roles per user
Diffstat (limited to 'src/main/kotlin/DatabaseController.kt')
-rw-r--r-- | src/main/kotlin/DatabaseController.kt | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/src/main/kotlin/DatabaseController.kt b/src/main/kotlin/DatabaseController.kt index 4788057..8b82093 100644 --- a/src/main/kotlin/DatabaseController.kt +++ b/src/main/kotlin/DatabaseController.kt @@ -33,7 +33,6 @@ class DatabaseController(dbFileLocation: String = "main.db") { /** * Database table indexing the users with their regarding role (multi line per user) - * TODO: Add support for multiple roles per user (read, write, edit, etc) */ object UserRoles : Table() { val id = integer("id").autoIncrement().primaryKey() @@ -166,15 +165,32 @@ class DatabaseController(dbFileLocation: String = "main.db") { /** * Returns the corresponding role using [usernameString] */ - fun getRole(usernameString: String): Roles { + fun getRoles(usernameString: String): List<Roles> { return transaction { try { val userId = UserData.select { UserData.username eq usernameString }.map { it[UserData.id] }[0] val userRoleId = UserRoles.select { UserRoles.userId eq userId }.map { it[UserRoles.roleId] }[0] - val userRole = RolesData.select { RolesData.id eq userRoleId }.map { it[RolesData.role] }[0] - if (userRole == "ADMIN") Roles.ADMIN else Roles.USER + + val userRoles = mutableListOf<Roles>() + RolesData.select { RolesData.id eq userRoleId }.map { it[RolesData.role] }.forEach { + when (it) { + "GUEST" -> { + userRoles.add(Roles.GUEST) + } + "USER" -> { + userRoles.add(Roles.GUEST) + userRoles.add(Roles.USER) + } + "ADMIN" -> { + userRoles.add(Roles.GUEST) + userRoles.add(Roles.USER) + userRoles.add(Roles.ADMIN) + } + } + } + userRoles } catch (_: Exception) { - Roles.GUEST + listOf(Roles.GUEST) } } } |