Slightly change how flag permissions are handled (#3201)

This commit is contained in:
dordsor21 2021-08-14 14:13:01 +01:00 committed by GitHub
parent b841a7c03b
commit da4ae9f4f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 223 additions and 16 deletions

View File

@ -92,6 +92,16 @@ public class BukkitPermissionHandler implements PermissionHandler {
return player != null && player.hasPermission(permission);
}
@Override
public boolean hasKeyedPermission(
final @Nullable String world,
final @NonNull String stub,
final @NonNull String key
) {
final Player player = this.playerReference.get();
return player != null && (player.hasPermission(stub + "." + key) || player.hasPermission(stub + ".*"));
}
}
}

View File

@ -117,6 +117,26 @@ public class VaultPermissionHandler implements PermissionHandler {
return permissions.playerHas(world, offlinePlayer, permission);
}
@Override
public boolean hasKeyedPermission(
final @Nullable String world,
final @NonNull String stub,
final @NonNull String key
) {
if (permissions == null) {
return false;
}
if (world == null && offlinePlayer instanceof BukkitPlayer) {
return permissions.playerHas(
((BukkitPlayer) offlinePlayer).getPlatformPlayer(),
stub + ".*"
) || permissions.playerHas(((BukkitPlayer) offlinePlayer).getPlatformPlayer(), stub + "." + key);
}
return permissions.playerHas(world, offlinePlayer, stub + ".*") || permissions.playerHas(world, offlinePlayer,
stub + "." + key
);
}
}
}

View File

@ -82,4 +82,14 @@ public class BukkitOfflinePlayer implements OfflinePlotPlayer {
return this.permissionProfile.hasPermission(world, permission);
}
@Override
public boolean hasKeyedPermission(
final @Nullable String world,
final @NonNull String stub,
final @NonNull String key
) {
return this.permissionProfile.hasPermission(world, stub + "." + key) || this.permissionProfile.hasPermission(world,
stub + ".*");
}
}

View File

@ -319,45 +319,78 @@ permissions:
plots.flag.remove: true
plots.flag.list: true
plots.flag.info: true
plots.set.flag.titles: true
plots.set.flag.titles.*: true
plots.set.flag.greeting.*: true
plots.set.flag.farewell.*: true
plots.set.flag.description: true
plots.set.flag.greeting: true
plots.set.flag.farewell: true
plots.set.flag.notify-enter: true
plots.set.flag.notify-enter.*: true
plots.set.flag.notify-leave: true
plots.set.flag.notify-leave.*: true
plots.set.flag.feed: true
plots.set.flag.feed.*: true
plots.set.flag.heal: true
plots.set.flag.heal.*: true
plots.set.flag.invincible: true
plots.set.flag.invincible.*: true
plots.set.flag.instabreak: true
plots.set.flag.instabreak.*: true
plots.set.flag.fly: true
plots.set.flag.fly.*: true
plots.set.flag.gamemode: true
plots.set.flag.gamemode.creative: true
plots.set.flag.gamemode.survival: true
plots.set.flag.gamemode.adventure: true
plots.set.flag.time.*: true
plots.set.flag.time: true
plots.set.flag.weather: true
plots.set.flag.weather.*: true
plots.set.flag.music: true
plots.set.flag.music.*: true
plots.set.flag.disable-physics: true
plots.set.flag.disable-physics.*: true
plots.set.flag.pve: true
plots.set.flag.pve.*: true
plots.set.flag.pvp: true
plots.set.flag.pvp.*: true
plots.set.flag.explosion: true
plots.set.flag.explosion.*: true
plots.set.flag.hostile-interact: true
plots.set.flag.hostile-interact.*: true
plots.set.flag.hostile-attack: true
plots.set.flag.hostile-attack.*: true
plots.set.flag.player-interact: true
plots.set.flag.player-interact.*: true
plots.set.flag.animal-interact: true
plots.set.flag.animal-interact.*: true
plots.set.flag.animal-attack: true
plots.set.flag.animal-attack.*: true
plots.set.flag.tamed-interact: true
plots.set.flag.tamed-interact.*: true
plots.set.flag.tamed-attack: true
plots.set.flag.tamed-attack.*: true
plots.set.flag.misc-interact: true
plots.set.flag.misc-interact.*: true
plots.set.flag.hanging-place: true
plots.set.flag.hanging-place.*: true
plots.set.flag.hanging-break: true
plots.set.flag.hanging-break.*: true
plots.set.flag.vehicle-use: true
plots.set.flag.vehicle-use.*: true
plots.set.flag.vehicle-place: true
plots.set.flag.vehicle-place.*: true
plots.set.flag.vehicle-break: true
plots.set.flag.vehicle-break.*: true
plots.set.flag.place: true
plots.set.flag.place.*: true
plots.set.flag.break: true
plots.set.flag.break.*: true
plots.set.flag.use: true
plots.set.flag.use.*: true
plots.set.flag.forcefield: true
plots.set.flag.forcefield.*: true
plots.set.flag.price.*: true
plots.set.flag.price: true
plots.set.flag.no-worldedit: true
plots.set.flag.no-worldedit.*: true
plots.permpack.basicinbox:
default: false

