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 all 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 @@ -1784,6 +1784,7 @@ public abstract interface class net/mamoe/mirai/console/permission/PermitteeId {
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 isChildOf (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 +1797,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 isChildOf (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.isChildOf

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.isChildOf(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.isChildOf(pid)) {
get(permissionIdentifier)?.let { yield(pid to it) }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,19 @@ public interface PermitteeId {

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.isChildOf(parent: PermitteeId): Boolean {
return allParentsWithSelf.any { it.asString() == parent.asString() } // asString is for compatibility issue with external implementations
}

@JvmStatic
@DeprecatedSinceMirai(warningSince = "2.16")
public fun PermitteeId.hasChild(child: PermitteeId): Boolean = isChildOf(child)

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