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
15 changed files with 223 additions and 16 deletions

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.
*