Remove dumb permission mess.

This commit is contained in:
Alexander Söderberg 2020-07-24 12:44:04 +02:00
parent 5608c5a901
commit c9c62a1083
3 changed files with 15 additions and 70 deletions

View File

@ -89,6 +89,6 @@ public interface PermissionHolder {
* @param permission Permission * @param permission Permission
* @return {@code true} if the owner has the given permission, else {@code false} * @return {@code true} if the owner has the given permission, else {@code false}
*/ */
boolean hasPermission(@Nullable final String world, @Nonnull String permission); boolean hasPermission(@Nullable String world, @Nonnull String permission);
} }

View File

@ -25,16 +25,13 @@
*/ */
package com.plotsquared.core.util; package com.plotsquared.core.util;
import com.plotsquared.core.command.CommandCaller; import com.plotsquared.core.configuration.Caption;
import com.plotsquared.core.configuration.Captions; import com.plotsquared.core.configuration.Captions;
import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.player.MetaDataAccess;
import com.plotsquared.core.player.PlayerMetaDataKeys;
import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.permissions.PermissionHolder; import com.plotsquared.core.permissions.PermissionHolder;
import com.plotsquared.core.player.PlotPlayer;
import java.util.HashMap; import javax.annotation.Nonnull;
import java.util.Map;
/** /**
* The Permissions class handles checking user permissions.<br> * The Permissions class handles checking user permissions.<br>
@ -48,75 +45,23 @@ public class Permissions {
} }
/** /**
* Check if a player has a permission (Captions class helps keep track of permissions). * Check if the owner of the profile has a given (global) permission
* *
* @param player * @param permission Permission
* @param caption * @return {@code true} if the owner has the given permission, else {@code false}
* @return
*/ */
public static boolean hasPermission(PlotPlayer<?> player, Captions caption) { public static boolean hasPermission(@Nonnull final PermissionHolder caller, @Nonnull final Caption permission) {
return hasPermission(player, caption.getTranslated()); return caller.hasPermission(permission.getTranslated());
} }
/** /**
* Check if a {@link PlotPlayer} has a permission. * Check if the owner of the profile has a given (global) permission
* *
* @param player * @param permission Permission
* @param permission * @return {@code true} if the owner has the given permission, else {@code false}
* @return
*/ */
public static boolean hasPermission(PlotPlayer<?> player, String permission) { public static boolean hasPermission(@Nonnull final PermissionHolder caller, @Nonnull final String permission) {
if (!Settings.Enabled_Components.PERMISSION_CACHE) { return caller.hasPermission(permission);
return hasPermission((PermissionHolder) player, permission);
}
try (final MetaDataAccess<Map<String, Boolean>> mapAccess =
player.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_PERMISSIONS)) {
Map<String, Boolean> map = mapAccess.get().orElse(null);
if (map != null) {
final Boolean result = map.get(permission);
if (result != null) {
return result;
}
} else {
mapAccess.set((map = new HashMap<>()));
}
boolean result = hasPermission((PermissionHolder) player, permission);
map.put(permission, result);
return result;
}
}
/**
* Check if a {@code CommandCaller} has a permission.
*
* @param caller
* @param permission
* @return
*/
public static boolean hasPermission(PermissionHolder caller, String permission) {
if (caller.hasPermission(permission)) {
return true;
}/* TODO: DECIDE WHAT TO DO HERE; else if (caller.isPermissionSet(permission)) {
return false;
}*/
if (caller.hasPermission(Captions.PERMISSION_ADMIN.getTranslated())) {
return true;
}
permission = permission.toLowerCase().replaceAll("^[^a-z|0-9|\\.|_|-]", "");
String[] nodes = permission.split("\\.");
StringBuilder n = new StringBuilder();
for (int i = 0; i <= (nodes.length - 1); i++) {
n.append(nodes[i] + ".");
String combined = n + Captions.PERMISSION_STAR.getTranslated();
if (!permission.equals(combined)) {
if (caller.hasPermission(combined)) {
return true;
}/* TODO: DECIDE WHAT TO DO HERE; else if (caller.isPermissionSet(combined)) {
return false;
}*/
}
}
return false;
} }
/** /**

View File

@ -239,7 +239,7 @@ public abstract class PlayerManager<P extends PlotPlayer<? extends T>, T> {
/** /**
* Get a plot player from a platform player object. This method requires * Get a plot player from a platform player object. This method requires
* that the caller actually knows that the player exists. * that the caller actually knows that the player exists and is online.
* <p> * <p>
* The method will throw an exception if there is no such * The method will throw an exception if there is no such
* player online. * player online.