Transfer allocatePermissionIdForPlugin to member function

This commit is contained in:
Karlatemp 2020-10-07 13:25:14 +08:00
parent b1bb417388
commit ab5f9f5cee
No known key found for this signature in database
GPG Key ID: 21FBDDF664FF06F8
4 changed files with 37 additions and 6 deletions

View File

@ -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"
)
}

View File

@ -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"
}

View File

@ -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<P : Permission> {
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<P : Permission> {
@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<P : Permission> {
public fun <P : Permission> PermissionService<P>.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]

View File

@ -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<JvmPluginInternal>.loader
public final override fun permissionId(name: String): PermissionId = PermissionId(description.id, name)
public final override fun permissionId(name: String): PermissionId =
PermissionService.INSTANCE.allocatePermissionIdForPlugin(this, "*", PermissionService.PluginPermissionIdRequestType.PERMISSION_ID)
/**
* 重载 [PluginData]