Skip to content

Commit

Permalink
添加谱面上传权限和控制台权限控制
Browse files Browse the repository at this point in the history
  • Loading branch information
Taskeren committed Feb 11, 2023
1 parent 39cea0a commit a249907
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ object Localization {

const val UserNotFound = "USER_NOT_FOUND"

const val PermissionDenied = "PERMISSION_DENIED"

const val UserAlreadyExists = "USER_ALREADY_EXISTS"

const val UserLoginFailed = "USER_LOGIN_FAILED"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package explode2.booster.bomb.submods

import explode2.booster.bomb.logger
import explode2.booster.bomb.superstarMarker
import explode2.gateau.GameRecord
import explode2.gateau.GameUser
import explode2.gateau.ScoreOrRanking
import explode2.gateau.SongSet
import explode2.gateau.*
import io.ktor.server.auth.*
import java.time.OffsetDateTime

Expand Down Expand Up @@ -42,6 +39,7 @@ object SuperstarPrincipal : BombPrincipal {
override fun getAllRecords(limit: Int, skip: Int): List<GameRecord> = emptyList()
override val omegaCount: Int = 0
override fun hasPermission(permissionKey: String): Boolean = false
override fun hasPermission(permission: Permission): Boolean = false
override fun grantPermission(permissionKey: String) {
logger.warn(superstarMarker, "Unexpected \"grantPermission($permissionKey)\" has been invoked!")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package explode2.booster.bomb.submods.extra

import explode2.booster.bomb.*
import explode2.booster.bomb.bombCall
import explode2.booster.bomb.respondData
import explode2.booster.bomb.submods.*
import explode2.booster.bomb.submods.BombPrincipal
import explode2.booster.bomb.submods.ExceptionContext
import explode2.booster.bomb.submods.chart.toBO
import explode2.booster.bomb.submods.toData
import explode2.booster.bomb.submods.toError
import explode2.booster.event.RouteConfigure
import explode2.gateau.*
import explode2.gateau.GameUser
import explode2.gateau.Permission
import explode2.gateau.SongChart
import explode2.gateau.SongState
import explode2.labyrinth.LabyrinthPlugin.Companion.labyrinth
import io.ktor.server.auth.*
import io.ktor.server.routing.*
Expand Down Expand Up @@ -38,6 +42,8 @@ internal val newSongModule: RouteConfigure = {
val noter_user: GameUser? = null
)

val permissionUpload = Permission.getOrCreate("bomb.upload")

post<NewSongPayload> {
val (
musicName,
Expand All @@ -57,6 +63,10 @@ internal val newSongModule: RouteConfigure = {
val auth = bombCall.principal<BombPrincipal>() ?: return@post bombCall.respondError(toError(Localization.UserLoginFailed))
val user = auth.user ?: return@post bombCall.respondError(toError(Localization.UserNotFound))

if(!user.hasPermission(permissionUpload)) {
return@post bombCall.respondError(toError(Localization.PermissionDenied))
}

logger.info("Received new song uploading request from ${user.username}(${user.id})")

// 记录异常,在返回时方便管理员排查处理
Expand Down
1 change: 1 addition & 0 deletions gateau/src/main/kotlin/explode2/gateau/GameUser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ interface GameUser {
val omegaCount: Int

fun hasPermission(permissionKey: String): Boolean
fun hasPermission(permission: Permission): Boolean

fun grantPermission(permissionKey: String)
fun revokePermission(permissionKey: String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.mojang.brigadier.CommandDispatcher
import com.mojang.brigadier.exceptions.CommandSyntaxException
import explode2.booster.BoosterPlugin
import explode2.booster.gatekeeper.command.addGenerateExampleModule
import explode2.booster.gatekeeper.command.addUserCommand
import explode2.labyrinth.LabyrinthPlugin
import net.minecrell.terminalconsole.SimpleTerminalConsole
import org.slf4j.Logger
Expand Down Expand Up @@ -51,6 +52,7 @@ class GatekeeperPlugin : BoosterPlugin {
}

addGenerateExampleModule()
addUserCommand()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package explode2.booster.gatekeeper.command

import cn.taskeren.brigadierx.argument
import cn.taskeren.brigadierx.executesX
import cn.taskeren.brigadierx.literal
import cn.taskeren.brigadierx.register
import com.mojang.brigadier.CommandDispatcher
import com.mojang.brigadier.arguments.StringArgumentType
import explode2.booster.gatekeeper.GatekeeperSource
import explode2.labyrinth.LabyrinthPlugin.Companion.labyrinth

fun CommandDispatcher<GatekeeperSource>.addUserCommand() = register("user") {
argument("name", StringArgumentType.string()) {
executesX {
val id = StringArgumentType.getString(it, "name")
val user = labyrinth.gameUserFactory.getGameUserByName(id)
if(user == null) {
println("User not found!")
} else {
println("User: ${user.id}")
}
}

literal("permission") {
argument("permission-key", StringArgumentType.string()) {
literal("get") {
executesX {
val id = StringArgumentType.getString(it, "name")
val user = labyrinth.gameUserFactory.getGameUserByName(id) ?: return@executesX println("User not found!")
val permKey = StringArgumentType.getString(it, "permission-key")
val hasPerm = user.hasPermission(permissionKey = permKey)
if(hasPerm) {
println("User has permission: $permKey")
} else {
println("User does not have permission: $permKey")
}
}
}

literal("grant") {
executesX {
val id = StringArgumentType.getString(it, "name")
val user = labyrinth.gameUserFactory.getGameUserByName(id) ?: return@executesX println("User not found!")
val permKey = StringArgumentType.getString(it, "permission-key")
user.grantPermission(permKey)
println("Granted user permission: $permKey")
}
}

literal("revoke") {
executesX {
val id = StringArgumentType.getString(it, "name")
val user = labyrinth.gameUserFactory.getGameUserByName(id) ?: return@executesX println("User not found!")
val permKey = StringArgumentType.getString(it, "permission-key")
user.revokePermission(permKey)
println("Granted user permission: $permKey")
}
}

literal("reset") {
executesX {
val id = StringArgumentType.getString(it, "name")
val user = labyrinth.gameUserFactory.getGameUserByName(id) ?: return@executesX println("User not found!")
val permKey = StringArgumentType.getString(it, "permission-key")
user.resetPermission(permKey)
println("Granted user permission: $permKey")
}
}
}

executesX {
println("/user <name> permission <permission> (get|grant|revoke|reset)")
}
}
}

executesX {
println("/user <name> - check the basic info of the user")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,21 @@ class MongoManager(private val provider: LabyrinthMongoBuilder = LabyrinthMongoB
}
}

override fun hasPermission(permission: Permission): Boolean {
return if(delegate.permissionGranted?.contains(permission.key) == true) {
// granted
true
} else {
if(permission.default) {
// default is true, and check if revoked
delegate.permissionRevoked?.contains(permission.key) == false
} else {
// default is false
false
}
}
}

override fun grantPermission(permissionKey: String) {
val newGrant = delegate.permissionGranted?.toMutableList() ?: mutableListOf()
newGrant += permissionKey
Expand Down

0 comments on commit a249907

Please sign in to comment.