-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat[onebot11,common,permission]: add permission manager
- Loading branch information
Showing
13 changed files
with
190 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
ronebot-common/src/main/kotlin/cn/rtast/rob/entity/IFiredUser.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
14 changes: 14 additions & 0 deletions
14
ronebot-onebot-v11/src/main/kotlin/cn/rtast/rob/entity/FiredUser.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
dependencies { | ||
api(project(":ronebot-common")) | ||
} |
34 changes: 34 additions & 0 deletions
34
ronebot-permission/src/main/kotlin/cn/rtast/rob/permission/Permission.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
64 changes: 64 additions & 0 deletions
64
ronebot-permission/src/main/kotlin/cn/rtast/rob/permission/PermissionManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
ronebot-permission/src/main/kotlin/cn/rtast/rob/permission/enums/BasicPermission.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters