diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/Permission.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/Permission.kt index f687376cb..c51d96dfc 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/Permission.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/Permission.kt @@ -10,6 +10,7 @@ package net.mamoe.mirai.console.permission import kotlinx.serialization.Serializable +import net.mamoe.mirai.console.util.ConsoleExperimentalAPI /** @@ -19,9 +20,29 @@ import kotlinx.serialization.Serializable public interface Permission { public val id: PermissionId public val description: String - public val base: PermissionId? + public val parent: PermissionId } +/** + * 所有权限的父权限. + */ +@ExperimentalPermission +public object BasePermission : + Permission { + override val id: PermissionId = PermissionId("console", "base") + override val description: String get() = "The parent of any permission" + override val parent: PermissionId get() = id + +} + +@ConsoleExperimentalAPI +@ExperimentalPermission +public fun Permission.parentsWithSelfSequence(): Sequence = + generateSequence(this) { p -> + p.parent.let { PermissionService.INSTANCE[it] } + ?.takeIf { parent -> parent != p } + } + /** * [Permission] 的简单实现 */ @@ -30,5 +51,5 @@ public interface Permission { public class PermissionImpl( override val id: PermissionId, override val description: String, - override val base: PermissionId? + override val parent: PermissionId = BasePermission.id ) : Permission \ No newline at end of file diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/PermissionService.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/PermissionService.kt index 2dace27ea..7d770db76 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/PermissionService.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/PermissionService.kt @@ -29,8 +29,12 @@ public interface PermissionService

{ public fun getGrantedPermissions(permissibleIdentifier: PermissibleIdentifier): Sequence

- public fun testPermission(permissibleIdentifier: PermissibleIdentifier, permission: P): Boolean = - getGrantedPermissions(permissibleIdentifier).any { it == permission } + public fun testPermission(permissibleIdentifier: PermissibleIdentifier, permissionId: PermissionId): Boolean { + val all = this[permissionId]?.parentsWithSelfSequence() ?: return false + return getGrantedPermissions(permissibleIdentifier).any { p -> + all.any { p.id == it.id } + } + } /////////////////////////////////////////////////////////////////////////// @@ -56,6 +60,9 @@ public interface PermissionService

{ } } +@ExperimentalPermission +public fun PermissionId.findCorrespondingPermission(): Permission? = PermissionService.INSTANCE[this] + @ExperimentalPermission public fun PermissibleIdentifier.grant(permission: Permission) { PermissionService.INSTANCE.checkType(permission::class).grant(this, permission) @@ -71,15 +78,14 @@ public fun PermissibleIdentifier.hasPermission(permission: Permission): Boolean @Suppress("UNCHECKED_CAST") @ExperimentalPermission -public fun PermissibleIdentifier.hasPermission(permission: PermissionId): Boolean = +public fun PermissibleIdentifier.hasPermission(permissionId: PermissionId): Boolean = (PermissionService.INSTANCE as PermissionService).run { - val p = this[permission] ?: return false - testPermission(this@hasPermission, p) + testPermission(this@hasPermission, permissionId) } @ExperimentalPermission public fun Permissible.hasPermission(permissionId: PermissionId): Boolean = - PermissionService.run { permissionId.testPermission(this@hasPermission) } + permissionId.testPermission(this@hasPermission) @JvmSynthetic @ExperimentalPermission @@ -94,12 +100,12 @@ public fun PermissibleIdentifier.getGrantedPermissions(): Sequence = @JvmSynthetic @ExperimentalPermission public fun Permission.testPermission(permissible: Permissible): Boolean = - PermissionService.INSTANCE.checkType(this::class).testPermission(permissible.identifier, this@testPermission) + PermissionService.INSTANCE.checkType(this::class).testPermission(permissible.identifier, this@testPermission.id) @JvmSynthetic @ExperimentalPermission public fun Permission.testPermission(permissibleIdentifier: PermissibleIdentifier): Boolean = - PermissionService.INSTANCE.checkType(this::class).testPermission(permissibleIdentifier, this@testPermission) + PermissionService.INSTANCE.checkType(this::class).testPermission(permissibleIdentifier, this@testPermission.id) @JvmSynthetic @ExperimentalPermission