mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-11 02:50:15 +08:00
Add extensions for permission
This commit is contained in:
parent
3183bc4018
commit
a830a095de
@ -44,13 +44,12 @@ public abstract class AbstractConcurrentPermissionService<P : Permission> : Perm
|
|||||||
grantedPermissionsMap[permission.id]?.remove(permissibleIdentifier)
|
grantedPermissionsMap[permission.id]?.remove(permissibleIdentifier)
|
||||||
}
|
}
|
||||||
|
|
||||||
public override fun getGrantedPermissions(permissible: Permissible): Sequence<P> = sequence<P> {
|
public override fun getGrantedPermissions(permissibleIdentifier: PermissibleIdentifier): Sequence<P> = sequence<P> {
|
||||||
for ((permissionIdentifier, permissibleIdentifiers) in grantedPermissionsMap) {
|
for ((permissionIdentifier, permissibleIdentifiers) in grantedPermissionsMap) {
|
||||||
val myIdentifier = permissible.identifier
|
|
||||||
|
|
||||||
val granted =
|
val granted =
|
||||||
if (permissibleIdentifiers.isEmpty()) false
|
if (permissibleIdentifiers.isEmpty()) false
|
||||||
else permissibleIdentifiers.any { myIdentifier grantedWith it }
|
else permissibleIdentifiers.any { permissibleIdentifier grantedWith it }
|
||||||
|
|
||||||
if (granted) get(permissionIdentifier)?.let { yield(it) }
|
if (granted) get(permissionIdentifier)?.let { yield(it) }
|
||||||
}
|
}
|
||||||
|
@ -42,11 +42,15 @@ public object AllGrantPermissionService : PermissionService<PermissionImpl> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun get(id: PermissionId): PermissionImpl? = all[id]
|
override fun get(id: PermissionId): PermissionImpl? = all[id]
|
||||||
override fun getGrantedPermissions(permissible: Permissible): Sequence<PermissionImpl> = all.values.asSequence()
|
override fun getGrantedPermissions(permissibleIdentifier: PermissibleIdentifier): Sequence<PermissionImpl> =
|
||||||
|
all.values.asSequence()
|
||||||
|
|
||||||
override fun grant(permissibleIdentifier: PermissibleIdentifier, permission: PermissionImpl) {
|
override fun grant(permissibleIdentifier: PermissibleIdentifier, permission: PermissionImpl) {
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun testPermission(permissible: Permissible, permission: PermissionImpl): Boolean = true
|
override fun testPermission(permissibleIdentifier: PermissibleIdentifier, permission: PermissionImpl): Boolean =
|
||||||
|
true
|
||||||
|
|
||||||
override fun deny(permissibleIdentifier: PermissibleIdentifier, permission: PermissionImpl) {
|
override fun deny(permissibleIdentifier: PermissibleIdentifier, permission: PermissionImpl) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -70,11 +74,15 @@ public object AllDenyPermissionService : PermissionService<PermissionImpl> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun get(id: PermissionId): PermissionImpl? = all[id]
|
override fun get(id: PermissionId): PermissionImpl? = all[id]
|
||||||
override fun getGrantedPermissions(permissible: Permissible): Sequence<PermissionImpl> = emptySequence()
|
override fun getGrantedPermissions(permissibleIdentifier: PermissibleIdentifier): Sequence<PermissionImpl> =
|
||||||
|
emptySequence()
|
||||||
|
|
||||||
override fun grant(permissibleIdentifier: PermissibleIdentifier, permission: PermissionImpl) {
|
override fun grant(permissibleIdentifier: PermissibleIdentifier, permission: PermissionImpl) {
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun testPermission(permissible: Permissible, permission: PermissionImpl): Boolean = false
|
override fun testPermission(permissibleIdentifier: PermissibleIdentifier, permission: PermissionImpl): Boolean =
|
||||||
|
false
|
||||||
|
|
||||||
override fun deny(permissibleIdentifier: PermissibleIdentifier, permission: PermissionImpl) {
|
override fun deny(permissibleIdentifier: PermissibleIdentifier, permission: PermissionImpl) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,10 +27,10 @@ public interface PermissionService<P : Permission> {
|
|||||||
|
|
||||||
public operator fun get(id: PermissionId): P?
|
public operator fun get(id: PermissionId): P?
|
||||||
|
|
||||||
public fun getGrantedPermissions(permissible: Permissible): Sequence<P>
|
public fun getGrantedPermissions(permissibleIdentifier: PermissibleIdentifier): Sequence<P>
|
||||||
|
|
||||||
public fun testPermission(permissible: Permissible, permission: P): Boolean =
|
public fun testPermission(permissibleIdentifier: PermissibleIdentifier, permission: P): Boolean =
|
||||||
permissible.getGrantedPermissions().any { it == permission }
|
getGrantedPermissions(permissibleIdentifier).any { it == permission }
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////
|
||||||
@ -57,34 +57,49 @@ public interface PermissionService<P : Permission> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@ExperimentalPermission
|
@ExperimentalPermission
|
||||||
public inline fun Permissible.hasPermission(permission: Permission): Boolean =
|
public fun PermissibleIdentifier.grant(permission: Permission) {
|
||||||
PermissionService.run { permission.testPermission(this@hasPermission) }
|
PermissionService.INSTANCE.checkType(permission::class).grant(this, permission)
|
||||||
|
}
|
||||||
|
|
||||||
@ExperimentalPermission
|
@ExperimentalPermission
|
||||||
public inline fun Permissible.hasPermission(permission: PermissionId): Boolean =
|
public fun Permissible.hasPermission(permission: Permission): Boolean =
|
||||||
PermissionService.run { permission.testPermission(this@hasPermission) }
|
permission.testPermission(this@hasPermission)
|
||||||
|
|
||||||
|
@ExperimentalPermission
|
||||||
|
public fun PermissibleIdentifier.hasPermission(permission: Permission): Boolean =
|
||||||
|
permission.testPermission(this@hasPermission)
|
||||||
|
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
@ExperimentalPermission
|
||||||
|
public fun PermissibleIdentifier.hasPermission(permission: PermissionId): Boolean =
|
||||||
|
(PermissionService.INSTANCE as PermissionService<Permission>).run {
|
||||||
|
val p = this[permission] ?: return false
|
||||||
|
testPermission(this@hasPermission, p)
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExperimentalPermission
|
||||||
|
public fun Permissible.hasPermission(permissionId: PermissionId): Boolean =
|
||||||
|
PermissionService.run { permissionId.testPermission(this@hasPermission) }
|
||||||
|
|
||||||
@JvmSynthetic
|
@JvmSynthetic
|
||||||
@ExperimentalPermission
|
@ExperimentalPermission
|
||||||
public inline fun Permissible.getGrantedPermissions(): Sequence<Permission> =
|
public fun Permissible.getGrantedPermissions(): Sequence<Permission> =
|
||||||
PermissionService.INSTANCE.run {
|
PermissionService.INSTANCE.getGrantedPermissions(this@getGrantedPermissions.identifier)
|
||||||
getGrantedPermissions(this@getGrantedPermissions)
|
|
||||||
}
|
@JvmSynthetic
|
||||||
|
@ExperimentalPermission
|
||||||
|
public fun PermissibleIdentifier.getGrantedPermissions(): Sequence<Permission> =
|
||||||
|
PermissionService.INSTANCE.getGrantedPermissions(this@getGrantedPermissions)
|
||||||
|
|
||||||
@JvmSynthetic
|
@JvmSynthetic
|
||||||
@ExperimentalPermission
|
@ExperimentalPermission
|
||||||
public fun Permission.testPermission(permissible: Permissible): Boolean =
|
public fun Permission.testPermission(permissible: Permissible): Boolean =
|
||||||
PermissionService.INSTANCE.run {
|
PermissionService.INSTANCE.checkType(this::class).testPermission(permissible.identifier, this@testPermission)
|
||||||
require(permissionType.isInstance(this@testPermission)) {
|
|
||||||
"Custom-constructed Permission instance is not allowed. " +
|
|
||||||
"Please obtain Permission from PermissionService.INSTANCE.register or PermissionService.INSTANCE.get"
|
|
||||||
}
|
|
||||||
|
|
||||||
@Suppress("UNCHECKED_CAST")
|
@JvmSynthetic
|
||||||
this as PermissionService<Permission>
|
@ExperimentalPermission
|
||||||
|
public fun Permission.testPermission(permissibleIdentifier: PermissibleIdentifier): Boolean =
|
||||||
testPermission(permissible, this@testPermission)
|
PermissionService.INSTANCE.checkType(this::class).testPermission(permissibleIdentifier, this@testPermission)
|
||||||
}
|
|
||||||
|
|
||||||
@JvmSynthetic
|
@JvmSynthetic
|
||||||
@ExperimentalPermission
|
@ExperimentalPermission
|
||||||
@ -92,3 +107,23 @@ public fun PermissionId.testPermission(permissible: Permissible): Boolean {
|
|||||||
val p = PermissionService.INSTANCE[this] ?: return false
|
val p = PermissionService.INSTANCE[this] ?: return false
|
||||||
return p.testPermission(permissible)
|
return p.testPermission(permissible)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@JvmSynthetic
|
||||||
|
@ExperimentalPermission
|
||||||
|
public fun PermissionId.testPermission(permissible: PermissibleIdentifier): Boolean {
|
||||||
|
val p = PermissionService.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(permissionType.isInstance(this@checkType)) {
|
||||||
|
"Custom-constructed Permission instance is not allowed. " +
|
||||||
|
"Please obtain Permission from PermissionService.INSTANCE.register or PermissionService.INSTANCE.get"
|
||||||
|
}
|
||||||
|
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
this as PermissionService<Permission>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user