View File

@ -102,22 +102,23 @@ public final class FlagCommand extends Command {
try {
int numeric = Integer.parseInt(value);
perm = perm.substring(0, perm.length() - value.length() - 1);
boolean result = false;
if (numeric > 0) {
int checkRange = PlotSquared.get().getPlatform().equalsIgnoreCase("bukkit") ?
numeric :
Settings.Limit.MAX_PLOTS;
final boolean result = player.hasPermissionRange(perm, checkRange) >= numeric;
if (!result) {
player.sendMessage(
TranslatableCaption.of("permission.no_permission"),
Template.of(
"node",
Permission.PERMISSION_SET_FLAG_KEY_VALUE.format(key.toLowerCase(), value.toLowerCase())
)
);
}
return result;
result = player.hasPermissionRange(perm, checkRange) >= numeric;
}
if (!result) {
player.sendMessage(
TranslatableCaption.of("permission.no_permission"),
Template.of(
"node",
perm
)
);
}
return result;
} catch (NumberFormatException ignore) {
}
} else if (flag instanceof final ListFlag<?, ?> listFlag) {
@ -147,7 +148,14 @@ public final class FlagCommand extends Command {
}
return true;
}
final boolean result = Permissions.hasPermission(player, perm);
boolean result;
String basePerm = Permission.PERMISSION_SET_FLAG_KEY.format(key.toLowerCase());
if (flag.isValuedPermission()) {
result = Permissions.hasKeyedPermission(player, basePerm, value);
} else {
result = Permissions.hasPermission(player, basePerm);
perm = basePerm;
}
if (!result) {
player.sendMessage(TranslatableCaption.of("permission.no_permission"), Template.of("node", perm));
}

View File

@ -39,4 +39,13 @@ public enum ConsolePermissionProfile implements PermissionProfile {
return true;
}
@Override
public boolean hasKeyedPermission(
final @Nullable String world,
final @NonNull String permission,
final @NonNull String key
) {
return true;
}
}

View File

@ -39,4 +39,13 @@ public enum NullPermissionProfile implements PermissionProfile {
return false;
}
@Override
public boolean hasKeyedPermission(
final @Nullable String world,
final @NonNull String permission,
final @NonNull String key
) {
return false;
}
}

View File

@ -45,6 +45,21 @@ public interface PermissionHolder {
return hasPermission(null, permission);
}
/**
* Check if the owner of the profile has a given (global) keyed permission. Checks both {@code permission.key}
* and {@code permission.*}
*
* @param permission Permission
* @param key Permission "key"
* @return {@code true} if the owner has the given permission, else {@code false}
*/
default boolean hasKeyedPermission(
final @NonNull String permission,
final @NonNull String key
) {
return hasKeyedPermission(null, permission, key);
}
/**
* Check the highest permission a PlotPlayer has within a specified range.<br>
* - Excessively high values will lag<br>
@ -92,4 +107,15 @@ public interface PermissionHolder {
*/
boolean hasPermission(@Nullable String world, @NonNull String permission);
/**
* Check if the owner of the profile has a given keyed permission. Checks both {@code permission.key}
* and {@code permission.*}
*
* @param world World name
* @param permission Permission
* @param key Permission "key"
* @return {@code true} if the owner has the given permission, else {@code false}
*/
boolean hasKeyedPermission(@Nullable String world, @NonNull String permission, @NonNull String key);
}

