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 9cd06244f..9284baaa1 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,6 @@ import net.mamoe.mirai.console.extension.PluginComponentStorage import net.mamoe.mirai.console.internal.data.mkdir import net.mamoe.mirai.console.permission.Permission import net.mamoe.mirai.console.permission.PermissionService -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 @@ -26,7 +25,6 @@ import net.mamoe.mirai.console.plugin.ResourceContainer.Companion.asResourceCont import net.mamoe.mirai.console.plugin.jvm.AbstractJvmPlugin import net.mamoe.mirai.console.plugin.jvm.JvmPlugin import net.mamoe.mirai.console.plugin.jvm.JvmPlugin.Companion.onLoad -import net.mamoe.mirai.console.plugin.name import net.mamoe.mirai.console.util.NamedSupervisorJob import net.mamoe.mirai.utils.MiraiLogger import java.io.File @@ -50,7 +48,7 @@ internal abstract class JvmPluginInternal( final override val parentPermission: Permission by lazy { PermissionService.INSTANCE.register( - PermissionService.INSTANCE.allocatePermissionIdForPlugin(name, "*"), + PermissionService.INSTANCE.allocatePermissionIdForPlugin(this, "*", PermissionService.PluginPermissionIdRequestType.ROOT_PERMISSION), "The base permission" ) } diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/PermissionId.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/PermissionId.kt index 86fb7e3c3..9a92e8be2 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/PermissionId.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/PermissionId.kt @@ -31,6 +31,13 @@ public data class PermissionId( @ResolveContext(PERMISSION_NAME) public val name: String, ) { init { + require(!namespace.contains(' ')) { + "' ' is not allowed in namespace" + } + require(!name.contains(' ')) { + "' ' is not allowed in id" + } + require(!namespace.contains(':')) { "':' is not allowed in namespace" } 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 88c55556d..1a12b8af1 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 @@ -16,6 +16,8 @@ import net.mamoe.mirai.console.compiler.common.ResolveContext.Kind.COMMAND_NAME import net.mamoe.mirai.console.extensions.PermissionServiceProvider import net.mamoe.mirai.console.internal.permission.checkType import net.mamoe.mirai.console.permission.Permission.Companion.parentsWithSelf +import net.mamoe.mirai.console.plugin.Plugin +import net.mamoe.mirai.console.plugin.name import kotlin.reflect.KClass /** @@ -89,6 +91,13 @@ public interface PermissionService
{ parent: Permission = RootPermission, ): P + /** 为 [Plugin] 分配一个 [PermissionId] */ + public fun allocatePermissionIdForPlugin( + plugin: Plugin, + @ResolveContext(COMMAND_NAME) permissionName: String, + reason: PluginPermissionIdRequestType + ): PermissionId = allocatePermissionIdForPluginDefaultImplement(plugin, permissionName, reason) + /////////////////////////////////////////////////////////////////////////// /** @@ -116,6 +125,15 @@ public interface PermissionService
{ @Throws(UnsupportedOperationException::class) public fun cancel(permitteeId: PermitteeId, permission: P, recursive: Boolean) + /** [Plugin] 尝试分配的 [PermissionId] 来源 */ + public enum class PluginPermissionIdRequestType { + /** For [Plugin.parentPermission] */ + ROOT_PERMISSION, + + /** For [Plugin.permissionId] */ + PERMISSION_ID + } + public companion object { internal var instanceField: PermissionService<*>? = null @@ -131,8 +149,14 @@ public interface PermissionService
{ public fun
PermissionService
.getOrFail(id: PermissionId): P =
get(id) ?: throw NoSuchElementException("Permission not found: $id")
- internal fun PermissionService<*>.allocatePermissionIdForPlugin(pluginName: String, @ResolveContext(COMMAND_NAME) permissionName: String) =
- PermissionId("plugin.${pluginName.toLowerCase()}", permissionName.toLowerCase())
+ internal fun PermissionService<*>.allocatePermissionIdForPluginDefaultImplement(
+ plugin: Plugin,
+ @ResolveContext(COMMAND_NAME) permissionName: String,
+ reason: PluginPermissionIdRequestType
+ ) = PermissionId(
+ plugin.name.toLowerCase().replace(' ', '.'),
+ permissionName.toLowerCase().replace(' ', '.')
+ )
public fun PermissionId.findCorrespondingPermission(): Permission? = INSTANCE[this]
diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/AbstractJvmPlugin.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/AbstractJvmPlugin.kt
index 9ed8caa29..9b64d84f9 100644
--- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/AbstractJvmPlugin.kt
+++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/AbstractJvmPlugin.kt
@@ -16,6 +16,7 @@ import net.mamoe.mirai.console.data.PluginConfig
import net.mamoe.mirai.console.data.PluginData
import net.mamoe.mirai.console.internal.plugin.JvmPluginInternal
import net.mamoe.mirai.console.permission.PermissionId
+import net.mamoe.mirai.console.permission.PermissionService
import net.mamoe.mirai.console.util.ConsoleExperimentalApi
import net.mamoe.mirai.utils.minutesToMillis
import net.mamoe.mirai.utils.secondsToMillis
@@ -37,7 +38,8 @@ public abstract class AbstractJvmPlugin @JvmOverloads constructor(
public final override val loader: JvmPluginLoader get() = super