Skip to content

Commit

Permalink
feat[onebot11,common,permission]: add permission manager
Browse files Browse the repository at this point in the history
  • Loading branch information
RTAkland committed Jan 29, 2025
1 parent d01da95 commit 52a864a
Show file tree
Hide file tree
Showing 13 changed files with 190 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs
Submodule docs updated 1 files
+19 −0 docs/ronebot/onebot.md
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
kotlin.code.style=official
libVersion=2.6.8
libVersion=2.6.9

#systemProp.http.proxyHost=127.0.0.1
#systemProp.http.proxyPort=12334
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package cn.rtast.rob.command

import cn.rtast.rob.BaseBotInstance
import cn.rtast.rob.entity.IFiredUser
import cn.rtast.rob.entity.IGroupMessage
import cn.rtast.rob.entity.IMessage
import cn.rtast.rob.entity.IPrivateMessage
Expand Down Expand Up @@ -41,4 +42,9 @@ interface ICommandSource {
* 私聊消息(可能为空)
*/
val privateMessage: IPrivateMessage?

/**
* 触发命令的用户
*/
val firedUser: IFiredUser
}
15 changes: 15 additions & 0 deletions ronebot-common/src/main/kotlin/cn/rtast/rob/entity/IFiredUser.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright © 2025 RTAkland
* Author: RTAkland
* Date: 2025/1/29
*/


package cn.rtast.rob.entity

