diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/AbstractConcurrentPermissionService.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/AbstractConcurrentPermissionService.kt index 2abe901de..fe5022af0 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/AbstractConcurrentPermissionService.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/AbstractConcurrentPermissionService.kt @@ -44,13 +44,12 @@ public abstract class AbstractConcurrentPermissionService

: Perm grantedPermissionsMap[permission.id]?.remove(permissibleIdentifier) } - public override fun getGrantedPermissions(permissible: Permissible): Sequence

= sequence

{ + public override fun getGrantedPermissions(permissibleIdentifier: PermissibleIdentifier): Sequence

= sequence

{ 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) } } diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/BuiltInPermissionServices.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/BuiltInPermissionServices.kt index bb84dc27a..1a69b2e71 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/BuiltInPermissionServices.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/BuiltInPermissionServices.kt @@ -42,11 +42,15 @@ public object AllGrantPermissionService : PermissionService { } override fun get(id: PermissionId): PermissionImpl? = all[id] - override fun getGrantedPermissions(permissible: Permissible): Sequence = all.values.asSequence() + override fun getGrantedPermissions(permissibleIdentifier: PermissibleIdentifier): Sequence = + 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 { } override fun get(id: PermissionId): PermissionImpl? = all[id] - override fun getGrantedPermissions(permissible: Permissible): Sequence = emptySequence() + override fun getGrantedPermissions(permissibleIdentifier: PermissibleIdentifier): Sequence = + 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) { } } diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/PermissionService.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/PermissionService.kt index 77bb77cb0..2dace27ea 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/PermissionService.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/PermissionService.kt @@ -27,10 +27,10 @@ public interface PermissionService

{ public operator fun get(id: PermissionId): P? - public fun getGrantedPermissions(permissible: Permissible): Sequence

+ public fun getGrantedPermissions(permissibleIdentifier: PermissibleIdentifier): Sequence

- 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,38 +57,73 @@ public interface PermissionService

{ } @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).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 = - PermissionService.INSTANCE.run { - getGrantedPermissions(this@getGrantedPermissions) - } +public fun Permissible.getGrantedPermissions(): Sequence = + PermissionService.INSTANCE.getGrantedPermissions(this@getGrantedPermissions.identifier) + +@JvmSynthetic +@ExperimentalPermission +public fun PermissibleIdentifier.getGrantedPermissions(): Sequence = + 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 - - testPermission(permissible, this@testPermission) - } +@JvmSynthetic +@ExperimentalPermission +public fun Permission.testPermission(permissibleIdentifier: PermissibleIdentifier): Boolean = + PermissionService.INSTANCE.checkType(this::class).testPermission(permissibleIdentifier, this@testPermission) @JvmSynthetic @ExperimentalPermission public fun PermissionId.testPermission(permissible: Permissible): Boolean { val p = PermissionService.INSTANCE[this] ?: return false return p.testPermission(permissible) -} \ No newline at end of file +} + +@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): PermissionService { + 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 + } +}