View File

@ -52,4 +52,33 @@ public interface PermissionProfile {
*/
boolean hasPermission(final @Nullable String world, @NonNull String permission);
/**
* Check if the owner of the profile has a given (global) keyed permission. Checks both {@code permission.key}
* and {@code permission.*}
*
* @param permission Permission
* @param key Permission "key"
* @return {@code true} if the owner has the given permission, else {@code false}
*/
default boolean hasKeyedPermission(
final @NonNull String permission,
final @NonNull String key
) {
return hasKeyedPermission(null, permission, key);
}
/**
* Check if the owner of the profile has a given keyed permission. Checks both {@code permission.key}
* and {@code permission.*}
*
* @param world World name
* @param permission Permission
* @param key Permission "key"
* @return {@code true} if the owner has the given permission, else {@code false}
*/
boolean hasKeyedPermission(
@Nullable String world, final @NonNull String permission,
final @NonNull String key
);
}

View File

@ -197,6 +197,15 @@ public abstract class PlotPlayer<P> implements CommandCaller, OfflinePlotPlayer,
return this.permissionProfile.hasPermission(world, permission);
}
@Override
public final boolean hasKeyedPermission(
final @Nullable String world,
final @NonNull String permission,
final @NonNull String key
) {
return this.permissionProfile.hasKeyedPermission(world, permission, key);
}
public abstract Actor toActor();
public abstract P getPlatformPlayer();

View File

@ -158,6 +158,15 @@ public abstract class PlotFlag<T, F extends PlotFlag<T, F>> {
return this.flagCategory;
}
/**
* Get if the flag's permission should check for values. E.g. plots.flag.set.music.VALUE
*
* @return if valued permission
*/
public boolean isValuedPermission() {
return true;
}
/**
* An example of a string that would parse into a valid
* flag value.

View File

@ -68,4 +68,9 @@ public abstract class DoubleFlag<F extends NumberFlag<Double, F>> extends Number
}
}
@Override
public boolean isValuedPermission() {
return false;
}
}

View File

@ -72,4 +72,9 @@ public abstract class LongFlag<F extends NumberFlag<Long, F>> extends NumberFlag
}
}
@Override
public boolean isValuedPermission() {
return false;
}
}

View File

@ -29,6 +29,10 @@ import com.plotsquared.core.configuration.caption.Caption;
import com.plotsquared.core.plot.flag.PlotFlag;
import org.checkerframework.checker.nullness.qual.NonNull;
/**
* Plot flag representing a string value.
* This should be used where strings are not "keys" themselves, e.g. when setting enums
*/
public abstract class StringFlag<F extends StringFlag<F>> extends PlotFlag<String, F> {
/**
@ -46,4 +50,9 @@ public abstract class StringFlag<F extends StringFlag<F>> extends PlotFlag<Strin
super(value, flagCategory, flagDescription);
}
@Override
public boolean isValuedPermission() {
return false;
}
}

View File

@ -66,6 +66,22 @@ public class Permissions {
return caller.hasPermission(permission);
}
/**
* Check if the owner of the profile has a given (global) keyed permission. Checks both {@code permission.key}
* and {@code permission.*}
*
* @param caller permission holder
* @param permission Permission
* @param key Permission "key"
* @return {@code true} if the owner has the given permission, else {@code false}
*/
public static boolean hasKeyedPermission(
final @NonNull PermissionHolder caller, final @NonNull String permission,
final @NonNull String key
) {
return caller.hasKeyedPermission(permission, key);
}
/**
* Checks if a PlotPlayer has a permission, and optionally send the no permission message if applicable.
*