mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-10 18:40: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)
|
||||
}
|
||||
|
||||
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) {
|
||||
val myIdentifier = permissible.identifier
|
||||
|
||||
val granted =
|
||||
if (permissibleIdentifiers.isEmpty()) false
|
||||
else permissibleIdentifiers.any { myIdentifier grantedWith it }
|
||||
else permissibleIdentifiers.any { permissibleIdentifier grantedWith 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 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 testPermission(permissible: Permissible, permission: PermissionImpl): Boolean = true
|
||||
override fun testPermission(permissibleIdentifier: PermissibleIdentifier, permission: PermissionImpl): Boolean =
|
||||
true
|
||||
|
||||
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 getGrantedPermissions(permissible: Permissible): Sequence<PermissionImpl> = emptySequence()
|
||||
override fun getGrantedPermissions(permissibleIdentifier: PermissibleIdentifier): Sequence<PermissionImpl> =
|
||||
emptySequence()
|
||||
|
||||
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) {
|
||||
}
|
||||
}
|
||||
|
@ -27,10 +27,10 @@ public interface PermissionService<P : Permission> {
|
||||
|
||||
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 =
|
||||
permissible.getGrantedPermissions().any { it == permission }
|
||||
public fun testPermission(permissibleIdentifier: PermissibleIdentifier, permission: P): Boolean =
|
||||
getGrantedPermissions(permissibleIdentifier).any { it == permission }
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
@ -57,34 +57,49 @@ public interface PermissionService<P : Permission> {
|
||||
}
|
||||
|
||||
@ExperimentalPermission
|
||||
public inline fun Permissible.hasPermission(permission: Permission): Boolean =
|
||||
PermissionService.run { permission.testPermission(this@hasPermission) }
|
||||
public fun PermissibleIdentifier.grant(permission: Permission) {
|
||||
PermissionService.INSTANCE.checkType(permission::class).grant(this, permission)
|
||||
}
|
||||
|
||||
@ExperimentalPermission
|
||||
public inline fun Permissible.hasPermission(permission: PermissionId): Boolean =
|
||||
PermissionService.run { permission.testPermission(this@hasPermission) }
|
||||
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(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
|
||||
@ExperimentalPermission
|
||||
public inline fun Permissible.getGrantedPermissions(): Sequence<Permission> =
|
||||
PermissionService.INSTANCE.run {
|
||||
getGrantedPermissions(this@getGrantedPermissions)
|
||||
}
|
||||
public fun Permissible.getGrantedPermissions(): Sequence<Permission> =
|
||||
PermissionService.INSTANCE.getGrantedPermissions(this@getGrantedPermissions.identifier)
|
||||
|
||||
@JvmSynthetic
|
||||
@ExperimentalPermission
|
||||
public fun PermissibleIdentifier.getGrantedPermissions(): Sequence<Permission> =
|
||||
PermissionService.INSTANCE.getGrantedPermissions(this@getGrantedPermissions)
|
||||
|
||||
@JvmSynthetic
|
||||
@ExperimentalPermission
|
||||
public fun Permission.testPermission(permissible: Permissible): Boolean =
|
||||
PermissionService.INSTANCE.run {
|
||||
require(permissionType.isInstance(this@testPermission)) {
|
||||
"Custom-constructed Permission instance is not allowed. " +
|
||||
"Please obtain Permission from PermissionService.INSTANCE.register or PermissionService.INSTANCE.get"
|
||||
}
|
||||
PermissionService.INSTANCE.checkType(this::class).testPermission(permissible.identifier, this@testPermission)
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
this as PermissionService<Permission>
|
||||
|
||||
testPermission(permissible, this@testPermission)
|
||||
}
|
||||
@JvmSynthetic
|
||||
@ExperimentalPermission
|
||||
public fun Permission.testPermission(permissibleIdentifier: PermissibleIdentifier): Boolean =
|
||||
PermissionService.INSTANCE.checkType(this::class).testPermission(permissibleIdentifier, this@testPermission)
|
||||
|
||||
@JvmSynthetic
|
||||
@ExperimentalPermission
|
||||
@ -92,3 +107,23 @@ public fun PermissionId.testPermission(permissible: Permissible): Boolean {
|
||||
val p = PermissionService.INSTANCE[this] ?: return false
|
||||
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