Move extensions into PermissionService.Companion

This commit is contained in:
Him188 2020-09-09 22:20:18 +08:00
parent 4822a8c5fe
commit 6ec3390b8d
6 changed files with 81 additions and 77 deletions

View File

@ -25,6 +25,7 @@ import net.mamoe.mirai.console.MiraiConsoleImplementation
import net.mamoe.mirai.console.command.BuiltInCommands import net.mamoe.mirai.console.command.BuiltInCommands
import net.mamoe.mirai.console.command.Command.Companion.primaryName import net.mamoe.mirai.console.command.Command.Companion.primaryName
import net.mamoe.mirai.console.command.CommandManager 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.data.PluginDataStorage
import net.mamoe.mirai.console.extension.useExtensions import net.mamoe.mirai.console.extension.useExtensions
import net.mamoe.mirai.console.extensions.PostStartupExtension 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.BuiltInPermissionService
import net.mamoe.mirai.console.permission.ExperimentalPermission import net.mamoe.mirai.console.permission.ExperimentalPermission
import net.mamoe.mirai.console.permission.PermissionService 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.PluginLoader
import net.mamoe.mirai.console.plugin.PluginManager import net.mamoe.mirai.console.plugin.PluginManager
import net.mamoe.mirai.console.plugin.center.PluginCenter import net.mamoe.mirai.console.plugin.center.PluginCenter
@ -150,6 +153,8 @@ internal object MiraiConsoleImplementationBridge : CoroutineScope, MiraiConsoleI
mainLogger.verbose { "Reloaded PermissionService settings." } mainLogger.verbose { "Reloaded PermissionService settings." }
} }
} }
ConsoleCommandSender.grantPermission(RootPermission)
} }
phase `prepare commands`@{ phase `prepare commands`@{

View File

@ -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.CommandArgumentContext
import net.mamoe.mirai.console.command.description.CommandArgumentContextAware import net.mamoe.mirai.console.command.description.CommandArgumentContextAware
import net.mamoe.mirai.console.internal.data.kClassQualifiedNameOrTip 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 net.mamoe.mirai.message.data.*
import kotlin.reflect.KAnnotatedElement import kotlin.reflect.KAnnotatedElement
import kotlin.reflect.KClass import kotlin.reflect.KClass

View File

@ -11,7 +11,11 @@ package net.mamoe.mirai.console.internal.command
import net.mamoe.mirai.console.command.* import net.mamoe.mirai.console.command.*
import net.mamoe.mirai.console.command.Command.Companion.primaryName 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.Group
import net.mamoe.mirai.contact.Member import net.mamoe.mirai.contact.Member
import net.mamoe.mirai.message.data.MessageChain import net.mamoe.mirai.message.data.MessageChain

View File

@ -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.ExperimentalPermission
import net.mamoe.mirai.console.permission.PermissionId import net.mamoe.mirai.console.permission.PermissionId
import net.mamoe.mirai.console.permission.PermissionService 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.Plugin
import net.mamoe.mirai.console.plugin.PluginManager import net.mamoe.mirai.console.plugin.PluginManager
import net.mamoe.mirai.console.plugin.PluginManager.INSTANCE.safeLoader import net.mamoe.mirai.console.plugin.PluginManager.INSTANCE.safeLoader

View File

@ -10,6 +10,7 @@
package net.mamoe.mirai.console.permission package net.mamoe.mirai.console.permission
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import net.mamoe.mirai.console.permission.PermissionService.Companion.findCorrespondingPermission
import net.mamoe.mirai.console.util.ConsoleExperimentalAPI import net.mamoe.mirai.console.util.ConsoleExperimentalAPI

View File

@ -7,7 +7,7 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE * 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 package net.mamoe.mirai.console.permission
@ -59,86 +59,76 @@ public interface PermissionService<P : Permission> {
public val INSTANCE: PermissionService<out Permission> by lazy { public val INSTANCE: PermissionService<out Permission> by lazy {
PermissionServiceProvider.findSingleton()?.instance ?: BuiltInPermissionService PermissionServiceProvider.findSingleton()?.instance ?: BuiltInPermissionService
} }
}
}
@ExperimentalPermission public fun <P : Permission> PermissionService<P>.getOrFail(id: PermissionId): P =
public fun <P : Permission> PermissionService<P>.getOrFail(id: PermissionId): P =
get(id) ?: throw PermissionNotFoundException(id) get(id) ?: throw PermissionNotFoundException(id)
@ExperimentalPermission internal fun PermissionService<*>.allocatePermissionIdForPlugin(name: String, id: String) =
internal fun PermissionService<*>.allocatePermissionIdForPlugin(name: String, id: String) =
PermissionId("plugin.${name.toLowerCase()}", id.toLowerCase()) PermissionId("plugin.${name.toLowerCase()}", id.toLowerCase())
@ExperimentalPermission public fun PermissionId.findCorrespondingPermission(): Permission? = INSTANCE[this]
public fun PermissionId.findCorrespondingPermission(): Permission? = PermissionService.INSTANCE[this]
@ExperimentalPermission public fun PermissionId.findCorrespondingPermissionOrFail(): Permission = INSTANCE.getOrFail(this)
public fun PermissionId.findCorrespondingPermissionOrFail(): Permission = PermissionService.INSTANCE.getOrFail(this)
@ExperimentalPermission public fun PermissibleIdentifier.grantPermission(permission: Permission) {
public fun PermissibleIdentifier.grant(permission: Permission) { INSTANCE.checkType(permission::class).grant(this, 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<Permission>).run {
testPermission(this@hasPermission, getOrFail(permissionId))
} }
@ExperimentalPermission public fun Permissible.hasPermission(permission: Permission): Boolean =
public fun Permissible.hasPermission(permissionId: PermissionId): 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) permissionId.testPermission(this@hasPermission)
@JvmSynthetic public fun Permissible.getGrantedPermissions(): Sequence<Permission> =
@ExperimentalPermission INSTANCE.getGrantedPermissions(this@getGrantedPermissions.identifier)
public fun Permissible.getGrantedPermissions(): Sequence<Permission> =
PermissionService.INSTANCE.getGrantedPermissions(this@getGrantedPermissions.identifier)
@JvmSynthetic public fun Permissible.grantPermission(vararg permissions: Permission) {
@ExperimentalPermission for (permission in permissions) {
public fun PermissibleIdentifier.getGrantedPermissions(): Sequence<Permission> = INSTANCE.checkType(permission::class).grant(this.identifier, permission)
PermissionService.INSTANCE.getGrantedPermissions(this@getGrantedPermissions) }
}
@JvmSynthetic public fun Permissible.denyPermission(vararg permissions: Permission) {
@ExperimentalPermission for (permission in permissions) {
public fun Permission.testPermission(permissible: Permissible): Boolean = INSTANCE.checkType(permission::class).deny(this.identifier, permission)
PermissionService.INSTANCE.checkType(this::class).testPermission(permissible.identifier, this@testPermission) }
}
@JvmSynthetic public fun PermissibleIdentifier.getGrantedPermissions(): Sequence<Permission> =
@ExperimentalPermission INSTANCE.getGrantedPermissions(this@getGrantedPermissions)
public fun Permission.testPermission(permissibleIdentifier: PermissibleIdentifier): Boolean =
PermissionService.INSTANCE.checkType(this::class).testPermission(permissibleIdentifier, this@testPermission)
@JvmSynthetic public fun Permission.testPermission(permissible: Permissible): Boolean =
@ExperimentalPermission INSTANCE.checkType(this::class).testPermission(permissible.identifier, this@testPermission)
public fun PermissionId.testPermission(permissible: Permissible): Boolean {
val p = PermissionService.INSTANCE[this] ?: return false 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) return p.testPermission(permissible)
} }
@JvmSynthetic public fun PermissionId.testPermission(permissible: PermissibleIdentifier): Boolean {
@ExperimentalPermission val p = INSTANCE[this] ?: return false
public fun PermissionId.testPermission(permissible: PermissibleIdentifier): Boolean {
val p = PermissionService.INSTANCE[this] ?: return false
return p.testPermission(permissible) return p.testPermission(permissible)
}
}
} }
@OptIn(ExperimentalPermission::class) @OptIn(ExperimentalPermission::class)
internal fun PermissionService<*>.checkType(permissionType: KClass<out Permission>): PermissionService<Permission> { internal fun PermissionService<*>.checkType(permissionType: KClass<out Permission>): PermissionService<Permission> {
return PermissionService.INSTANCE.run { return PermissionService.INSTANCE.run {
require(this.permissionType.isSuperclassOf(permissionType)) { 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" "Please obtain Permission from PermissionService.INSTANCE.register or PermissionService.INSTANCE.get"
} }