From 550df7f232e48f4db39ac5abfc3dcec1fdc1de26 Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Thu, 17 May 2018 17:46:54 +1000 Subject: [PATCH] Fixes #1952 --- .../bukkit/object/BukkitPlayer.java | 44 ++++++++++++++++--- .../plot/commands/FlagCmd.java | 27 ++++++++---- .../plot/object/PlotPlayer.java | 25 +++++++++++ .../plot/util/Permissions.java | 23 +--------- 4 files changed, 84 insertions(+), 35 deletions(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/object/BukkitPlayer.java b/Bukkit/src/main/java/com/plotsquared/bukkit/object/BukkitPlayer.java index 870658b0b..8762101ba 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/object/BukkitPlayer.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/object/BukkitPlayer.java @@ -1,14 +1,12 @@ package com.plotsquared.bukkit.object; import com.intellectualcrafters.plot.PS; +import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.object.Location; import com.intellectualcrafters.plot.object.PlotPlayer; -import com.intellectualcrafters.plot.util.EconHandler; -import com.intellectualcrafters.plot.util.PlotGameMode; -import com.intellectualcrafters.plot.util.PlotWeather; -import com.intellectualcrafters.plot.util.StringMan; -import com.intellectualcrafters.plot.util.UUIDHandler; +import com.intellectualcrafters.plot.util.*; import com.plotsquared.bukkit.util.BukkitUtil; +import org.apache.commons.lang.StringUtils; import org.bukkit.Effect; import org.bukkit.GameMode; import org.bukkit.WeatherType; @@ -18,6 +16,8 @@ import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; import java.util.UUID; + +import org.bukkit.permissions.PermissionAttachmentInfo; import org.bukkit.plugin.RegisteredListener; public class BukkitPlayer extends PlotPlayer { @@ -102,6 +102,40 @@ public class BukkitPlayer extends PlotPlayer { return this.player.hasPermission(permission); } + @Override + public int hasPermissionRange(String stub, int range) { + if (hasPermission(C.PERMISSION_ADMIN.s())) { + return Integer.MAX_VALUE; + } + String[] nodes = stub.split("\\."); + StringBuilder n = new StringBuilder(); + for (int i = 0; i < (nodes.length - 1); i++) { + n.append(nodes[i]).append("."); + if (!stub.equals(n + C.PERMISSION_STAR.s())) { + if (hasPermission(n + C.PERMISSION_STAR.s())) { + return Integer.MAX_VALUE; + } + } + } + if (hasPermission(stub + ".*")) { + return Integer.MAX_VALUE; + } + int max = 0; + String stubPlus = stub + "."; + for (PermissionAttachmentInfo attach : player.getEffectivePermissions()) { + String perm = attach.getPermission(); + if (perm.startsWith(stubPlus)) { + String end = perm.substring(stubPlus.length()); + if (MathMan.isInteger(end)) { + int val = Integer.parseInt(end); + if (val > range) return val; + if (val > max) max = val; + } + } + } + return max; + } + @Override public boolean isPermissionSet(String permission) { return this.player.isPermissionSet(permission); diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/FlagCmd.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/FlagCmd.java index f9d8c8df4..c357e71fe 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/FlagCmd.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/FlagCmd.java @@ -10,11 +10,7 @@ import com.intellectualcrafters.plot.flag.ListFlag; import com.intellectualcrafters.plot.object.Location; import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotPlayer; -import com.intellectualcrafters.plot.util.MainUtil; -import com.intellectualcrafters.plot.util.Permissions; -import com.intellectualcrafters.plot.util.PlotWeather; -import com.intellectualcrafters.plot.util.StringComparison; -import com.intellectualcrafters.plot.util.StringMan; +import com.intellectualcrafters.plot.util.*; import com.plotsquared.general.commands.CommandDeclaration; import java.util.ArrayList; @@ -33,6 +29,21 @@ import java.util.Map; permission = "plots.flag") public class FlagCmd extends SubCommand { + private boolean checkPermValue(PlotPlayer player, String key, String value) { + key = key.toLowerCase(); + value = value.toLowerCase(); + String perm = C.PERMISSION_SET_FLAG_KEY_VALUE.f(key.toLowerCase(), value.toLowerCase()); + if (MathMan.isInteger(value)) { + try { + int numeric = Integer.parseInt(value); + perm.substring(0, perm.length() - value.length() - 1); + return player.hasPermissionRange(perm, numeric) >= numeric; + + } catch (NumberFormatException ignore) {} + } + return Permissions.hasPermission(player, perm); + } + @Override public boolean onCommand(PlotPlayer player, String[] args) { @@ -108,7 +119,7 @@ public class FlagCmd extends SubCommand { return false; } String value = StringMan.join(Arrays.copyOfRange(args, 2, args.length), " "); - if (!Permissions.hasPermission(player, C.PERMISSION_SET_FLAG_KEY_VALUE.f(args[1].toLowerCase(), value.toLowerCase()))) { + if (!checkPermValue(player, args[1], value)) { MainUtil.sendMessage(player, C.NO_PERMISSION, C.PERMISSION_SET_FLAG_KEY_VALUE.f(args[1].toLowerCase(), value.toLowerCase())); return false; } @@ -140,7 +151,7 @@ public class FlagCmd extends SubCommand { return false; } for (String entry : args[2].split(",")) { - if (!Permissions.hasPermission(player, C.PERMISSION_SET_FLAG_KEY_VALUE.f(args[1].toLowerCase(), entry))) { + if (!checkPermValue(player, args[1], entry)) { MainUtil.sendMessage(player, C.NO_PERMISSION, C.PERMISSION_SET_FLAG_KEY_VALUE.f(args[1].toLowerCase(), entry)); return false; } @@ -185,7 +196,7 @@ public class FlagCmd extends SubCommand { return false; } for (String entry : args[2].split(",")) { - if (!Permissions.hasPermission(player, C.PERMISSION_SET_FLAG_KEY_VALUE.f(args[1].toLowerCase(), entry))) { + if (!checkPermValue(player, args[1], entry)) { MainUtil.sendMessage(player, C.NO_PERMISSION, C.PERMISSION_SET_FLAG_KEY_VALUE.f(args[1].toLowerCase(), entry)); return false; } diff --git a/Core/src/main/java/com/intellectualcrafters/plot/object/PlotPlayer.java b/Core/src/main/java/com/intellectualcrafters/plot/object/PlotPlayer.java index b1d3983bb..e397ecd3a 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/object/PlotPlayer.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/object/PlotPlayer.java @@ -135,6 +135,31 @@ public abstract class PlotPlayer implements CommandCaller, OfflinePlotPlayer { return Permissions.hasPermissionRange(this, "plots.cluster", Settings.Limit.MAX_PLOTS); } + public int hasPermissionRange(String stub, int range) { + if (hasPermission(C.PERMISSION_ADMIN.s())) { + return Integer.MAX_VALUE; + } + String[] nodes = stub.split("\\."); + StringBuilder n = new StringBuilder(); + for (int i = 0; i < (nodes.length - 1); i++) { + n.append(nodes[i]).append("."); + if (!stub.equals(n + C.PERMISSION_STAR.s())) { + if (hasPermission(n + C.PERMISSION_STAR.s())) { + return Integer.MAX_VALUE; + } + } + } + if (hasPermission(stub + ".*")) { + return Integer.MAX_VALUE; + } + for (int i = range; i > 0; i--) { + if (hasPermission(stub + "." + i)) { + return i; + } + } + return 0; + } + /** * Get the number of plots this player owns. * diff --git a/Core/src/main/java/com/intellectualcrafters/plot/util/Permissions.java b/Core/src/main/java/com/intellectualcrafters/plot/util/Permissions.java index 31686b511..e41cbf15d 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/util/Permissions.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/util/Permissions.java @@ -116,27 +116,6 @@ public class Permissions { * @return The highest permission they have within that range */ public static int hasPermissionRange(PlotPlayer player, String stub, int range) { - if (player.hasPermission(C.PERMISSION_ADMIN.s())) { - return Integer.MAX_VALUE; - } - String[] nodes = stub.split("\\."); - StringBuilder n = new StringBuilder(); - for (int i = 0; i < (nodes.length - 1); i++) { - n.append(nodes[i]).append("."); - if (!stub.equals(n + C.PERMISSION_STAR.s())) { - if (player.hasPermission(n + C.PERMISSION_STAR.s())) { - return Integer.MAX_VALUE; - } - } - } - if (player.hasPermission(stub + ".*")) { - return Integer.MAX_VALUE; - } - for (int i = range; i > 0; i--) { - if (player.hasPermission(stub + "." + i)) { - return i; - } - } - return 0; + return player.hasPermissionRange(stub, range); } }