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.Command.Companion.primaryName
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.extension.useExtensions
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.ExperimentalPermission
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.PluginManager
import net.mamoe.mirai.console.plugin.center.PluginCenter
@ -150,6 +153,8 @@ internal object MiraiConsoleImplementationBridge : CoroutineScope, MiraiConsoleI
mainLogger.verbose { "Reloaded PermissionService settings." }
}
}
ConsoleCommandSender.grantPermission(RootPermission)
}
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.CommandArgumentContextAware
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 kotlin.reflect.KAnnotatedElement
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.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.Member
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.PermissionId
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.PluginManager
import net.mamoe.mirai.console.plugin.PluginManager.INSTANCE.safeLoader

View File

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

View File

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