mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-11 02:50:15 +08:00
Introduce BasePermission
This commit is contained in:
parent
a830a095de
commit
44ef4e7c56
@ -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.util.ConsoleExperimentalAPI
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -19,7 +20,27 @@ import kotlinx.serialization.Serializable
|
|||||||
public interface Permission {
|
public interface Permission {
|
||||||
public val id: PermissionId
|
public val id: PermissionId
|
||||||
public val description: String
|
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 }
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -30,5 +51,5 @@ public interface Permission {
|
|||||||
public class PermissionImpl(
|
public class PermissionImpl(
|
||||||
override val id: PermissionId,
|
override val id: PermissionId,
|
||||||
override val description: String,
|
override val description: String,
|
||||||
override val base: PermissionId?
|
override val parent: PermissionId = BasePermission.id
|
||||||
) : Permission
|
) : Permission
|
@ -29,8 +29,12 @@ public interface PermissionService<P : Permission> {
|
|||||||
|
|
||||||
public fun getGrantedPermissions(permissibleIdentifier: PermissibleIdentifier): Sequence<P>
|
public fun getGrantedPermissions(permissibleIdentifier: PermissibleIdentifier): Sequence<P>
|
||||||
|
|
||||||
public fun testPermission(permissibleIdentifier: PermissibleIdentifier, permission: P): Boolean =
|
public fun testPermission(permissibleIdentifier: PermissibleIdentifier, permissionId: PermissionId): Boolean {
|
||||||
getGrantedPermissions(permissibleIdentifier).any { it == permission }
|
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
|
@ExperimentalPermission
|
||||||
public fun PermissibleIdentifier.grant(permission: Permission) {
|
public fun PermissibleIdentifier.grant(permission: Permission) {
|
||||||
PermissionService.INSTANCE.checkType(permission::class).grant(this, permission)
|
PermissionService.INSTANCE.checkType(permission::class).grant(this, permission)
|
||||||
@ -71,15 +78,14 @@ public fun PermissibleIdentifier.hasPermission(permission: Permission): Boolean
|
|||||||
|
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
@ExperimentalPermission
|
@ExperimentalPermission
|
||||||
public fun PermissibleIdentifier.hasPermission(permission: PermissionId): Boolean =
|
public fun PermissibleIdentifier.hasPermission(permissionId: PermissionId): Boolean =
|
||||||
(PermissionService.INSTANCE as PermissionService<Permission>).run {
|
(PermissionService.INSTANCE as PermissionService<Permission>).run {
|
||||||
val p = this[permission] ?: return false
|
testPermission(this@hasPermission, permissionId)
|
||||||
testPermission(this@hasPermission, p)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExperimentalPermission
|
@ExperimentalPermission
|
||||||
public fun Permissible.hasPermission(permissionId: PermissionId): Boolean =
|
public fun Permissible.hasPermission(permissionId: PermissionId): Boolean =
|
||||||
PermissionService.run { permissionId.testPermission(this@hasPermission) }
|
permissionId.testPermission(this@hasPermission)
|
||||||
|
|
||||||
@JvmSynthetic
|
@JvmSynthetic
|
||||||
@ExperimentalPermission
|
@ExperimentalPermission
|
||||||
@ -94,12 +100,12 @@ public fun PermissibleIdentifier.getGrantedPermissions(): Sequence<Permission> =
|
|||||||
@JvmSynthetic
|
@JvmSynthetic
|
||||||
@ExperimentalPermission
|
@ExperimentalPermission
|
||||||
public fun Permission.testPermission(permissible: Permissible): Boolean =
|
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
|
@JvmSynthetic
|
||||||
@ExperimentalPermission
|
@ExperimentalPermission
|
||||||
public fun Permission.testPermission(permissibleIdentifier: PermissibleIdentifier): Boolean =
|
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
|
@JvmSynthetic
|
||||||
@ExperimentalPermission
|
@ExperimentalPermission
|
||||||
|
Loading…
Reference in New Issue
Block a user