From 42d48c8da2eb5b7de6e71d785bbebdb72782a301 Mon Sep 17 00:00:00 2001 From: Luck Date: Fri, 7 Jul 2017 12:43:13 +0100 Subject: [PATCH] Implement argument based permissions for LP commands - closes #52, #170, #174 --- .../luckperms/bukkit/BukkitSenderFactory.java | 9 + .../luckperms/bukkit/model/LPPermissible.java | 20 +- bukkit/src/main/resources/config.yml | 9 + .../luckperms/bungee/BungeeListener.java | 33 +++ .../luckperms/bungee/BungeeSenderFactory.java | 7 + .../bungee/event/TristateCheckEvent.java | 62 +++++ bungee/src/main/resources/config.yml | 9 + bungee/src/main/resources/plugin.yml | 2 +- .../common/commands/ArgumentPermissions.java | 222 ++++++++++++++++++ .../common/commands/CommandManager.java | 2 +- .../commands/abstraction/MainCommand.java | 2 +- .../abstraction/SharedMainCommand.java | 4 +- .../abstraction/SharedSubCommand.java | 2 +- .../impl/generic/meta/MetaAddChatMeta.java | 13 +- .../generic/meta/MetaAddTempChatMeta.java | 13 +- .../commands/impl/generic/meta/MetaClear.java | 13 +- .../commands/impl/generic/meta/MetaInfo.java | 8 +- .../impl/generic/meta/MetaRemoveChatMeta.java | 13 +- .../generic/meta/MetaRemoveTempChatMeta.java | 13 +- .../commands/impl/generic/meta/MetaSet.java | 18 +- .../impl/generic/meta/MetaSetTemp.java | 18 +- .../commands/impl/generic/meta/MetaUnset.java | 18 +- .../impl/generic/meta/MetaUnsetTemp.java | 18 +- .../impl/generic/other/HolderClear.java | 11 + .../impl/generic/other/HolderEditor.java | 6 + .../impl/generic/other/HolderShowTracks.java | 6 + .../impl/generic/parent/ParentAdd.java | 18 +- .../impl/generic/parent/ParentAddTemp.java | 18 +- .../impl/generic/parent/ParentClear.java | 13 +- .../impl/generic/parent/ParentClearTrack.java | 19 +- .../impl/generic/parent/ParentInfo.java | 8 +- .../impl/generic/parent/ParentRemove.java | 18 +- .../impl/generic/parent/ParentRemoveTemp.java | 18 +- .../impl/generic/parent/ParentSet.java | 18 +- .../impl/generic/parent/ParentSetTrack.java | 18 +- .../generic/permission/PermissionCheck.java | 8 +- .../permission/PermissionCheckInherits.java | 8 +- .../generic/permission/PermissionInfo.java | 8 +- .../generic/permission/PermissionSet.java | 18 +- .../generic/permission/PermissionSetTemp.java | 18 +- .../generic/permission/PermissionUnset.java | 18 +- .../permission/PermissionUnsetTemp.java | 18 +- .../commands/impl/group/GroupClone.java | 11 + .../common/commands/impl/group/GroupInfo.java | 6 + .../commands/impl/group/GroupListMembers.java | 6 + .../commands/impl/group/GroupSetWeight.java | 6 + .../commands/impl/misc/ApplyEditsCommand.java | 6 + .../common/commands/impl/user/UserDemote.java | 20 +- .../common/commands/impl/user/UserInfo.java | 6 + .../commands/impl/user/UserPromote.java | 21 ++ .../impl/user/UserSwitchPrimaryGroup.java | 6 + .../commands/sender/AbstractSender.java | 27 +++ .../common/commands/sender/Sender.java | 17 ++ .../common/commands/sender/SenderFactory.java | 3 + .../luckperms/common/config/ConfigKeys.java | 5 + .../common/constants/Permission.java | 11 +- .../lucko/luckperms/common/data/Importer.java | 11 + .../luckperms/sponge/SpongeSenderFactory.java | 7 + sponge/src/main/resources/luckperms.conf | 9 + 59 files changed, 929 insertions(+), 44 deletions(-) create mode 100644 bungee/src/main/java/me/lucko/luckperms/bungee/event/TristateCheckEvent.java create mode 100644 common/src/main/java/me/lucko/luckperms/common/commands/ArgumentPermissions.java diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitSenderFactory.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitSenderFactory.java index 73e2d4600..a4f889b3d 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitSenderFactory.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitSenderFactory.java @@ -25,6 +25,7 @@ package me.lucko.luckperms.bukkit; +import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.bukkit.compat.MessageHandler; import me.lucko.luckperms.common.commands.sender.SenderFactory; import me.lucko.luckperms.common.constants.Constants; @@ -79,6 +80,14 @@ public class BukkitSenderFactory extends SenderFactory { messageHandler.sendJsonMessage(sender, message); } + @Override + protected Tristate getPermissionValue(CommandSender sender, String node) { + boolean isSet = sender.isPermissionSet(node); + boolean val = sender.hasPermission(node); + + return !isSet ? val ? Tristate.TRUE : Tristate.UNDEFINED : Tristate.fromBoolean(val); + } + @Override protected boolean hasPermission(CommandSender sender, String node) { return sender.hasPermission(node); diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/LPPermissible.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/LPPermissible.java index 9900f3005..d7c49f1e3 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/LPPermissible.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/LPPermissible.java @@ -146,22 +146,28 @@ public class LPPermissible extends PermissibleBase { @Override public boolean isPermissionSet(@NonNull String name) { - return user.getUserData().getPermissionData(calculateContexts()).getPermissionValue(name) != Tristate.UNDEFINED; + Tristate ts = user.getUserData().getPermissionData(calculateContexts()).getPermissionValue(name); + return ts != Tristate.UNDEFINED || Permission.DEFAULT_PERMISSION.getValue(isOp()); } @Override public boolean isPermissionSet(@NonNull Permission perm) { - return isPermissionSet(perm.getName()); + Tristate ts = user.getUserData().getPermissionData(calculateContexts()).getPermissionValue(perm.getName()); + if (ts != Tristate.UNDEFINED) { + return true; + } + + if (!plugin.getConfiguration().get(ConfigKeys.APPLY_BUKKIT_DEFAULT_PERMISSIONS)) { + return Permission.DEFAULT_PERMISSION.getValue(isOp()); + } else { + return perm.getDefault().getValue(isOp()); + } } @Override public boolean hasPermission(@NonNull String name) { Tristate ts = user.getUserData().getPermissionData(calculateContexts()).getPermissionValue(name); - if (ts != Tristate.UNDEFINED) { - return ts.asBoolean(); - } - - return Permission.DEFAULT_PERMISSION.getValue(isOp()); + return ts != Tristate.UNDEFINED ? ts.asBoolean() : Permission.DEFAULT_PERMISSION.getValue(isOp()); } @Override diff --git a/bukkit/src/main/resources/config.yml b/bukkit/src/main/resources/config.yml index 759d6bcf9..4007a017d 100644 --- a/bukkit/src/main/resources/config.yml +++ b/bukkit/src/main/resources/config.yml @@ -101,6 +101,15 @@ temporary-add-behaviour: deny # directly and indirectly primary-group-calculation: stored +# If the plugin should check for "extra" permissions with users run LP commands. +# +# These extra permissions allow finer control over what users can do with each command, and +# who they have access to edit. +# +# The permissions are *not* static, unlike the 'base' permisssions, and will depend upon the +# arguments given within the command. +argument-based-command-permissions: false + diff --git a/bungee/src/main/java/me/lucko/luckperms/bungee/BungeeListener.java b/bungee/src/main/java/me/lucko/luckperms/bungee/BungeeListener.java index b39d1d43e..d601567b3 100644 --- a/bungee/src/main/java/me/lucko/luckperms/bungee/BungeeListener.java +++ b/bungee/src/main/java/me/lucko/luckperms/bungee/BungeeListener.java @@ -30,6 +30,7 @@ import lombok.RequiredArgsConstructor; import me.lucko.luckperms.api.Contexts; import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.api.context.MutableContextSet; +import me.lucko.luckperms.bungee.event.TristateCheckEvent; import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.core.model.User; import me.lucko.luckperms.common.locale.Message; @@ -165,6 +166,38 @@ public class BungeeListener implements Listener { e.setHasPermission(result.asBoolean()); } + @EventHandler(priority = EventPriority.HIGH) + public void onPlayerTristateCheck(TristateCheckEvent e) { + if (!(e.getSender() instanceof ProxiedPlayer)) { + return; + } + + final ProxiedPlayer player = ((ProxiedPlayer) e.getSender()); + + User user = plugin.getUserManager().getIfLoaded(plugin.getUuidCache().getUUID(player.getUniqueId())); + if (user == null) { + e.setResult(Tristate.UNDEFINED); + return; + } + + Contexts contexts = new Contexts( + plugin.getContextManager().getApplicableContext(player), + plugin.getConfiguration().get(ConfigKeys.INCLUDING_GLOBAL_PERMS), + plugin.getConfiguration().get(ConfigKeys.INCLUDING_GLOBAL_WORLD_PERMS), + true, + plugin.getConfiguration().get(ConfigKeys.APPLYING_GLOBAL_GROUPS), + plugin.getConfiguration().get(ConfigKeys.APPLYING_GLOBAL_WORLD_GROUPS), + false + ); + + Tristate result = user.getUserData().getPermissionData(contexts).getPermissionValue(e.getPermission()); + if (result == Tristate.UNDEFINED && plugin.getConfiguration().get(ConfigKeys.APPLY_BUNGEE_CONFIG_PERMISSIONS)) { + return; // just use the result provided by the proxy when the event was created + } + + e.setResult(result); + } + // We don't pre-process all servers, so we have to do it here. @EventHandler(priority = EventPriority.LOWEST) public void onServerSwitch(ServerConnectEvent e) { diff --git a/bungee/src/main/java/me/lucko/luckperms/bungee/BungeeSenderFactory.java b/bungee/src/main/java/me/lucko/luckperms/bungee/BungeeSenderFactory.java index 6c570f264..f3864dca3 100644 --- a/bungee/src/main/java/me/lucko/luckperms/bungee/BungeeSenderFactory.java +++ b/bungee/src/main/java/me/lucko/luckperms/bungee/BungeeSenderFactory.java @@ -25,6 +25,8 @@ package me.lucko.luckperms.bungee; +import me.lucko.luckperms.api.Tristate; +import me.lucko.luckperms.bungee.event.TristateCheckEvent; import me.lucko.luckperms.common.commands.sender.SenderFactory; import me.lucko.luckperms.common.constants.Constants; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; @@ -72,6 +74,11 @@ public class BungeeSenderFactory extends SenderFactory { } } + @Override + protected Tristate getPermissionValue(CommandSender sender, String node) { + return TristateCheckEvent.call(sender, node); + } + @Override protected boolean hasPermission(CommandSender sender, String node) { return sender.hasPermission(node); diff --git a/bungee/src/main/java/me/lucko/luckperms/bungee/event/TristateCheckEvent.java b/bungee/src/main/java/me/lucko/luckperms/bungee/event/TristateCheckEvent.java new file mode 100644 index 000000000..e36a67a90 --- /dev/null +++ b/bungee/src/main/java/me/lucko/luckperms/bungee/event/TristateCheckEvent.java @@ -0,0 +1,62 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.lucko.luckperms.bungee.event; + +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; + +import me.lucko.luckperms.api.Tristate; + +import net.md_5.bungee.api.CommandSender; +import net.md_5.bungee.api.ProxyServer; +import net.md_5.bungee.api.plugin.Event; + +/** + * Copy of the internal BungeeCord "PermissionCheckEvent", returning a tristate instead of a boolean. + */ +@Getter +@AllArgsConstructor +@EqualsAndHashCode(callSuper = false) +@ToString +public class TristateCheckEvent extends Event { + public static Tristate call(CommandSender sender, String permission) { + return ProxyServer.getInstance().getPluginManager().callEvent(new TristateCheckEvent(sender, permission)).getResult(); + } + + private final CommandSender sender; + private final String permission; + + @Setter + private Tristate result; + + public TristateCheckEvent(CommandSender sender, String permission) { + this(sender, permission, sender.getPermissions().contains(permission) ? Tristate.TRUE : Tristate.UNDEFINED); + } + +} diff --git a/bungee/src/main/resources/config.yml b/bungee/src/main/resources/config.yml index f19a6e9b7..5cbaeb35e 100644 --- a/bungee/src/main/resources/config.yml +++ b/bungee/src/main/resources/config.yml @@ -104,6 +104,15 @@ temporary-add-behaviour: deny # directly and indirectly primary-group-calculation: stored +# If the plugin should check for "extra" permissions with users run LP commands. +# +# These extra permissions allow finer control over what users can do with each command, and +# who they have access to edit. +# +# The permissions are *not* static, unlike the 'base' permisssions, and will depend upon the +# arguments given within the command. +argument-based-command-permissions: false + diff --git a/bungee/src/main/resources/plugin.yml b/bungee/src/main/resources/plugin.yml index 5b71869aa..da91d647f 100644 --- a/bungee/src/main/resources/plugin.yml +++ b/bungee/src/main/resources/plugin.yml @@ -3,4 +3,4 @@ version: ${full.version} description: A permissions plugin author: Luck main: me.lucko.luckperms.bungee.LPBungeePlugin -softDepends: ["RedisBungee"] \ No newline at end of file +softDepends: ["RedisBungee"] diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/ArgumentPermissions.java b/common/src/main/java/me/lucko/luckperms/common/commands/ArgumentPermissions.java new file mode 100644 index 000000000..34e4e492f --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/common/commands/ArgumentPermissions.java @@ -0,0 +1,222 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.lucko.luckperms.common.commands; + +import lombok.experimental.UtilityClass; + +import me.lucko.luckperms.api.Tristate; +import me.lucko.luckperms.api.context.ContextSet; +import me.lucko.luckperms.common.commands.sender.Sender; +import me.lucko.luckperms.common.config.ConfigKeys; +import me.lucko.luckperms.common.constants.Permission; +import me.lucko.luckperms.common.core.model.Group; +import me.lucko.luckperms.common.core.model.Track; +import me.lucko.luckperms.common.core.model.User; +import me.lucko.luckperms.common.plugin.LuckPermsPlugin; + +import java.util.Map; +import java.util.function.BiFunction; +import java.util.function.Function; + +@UtilityClass +public class ArgumentPermissions { + private static final String USER_MODIFY_SELF = Permission.ROOT + "modify.user.self"; + private static final String USER_MODIFY_OTHERS = Permission.ROOT + "modify.user.others"; + private static final Function GROUP_MODIFY = s -> Permission.ROOT + "modify.group." + s; + private static final Function TRACK_MODIFY = s -> Permission.ROOT + "modify.track." + s; + + private static final String USER_VIEW_SELF = Permission.ROOT + "view.user.self"; + private static final String USER_VIEW_OTHERS = Permission.ROOT + "view.user.others"; + private static final Function GROUP_VIEW = s -> Permission.ROOT + "view.group." + s; + private static final Function TRACK_VIEW = s -> Permission.ROOT + "view.track." + s; + + private static final String CONTEXT_USE_GLOBAL = Permission.ROOT + "usecontext.global"; + private static final BiFunction CONTEXT_USE = (k, v) -> Permission.ROOT + "usecontext." + k + "." + v; + + public static boolean checkArguments(LuckPermsPlugin plugin, Sender sender, Permission base, String... args) { + if (!plugin.getConfiguration().get(ConfigKeys.USE_ARGUMENT_BASED_COMMAND_PERMISSIONS)) { + return false; + } + + if (args.length == 0) { + throw new IllegalStateException(); + } + + StringBuilder permission = new StringBuilder(base.getPrimaryPermission()); + for (String arg : args) { + permission.append(".").append(arg); + } + + return !sender.hasPermission(permission.toString()); + } + + public static boolean checkModifyPerms(LuckPermsPlugin plugin, Sender sender, Permission base, Object target) { + if (!plugin.getConfiguration().get(ConfigKeys.USE_ARGUMENT_BASED_COMMAND_PERMISSIONS)) { + return false; + } + + if (target instanceof User) { + User targetUser = ((User) target); + + if (plugin.getUuidCache().getExternalUUID(targetUser.getUuid()).equals(sender.getUuid())) { + // the sender is trying to edit themselves + Tristate ret = sender.getPermissionValue(base.getPrimaryPermission() + ".modify.self"); + if (ret != Tristate.UNDEFINED) { + return !ret.asBoolean(); + } else { + // fallback to the global perm if the one for the specific command is undefined + Tristate globalRet = sender.getPermissionValue(USER_MODIFY_SELF); + return !globalRet.asBoolean(); + } + } else { + // they're trying to edit another user + Tristate ret = sender.getPermissionValue(base.getPrimaryPermission() + ".modify.others"); + if (ret != Tristate.UNDEFINED) { + return !ret.asBoolean(); + } else { + // fallback to the global perm if the one for the specific command is undefined + Tristate globalRet = sender.getPermissionValue(USER_MODIFY_OTHERS); + return !globalRet.asBoolean(); + } + } + } else if (target instanceof Group) { + Group targetGroup = ((Group) target); + + Tristate ret = sender.getPermissionValue(base.getPrimaryPermission() + ".modify." + targetGroup.getName()); + if (ret != Tristate.UNDEFINED) { + return !ret.asBoolean(); + } else { + // fallback to the global perm if the one for the specific command is undefined + Tristate globalRet = sender.getPermissionValue(GROUP_MODIFY.apply(targetGroup.getName())); + return !globalRet.asBoolean(); + } + } else if (target instanceof Track) { + Track targetTrack = ((Track) target); + + Tristate ret = sender.getPermissionValue(base.getPrimaryPermission() + ".modify." + targetTrack.getName()); + if (ret != Tristate.UNDEFINED) { + return !ret.asBoolean(); + } else { + // fallback to the global perm if the one for the specific command is undefined + Tristate globalRet = sender.getPermissionValue(TRACK_MODIFY.apply(targetTrack.getName())); + return !globalRet.asBoolean(); + } + } else { + throw new IllegalStateException(); + } + } + + public static boolean checkViewPerms(LuckPermsPlugin plugin, Sender sender, Permission base, Object target) { + if (!plugin.getConfiguration().get(ConfigKeys.USE_ARGUMENT_BASED_COMMAND_PERMISSIONS)) { + return false; + } + + if (target instanceof User) { + User targetUser = ((User) target); + + if (plugin.getUuidCache().getExternalUUID(targetUser.getUuid()).equals(sender.getUuid())) { + // the sender is trying to view themselves + Tristate ret = sender.getPermissionValue(base.getPrimaryPermission() + ".view.self"); + if (ret != Tristate.UNDEFINED) { + return !ret.asBoolean(); + } else { + // fallback to the global perm if the one for the specific command is undefined + Tristate globalRet = sender.getPermissionValue(USER_VIEW_SELF); + return !globalRet.asBoolean(); + } + } else { + // they're trying to view another user + Tristate ret = sender.getPermissionValue(base.getPrimaryPermission() + ".view.others"); + if (ret != Tristate.UNDEFINED) { + return !ret.asBoolean(); + } else { + // fallback to the global perm if the one for the specific command is undefined + Tristate globalRet = sender.getPermissionValue(USER_VIEW_OTHERS); + return !globalRet.asBoolean(); + } + } + } else if (target instanceof Group) { + Group targetGroup = ((Group) target); + + Tristate ret = sender.getPermissionValue(base.getPrimaryPermission() + ".view." + targetGroup.getName()); + if (ret != Tristate.UNDEFINED) { + return !ret.asBoolean(); + } else { + // fallback to the global perm if the one for the specific command is undefined + Tristate globalRet = sender.getPermissionValue(GROUP_VIEW.apply(targetGroup.getName())); + return !globalRet.asBoolean(); + } + } else if (target instanceof Track) { + Track targetTrack = ((Track) target); + + Tristate ret = sender.getPermissionValue(base.getPrimaryPermission() + ".view." + targetTrack.getName()); + if (ret != Tristate.UNDEFINED) { + return !ret.asBoolean(); + } else { + // fallback to the global perm if the one for the specific command is undefined + Tristate globalRet = sender.getPermissionValue(TRACK_VIEW.apply(targetTrack.getName())); + return !globalRet.asBoolean(); + } + } + + return false; + } + + public static boolean checkContext(LuckPermsPlugin plugin, Sender sender, Permission base, ContextSet contextSet) { + if (!plugin.getConfiguration().get(ConfigKeys.USE_ARGUMENT_BASED_COMMAND_PERMISSIONS)) { + return false; + } + + if (contextSet.isEmpty()) { + Tristate ret = sender.getPermissionValue(base.getPrimaryPermission() + ".usecontext.global"); + if (ret != Tristate.UNDEFINED) { + return !ret.asBoolean(); + } else { + // fallback to the global perm if the one for the specific command is undefined + Tristate globalRet = sender.getPermissionValue(CONTEXT_USE_GLOBAL); + return !globalRet.asBoolean(); + } + } + + for (Map.Entry context : contextSet.toSet()) { + Tristate ret = sender.getPermissionValue(base.getPrimaryPermission() + ".usecontext." + context.getKey() + "." + context.getValue()); + if (ret != Tristate.UNDEFINED) { + if (ret == Tristate.FALSE) { + return true; + } + } else { + // fallback to the global perm if the one for the specific command is undefined + Tristate globalRet = sender.getPermissionValue(CONTEXT_USE.apply(context.getKey(), context.getValue())); + if (globalRet == Tristate.FALSE) { + return true; + } + } + } + + return false; + } + +} diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/CommandManager.java b/common/src/main/java/me/lucko/luckperms/common/commands/CommandManager.java index 93e4a4b1e..25257dd74 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/CommandManager.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/CommandManager.java @@ -253,7 +253,7 @@ public class CommandManager { .filter(c -> c.isAuthorized(sender)) .forEach(c -> { @SuppressWarnings("unchecked") - String permission = (String) c.getPermission().map(p -> ((Permission) p).getExample()).orElse("None"); + String permission = (String) c.getPermission().map(p -> ((Permission) p).getPrimaryPermission()).orElse("None"); Component component = ComponentSerializer.parseFromLegacy("&3> &a" + String.format(c.getUsage(), label), Constants.FORMAT_CHAR) .applyRecursively(comp -> { diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/abstraction/MainCommand.java b/common/src/main/java/me/lucko/luckperms/common/commands/abstraction/MainCommand.java index 4c42c9fb8..92e55350e 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/abstraction/MainCommand.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/abstraction/MainCommand.java @@ -174,7 +174,7 @@ public abstract class MainCommand extends Command { @Override public boolean isAuthorized(Sender sender) { - return getChildren().get().stream().filter(sc -> sc.isAuthorized(sender)).count() != 0; + return getChildren().get().stream().anyMatch(sc -> sc.isAuthorized(sender)); } @Override diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/abstraction/SharedMainCommand.java b/common/src/main/java/me/lucko/luckperms/common/commands/abstraction/SharedMainCommand.java index e1e58ef0f..df515898b 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/abstraction/SharedMainCommand.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/abstraction/SharedMainCommand.java @@ -97,7 +97,7 @@ public class SharedMainCommand extends SubCommand CommandResult result; try { - result = sub.execute(plugin, sender, t, strippedArgs, label); + result = sub.execute(plugin, sender, t, strippedArgs, label, user ? sub.getUserPermission() : sub.getGroupPermission()); } catch (CommandException e) { result = handleException(e, sender, sub); } @@ -137,7 +137,7 @@ public class SharedMainCommand extends SubCommand @Override public boolean isAuthorized(Sender sender) { - return secondaryCommands.stream().filter(sc -> sc.isAuthorized(sender, user)).count() != 0; + return secondaryCommands.stream().anyMatch(sc -> sc.isAuthorized(sender, user)); } private void sendUsageDetailed(Sender sender, boolean user, String label) { diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/abstraction/SharedSubCommand.java b/common/src/main/java/me/lucko/luckperms/common/commands/abstraction/SharedSubCommand.java index e2eec3afc..ff419967c 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/abstraction/SharedSubCommand.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/abstraction/SharedSubCommand.java @@ -76,7 +76,7 @@ public abstract class SharedSubCommand { this.argumentCheck = argumentCheck; } - public abstract CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label) throws CommandException; + public abstract CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label, Permission permission) throws CommandException; public List onTabComplete(LuckPermsPlugin plugin, Sender sender, List args) { return Collections.emptyList(); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaAddChatMeta.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaAddChatMeta.java index c7d5c4e79..15516ae37 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaAddChatMeta.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaAddChatMeta.java @@ -28,6 +28,7 @@ package me.lucko.luckperms.common.commands.impl.generic.meta; import me.lucko.luckperms.api.ChatMetaType; import me.lucko.luckperms.api.DataMutateResult; import me.lucko.luckperms.api.context.MutableContextSet; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SharedSubCommand; @@ -62,11 +63,21 @@ public class MetaAddChatMeta extends SharedSubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label) throws CommandException { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label, Permission permission) throws CommandException { + if (ArgumentPermissions.checkModifyPerms(plugin, sender, permission, holder)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + int priority = ArgumentUtils.handlePriority(0, args); String meta = ArgumentUtils.handleString(1, args); MutableContextSet context = ArgumentUtils.handleContext(2, args, plugin); + if (ArgumentPermissions.checkContext(plugin, sender, permission, context)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + DataMutateResult result = holder.setPermission(NodeFactory.makeChatMetaNode(type, priority, meta).withExtraContext(context).build()); if (result.asBoolean()) { Message.ADD_CHATMETA_SUCCESS.send(sender, holder.getFriendlyName(), type.name().toLowerCase(), meta, priority, Util.contextSetToString(context)); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaAddTempChatMeta.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaAddTempChatMeta.java index e15f6faac..6720e2d44 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaAddTempChatMeta.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaAddTempChatMeta.java @@ -29,6 +29,7 @@ import me.lucko.luckperms.api.ChatMetaType; import me.lucko.luckperms.api.DataMutateResult; import me.lucko.luckperms.api.Node; import me.lucko.luckperms.api.context.MutableContextSet; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SharedSubCommand; @@ -67,13 +68,23 @@ public class MetaAddTempChatMeta extends SharedSubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label) throws CommandException { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label, Permission permission) throws CommandException { + if (ArgumentPermissions.checkModifyPerms(plugin, sender, permission, holder)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + int priority = ArgumentUtils.handlePriority(0, args); String meta = ArgumentUtils.handleString(1, args); long duration = ArgumentUtils.handleDuration(2, args); MutableContextSet context = ArgumentUtils.handleContext(3, args, plugin); TemporaryModifier modifier = plugin.getConfiguration().get(ConfigKeys.TEMPORARY_ADD_BEHAVIOUR); + if (ArgumentPermissions.checkContext(plugin, sender, permission, context)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + Map.Entry ret = holder.setPermission(NodeFactory.makeChatMetaNode(type, priority, meta).setExpiry(duration).withExtraContext(context).build(), modifier); if (ret.getKey().asBoolean()) { diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaClear.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaClear.java index 71af9839c..ef1786d68 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaClear.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaClear.java @@ -26,6 +26,7 @@ package me.lucko.luckperms.common.commands.impl.generic.meta; import me.lucko.luckperms.api.context.MutableContextSet; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SharedSubCommand; @@ -50,11 +51,21 @@ public class MetaClear extends SharedSubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label) throws CommandException { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label, Permission permission) throws CommandException { + if (ArgumentPermissions.checkModifyPerms(plugin, sender, permission, holder)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + int before = holder.getNodes().size(); MutableContextSet context = ArgumentUtils.handleContext(0, args, plugin); + if (ArgumentPermissions.checkContext(plugin, sender, permission, context)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + if (context.isEmpty()) { holder.clearMeta(); } else { diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaInfo.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaInfo.java index 9cb938c88..6157028d3 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaInfo.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaInfo.java @@ -26,6 +26,7 @@ package me.lucko.luckperms.common.commands.impl.generic.meta; import me.lucko.luckperms.api.LocalizedNode; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SharedSubCommand; @@ -62,7 +63,12 @@ public class MetaInfo extends SharedSubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label) throws CommandException { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label, Permission permission) throws CommandException { + if (ArgumentPermissions.checkViewPerms(plugin, sender, permission, holder)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + SortedSet> prefixes = new TreeSet<>(MetaComparator.INSTANCE.reversed()); SortedSet> suffixes = new TreeSet<>(MetaComparator.INSTANCE.reversed()); Set meta = new HashSet<>(); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaRemoveChatMeta.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaRemoveChatMeta.java index 60cb6b334..1dd0b04c9 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaRemoveChatMeta.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaRemoveChatMeta.java @@ -28,6 +28,7 @@ package me.lucko.luckperms.common.commands.impl.generic.meta; import me.lucko.luckperms.api.ChatMetaType; import me.lucko.luckperms.api.DataMutateResult; import me.lucko.luckperms.api.context.MutableContextSet; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SharedSubCommand; @@ -62,11 +63,21 @@ public class MetaRemoveChatMeta extends SharedSubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label) throws CommandException { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label, Permission permission) throws CommandException { + if (ArgumentPermissions.checkModifyPerms(plugin, sender, permission, holder)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + int priority = ArgumentUtils.handlePriority(0, args); String meta = ArgumentUtils.handleStringOrElse(1, args, "null"); MutableContextSet context = ArgumentUtils.handleContext(2, args, plugin); + if (ArgumentPermissions.checkContext(plugin, sender, permission, context)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + // Handle bulk removal if (meta.equalsIgnoreCase("null") || meta.equals("*")) { holder.removeIf(n -> diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaRemoveTempChatMeta.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaRemoveTempChatMeta.java index 99a6baf86..03def0f82 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaRemoveTempChatMeta.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaRemoveTempChatMeta.java @@ -28,6 +28,7 @@ package me.lucko.luckperms.common.commands.impl.generic.meta; import me.lucko.luckperms.api.ChatMetaType; import me.lucko.luckperms.api.DataMutateResult; import me.lucko.luckperms.api.context.MutableContextSet; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SharedSubCommand; @@ -62,11 +63,21 @@ public class MetaRemoveTempChatMeta extends SharedSubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label) throws CommandException { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label, Permission permission) throws CommandException { + if (ArgumentPermissions.checkModifyPerms(plugin, sender, permission, holder)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + int priority = ArgumentUtils.handlePriority(0, args); String meta = ArgumentUtils.handleStringOrElse(1, args, "null"); MutableContextSet context = ArgumentUtils.handleContext(2, args, plugin); + if (ArgumentPermissions.checkContext(plugin, sender, permission, context)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + // Handle bulk removal if (meta.equalsIgnoreCase("null") || meta.equals("*")) { holder.removeIf(n -> diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaSet.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaSet.java index fafbab253..4626358bc 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaSet.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaSet.java @@ -27,6 +27,7 @@ package me.lucko.luckperms.common.commands.impl.generic.meta; import me.lucko.luckperms.api.Node; import me.lucko.luckperms.api.context.MutableContextSet; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SharedSubCommand; @@ -52,11 +53,26 @@ public class MetaSet extends SharedSubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label) throws CommandException { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label, Permission permission) throws CommandException { + if (ArgumentPermissions.checkModifyPerms(plugin, sender, permission, holder)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + String key = args.get(0); String value = args.get(1); MutableContextSet context = ArgumentUtils.handleContext(2, args, plugin); + if (ArgumentPermissions.checkContext(plugin, sender, permission, context)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + + if (ArgumentPermissions.checkArguments(plugin, sender, permission, key)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + Node n = NodeFactory.makeMetaNode(key, value).withExtraContext(context).build(); if (holder.hasPermission(n).asBoolean()) { diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaSetTemp.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaSetTemp.java index 2206cbeef..c3c97b36a 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaSetTemp.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaSetTemp.java @@ -27,6 +27,7 @@ package me.lucko.luckperms.common.commands.impl.generic.meta; import me.lucko.luckperms.api.Node; import me.lucko.luckperms.api.context.MutableContextSet; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SharedSubCommand; @@ -55,13 +56,28 @@ public class MetaSetTemp extends SharedSubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label) throws CommandException { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label, Permission permission) throws CommandException { + if (ArgumentPermissions.checkModifyPerms(plugin, sender, permission, holder)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + String key = args.get(0); String value = args.get(1); long duration = ArgumentUtils.handleDuration(2, args); MutableContextSet context = ArgumentUtils.handleContext(2, args, plugin); TemporaryModifier modifier = plugin.getConfiguration().get(ConfigKeys.TEMPORARY_ADD_BEHAVIOUR); + if (ArgumentPermissions.checkContext(plugin, sender, permission, context)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + + if (ArgumentPermissions.checkArguments(plugin, sender, permission, key)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + Node n = NodeFactory.makeMetaNode(key, value).withExtraContext(context).setExpiry(duration).build(); if (holder.hasPermission(n).asBoolean()) { diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaUnset.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaUnset.java index 1b006afd3..2047fd02f 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaUnset.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaUnset.java @@ -26,6 +26,7 @@ package me.lucko.luckperms.common.commands.impl.generic.meta; import me.lucko.luckperms.api.context.MutableContextSet; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SharedSubCommand; @@ -50,10 +51,25 @@ public class MetaUnset extends SharedSubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label) throws CommandException { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label, Permission permission) throws CommandException { + if (ArgumentPermissions.checkModifyPerms(plugin, sender, permission, holder)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + String key = args.get(0); MutableContextSet context = ArgumentUtils.handleContext(1, args, plugin); + if (ArgumentPermissions.checkContext(plugin, sender, permission, context)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + + if (ArgumentPermissions.checkArguments(plugin, sender, permission, key)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + holder.clearMetaKeys(key, context, false); Message.UNSET_META_SUCCESS.send(sender, key, holder.getFriendlyName(), Util.contextSetToString(context)); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaUnsetTemp.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaUnsetTemp.java index 4c0df0d8d..5875272dd 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaUnsetTemp.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaUnsetTemp.java @@ -26,6 +26,7 @@ package me.lucko.luckperms.common.commands.impl.generic.meta; import me.lucko.luckperms.api.context.MutableContextSet; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SharedSubCommand; @@ -50,10 +51,25 @@ public class MetaUnsetTemp extends SharedSubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label) throws CommandException { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label, Permission permission) throws CommandException { + if (ArgumentPermissions.checkModifyPerms(plugin, sender, permission, holder)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + String key = args.get(0); MutableContextSet context = ArgumentUtils.handleContext(1, args, plugin); + if (ArgumentPermissions.checkContext(plugin, sender, permission, context)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + + if (ArgumentPermissions.checkArguments(plugin, sender, permission, key)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + holder.clearMetaKeys(key, context, true); Message.UNSET_META_TEMP_SUCCESS.send(sender, key, holder.getFriendlyName(), Util.contextSetToString(context)); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/other/HolderClear.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/other/HolderClear.java index 578ced635..50e9156de 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/other/HolderClear.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/other/HolderClear.java @@ -26,6 +26,7 @@ package me.lucko.luckperms.common.commands.impl.generic.other; import me.lucko.luckperms.api.context.MutableContextSet; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SubCommand; @@ -53,10 +54,20 @@ public class HolderClear extends SubCommand { @Override public CommandResult execute(LuckPermsPlugin plugin, Sender sender, T holder, List args, String label) throws CommandException { + if (ArgumentPermissions.checkModifyPerms(plugin, sender, getPermission().get(), holder)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + int before = holder.getNodes().size(); MutableContextSet context = ArgumentUtils.handleContext(0, args, plugin); + if (ArgumentPermissions.checkContext(plugin, sender, getPermission().get(), context)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + if (context.isEmpty()) { holder.clearNodes(); } else { diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/other/HolderEditor.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/other/HolderEditor.java index 1433e84b2..5369cd6c2 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/other/HolderEditor.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/other/HolderEditor.java @@ -32,6 +32,7 @@ import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.stream.JsonWriter; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SubCommand; @@ -76,6 +77,11 @@ public class HolderEditor extends SubCommand { @Override public CommandResult execute(LuckPermsPlugin plugin, Sender sender, T holder, List args, String label) throws CommandException { + if (ArgumentPermissions.checkViewPerms(plugin, sender, getPermission().get(), holder)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + JsonObject data = new JsonObject(); Set nodes = holder.getNodes().values().stream().map(NodeModel::fromNode).collect(Collectors.toCollection(LinkedHashSet::new)); data.add("nodes", serializePermissions(nodes)); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/other/HolderShowTracks.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/other/HolderShowTracks.java index 06477025b..bb922f428 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/other/HolderShowTracks.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/other/HolderShowTracks.java @@ -26,6 +26,7 @@ package me.lucko.luckperms.common.commands.impl.generic.other; import me.lucko.luckperms.api.Node; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SubCommand; @@ -50,6 +51,11 @@ public class HolderShowTracks extends SubCommand @Override public CommandResult execute(LuckPermsPlugin plugin, Sender sender, T holder, List args, String label) throws CommandException { + if (ArgumentPermissions.checkViewPerms(plugin, sender, getPermission().get(), holder)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + if (!plugin.getStorage().loadAllTracks().join()) { Message.TRACKS_LOAD_ERROR.send(sender); return CommandResult.LOADING_ERROR; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentAdd.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentAdd.java index 2a89d4af1..f970b62b7 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentAdd.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentAdd.java @@ -27,6 +27,7 @@ package me.lucko.luckperms.common.commands.impl.generic.parent; import me.lucko.luckperms.api.DataMutateResult; import me.lucko.luckperms.api.context.MutableContextSet; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SharedSubCommand; @@ -54,7 +55,12 @@ public class ParentAdd extends SharedSubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label) throws CommandException { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label, Permission permission) throws CommandException { + if (ArgumentPermissions.checkModifyPerms(plugin, sender, permission, holder)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + String groupName = ArgumentUtils.handleName(0, args); MutableContextSet context = ArgumentUtils.handleContext(1, args, plugin); @@ -69,6 +75,16 @@ public class ParentAdd extends SharedSubCommand { return CommandResult.LOADING_ERROR; } + if (ArgumentPermissions.checkContext(plugin, sender, permission, context)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + + if (ArgumentPermissions.checkArguments(plugin, sender, permission, group.getName())) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + DataMutateResult result = holder.setInheritGroup(group, context); if (result.asBoolean()) { diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentAddTemp.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentAddTemp.java index 8a3dd1894..796c761c4 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentAddTemp.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentAddTemp.java @@ -28,6 +28,7 @@ package me.lucko.luckperms.common.commands.impl.generic.parent; import me.lucko.luckperms.api.DataMutateResult; import me.lucko.luckperms.api.Node; import me.lucko.luckperms.api.context.MutableContextSet; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SharedSubCommand; @@ -60,7 +61,12 @@ public class ParentAddTemp extends SharedSubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label) throws CommandException { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label, Permission permission) throws CommandException { + if (ArgumentPermissions.checkModifyPerms(plugin, sender, permission, holder)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + String groupName = ArgumentUtils.handleName(0, args); long duration = ArgumentUtils.handleDuration(1, args); MutableContextSet context = ArgumentUtils.handleContext(2, args, plugin); @@ -77,6 +83,16 @@ public class ParentAddTemp extends SharedSubCommand { return CommandResult.INVALID_ARGS; } + if (ArgumentPermissions.checkContext(plugin, sender, permission, context)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + + if (ArgumentPermissions.checkArguments(plugin, sender, permission, group.getName())) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + if (group.getName().equalsIgnoreCase(holder.getObjectName())) { Message.ALREADY_TEMP_INHERITS.send(sender, holder.getFriendlyName(), group.getDisplayName()); return CommandResult.STATE_ERROR; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentClear.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentClear.java index fb550f30d..c4d2b6700 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentClear.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentClear.java @@ -26,6 +26,7 @@ package me.lucko.luckperms.common.commands.impl.generic.parent; import me.lucko.luckperms.api.context.MutableContextSet; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SharedSubCommand; @@ -50,11 +51,21 @@ public class ParentClear extends SharedSubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label) throws CommandException { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label, Permission permission) throws CommandException { + if (ArgumentPermissions.checkModifyPerms(plugin, sender, permission, holder)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + int before = holder.getNodes().size(); MutableContextSet context = ArgumentUtils.handleContext(0, args, plugin); + if (ArgumentPermissions.checkContext(plugin, sender, permission, context)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + if (context.isEmpty()) { holder.clearParents(true); } else { diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentClearTrack.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentClearTrack.java index de70215aa..89c1d9635 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentClearTrack.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentClearTrack.java @@ -26,6 +26,7 @@ package me.lucko.luckperms.common.commands.impl.generic.parent; import me.lucko.luckperms.api.context.MutableContextSet; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SharedSubCommand; @@ -55,7 +56,12 @@ public class ParentClearTrack extends SharedSubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label) throws CommandException { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label, Permission permission) throws CommandException { + if (ArgumentPermissions.checkModifyPerms(plugin, sender, permission, holder)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + final String trackName = args.get(0).toLowerCase(); if (!DataConstraints.TRACK_NAME_TEST.test(trackName)) { Message.TRACK_INVALID_ENTRY.send(sender); @@ -81,6 +87,17 @@ public class ParentClearTrack extends SharedSubCommand { int before = holder.getNodes().size(); MutableContextSet context = ArgumentUtils.handleContext(1, args, plugin); + + if (ArgumentPermissions.checkContext(plugin, sender, permission, context)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + + if (ArgumentPermissions.checkArguments(plugin, sender, permission, track.getName())) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + if (context.isEmpty()) { holder.removeIf(node -> node.isGroupNode() && track.containsGroup(node.getGroupName())); } else { diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentInfo.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentInfo.java index 3a1455e02..7c0e2a896 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentInfo.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentInfo.java @@ -27,6 +27,7 @@ package me.lucko.luckperms.common.commands.impl.generic.parent; import me.lucko.luckperms.api.LocalizedNode; import me.lucko.luckperms.api.Node; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SharedSubCommand; @@ -50,7 +51,12 @@ public class ParentInfo extends SharedSubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label) throws CommandException { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label, Permission permission) throws CommandException { + if (ArgumentPermissions.checkViewPerms(plugin, sender, permission, holder)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + Message.LISTPARENTS.send(sender, holder.getFriendlyName(), permGroupsToString(holder.mergePermissionsToSortedSet())); Message.LISTPARENTS_TEMP.send(sender, holder.getFriendlyName(), tempGroupsToString(holder.mergePermissionsToSortedSet())); return CommandResult.SUCCESS; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentRemove.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentRemove.java index 38bd0375f..ea73d81ed 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentRemove.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentRemove.java @@ -27,6 +27,7 @@ package me.lucko.luckperms.common.commands.impl.generic.parent; import me.lucko.luckperms.api.DataMutateResult; import me.lucko.luckperms.api.context.MutableContextSet; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SharedSubCommand; @@ -56,10 +57,25 @@ public class ParentRemove extends SharedSubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label) throws CommandException { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label, Permission permission) throws CommandException { + if (ArgumentPermissions.checkModifyPerms(plugin, sender, permission, holder)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + String groupName = ArgumentUtils.handleNameWithSpace(0, args); MutableContextSet context = ArgumentUtils.handleContext(1, args, plugin); + if (ArgumentPermissions.checkContext(plugin, sender, permission, context)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + + if (ArgumentPermissions.checkArguments(plugin, sender, permission, groupName)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + if (holder instanceof User) { User user = (User) holder; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentRemoveTemp.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentRemoveTemp.java index 5cddb038f..3245cd2f9 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentRemoveTemp.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentRemoveTemp.java @@ -27,6 +27,7 @@ package me.lucko.luckperms.common.commands.impl.generic.parent; import me.lucko.luckperms.api.DataMutateResult; import me.lucko.luckperms.api.context.MutableContextSet; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SharedSubCommand; @@ -54,10 +55,25 @@ public class ParentRemoveTemp extends SharedSubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label) throws CommandException { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label, Permission permission) throws CommandException { + if (ArgumentPermissions.checkModifyPerms(plugin, sender, permission, holder)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + String groupName = ArgumentUtils.handleNameWithSpace(0, args); MutableContextSet context = ArgumentUtils.handleContext(1, args, plugin); + if (ArgumentPermissions.checkContext(plugin, sender, permission, context)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + + if (ArgumentPermissions.checkArguments(plugin, sender, permission, groupName)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + DataMutateResult result = holder.unsetPermission(NodeFactory.newBuilder("group." + groupName).setExpiry(10L).withExtraContext(context).build()); if (result.asBoolean()) { diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentSet.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentSet.java index 3e88dab69..6d2e84ef8 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentSet.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentSet.java @@ -26,6 +26,7 @@ package me.lucko.luckperms.common.commands.impl.generic.parent; import me.lucko.luckperms.api.context.MutableContextSet; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SharedSubCommand; @@ -54,7 +55,12 @@ public class ParentSet extends SharedSubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label) throws CommandException { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label, Permission permission) throws CommandException { + if (ArgumentPermissions.checkModifyPerms(plugin, sender, permission, holder)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + String groupName = ArgumentUtils.handleName(0, args); MutableContextSet context = ArgumentUtils.handleContext(1, args, plugin); @@ -69,6 +75,16 @@ public class ParentSet extends SharedSubCommand { return CommandResult.LOADING_ERROR; } + if (ArgumentPermissions.checkContext(plugin, sender, permission, context)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + + if (ArgumentPermissions.checkArguments(plugin, sender, permission, group.getName())) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + holder.clearParents(context, false); holder.setInheritGroup(group, context); if (holder instanceof User) { diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentSetTrack.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentSetTrack.java index d5aef3a0f..e95898cf7 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentSetTrack.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/parent/ParentSetTrack.java @@ -26,6 +26,7 @@ package me.lucko.luckperms.common.commands.impl.generic.parent; import me.lucko.luckperms.api.context.MutableContextSet; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SharedSubCommand; @@ -56,7 +57,12 @@ public class ParentSetTrack extends SharedSubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label) throws CommandException { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label, Permission permission) throws CommandException { + if (ArgumentPermissions.checkModifyPerms(plugin, sender, permission, holder)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + final String trackName = args.get(0).toLowerCase(); if (!DataConstraints.TRACK_NAME_TEST.test(trackName)) { Message.TRACK_INVALID_ENTRY.send(sender); @@ -109,6 +115,16 @@ public class ParentSetTrack extends SharedSubCommand { return CommandResult.LOADING_ERROR; } + if (ArgumentPermissions.checkContext(plugin, sender, permission, context)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + + if (ArgumentPermissions.checkArguments(plugin, sender, permission, track.getName(), group.getName())) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + holder.removeIf(node -> node.isGroupNode() && node.getFullContexts().equals(context) && track.containsGroup(node.getGroupName())); holder.setInheritGroup(group, context); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/permission/PermissionCheck.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/permission/PermissionCheck.java index d716b147b..08aa8b100 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/permission/PermissionCheck.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/permission/PermissionCheck.java @@ -27,6 +27,7 @@ package me.lucko.luckperms.common.commands.impl.generic.permission; import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.api.context.MutableContextSet; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SharedSubCommand; @@ -52,7 +53,12 @@ public class PermissionCheck extends SharedSubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label) throws CommandException { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label, Permission permission) throws CommandException { + if (ArgumentPermissions.checkViewPerms(plugin, sender, permission, holder)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + String node = ArgumentUtils.handleString(0, args); MutableContextSet context = ArgumentUtils.handleContext(1, args, plugin); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/permission/PermissionCheckInherits.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/permission/PermissionCheckInherits.java index 57a76943c..2cd973d2b 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/permission/PermissionCheckInherits.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/permission/PermissionCheckInherits.java @@ -26,6 +26,7 @@ package me.lucko.luckperms.common.commands.impl.generic.permission; import me.lucko.luckperms.api.context.MutableContextSet; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SharedSubCommand; @@ -52,7 +53,12 @@ public class PermissionCheckInherits extends SharedSubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label) throws CommandException { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label, Permission permission) throws CommandException { + if (ArgumentPermissions.checkViewPerms(plugin, sender, permission, holder)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + String node = ArgumentUtils.handleString(0, args); MutableContextSet context = ArgumentUtils.handleContext(1, args, plugin); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/permission/PermissionInfo.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/permission/PermissionInfo.java index 2c710a257..cc6ca15ed 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/permission/PermissionInfo.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/permission/PermissionInfo.java @@ -29,6 +29,7 @@ import com.google.common.collect.Maps; import me.lucko.luckperms.api.LocalizedNode; import me.lucko.luckperms.api.Node; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SharedSubCommand; @@ -66,7 +67,12 @@ public class PermissionInfo extends SharedSubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label) throws CommandException { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label, Permission permission) throws CommandException { + if (ArgumentPermissions.checkViewPerms(plugin, sender, permission, holder)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + String filter = null; if (args.size() == 1) { // it might be a filter, if it's a number, then it relates to a page. diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/permission/PermissionSet.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/permission/PermissionSet.java index 1f5286f53..009945795 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/permission/PermissionSet.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/permission/PermissionSet.java @@ -27,6 +27,7 @@ package me.lucko.luckperms.common.commands.impl.generic.permission; import me.lucko.luckperms.api.DataMutateResult; import me.lucko.luckperms.api.context.MutableContextSet; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SharedSubCommand; @@ -55,11 +56,26 @@ public class PermissionSet extends SharedSubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label) throws CommandException { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label, Permission permission) throws CommandException { + if (ArgumentPermissions.checkModifyPerms(plugin, sender, permission, holder)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + boolean b = ArgumentUtils.handleBoolean(1, args); String node = b ? ArgumentUtils.handleNode(0, args) : ArgumentUtils.handleString(0, args); MutableContextSet context = ArgumentUtils.handleContext(2, args, plugin); + if (ArgumentPermissions.checkContext(plugin, sender, permission, context)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + + if (ArgumentPermissions.checkArguments(plugin, sender, permission, node)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + DataMutateResult result = holder.setPermission(NodeFactory.newBuilder(node).setValue(b).withExtraContext(context).build()); if (result.asBoolean()) { diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/permission/PermissionSetTemp.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/permission/PermissionSetTemp.java index c130b27cf..958dd2481 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/permission/PermissionSetTemp.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/permission/PermissionSetTemp.java @@ -28,6 +28,7 @@ package me.lucko.luckperms.common.commands.impl.generic.permission; import me.lucko.luckperms.api.DataMutateResult; import me.lucko.luckperms.api.Node; import me.lucko.luckperms.api.context.MutableContextSet; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SharedSubCommand; @@ -60,12 +61,27 @@ public class PermissionSetTemp extends SharedSubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label) throws CommandException { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label, Permission permission) throws CommandException { + if (ArgumentPermissions.checkModifyPerms(plugin, sender, permission, holder)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + boolean b = ArgumentUtils.handleBoolean(1, args); String node = b ? ArgumentUtils.handleNode(0, args) : ArgumentUtils.handleString(0, args); long duration = ArgumentUtils.handleDuration(2, args); MutableContextSet context = ArgumentUtils.handleContext(3, args, plugin); + if (ArgumentPermissions.checkContext(plugin, sender, permission, context)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + + if (ArgumentPermissions.checkArguments(plugin, sender, permission, node)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + TemporaryModifier modifier = plugin.getConfiguration().get(ConfigKeys.TEMPORARY_ADD_BEHAVIOUR); Map.Entry result = holder.setPermission(NodeFactory.newBuilder(node).setValue(b).withExtraContext(context).setExpiry(duration).build(), modifier); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/permission/PermissionUnset.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/permission/PermissionUnset.java index dfa50b1eb..b5229fe8f 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/permission/PermissionUnset.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/permission/PermissionUnset.java @@ -27,6 +27,7 @@ package me.lucko.luckperms.common.commands.impl.generic.permission; import me.lucko.luckperms.api.DataMutateResult; import me.lucko.luckperms.api.context.MutableContextSet; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SharedSubCommand; @@ -54,10 +55,25 @@ public class PermissionUnset extends SharedSubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label) throws CommandException { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label, Permission permission) throws CommandException { + if (ArgumentPermissions.checkModifyPerms(plugin, sender, permission, holder)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + String node = ArgumentUtils.handleString(0, args); MutableContextSet context = ArgumentUtils.handleContext(1, args, plugin); + if (ArgumentPermissions.checkContext(plugin, sender, permission, context)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + + if (ArgumentPermissions.checkArguments(plugin, sender, permission, node)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + DataMutateResult result; if (node.startsWith("group.")) { // unset exact - with false value only diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/permission/PermissionUnsetTemp.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/permission/PermissionUnsetTemp.java index 3afbf78b7..aafd054e6 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/permission/PermissionUnsetTemp.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/permission/PermissionUnsetTemp.java @@ -27,6 +27,7 @@ package me.lucko.luckperms.common.commands.impl.generic.permission; import me.lucko.luckperms.api.DataMutateResult; import me.lucko.luckperms.api.context.MutableContextSet; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SharedSubCommand; @@ -54,10 +55,25 @@ public class PermissionUnsetTemp extends SharedSubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label) throws CommandException { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args, String label, Permission permission) throws CommandException { + if (ArgumentPermissions.checkModifyPerms(plugin, sender, permission, holder)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + String node = ArgumentUtils.handleString(0, args); MutableContextSet context = ArgumentUtils.handleContext(1, args, plugin); + if (ArgumentPermissions.checkContext(plugin, sender, permission, context)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + + if (ArgumentPermissions.checkArguments(plugin, sender, permission, node)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + DataMutateResult result = holder.unsetPermission(NodeFactory.newBuilder(node).setExpiry(10L).withExtraContext(context).build()); if (result.asBoolean()) { diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/group/GroupClone.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/group/GroupClone.java index 7c7357b34..0fbd928b0 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/group/GroupClone.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/group/GroupClone.java @@ -26,6 +26,7 @@ package me.lucko.luckperms.common.commands.impl.group; import me.lucko.luckperms.api.event.cause.CreationCause; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SubCommand; @@ -49,6 +50,11 @@ public class GroupClone extends SubCommand { @Override public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Group group, List args, String label) throws CommandException { + if (ArgumentPermissions.checkViewPerms(plugin, sender, getPermission().get(), group)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + String newGroupName = args.get(0).toLowerCase(); if (!DataConstraints.GROUP_NAME_TEST.test(newGroupName)) { Message.GROUP_INVALID_ENTRY.send(sender); @@ -63,6 +69,11 @@ public class GroupClone extends SubCommand { return CommandResult.LOADING_ERROR; } + if (ArgumentPermissions.checkModifyPerms(plugin, sender, getPermission().get(), newGroup)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + newGroup.replaceNodes(group.getNodes()); Message.CLONE_SUCCESS.send(sender, group.getName(), newGroup.getName()); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/group/GroupInfo.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/group/GroupInfo.java index 0ff2ea063..8cfff35cd 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/group/GroupInfo.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/group/GroupInfo.java @@ -26,6 +26,7 @@ package me.lucko.luckperms.common.commands.impl.group; import me.lucko.luckperms.api.Node; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SubCommand; @@ -51,6 +52,11 @@ public class GroupInfo extends SubCommand { @Override public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Group group, List args, String label) throws CommandException { + if (ArgumentPermissions.checkViewPerms(plugin, sender, getPermission().get(), group)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + Message.GROUP_INFO_GENERAL.send(sender, group.getId(), group.getDisplayName(), diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/group/GroupListMembers.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/group/GroupListMembers.java index a5c7f497b..de94d5854 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/group/GroupListMembers.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/group/GroupListMembers.java @@ -29,6 +29,7 @@ import com.google.common.collect.Maps; import me.lucko.luckperms.api.HeldPermission; import me.lucko.luckperms.api.Node; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SubCommand; @@ -70,6 +71,11 @@ public class GroupListMembers extends SubCommand { @Override public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Group group, List args, String label) throws CommandException { + if (ArgumentPermissions.checkViewPerms(plugin, sender, getPermission().get(), group)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + String query = "group." + group.getName(); int page = ArgumentUtils.handleIntOrElse(0, args, 1); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/group/GroupSetWeight.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/group/GroupSetWeight.java index 2dd901d1c..52f5d775b 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/group/GroupSetWeight.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/group/GroupSetWeight.java @@ -25,6 +25,7 @@ package me.lucko.luckperms.common.commands.impl.group; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SubCommand; @@ -48,6 +49,11 @@ public class GroupSetWeight extends SubCommand { @Override public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Group group, List args, String label) throws CommandException { + if (ArgumentPermissions.checkModifyPerms(plugin, sender, getPermission().get(), group)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + int weight = ArgumentUtils.handlePriority(0, args); group.removeIf(n -> n.getPermission().startsWith("weight.")); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/misc/ApplyEditsCommand.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/misc/ApplyEditsCommand.java index bd8fadba0..266dbb6b9 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/misc/ApplyEditsCommand.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/misc/ApplyEditsCommand.java @@ -32,6 +32,7 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import me.lucko.luckperms.api.context.ImmutableContextSet; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SharedSubCommand; @@ -134,6 +135,11 @@ public class ApplyEditsCommand extends SingleCommand { return CommandResult.STATE_ERROR; } + if (ArgumentPermissions.checkModifyPerms(plugin, sender, getPermission().get(), holder)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + Set nodes = deserializePermissions(data.getAsJsonArray("nodes")); holder.setNodes(nodes.stream().map(NodeModel::toNode).collect(Collectors.toSet())); Message.APPLY_EDITS_SUCCESS.send(sender, nodes.size(), holder.getFriendlyName()); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserDemote.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserDemote.java index c1cc88c75..1c2c808c3 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserDemote.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserDemote.java @@ -25,8 +25,11 @@ package me.lucko.luckperms.common.commands.impl.user; +import com.google.common.collect.Iterables; + import me.lucko.luckperms.api.Node; import me.lucko.luckperms.api.context.MutableContextSet; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SubCommand; @@ -58,6 +61,11 @@ public class UserDemote extends SubCommand { @Override public CommandResult execute(LuckPermsPlugin plugin, Sender sender, User user, List args, String label) throws CommandException { + if (ArgumentPermissions.checkModifyPerms(plugin, sender, getPermission().get(), user)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + final String trackName = args.get(0).toLowerCase(); if (!DataConstraints.TRACK_NAME_TEST.test(trackName)) { Message.TRACK_INVALID_ENTRY.send(sender); @@ -83,6 +91,11 @@ public class UserDemote extends SubCommand { boolean silent = args.remove("-s"); MutableContextSet context = ArgumentUtils.handleContext(1, args, plugin); + if (ArgumentPermissions.checkContext(plugin, sender, getPermission().get(), context)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + // Load applicable groups Set nodes = user.getNodes().values().stream() .filter(Node::isGroupNode) @@ -102,7 +115,7 @@ public class UserDemote extends SubCommand { return CommandResult.FAILURE; } - final Node oldNode = nodes.stream().findAny().get(); + final Node oldNode = Iterables.getFirst(nodes, null); final String old = oldNode.getGroupName(); final String previous; try { @@ -112,6 +125,11 @@ public class UserDemote extends SubCommand { return CommandResult.STATE_ERROR; } + if (ArgumentPermissions.checkArguments(plugin, sender, getPermission().get(), track.getName(), oldNode.getGroupName())) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + if (previous == null) { user.unsetPermission(oldNode); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserInfo.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserInfo.java index e6c9aae69..56b392a97 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserInfo.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserInfo.java @@ -28,6 +28,7 @@ package me.lucko.luckperms.common.commands.impl.user; import me.lucko.luckperms.api.Contexts; import me.lucko.luckperms.api.Node; import me.lucko.luckperms.api.caching.MetaData; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SubCommand; @@ -54,6 +55,11 @@ public class UserInfo extends SubCommand { @SuppressWarnings("unchecked") @Override public CommandResult execute(LuckPermsPlugin plugin, Sender sender, User user, List args, String label) throws CommandException { + if (ArgumentPermissions.checkViewPerms(plugin, sender, getPermission().get(), user)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + Message.USER_INFO_GENERAL.send(sender, user.getName().orElse("Unknown"), user.getUuid(), diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserPromote.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserPromote.java index 5801f954c..37a74ff62 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserPromote.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserPromote.java @@ -27,6 +27,7 @@ package me.lucko.luckperms.common.commands.impl.user; import me.lucko.luckperms.api.Node; import me.lucko.luckperms.api.context.MutableContextSet; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SubCommand; @@ -58,6 +59,11 @@ public class UserPromote extends SubCommand { @Override public CommandResult execute(LuckPermsPlugin plugin, Sender sender, User user, List args, String label) throws CommandException { + if (ArgumentPermissions.checkModifyPerms(plugin, sender, getPermission().get(), user)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + final String trackName = args.get(0).toLowerCase(); if (!DataConstraints.TRACK_NAME_TEST.test(trackName)) { Message.TRACK_INVALID_ENTRY.send(sender); @@ -83,6 +89,11 @@ public class UserPromote extends SubCommand { boolean silent = args.remove("-s"); MutableContextSet context = ArgumentUtils.handleContext(1, args, plugin); + if (ArgumentPermissions.checkContext(plugin, sender, getPermission().get(), context)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + // Load applicable groups Set nodes = user.getNodes().values().stream() .filter(Node::isGroupNode) @@ -101,6 +112,11 @@ public class UserPromote extends SubCommand { return CommandResult.LOADING_ERROR; } + if (ArgumentPermissions.checkArguments(plugin, sender, getPermission().get(), track.getName(), nextGroup.getName())) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + user.setPermission(NodeFactory.newBuilder("group." + first).withExtraContext(context).build()); Message.USER_TRACK_ADDED_TO_FIRST.send(sender, user.getFriendlyName(), first, Util.contextSetToString(context)); @@ -143,6 +159,11 @@ public class UserPromote extends SubCommand { return CommandResult.LOADING_ERROR; } + if (ArgumentPermissions.checkArguments(plugin, sender, getPermission().get(), track.getName(), nextGroup.getName())) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + user.unsetPermission(oldNode); user.setPermission(NodeFactory.newBuilder("group." + nextGroup.getName()).withExtraContext(context).build()); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserSwitchPrimaryGroup.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserSwitchPrimaryGroup.java index 5583426ba..cde78a82e 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserSwitchPrimaryGroup.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserSwitchPrimaryGroup.java @@ -26,6 +26,7 @@ package me.lucko.luckperms.common.commands.impl.user; import me.lucko.luckperms.api.context.ContextSet; +import me.lucko.luckperms.common.commands.ArgumentPermissions; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SubCommand; @@ -50,6 +51,11 @@ public class UserSwitchPrimaryGroup extends SubCommand { @Override public CommandResult execute(LuckPermsPlugin plugin, Sender sender, User user, List args, String label) throws CommandException { + if (ArgumentPermissions.checkModifyPerms(plugin, sender, getPermission().get(), user)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + String opt = plugin.getConfiguration().get(ConfigKeys.PRIMARY_GROUP_CALCULATION_METHOD); if (!opt.equals("stored")) { Message.USER_PRIMARYGROUP_WARN_OPTION.send(sender, opt); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/sender/AbstractSender.java b/common/src/main/java/me/lucko/luckperms/common/commands/sender/AbstractSender.java index 6065f12d3..65a14ce6f 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/sender/AbstractSender.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/sender/AbstractSender.java @@ -30,6 +30,7 @@ import lombok.Getter; import com.google.common.base.Splitter; +import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.common.constants.Constants; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; @@ -94,6 +95,32 @@ public final class AbstractSender implements Sender { } } + @Override + public Tristate getPermissionValue(String permission) { + if (isConsole()) return Tristate.TRUE; + + T t = ref.get(); + if (t != null) { + return factory.getPermissionValue(t, permission); + } + + return Tristate.UNDEFINED; + } + + @Override + public boolean hasPermission(String permission) { + if (isConsole()) return true; + + T t = ref.get(); + if (t != null) { + if (factory.hasPermission(t, permission)) { + return true; + } + } + + return false; + } + @Override public boolean hasPermission(Permission permission) { if (isConsole()) return true; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/sender/Sender.java b/common/src/main/java/me/lucko/luckperms/common/commands/sender/Sender.java index 2ef768a42..b01af03b8 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/sender/Sender.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/sender/Sender.java @@ -25,6 +25,7 @@ package me.lucko.luckperms.common.commands.sender; +import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.common.constants.Constants; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; @@ -73,6 +74,22 @@ public interface Sender { */ void sendMessage(Component message); + /** + * Gets the tristate a permission is set to. + * + * @param permission the permission to check for + * @return a tristate + */ + Tristate getPermissionValue(String permission); + + /** + * Check if the Sender has a permission. + * + * @param permission the permission to check for + * @return true if the sender has the permission + */ + boolean hasPermission(String permission); + /** * Check if the Sender has a permission. * diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/sender/SenderFactory.java b/common/src/main/java/me/lucko/luckperms/common/commands/sender/SenderFactory.java index d6be834c3..295508166 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/sender/SenderFactory.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/sender/SenderFactory.java @@ -29,6 +29,7 @@ import lombok.AccessLevel; import lombok.Getter; import lombok.RequiredArgsConstructor; +import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import net.kyori.text.Component; @@ -54,6 +55,8 @@ public abstract class SenderFactory { protected abstract void sendMessage(T t, Component message); + protected abstract Tristate getPermissionValue(T t, String node); + protected abstract boolean hasPermission(T t, String node); public final Sender wrap(T t) { diff --git a/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java b/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java index eaedd8aeb..105ab4b7c 100644 --- a/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java +++ b/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java @@ -160,6 +160,11 @@ public class ConfigKeys { } })); + /** + * If the plugin should check for "extra" permissions with users run LP commands + */ + public static final ConfigKey USE_ARGUMENT_BASED_COMMAND_PERMISSIONS = BooleanKey.of("argument-based-command-permissions", false); + /** * If wildcards are being applied */ diff --git a/common/src/main/java/me/lucko/luckperms/common/constants/Permission.java b/common/src/main/java/me/lucko/luckperms/common/constants/Permission.java index c81dcb57d..9aead92bd 100644 --- a/common/src/main/java/me/lucko/luckperms/common/constants/Permission.java +++ b/common/src/main/java/me/lucko/luckperms/common/constants/Permission.java @@ -28,6 +28,8 @@ package me.lucko.luckperms.common.constants; import lombok.AllArgsConstructor; import lombok.Getter; +import com.google.common.base.Preconditions; + import me.lucko.luckperms.common.commands.sender.Sender; import me.lucko.luckperms.common.utils.ImmutableCollectors; @@ -163,7 +165,7 @@ public enum Permission { SPONGE_OPTION_UNSET(list("option.unset"), Type.SPONGE), SPONGE_OPTION_CLEAR(list("option.clear"), Type.SPONGE); - private static final String IDENTIFIER = "luckperms."; + public static final String ROOT = "luckperms."; private static List list(String... args) { return Arrays.asList(args); @@ -174,15 +176,16 @@ public enum Permission { Permission(List tags, Type type) { this.type = type; + Preconditions.checkArgument(tags != null && !tags.isEmpty()); if (type == Type.NONE) { - this.nodes = tags.stream().map(t -> IDENTIFIER + t).collect(ImmutableCollectors.toImmutableList()); + this.nodes = tags.stream().map(t -> ROOT + t).collect(ImmutableCollectors.toImmutableList()); } else { - this.nodes = tags.stream().map(t -> IDENTIFIER + type.getTag() + "." + t).collect(ImmutableCollectors.toImmutableList()); + this.nodes = tags.stream().map(t -> ROOT + type.getTag() + "." + t).collect(ImmutableCollectors.toImmutableList()); } } - public String getExample() { + public String getPrimaryPermission() { return nodes.get(0); } diff --git a/common/src/main/java/me/lucko/luckperms/common/data/Importer.java b/common/src/main/java/me/lucko/luckperms/common/data/Importer.java index 09915ccb4..d856efc24 100644 --- a/common/src/main/java/me/lucko/luckperms/common/data/Importer.java +++ b/common/src/main/java/me/lucko/luckperms/common/data/Importer.java @@ -32,6 +32,7 @@ import lombok.Setter; import com.google.common.base.Splitter; import com.google.common.collect.ImmutableSet; +import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.common.commands.CommandManager; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.sender.Sender; @@ -208,6 +209,16 @@ public class Importer implements Runnable { logMessage(ComponentSerializer.toLegacy(message, Constants.COLOR_CHAR)); } + @Override + public Tristate getPermissionValue(String permission) { + return Tristate.TRUE; + } + + @Override + public boolean hasPermission(String permission) { + return true; + } + @Override public boolean hasPermission(Permission permission) { return true; diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeSenderFactory.java b/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeSenderFactory.java index f046e0bc1..118be6f29 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeSenderFactory.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeSenderFactory.java @@ -25,9 +25,11 @@ package me.lucko.luckperms.sponge; +import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.common.commands.sender.SenderFactory; import me.lucko.luckperms.common.constants.Constants; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; +import me.lucko.luckperms.sponge.service.model.CompatibilityUtil; import net.kyori.text.Component; import net.kyori.text.serializer.ComponentSerializer; @@ -74,6 +76,11 @@ public class SpongeSenderFactory extends SenderFactory { } } + @Override + protected Tristate getPermissionValue(CommandSource source, String node) { + return CompatibilityUtil.convertTristate(source.getPermissionValue(source.getActiveContexts(), node)); + } + @Override protected boolean hasPermission(CommandSource source, String node) { return source.hasPermission(node); diff --git a/sponge/src/main/resources/luckperms.conf b/sponge/src/main/resources/luckperms.conf index 6003d2db5..bcf96b3ba 100644 --- a/sponge/src/main/resources/luckperms.conf +++ b/sponge/src/main/resources/luckperms.conf @@ -102,6 +102,15 @@ temporary-add-behaviour="deny" # directly and indirectly primary-group-calculation="parents-by-weight" +# If the plugin should check for "extra" permissions with users run LP commands. +# +# These extra permissions allow finer control over what users can do with each command, and +# who they have access to edit. +# +# The permissions are *not* static, unlike the 'base' permisssions, and will depend upon the +# arguments given within the command. +argument-based-command-permissions=false +