From 91d75424f487cd1164ee9f509b63947c4d755009 Mon Sep 17 00:00:00 2001 From: Luck Date: Sat, 30 May 2020 02:13:18 +0100 Subject: [PATCH] Include users which inherit the group when running /lp group editor --- .../commands/generic/other/HolderEditor.java | 61 ++++++++++++++++++- .../common/commands/misc/EditorCommand.java | 45 ++++++++------ 2 files changed, 85 insertions(+), 21 deletions(-) diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/other/HolderEditor.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/other/HolderEditor.java index 1ff1915b4..045a0a5e5 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/other/HolderEditor.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/other/HolderEditor.java @@ -31,19 +31,35 @@ import me.lucko.luckperms.common.command.CommandResult; import me.lucko.luckperms.common.command.abstraction.ChildCommand; import me.lucko.luckperms.common.command.access.ArgumentPermissions; import me.lucko.luckperms.common.command.access.CommandPermission; +import me.lucko.luckperms.common.commands.misc.EditorCommand; import me.lucko.luckperms.common.context.contextset.ImmutableContextSetImpl; import me.lucko.luckperms.common.locale.LocaleManager; import me.lucko.luckperms.common.locale.command.CommandSpec; import me.lucko.luckperms.common.locale.message.Message; +import me.lucko.luckperms.common.model.Group; import me.lucko.luckperms.common.model.HolderType; import me.lucko.luckperms.common.model.PermissionHolder; +import me.lucko.luckperms.common.model.User; +import me.lucko.luckperms.common.node.matcher.ConstraintNodeMatcher; +import me.lucko.luckperms.common.node.matcher.StandardNodeMatchers; +import me.lucko.luckperms.common.node.types.Inheritance; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.sender.Sender; +import me.lucko.luckperms.common.storage.misc.NodeEntry; import me.lucko.luckperms.common.util.Predicates; +import me.lucko.luckperms.common.verbose.event.MetaCheckEvent; import me.lucko.luckperms.common.web.WebEditor; +import net.luckperms.api.node.Node; +import net.luckperms.api.query.QueryOptions; + +import java.util.ArrayList; import java.util.Collections; +import java.util.Comparator; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; +import java.util.UUID; public class HolderEditor extends ChildCommand { public HolderEditor(LocaleManager locale, HolderType type) { @@ -57,9 +73,52 @@ public class HolderEditor extends ChildCommand { return CommandResult.NO_PERMISSION; } + List holders = new ArrayList<>(); + + // also include users who are a member of the group + if (holder instanceof Group) { + Group group = (Group) holder; + ConstraintNodeMatcher matcher = StandardNodeMatchers.key(Inheritance.key(group.getName())); + + Map users = new LinkedHashMap<>(plugin.getUserManager().getAll()); + + // only include online players who are in the group + users.values().removeIf(user -> user.normalData().immutable().values().stream().noneMatch(matcher)); + + // fill up with other matching users + if (users.size() < EditorCommand.MAX_USERS) { + plugin.getStorage().searchUserNodes(matcher).join().stream() + .map(NodeEntry::getHolder) + .distinct() + .filter(uuid -> !users.containsKey(uuid)) + .sorted() + .limit(EditorCommand.MAX_USERS - users.size()) + .forEach(uuid -> { + User user = plugin.getStorage().loadUser(uuid, null).join(); + if (user != null) { + users.put(uuid, user); + } + plugin.getUserManager().getHouseKeeper().cleanup(uuid); + }); + } + + users.values().stream() + .sorted(Comparator + .comparingInt(u -> u.getCachedData().getMetaData(QueryOptions.nonContextual()).getWeight(MetaCheckEvent.Origin.INTERNAL)).reversed() + .thenComparing(User::getFormattedDisplayName, String.CASE_INSENSITIVE_ORDER) + ) + .forEach(holders::add); + + // remove holders which the sender doesn't have perms to view + holders.removeIf(h -> ArgumentPermissions.checkViewPerms(plugin, sender, getPermission().get(), h) || ArgumentPermissions.checkGroup(plugin, sender, h, ImmutableContextSetImpl.EMPTY)); + } + + // include the original holder too + holders.add(holder); + Message.EDITOR_START.send(sender); - JsonObject payload = WebEditor.formPayload(Collections.singletonList(holder), Collections.emptyList(), sender, label, plugin); + JsonObject payload = WebEditor.formPayload(holders, Collections.emptyList(), sender, label, plugin); return WebEditor.post(payload, sender, plugin); } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/misc/EditorCommand.java b/common/src/main/java/me/lucko/luckperms/common/commands/misc/EditorCommand.java index 54844bf7b..5b52e0836 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/misc/EditorCommand.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/misc/EditorCommand.java @@ -60,7 +60,7 @@ import java.util.Map; import java.util.UUID; public class EditorCommand extends SingleCommand { - private static final int MAX_USERS = 1000; + public static final int MAX_USERS = 1000; public EditorCommand(LocaleManager locale) { super(CommandSpec.EDITOR.localize(locale), "Editor", CommandPermission.EDITOR, Predicates.notInRange(0, 2)); @@ -99,29 +99,34 @@ public class EditorCommand extends SingleCommand { tracks.addAll(plugin.getTrackManager().getAll().values()); } if (type.includingUsers) { - Map users; + // include all online players + Map users = new LinkedHashMap<>(plugin.getUserManager().getAll()); if (filter != null) { - // return users matching the filter - users = new LinkedHashMap<>(); ConstraintNodeMatcher matcher = StandardNodeMatchers.keyStartsWith(filter); - plugin.getStorage().searchUserNodes(matcher).join().stream() - .map(NodeEntry::getHolder) - .distinct() - .sorted() - .limit(MAX_USERS) - .forEach(uuid -> { - User user = plugin.getStorage().loadUser(uuid, null).join(); - if (user != null) { - users.put(uuid, user); - } - plugin.getUserManager().getHouseKeeper().cleanup(uuid); - }); - } else { - // include all online players - users = new LinkedHashMap<>(plugin.getUserManager().getAll()); - // then fill up with other users with permissions + // only include online players matching the permission + users.values().removeIf(user -> user.normalData().immutable().values().stream().noneMatch(matcher)); + + // fill up with other matching users + if (type.includingOffline && users.size() < MAX_USERS) { + plugin.getStorage().searchUserNodes(matcher).join().stream() + .map(NodeEntry::getHolder) + .distinct() + .filter(uuid -> !users.containsKey(uuid)) + .sorted() + .limit(MAX_USERS - users.size()) + .forEach(uuid -> { + User user = plugin.getStorage().loadUser(uuid, null).join(); + if (user != null) { + users.put(uuid, user); + } + plugin.getUserManager().getHouseKeeper().cleanup(uuid); + }); + } + } else { + + // fill up with other users if (type.includingOffline && users.size() < MAX_USERS) { plugin.getStorage().getUniqueUsers().join().stream() .filter(uuid -> !users.containsKey(uuid))