Skip to content

Commit

Permalink
Add: register default group
Browse files Browse the repository at this point in the history
  • Loading branch information
vulcandragi committed Sep 13, 2024
1 parent 4230c8c commit b8a4724
Show file tree
Hide file tree
Showing 16 changed files with 190 additions and 37 deletions.
4 changes: 3 additions & 1 deletion src/main/kotlin/com/fardragi/nyaruko/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.fardragi.nyaruko.core.CoreModule
import com.fardragi.nyaruko.database.DatabaseConnection
import com.fardragi.nyaruko.permission.PermissionModule
import com.fardragi.nyaruko.services.CommandService
import com.fardragi.nyaruko.services.GroupService
import com.fardragi.nyaruko.services.SessionsService
import com.fardragi.nyaruko.services.UserService
import org.koin.core.module.dsl.createdAtStart
Expand All @@ -29,7 +30,8 @@ val appModule = module {
single { AuthModule() }
single { PermissionModule() }

single { UserService() }
single { UserService(get()) }
single { SessionsService() }
single { CommandService() }
single { GroupService() }
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.fardragi.nyaruko.database

import com.fardragi.nyaruko.config.DatabaseConfig
import com.fardragi.nyaruko.database.tables.Commands
import com.fardragi.nyaruko.database.tables.Users
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import com.fardragi.nyaruko.database.tables.*
import kotlinx.coroutines.runBlocking
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.SchemaUtils

Expand All @@ -18,9 +15,11 @@ class DatabaseConnection(config: DatabaseConfig) {
password = config.password
)

CoroutineScope(Dispatchers.IO).launch {
runBlocking {
query {
SchemaUtils.createMissingTablesAndColumns(Users, Commands)
SchemaUtils.createMissingTablesAndColumns(
Users, Commands, Groups, UserGroups, GroupCommands
)
}
}
}
Expand Down
15 changes: 8 additions & 7 deletions src/main/kotlin/com/fardragi/nyaruko/database/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@ package com.fardragi.nyaruko.database
import com.fardragi.nyaruko.NyarukoLog
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.jetbrains.exposed.sql.SqlLogger
import org.jetbrains.exposed.sql.Transaction
import org.jetbrains.exposed.sql.addLogger
import org.jetbrains.exposed.sql.statements.StatementContext
import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.transactions.experimental.withSuspendTransaction

object CustomLogger : SqlLogger {

override fun log(context: StatementContext, transaction: Transaction) {
NyarukoLog.info(context.sql(TransactionManager.current()))
}
}

suspend fun <T> query(dispatcher: CoroutineDispatcher = Dispatchers.IO, block: () -> T): T = withContext(dispatcher) {
transaction {
addLogger(CustomLogger)
block()
suspend fun <T> query(dispatcher: CoroutineDispatcher = Dispatchers.IO, block: suspend () -> T): T {
return newSuspendedTransaction(dispatcher) {
withSuspendTransaction {
addLogger(CustomLogger)
block()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import com.fardragi.nyaruko.enums.PermissionLevel
import org.jetbrains.exposed.dao.id.IdTable

object Commands : IdTable<UInt>("commands") {
override val id = uinteger("id").entityId().autoIncrement()
val name = varchar("name", 255)
override val id = uinteger("id").autoIncrement().entityId()
val name = varchar("name", 255).uniqueIndex()
val level = enumerationByName("level", 6, PermissionLevel::class)

override val primaryKey = PrimaryKey(id)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.fardragi.nyaruko.database.tables

import org.jetbrains.exposed.dao.id.IdTable

object GroupCommands : IdTable<UInt>("group_commands") {
override val id = uinteger("id").autoIncrement().entityId()
val groupId = reference("group_id", Groups)
val command = reference("command_id", Commands)
val permission = bool("permission").default(true)

override val primaryKey = PrimaryKey(id)

init {
uniqueIndex(groupId, command)
}
}
12 changes: 12 additions & 0 deletions src/main/kotlin/com/fardragi/nyaruko/database/tables/Groups.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.fardragi.nyaruko.database.tables

import org.jetbrains.exposed.dao.id.IdTable

object Groups : IdTable<UInt>("groups") {
override val id = uinteger("id").autoIncrement().entityId()
val name = varchar("name", 255).uniqueIndex()
val order = uinteger("order").uniqueIndex()
val default = bool("default").uniqueIndex().default(false)

override val primaryKey = PrimaryKey(id)
}
15 changes: 15 additions & 0 deletions src/main/kotlin/com/fardragi/nyaruko/database/tables/UserGroups.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.fardragi.nyaruko.database.tables

import org.jetbrains.exposed.dao.id.IdTable

object UserGroups : IdTable<UInt>("user_groups") {
override val id = uinteger("id").autoIncrement().entityId()
val userId = reference("user_id", Users)
val groupId = reference("group_id", Groups)

override val primaryKey = PrimaryKey(id)

init {
uniqueIndex(userId, groupId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import java.util.UUID

object Users : IdTable<UUID>("users") {
override val id = uuid("id").entityId()
val name = varchar("name", 50)
val name = varchar("name", 50).uniqueIndex()
val hash = varchar("hash", 128).nullable()

override val primaryKey = PrimaryKey(id)
Expand Down
14 changes: 14 additions & 0 deletions src/main/kotlin/com/fardragi/nyaruko/models/Group.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.fardragi.nyaruko.models

import com.fardragi.nyaruko.database.tables.Groups
import org.jetbrains.exposed.dao.Entity
import org.jetbrains.exposed.dao.EntityClass
import org.jetbrains.exposed.dao.id.EntityID

class Group(id: EntityID<UInt>) : Entity<UInt>(id) {
companion object : EntityClass<UInt, Group>(Groups)

var name by Groups.name
var order by Groups.order
var default by Groups.default
}
15 changes: 15 additions & 0 deletions src/main/kotlin/com/fardragi/nyaruko/models/UserGroup.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.fardragi.nyaruko.models

import com.fardragi.nyaruko.database.tables.UserGroups
import org.jetbrains.exposed.dao.Entity
import org.jetbrains.exposed.dao.EntityClass
import org.jetbrains.exposed.dao.id.EntityID

class UserGroup(id: EntityID<UInt>) : Entity<UInt>(id) {
companion object : EntityClass<UInt, UserGroup>(UserGroups)

var userId by UserGroups.userId
var groupId by UserGroups.groupId
var user by User referencedOn UserGroups.userId
var group by Group referencedOn UserGroups.groupId
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
package com.fardragi.nyaruko.permission

import com.fardragi.nyaruko.enums.PermissionLevel
import com.fardragi.nyaruko.services.CommandService
import com.fardragi.nyaruko.permission.handlers.RegisterPermissionsHandler
import com.fardragi.nyaruko.shared.IModule
import net.minecraft.command.CommandBase
import net.minecraft.server.MinecraftServer
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

class PermissionModule() : IModule, KoinComponent {
private val commandService: CommandService by inject()

class PermissionModule() : IModule {
override suspend fun start() {
val commands = MinecraftServer.getServer().commandManager.commands.mapValues { (_, v) ->
return@mapValues if (v is CommandBase)
PermissionLevel.fromLevel(v.requiredPermissionLevel)
else PermissionLevel.Op
}

commandService.registerCommands(commands as HashMap<String, PermissionLevel>)
RegisterPermissionsHandler().register()
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.fardragi.nyaruko.permission.handlers

import com.fardragi.nyaruko.enums.PermissionLevel
import com.fardragi.nyaruko.services.CommandService
import com.fardragi.nyaruko.services.GroupService
import com.fardragi.nyaruko.shared.events.ServerStartingEvent
import com.fardragi.nyaruko.shared.handlers.NyarukoHandlerBase
import cpw.mods.fml.common.eventhandler.EventPriority
import cpw.mods.fml.common.eventhandler.SubscribeEvent
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import net.minecraft.command.CommandBase
import net.minecraft.server.MinecraftServer
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

class RegisterPermissionsHandler : NyarukoHandlerBase(), KoinComponent {
private val commandService: CommandService by inject()
private val groupService: GroupService by inject()

@SubscribeEvent(priority = EventPriority.HIGHEST)
fun onServerStart(event: ServerStartingEvent) {
val commands = MinecraftServer.getServer().commandManager.commands.mapValues { (_, v) ->
return@mapValues if (v is CommandBase)
PermissionLevel.fromLevel(v.requiredPermissionLevel)
else PermissionLevel.Op
}

CoroutineScope(Dispatchers.Default).launch {
groupService.checkDefault()
commandService.registerCommands(commands as HashMap<String, PermissionLevel>)
}
}
}
8 changes: 8 additions & 0 deletions src/main/kotlin/com/fardragi/nyaruko/server/ServerProxy.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.fardragi.nyaruko.server

import com.fardragi.nyaruko.NyarukoLog
import com.fardragi.nyaruko.appModule
import com.fardragi.nyaruko.auth.AuthModule
import com.fardragi.nyaruko.core.CoreModule
import com.fardragi.nyaruko.permission.PermissionModule
import com.fardragi.nyaruko.shared.IProxy
import com.fardragi.nyaruko.shared.events.ServerStartingEvent
import cpw.mods.fml.common.event.FMLInitializationEvent
import cpw.mods.fml.common.event.FMLPreInitializationEvent
import cpw.mods.fml.common.event.FMLServerStartingEvent
import kotlinx.coroutines.runBlocking
import org.koin.core.KoinApplication
import org.koin.core.context.startKoin
Expand All @@ -31,4 +34,9 @@ class ServerProxy : IProxy {
permissionModule.start()
}
}

override fun onServerStarting(event: FMLServerStartingEvent) {
NyarukoLog.info("Server starting")
ServerStartingEvent().send()
}
}
38 changes: 38 additions & 0 deletions src/main/kotlin/com/fardragi/nyaruko/services/GroupService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.fardragi.nyaruko.services

import com.fardragi.nyaruko.database.query
import com.fardragi.nyaruko.database.tables.Groups
import com.fardragi.nyaruko.exceptions.NotFoundException
import com.fardragi.nyaruko.models.Group
import com.fardragi.nyaruko.models.User
import com.fardragi.nyaruko.models.UserGroup

class GroupService {
suspend fun checkDefault() {
query {
val group = Group.find { Groups.default eq true }.firstOrNull()

if (group == null) {
Group.new {
name = "default"
order = Group.all().maxByOrNull { Groups.order }?.order?.plus(1u) ?: 0u
default = true
}
}
}
}

suspend fun registerUserInDefaultGroup(user: User) {
query {
val group = Group.find { Groups.default eq true }.firstOrNull() ?: throw NotFoundException(
Group::class.simpleName,
"default"
)

UserGroup.new {
userId = user.id
groupId = group.id
}
}
}
}
13 changes: 10 additions & 3 deletions src/main/kotlin/com/fardragi/nyaruko/services/UserService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,27 @@ import com.fardragi.nyaruko.exceptions.NotFoundException
import com.fardragi.nyaruko.models.User
import java.util.UUID

class UserService() {
class UserService(private val groupService: GroupService) {
suspend fun getOrCreateUser(id: UUID, name: String): User {
return query {
var isNew = false
val user = query {
var user = User.findById(id)

if (user == null) {
user = User.new(id) {

this.name = name
}
isNew = true
}

user
}

if (isNew) {
groupService.registerUserInDefaultGroup(user)
}

return user
}

suspend fun setPassword(id: UUID, password: String) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.fardragi.nyaruko.shared.events

class ServerStartingEvent() : NyarukoEvent()

0 comments on commit b8a4724

Please sign in to comment.