diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/MiraiConsoleImplementationBridge.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/MiraiConsoleImplementationBridge.kt index 59108b740..a604cc13d 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/MiraiConsoleImplementationBridge.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/MiraiConsoleImplementationBridge.kt @@ -25,6 +25,7 @@ import net.mamoe.mirai.console.MiraiConsoleImplementation import net.mamoe.mirai.console.command.BuiltInCommands import net.mamoe.mirai.console.command.Command.Companion.primaryName import net.mamoe.mirai.console.command.CommandManager +import net.mamoe.mirai.console.command.ConsoleCommandSender import net.mamoe.mirai.console.data.PluginDataStorage import net.mamoe.mirai.console.extension.useExtensions import net.mamoe.mirai.console.extensions.PostStartupExtension @@ -36,6 +37,8 @@ import net.mamoe.mirai.console.internal.plugin.PluginManagerImpl import net.mamoe.mirai.console.permission.BuiltInPermissionService import net.mamoe.mirai.console.permission.ExperimentalPermission import net.mamoe.mirai.console.permission.PermissionService +import net.mamoe.mirai.console.permission.PermissionService.Companion.grantPermission +import net.mamoe.mirai.console.permission.RootPermission import net.mamoe.mirai.console.plugin.PluginLoader import net.mamoe.mirai.console.plugin.PluginManager import net.mamoe.mirai.console.plugin.center.PluginCenter @@ -150,6 +153,8 @@ internal object MiraiConsoleImplementationBridge : CoroutineScope, MiraiConsoleI mainLogger.verbose { "Reloaded PermissionService settings." } } } + + ConsoleCommandSender.grantPermission(RootPermission) } phase `prepare commands`@{ diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/CompositeCommandInternal.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/CompositeCommandInternal.kt index c0d289edc..4251131ed 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/CompositeCommandInternal.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/CompositeCommandInternal.kt @@ -16,7 +16,11 @@ import net.mamoe.mirai.console.command.Command.Companion.primaryName import net.mamoe.mirai.console.command.description.CommandArgumentContext import net.mamoe.mirai.console.command.description.CommandArgumentContextAware import net.mamoe.mirai.console.internal.data.kClassQualifiedNameOrTip -import net.mamoe.mirai.console.permission.* +import net.mamoe.mirai.console.permission.ExperimentalPermission +import net.mamoe.mirai.console.permission.Permission +import net.mamoe.mirai.console.permission.PermissionId +import net.mamoe.mirai.console.permission.PermissionService +import net.mamoe.mirai.console.permission.PermissionService.Companion.testPermission import net.mamoe.mirai.message.data.* import kotlin.reflect.KAnnotatedElement import kotlin.reflect.KClass diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/internal.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/internal.kt index c6a2bc374..1958f7c61 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/internal.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/internal.kt @@ -11,7 +11,11 @@ package net.mamoe.mirai.console.internal.command import net.mamoe.mirai.console.command.* import net.mamoe.mirai.console.command.Command.Companion.primaryName -import net.mamoe.mirai.console.permission.* +import net.mamoe.mirai.console.permission.ExperimentalPermission +import net.mamoe.mirai.console.permission.Permission +import net.mamoe.mirai.console.permission.PermissionId +import net.mamoe.mirai.console.permission.PermissionService +import net.mamoe.mirai.console.permission.PermissionService.Companion.testPermission import net.mamoe.mirai.contact.Group import net.mamoe.mirai.contact.Member import net.mamoe.mirai.message.data.MessageChain diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JvmPluginInternal.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JvmPluginInternal.kt index e200e05b8..061bf6a15 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JvmPluginInternal.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JvmPluginInternal.kt @@ -18,7 +18,7 @@ import net.mamoe.mirai.console.internal.data.mkdir import net.mamoe.mirai.console.permission.ExperimentalPermission import net.mamoe.mirai.console.permission.PermissionId import net.mamoe.mirai.console.permission.PermissionService -import net.mamoe.mirai.console.permission.allocatePermissionIdForPlugin +import net.mamoe.mirai.console.permission.PermissionService.Companion.allocatePermissionIdForPlugin import net.mamoe.mirai.console.plugin.Plugin import net.mamoe.mirai.console.plugin.PluginManager import net.mamoe.mirai.console.plugin.PluginManager.INSTANCE.safeLoader 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 81c34e43e..ef1e8a212 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.permission.PermissionService.Companion.findCorrespondingPermission import net.mamoe.mirai.console.util.ConsoleExperimentalAPI 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 e4c87d146..d517392bf 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 @@ -7,7 +7,7 @@ * https://github.com/mamoe/mirai/blob/master/LICENSE */ -@file:Suppress("NOTHING_TO_INLINE", "unused") +@file:Suppress("NOTHING_TO_INLINE", "unused", "MemberVisibilityCanBePrivate") package net.mamoe.mirai.console.permission @@ -59,86 +59,76 @@ public interface PermissionService

{ public val INSTANCE: PermissionService by lazy { PermissionServiceProvider.findSingleton()?.instance ?: BuiltInPermissionService } + + public fun

PermissionService

.getOrFail(id: PermissionId): P = + get(id) ?: throw PermissionNotFoundException(id) + + internal fun PermissionService<*>.allocatePermissionIdForPlugin(name: String, id: String) = + PermissionId("plugin.${name.toLowerCase()}", id.toLowerCase()) + + public fun PermissionId.findCorrespondingPermission(): Permission? = INSTANCE[this] + + public fun PermissionId.findCorrespondingPermissionOrFail(): Permission = INSTANCE.getOrFail(this) + + public fun PermissibleIdentifier.grantPermission(permission: Permission) { + INSTANCE.checkType(permission::class).grant(this, permission) + } + + public fun Permissible.hasPermission(permission: Permission): Boolean = + permission.testPermission(this@hasPermission) + + public fun PermissibleIdentifier.hasPermission(permission: Permission): Boolean = + permission.testPermission(this@hasPermission) + + public fun PermissibleIdentifier.hasPermission(permissionId: PermissionId): Boolean { + val instance = permissionId.findCorrespondingPermissionOrFail() + return INSTANCE.checkType(instance::class).testPermission(this@hasPermission, instance) + } + + public fun Permissible.hasPermission(permissionId: PermissionId): Boolean = + permissionId.testPermission(this@hasPermission) + + public fun Permissible.getGrantedPermissions(): Sequence = + INSTANCE.getGrantedPermissions(this@getGrantedPermissions.identifier) + + public fun Permissible.grantPermission(vararg permissions: Permission) { + for (permission in permissions) { + INSTANCE.checkType(permission::class).grant(this.identifier, permission) + } + } + + public fun Permissible.denyPermission(vararg permissions: Permission) { + for (permission in permissions) { + INSTANCE.checkType(permission::class).deny(this.identifier, permission) + } + } + + public fun PermissibleIdentifier.getGrantedPermissions(): Sequence = + INSTANCE.getGrantedPermissions(this@getGrantedPermissions) + + public fun Permission.testPermission(permissible: Permissible): Boolean = + INSTANCE.checkType(this::class).testPermission(permissible.identifier, this@testPermission) + + public fun Permission.testPermission(permissibleIdentifier: PermissibleIdentifier): Boolean = + INSTANCE.checkType(this::class).testPermission(permissibleIdentifier, this@testPermission) + + public fun PermissionId.testPermission(permissible: Permissible): Boolean { + val p = INSTANCE[this] ?: return false + return p.testPermission(permissible) + } + + public fun PermissionId.testPermission(permissible: PermissibleIdentifier): Boolean { + val p = INSTANCE[this] ?: return false + return p.testPermission(permissible) + } } } -@ExperimentalPermission -public fun