/**
* 权限管理中触发命令的用户
*/
interface IFiredUser {
val id: String
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright © 2025 RTAkland
* Author: RTAkland
* Date: 2025/1/29
*/


package cn.rtast.rob.entity

data class FiredUser(
override val id: String,
val isGroup: Boolean,
val groupId: Long?
) : IFiredUser
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package cn.rtast.rob.util

import cn.rtast.rob.BotInstance
import cn.rtast.rob.command.BrigadierCommandManager
import cn.rtast.rob.entity.FiredUser
import cn.rtast.rob.entity.GroupMessage
import cn.rtast.rob.entity.IMessage
import cn.rtast.rob.entity.PrivateMessage
Expand Down Expand Up @@ -42,15 +43,17 @@ class BrigadierCommandManagerImpl internal constructor(
message,
messageType,
message as GroupMessage,
null
null,
FiredUser(message.sender.userId.toString(), true, message.groupId)
)

BrigadierMessageType.Private -> CommandSource(
it,
message,
messageType,
null,
message as PrivateMessage
message as PrivateMessage,
FiredUser(message.sender.userId.toString(), false, null),
)
}
dispatcher.execute(command, context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package cn.rtast.rob.util

import cn.rtast.rob.BotInstance
import cn.rtast.rob.command.ICommandSource
import cn.rtast.rob.entity.IFiredUser
import cn.rtast.rob.entity.GroupMessage
import cn.rtast.rob.entity.IMessage
import cn.rtast.rob.entity.PrivateMessage
Expand All @@ -22,5 +23,6 @@ data class CommandSource(
override val message: IMessage,
override val messageType: BrigadierMessageType,
override val groupMessage: GroupMessage?,
override val privateMessage: PrivateMessage?
override val privateMessage: PrivateMessage?,
override val firedUser: IFiredUser
) : ICommandSource
16 changes: 16 additions & 0 deletions ronebot-onebot-v11/src/test/kotlin/test/TestClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import cn.rtast.rob.entity.custom.ErrorEvent
import cn.rtast.rob.enums.QQFace
import cn.rtast.rob.onebot.OneBotListener
import cn.rtast.rob.onebot.sdl.messageChain
import cn.rtast.rob.permission.enums.BasicPermission
import cn.rtast.rob.permission.getPermissionManager
import cn.rtast.rob.permission.hasPermission
import cn.rtast.rob.segment.Text
import cn.rtast.rob.util.BaseCommand
import cn.rtast.rob.util.BrigadierCommand
Expand Down Expand Up @@ -58,6 +61,7 @@ class TestBrigadierCommand : BrigadierCommand() {

override fun register(dispatcher: CommandDispatcher<CommandSource>) {
val root = LiteralArgumentBuilder.literal<CommandSource>("/foo")
.requires { it.hasPermission(BasicPermission.Admin) }
.then(
RequiredArgumentBuilder.argument<CommandSource, String>("bar", StringArgumentType.string())
.executes {
Expand Down Expand Up @@ -117,9 +121,21 @@ suspend fun main() {
println(this)
}
instance1.addListeningGroup(985927054)
ROneBotFactory.getPermissionManager().apply {
// setUserPermissionLevel(3458671395.toString(), BasicPermission.User)
// setUserPermission(3458671395.toString(), BasicPermission.User)
// setUserPermission(3458671395.toString(), "command.test.main")
// 只要大于3就拥有所有权限, 所有权限指的是BasicPermission和Int level的所有权限
// 权限节点需要单独配置
setUserPermission(3458671395.toString(), 114514)

}
ROneBotFactory.brigadierCommandManager.register(TestBrigadierCommand())
ROneBotFactory.brigadierCommandManager.register(
Commands.literal("main")
// .requires { it.hasPermission(BasicPermission.Admin) }
// .requires { it.hasPermission("command.test.main") }
.requires { it.hasPermission(114514) }
.then(
Commands.argument("test", CharArgumentType.char())
.executes {
Expand Down
3 changes: 3 additions & 0 deletions ronebot-permission/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dependencies {
api(project(":ronebot-common"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright © 2025 RTAkland
* Author: RTAkland
* Date: 2025/1/29
*/

@file:Suppress("unused")

package cn.rtast.rob.permission

import cn.rtast.rob.command.ICommandSource
import cn.rtast.rob.permission.enums.BasicPermission


/**
* 使用权限等级[Int]来判断是否有权限
*/
fun <T : ICommandSource> T.hasPermission(level: Int): Boolean {
return permissionManager.hasPermission(this.firedUser.id, BasicPermission.fromLevel(level))
}

/**
* 使用权限等级[BasicPermission]来判断是否有权限
*/
fun <T : ICommandSource> T.hasPermission(permission: BasicPermission): Boolean {
return permissionManager.hasPermission(this.firedUser.id, permission)
}

/**
* 使用权限节点等级[String]来判断是否有权限
*/
fun <T : ICommandSource> T.hasPermission(permNode: String): Boolean {
return permissionManager.hasPermission(this.firedUser.id, permNode)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright © 2025 RTAkland
* Author: RTAkland
* Date: 2025/1/29
*/

@file:Suppress("unused")

package cn.rtast.rob.permission

import cn.rtast.rob.BotFactory
import cn.rtast.rob.permission.enums.BasicPermission

fun <T : BotFactory> T.getPermissionManager() = permissionManager

val permissionManager by lazy { PermissionManager() }

class PermissionManager {

private val userPermissions: MutableMap<String, BasicPermission> = mutableMapOf()
private val userPermissionNodes: MutableMap<String, MutableSet<String>> = mutableMapOf()

/**
* 使用内置的[BasicPermission]枚举类来确定一个用户的权限
*/
fun setUserPermission(userId: String, permission: BasicPermission) {
userPermissions[userId] = permission
}

/**
* 通过权限节点来确定用户权限
*/
fun setUserPermission(userId: String, permissionNode: String) {
userPermissionNodes.computeIfAbsent(userId) { mutableSetOf() }.add(permissionNode)
}

/**
* 通过权限等级来确定用户权限
*/
fun setUserPermission(userId: String, level: Int) {
userPermissions[userId] = BasicPermission.fromLevel(level)
}

/**
* 通过内置的[BasicPermission]判断是否拥有权限
*/
internal fun hasPermission(userId: String, requiredPermission: BasicPermission): Boolean {
val userPermission = userPermissions[userId] ?: BasicPermission.Other
if (userPermission == BasicPermission.Owner) {
return true
}
return userPermission.level >= requiredPermission.level
}
/**
* 通过权限节点判断是否拥有权限
*/
internal fun hasPermission(userId: String, permissionNode: String): Boolean {
val userPermission = userPermissions[userId] ?: BasicPermission.Other
if (userPermission == BasicPermission.Owner) {
return true
}
return userPermissionNodes[userId]?.contains(permissionNode) == true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright © 2025 RTAkland
* Author: RTAkland
* Date: 2025/1/29
*/

@file:Suppress("unused")

package cn.rtast.rob.permission.enums

/**
* 内置的基本权限
*/
enum class BasicPermission(val level: Int) {
Owner(3), Admin(2), User(1), Other(0);

companion object {
fun fromLevel(level: Int): BasicPermission {
return when {
level > 3 -> Owner
level == 2 -> Admin
level == 1 -> User
else -> Other
}
}
}
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ include(":ronebot-common-ext")
include(":ronebot-onebot-v11")
include(":ronebot-satori")
include(":ronebot-qqbot-webhook")
include(":ronebot-permission")

0 comments on commit 52a864a

Please sign in to comment.