mirror of
https://github.com/mamoe/mirai.git
synced 2025-02-13 20:02:57 +08:00
Review permission
This commit is contained in:
parent
b9580ffcbd
commit
648f2bf75f
@ -48,7 +48,7 @@ internal abstract class JvmPluginInternal(
|
||||
|
||||
final override val parentPermission: Permission by lazy {
|
||||
PermissionService.INSTANCE.register(
|
||||
PermissionService.INSTANCE.allocatePermissionIdForPlugin(this, "*", PermissionService.PluginPermissionIdRequestType.ROOT_PERMISSION),
|
||||
PermissionService.INSTANCE.allocatePermissionIdForPlugin(this, "*", PermissionService.PluginPermissionIdRequestType.PLUGIN_ROOT_PERMISSION),
|
||||
"The base permission"
|
||||
)
|
||||
}
|
||||
|
@ -7,6 +7,8 @@
|
||||
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
@file:Suppress("unused")
|
||||
|
||||
package net.mamoe.mirai.console.permission
|
||||
|
||||
import net.mamoe.mirai.console.command.BuiltInCommands
|
||||
@ -65,10 +67,10 @@ public interface Permission {
|
||||
* @see RootPermission 推荐 Kotlin 用户使用.
|
||||
*/
|
||||
@JvmStatic
|
||||
public fun getRootPermission(): Permission = PermissionService.INSTANCE.rootPermission
|
||||
public fun getRootPermission(): Permission = RootPermission
|
||||
|
||||
/**
|
||||
* 递归获取 [Permission.parent], `permission.parent.parent`, permission.parent.parent` ... 直到 [Permission.parent] 为它自己.
|
||||
* 递归获取 [Permission.parent], `permission.parent.parent`, permission.parent.parent.parent` ... 直到 [Permission.parent] 为它自己.
|
||||
*/
|
||||
@get:JvmStatic
|
||||
public val Permission.parentsWithSelf: Sequence<Permission>
|
||||
@ -82,5 +84,5 @@ public interface Permission {
|
||||
* 根权限. 是所有权限的父权限. 权限 ID 为 "*:*"
|
||||
*/
|
||||
@get:JvmSynthetic
|
||||
public val RootPermission: Permission
|
||||
public inline val RootPermission: Permission // It might be removed in the future, so make it inline to avoid ABI changes.
|
||||
get() = PermissionService.INSTANCE.rootPermission
|
@ -35,20 +35,20 @@ public data class PermissionId(
|
||||
"' ' is not allowed in namespace"
|
||||
}
|
||||
require(!name.contains(' ')) {
|
||||
"' ' is not allowed in id"
|
||||
"' ' is not allowed in name"
|
||||
}
|
||||
|
||||
require(!namespace.contains(':')) {
|
||||
"':' is not allowed in namespace"
|
||||
}
|
||||
require(!name.contains(':')) {
|
||||
"':' is not allowed in id"
|
||||
"':' is not allowed in name"
|
||||
}
|
||||
}
|
||||
|
||||
public object PermissionIdAsStringSerializer : KSerializer<PermissionId> by String.serializer().map(
|
||||
serializer = { it.namespace + ":" + it.name },
|
||||
deserializer = { it.split(':').let { (namespace, id) -> PermissionId(namespace, id) } }
|
||||
deserializer = ::parseFromString
|
||||
)
|
||||
|
||||
/**
|
||||
@ -76,11 +76,11 @@ public data class PermissionId(
|
||||
*/
|
||||
@JvmStatic
|
||||
@Throws(IllegalArgumentException::class)
|
||||
public fun checkPermissionIdName(@ResolveContext(PERMISSION_NAME) value: String) {
|
||||
public fun checkPermissionIdName(@ResolveContext(PERMISSION_NAME) name: String) {
|
||||
when {
|
||||
value.isBlank() -> throw IllegalArgumentException("PermissionId.name should not be blank.")
|
||||
value.any { it.isWhitespace() } -> throw IllegalArgumentException("Spaces is not yet allowed in PermissionId.name.")
|
||||
value.contains(':') -> throw IllegalArgumentException("':' is forbidden in PermissionId.name.")
|
||||
name.isBlank() -> throw IllegalArgumentException("PermissionId.name should not be blank.")
|
||||
name.any { it.isWhitespace() } -> throw IllegalArgumentException("Spaces are not yet allowed in PermissionId.name.")
|
||||
name.contains(':') -> throw IllegalArgumentException("':' is forbidden in PermissionId.name.")
|
||||
}
|
||||
}
|
||||
|
||||
@ -89,11 +89,11 @@ public data class PermissionId(
|
||||
*/
|
||||
@JvmStatic
|
||||
@Throws(IllegalArgumentException::class)
|
||||
public fun checkPermissionIdNamespace(@ResolveContext(PERMISSION_NAME) value: String) {
|
||||
public fun checkPermissionIdNamespace(@ResolveContext(PERMISSION_NAME) namespace: String) {
|
||||
when {
|
||||
value.isBlank() -> throw IllegalArgumentException("PermissionId.namespace should not be blank.")
|
||||
value.any { it.isWhitespace() } -> throw IllegalArgumentException("Spaces is not yet allowed in PermissionId.namespace.")
|
||||
value.contains(':') -> throw IllegalArgumentException("':' is forbidden in PermissionId.namespace.")
|
||||
namespace.isBlank() -> throw IllegalArgumentException("PermissionId.namespace should not be blank.")
|
||||
namespace.any { it.isWhitespace() } -> throw IllegalArgumentException("Spaces are not yet allowed in PermissionId.namespace.")
|
||||
namespace.contains(':') -> throw IllegalArgumentException("':' is forbidden in PermissionId.namespace.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -128,10 +128,10 @@ public interface PermissionService<P : Permission> {
|
||||
/** [Plugin] 尝试分配的 [PermissionId] 来源 */
|
||||
public enum class PluginPermissionIdRequestType {
|
||||
/** For [Plugin.parentPermission] */
|
||||
ROOT_PERMISSION,
|
||||
PLUGIN_ROOT_PERMISSION,
|
||||
|
||||
/** For [Plugin.permissionId] */
|
||||
PERMISSION_ID
|
||||
NORMAL
|
||||
}
|
||||
|
||||
public companion object {
|
||||
|
@ -39,7 +39,7 @@ public abstract class AbstractJvmPlugin @JvmOverloads constructor(
|
||||
public final override val loader: JvmPluginLoader get() = super<JvmPluginInternal>.loader
|
||||
|
||||
public final override fun permissionId(name: String): PermissionId =
|
||||
PermissionService.INSTANCE.allocatePermissionIdForPlugin(this, name, PermissionService.PluginPermissionIdRequestType.PERMISSION_ID)
|
||||
PermissionService.INSTANCE.allocatePermissionIdForPlugin(this, name, PermissionService.PluginPermissionIdRequestType.NORMAL)
|
||||
|
||||
/**
|
||||
* 重载 [PluginData]
|
||||
|
Loading…
Reference in New Issue
Block a user