Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[console] fix PermitteeId.hasChild #2762

Merged
merged 5 commits into from
Aug 12, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1780,10 +1780,12 @@ public abstract interface class net/mamoe/mirai/console/permission/Permittee {
public abstract interface class net/mamoe/mirai/console/permission/PermitteeId {
public static final field Companion Lnet/mamoe/mirai/console/permission/PermitteeId$Companion;
public abstract fun asString ()Ljava/lang/String;
public fun contains (Lnet/mamoe/mirai/console/permission/PermitteeId;)Z
public static fun getAllParents (Lnet/mamoe/mirai/console/permission/PermitteeId;)Lkotlin/sequences/Sequence;
public static fun getAllParentsWithSelf (Lnet/mamoe/mirai/console/permission/PermitteeId;)Lkotlin/sequences/Sequence;
public abstract fun getDirectParents ()[Lnet/mamoe/mirai/console/permission/PermitteeId;
public static fun hasChild (Lnet/mamoe/mirai/console/permission/PermitteeId;Lnet/mamoe/mirai/console/permission/PermitteeId;)Z
public static fun isChild (Lnet/mamoe/mirai/console/permission/PermitteeId;Lnet/mamoe/mirai/console/permission/PermitteeId;)Z
}

public final class net/mamoe/mirai/console/permission/PermitteeId$Companion {
Expand All @@ -1796,6 +1798,7 @@ public final class net/mamoe/mirai/console/permission/PermitteeId$Companion {
public final synthetic fun getPermitteeId (Lnet/mamoe/mirai/contact/User;)Lnet/mamoe/mirai/console/permission/AbstractPermitteeId$ExactUser;
public final synthetic fun getPermitteeIdOnTemp (Lnet/mamoe/mirai/contact/Member;)Lnet/mamoe/mirai/console/permission/AbstractPermitteeId$ExactGroupTemp;
public final fun hasChild (Lnet/mamoe/mirai/console/permission/PermitteeId;Lnet/mamoe/mirai/console/permission/PermitteeId;)Z
public final fun isChild (Lnet/mamoe/mirai/console/permission/PermitteeId;Lnet/mamoe/mirai/console/permission/PermitteeId;)Z
}

public abstract interface class net/mamoe/mirai/console/plugin/NotYetLoadedPlugin : net/mamoe/mirai/console/plugin/Plugin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import net.mamoe.mirai.console.data.PluginDataExtensions
import net.mamoe.mirai.console.permission.*
import net.mamoe.mirai.console.permission.Permission.Companion.parentsWithSelf
import net.mamoe.mirai.console.permission.PermitteeId.Companion.allParentsWithSelf
import net.mamoe.mirai.console.permission.PermitteeId.Companion.hasChild
import net.mamoe.mirai.console.permission.PermitteeId.Companion.isChild

internal abstract class AbstractConcurrentPermissionService<P : Permission> : PermissionService<P> {
protected abstract val permissions: MutableMap<PermissionId, P>
Expand Down Expand Up @@ -75,7 +75,7 @@ internal abstract class AbstractConcurrentPermissionService<P : Permission> : Pe
override fun getPermittedPermissions(permitteeId: PermitteeId): Sequence<P> = sequence {
for ((permissionIdentifier, permissibleIdentifiers) in grantedPermissionsMap) {

val granted = permissibleIdentifiers.any { permitteeId.hasChild(it) }
val granted = permissibleIdentifiers.any { permitteeId.isChild(it) }

if (granted) get(permissionIdentifier)?.let { yield(it) }
}
Expand All @@ -84,7 +84,7 @@ internal abstract class AbstractConcurrentPermissionService<P : Permission> : Pe
internal fun getPermittedPermissionsAndSource(permitteeId: PermitteeId): Sequence<Pair<PermitteeId, P>> = sequence {
for ((permissionIdentifier, permissibleIdentifiers) in grantedPermissionsMap) {
permissibleIdentifiers.forEach { pid ->
if (permitteeId.hasChild(pid)) {
if (permitteeId.isChild(pid)) {
get(permissionIdentifier)?.let { yield(pid to it) }
}
}
Expand Down
19 changes: 16 additions & 3 deletions mirai-console/backend/mirai-console/src/permission/PermitteeId.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,28 @@ public interface PermitteeId {
*/
public fun asString(): String

/**
* @since 2.16
*/
public operator fun contains(child: PermitteeId): Boolean {
cssxsh marked this conversation as resolved.
Show resolved Hide resolved
return child.isChild(this)
}

public companion object {
/**
* 当 [this] 或 [this] 的任意一个直接或间接父 [PermitteeId.asString] 与 `this.asString` 相同时返回 `true`
* 当 [this] 或 [this] 的任意一个直接或间接父 [PermitteeId.asString] 与 `parent.asString` 相同时返回 `true`
*
* @since 2.16
*/
@JvmStatic
public fun PermitteeId.hasChild(child: PermitteeId): Boolean {
return allParentsWithSelf.any { it.asString() == child.asString() } // asString is for compatibility issue with external implementations
public fun PermitteeId.isChild(parent: PermitteeId): Boolean {
cssxsh marked this conversation as resolved.
Show resolved Hide resolved
return allParentsWithSelf.any { it.asString() == parent.asString() } // asString is for compatibility issue with external implementations
}

@JvmStatic
@DeprecatedSinceMirai(warningSince = "2.16", hiddenSince = "2.17")
cssxsh marked this conversation as resolved.
Show resolved Hide resolved
public fun PermitteeId.hasChild(child: PermitteeId): Boolean = isChild(child)

/**
* 获取所有直接或间接父类的 [PermitteeId].
*/
Expand Down