PermissionService

.getOrFail(id: PermissionId): P = - get(id) ?: throw PermissionNotFoundException(id) - -@ExperimentalPermission -internal fun PermissionService<*>.allocatePermissionIdForPlugin(name: String, id: String) = - PermissionId("plugin.${name.toLowerCase()}", id.toLowerCase()) - -@ExperimentalPermission -public fun PermissionId.findCorrespondingPermission(): Permission? = PermissionService.INSTANCE[this] - -@ExperimentalPermission -public fun PermissionId.findCorrespondingPermissionOrFail(): Permission = PermissionService.INSTANCE.getOrFail(this) - -@ExperimentalPermission -public fun PermissibleIdentifier.grant(permission: Permission) { - PermissionService.INSTANCE.checkType(permission::class).grant(this, permission) -} - -@ExperimentalPermission -public fun Permissible.hasPermission(permission: Permission): Boolean = - permission.testPermission(this@hasPermission) - -@ExperimentalPermission -public fun PermissibleIdentifier.hasPermission(permission: Permission): Boolean = - permission.testPermission(this@hasPermission) - -@Suppress("UNCHECKED_CAST") -@ExperimentalPermission -public fun PermissibleIdentifier.hasPermission(permissionId: PermissionId): Boolean = - (PermissionService.INSTANCE as PermissionService).run { - testPermission(this@hasPermission, getOrFail(permissionId)) - } - -@ExperimentalPermission -public fun Permissible.hasPermission(permissionId: PermissionId): Boolean = - permissionId.testPermission(this@hasPermission) - -@JvmSynthetic -@ExperimentalPermission -public fun Permissible.getGrantedPermissions(): Sequence = - PermissionService.INSTANCE.getGrantedPermissions(this@getGrantedPermissions.identifier) - -@JvmSynthetic -@ExperimentalPermission -public fun PermissibleIdentifier.getGrantedPermissions(): Sequence = - PermissionService.INSTANCE.getGrantedPermissions(this@getGrantedPermissions) - -@JvmSynthetic -@ExperimentalPermission -public fun Permission.testPermission(permissible: Permissible): Boolean = - PermissionService.INSTANCE.checkType(this::class).testPermission(permissible.identifier, this@testPermission) - -@JvmSynthetic -@ExperimentalPermission -public fun Permission.testPermission(permissibleIdentifier: PermissibleIdentifier): Boolean = - PermissionService.INSTANCE.checkType(this::class).testPermission(permissibleIdentifier, this@testPermission) - -@JvmSynthetic -@ExperimentalPermission -public fun PermissionId.testPermission(permissible: Permissible): Boolean { - val p = PermissionService.INSTANCE[this] ?: return false - return p.testPermission(permissible) -} - -@JvmSynthetic -@ExperimentalPermission -public fun PermissionId.testPermission(permissible: PermissibleIdentifier): Boolean { - val p = PermissionService.INSTANCE[this] ?: return false - return p.testPermission(permissible) -} - @OptIn(ExperimentalPermission::class) internal fun PermissionService<*>.checkType(permissionType: KClass): PermissionService { return PermissionService.INSTANCE.run { require(this.permissionType.isSuperclassOf(permissionType)) { - "Custom-constructed Permission instance is not allowed. " + + "Custom-constructed Permission instance is not allowed (Required ${this.permissionType}, found ${permissionType}. " + "Please obtain Permission from PermissionService.INSTANCE.register or PermissionService.INSTANCE.get" }