mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-10 18:40:15 +08:00
Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
bde3d995be
@ -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"
|
||||
)
|
||||
}
|
||||
|
@ -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"
|
||||
}
|
||||
|
@ -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]
|
||||
|
||||
|
@ -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]
|
||||
|
Loading…
Reference in New Issue
Block a user