mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-10 18:40:15 +08:00
Introduce BasePermission
This commit is contained in:
parent
a830a095de
commit
44ef4e7c56
@ -10,6 +10,7 @@
|
||||
package net.mamoe.mirai.console.permission
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
import net.mamoe.mirai.console.util.ConsoleExperimentalAPI
|
||||
|
||||
|
||||
/**
|
||||
@ -19,9 +20,29 @@ import kotlinx.serialization.Serializable
|
||||
public interface Permission {
|
||||
public val id: PermissionId
|
||||
public val description: String
|
||||
public val base: PermissionId?
|
||||
public val parent: PermissionId
|
||||
}
|
||||
|
||||
/**
|
||||
* 所有权限的父权限.
|
||||
*/
|
||||
@ExperimentalPermission
|
||||
public object BasePermission :
|
||||
Permission {
|
||||
override val id: PermissionId = PermissionId("console", "base")
|
||||
override val description: String get() = "The parent of any permission"
|
||||
override val parent: PermissionId get() = id
|
||||
|
||||
}
|
||||
|
||||
@ConsoleExperimentalAPI
|
||||
@ExperimentalPermission
|
||||
public fun Permission.parentsWithSelfSequence(): Sequence<Permission> =
|
||||
generateSequence(this) { p ->
|
||||
p.parent.let { PermissionService.INSTANCE[it] }
|
||||
?.takeIf { parent -> parent != p }
|
||||
}
|
||||
|
||||
/**
|
||||
* [Permission] 的简单实现
|
||||
*/
|
||||
@ -30,5 +51,5 @@ public interface Permission {
|
||||
public class PermissionImpl(
|
||||
override val id: PermissionId,
|
||||
override val description: String,
|
||||
override val base: PermissionId?
|
||||
override val parent: PermissionId = BasePermission.id
|
||||
) : Permission
|
@ -29,8 +29,12 @@ public interface PermissionService<P : Permission> {
|
||||
|
||||
public fun getGrantedPermissions(permissibleIdentifier: PermissibleIdentifier): Sequence<P>
|
||||
|
||||
public fun testPermission(permissibleIdentifier: PermissibleIdentifier, permission: P): Boolean =
|
||||
getGrantedPermissions(permissibleIdentifier).any { it == permission }
|
||||
public fun testPermission(permissibleIdentifier: PermissibleIdentifier, permissionId: PermissionId): Boolean {
|
||||
val all = this[permissionId]?.parentsWithSelfSequence() ?: return false
|
||||
return getGrantedPermissions(permissibleIdentifier).any { p ->
|
||||
all.any { p.id == it.id }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
@ -56,6 +60,9 @@ public interface PermissionService<P : Permission> {
|
||||
}
|
||||
}
|
||||
|
||||
@ExperimentalPermission
|
||||
public fun PermissionId.findCorrespondingPermission(): Permission? = PermissionService.INSTANCE[this]
|
||||
|
||||
@ExperimentalPermission
|
||||
public fun PermissibleIdentifier.grant(permission: Permission) {
|
||||
PermissionService.INSTANCE.checkType(permission::class).grant(this, permission)
|
||||
@ -71,15 +78,14 @@ public fun PermissibleIdentifier.hasPermission(permission: Permission): Boolean
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
@ExperimentalPermission
|
||||
public fun PermissibleIdentifier.hasPermission(permission: PermissionId): Boolean =
|
||||
public fun PermissibleIdentifier.hasPermission(permissionId: PermissionId): Boolean =
|
||||
(PermissionService.INSTANCE as PermissionService<Permission>).run {
|
||||
val p = this[permission] ?: return false
|
||||
testPermission(this@hasPermission, p)
|
||||
testPermission(this@hasPermission, permissionId)
|
||||
}
|
||||
|
||||
@ExperimentalPermission
|
||||
public fun Permissible.hasPermission(permissionId: PermissionId): Boolean =
|
||||
PermissionService.run { permissionId.testPermission(this@hasPermission) }
|
||||
permissionId.testPermission(this@hasPermission)
|
||||
|
||||
@JvmSynthetic
|
||||
@ExperimentalPermission
|
||||
@ -94,12 +100,12 @@ public fun PermissibleIdentifier.getGrantedPermissions(): Sequence<Permission> =
|
||||
@JvmSynthetic
|
||||
@ExperimentalPermission
|
||||
public fun Permission.testPermission(permissible: Permissible): Boolean =
|
||||
PermissionService.INSTANCE.checkType(this::class).testPermission(permissible.identifier, this@testPermission)
|
||||
PermissionService.INSTANCE.checkType(this::class).testPermission(permissible.identifier, this@testPermission.id)
|
||||
|
||||
@JvmSynthetic
|
||||
@ExperimentalPermission
|
||||
public fun Permission.testPermission(permissibleIdentifier: PermissibleIdentifier): Boolean =
|
||||
PermissionService.INSTANCE.checkType(this::class).testPermission(permissibleIdentifier, this@testPermission)
|
||||
PermissionService.INSTANCE.checkType(this::class).testPermission(permissibleIdentifier, this@testPermission.id)
|
||||
|
||||
@JvmSynthetic
|
||||
@ExperimentalPermission
|
||||
|
Loading…
Reference in New Issue
Block a user