Cleanup & multiple small fixes
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
package me.lucko.luckperms.utils;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.regex.Matcher;
|
||||
@@ -10,12 +13,11 @@ import java.util.regex.Pattern;
|
||||
* https://github.com/drtshock/Essentials/blob/2.x/Essentials/src/com/earth2me/essentials/utils/DateUtil.java
|
||||
* https://github.com/essentials/Essentials/blob/2.x/Essentials/src/com/earth2me/essentials/utils/DateUtil.java
|
||||
*/
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class DateUtil {
|
||||
private static final Pattern TIME_PATTERN = Pattern.compile("(?:([0-9]+)\\s*y[a-z]*[,\\s]*)?" + "(?:([0-9]+)\\s*mo[a-z]*[,\\s]*)?" + "(?:([0-9]+)\\s*w[a-z]*[,\\s]*)?" + "(?:([0-9]+)\\s*d[a-z]*[,\\s]*)?" + "(?:([0-9]+)\\s*h[a-z]*[,\\s]*)?" + "(?:([0-9]+)\\s*m[a-z]*[,\\s]*)?" + "(?:([0-9]+)\\s*(?:s[a-z]*)?)?", Pattern.CASE_INSENSITIVE);
|
||||
private static final int MAX_YEARS = 100000;
|
||||
|
||||
private DateUtil() {}
|
||||
|
||||
public static boolean shouldExpire(long unixTime) {
|
||||
return unixTime < (System.currentTimeMillis() / 1000);
|
||||
}
|
||||
|
||||
@@ -1,13 +1,71 @@
|
||||
package me.lucko.luckperms.utils;
|
||||
|
||||
public interface LPConfiguration {
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import me.lucko.luckperms.LuckPermsPlugin;
|
||||
|
||||
String getServer();
|
||||
int getSyncTime();
|
||||
String getDefaultGroupNode();
|
||||
String getDefaultGroupName();
|
||||
boolean getIncludeGlobalPerms();
|
||||
String getDatabaseValue(String value);
|
||||
String getStorageMethod();
|
||||
public abstract class LPConfiguration<T extends LuckPermsPlugin> {
|
||||
|
||||
@Getter(AccessLevel.PROTECTED)
|
||||
private final T plugin;
|
||||
|
||||
private final String defaultServerName;
|
||||
private final boolean defaultIncludeGlobal;
|
||||
private final String defaultStorage;
|
||||
|
||||
public LPConfiguration(T plugin, String defaultServerName, boolean defaultIncludeGlobal, String defaultStorage) {
|
||||
this.plugin = plugin;
|
||||
this.defaultServerName = defaultServerName;
|
||||
this.defaultIncludeGlobal = defaultIncludeGlobal;
|
||||
this.defaultStorage = defaultStorage;
|
||||
init();
|
||||
|
||||
if (Patterns.NON_ALPHA_NUMERIC.matcher(getServer()).find()) {
|
||||
plugin.getLogger().severe("Server name defined in config.yml contains invalid characters. Server names can " +
|
||||
"only contain alphanumeric characters.\nDefined server name '" + getServer() + "' will be replaced with '" +
|
||||
defaultServerName + "' (the default)");
|
||||
set("server", defaultServerName);
|
||||
}
|
||||
|
||||
if (Patterns.NON_ALPHA_NUMERIC.matcher(getDefaultGroupName()).find()) {
|
||||
plugin.getLogger().severe("Default group defined in config.yml contains invalid characters. Group names can " +
|
||||
"only contain alphanumeric characters.\nDefined default group name '" + getDefaultGroupName() +
|
||||
"' will be replaced with 'default' (the default)");
|
||||
set("default-group", "default");
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract void init();
|
||||
protected abstract void set(String path, Object value);
|
||||
protected abstract String getString(String path, String def);
|
||||
protected abstract int getInt(String path, int def);
|
||||
protected abstract boolean getBoolean(String path, boolean def);
|
||||
|
||||
public String getServer() {
|
||||
return getString("server", defaultServerName);
|
||||
}
|
||||
|
||||
public int getSyncTime() {
|
||||
return getInt("sql.sync-minutes", 3);
|
||||
}
|
||||
|
||||
public String getDefaultGroupNode() {
|
||||
return "group." + getDefaultGroupName();
|
||||
}
|
||||
|
||||
public String getDefaultGroupName() {
|
||||
return getString("default-group", "default");
|
||||
}
|
||||
|
||||
public boolean getIncludeGlobalPerms() {
|
||||
return getBoolean("include-global", defaultIncludeGlobal);
|
||||
}
|
||||
|
||||
public String getDatabaseValue(String value) {
|
||||
return getString("sql." + value, null);
|
||||
}
|
||||
|
||||
public String getStorageMethod() {
|
||||
return getString("storage-method", defaultStorage);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
package me.lucko.luckperms.utils;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class Patterns {
|
||||
|
||||
public static final Pattern SERVER_SPLIT = Pattern.compile("\\/");
|
||||
public static final Pattern TEMP_SPLIT = Pattern.compile("\\$");
|
||||
public static final Pattern DOT_SPLIT = Pattern.compile("\\.");
|
||||
public static final Pattern GROUP_MATCH = Pattern.compile("group\\..*");
|
||||
public static final Pattern NON_ALPHA_NUMERIC = Pattern.compile("[^A-Za-z0-9]");
|
||||
|
||||
private Patterns() {}
|
||||
public static final Pattern NON_USERNAME = Pattern.compile("[^A-Za-z0-9_]");
|
||||
|
||||
}
|
||||
|
||||
@@ -1,52 +1,46 @@
|
||||
package me.lucko.luckperms.utils;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.lucko.luckperms.LuckPermsPlugin;
|
||||
import me.lucko.luckperms.exceptions.ObjectAlreadyHasException;
|
||||
import me.lucko.luckperms.exceptions.ObjectLacksException;
|
||||
import me.lucko.luckperms.groups.Group;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Represents an object that can hold permissions
|
||||
* For example a User or a Group
|
||||
*/
|
||||
@Getter
|
||||
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
|
||||
public abstract class PermissionObject {
|
||||
|
||||
/**
|
||||
* The UUID of the user / name of the group.
|
||||
* Used to prevent circular inheritance issues
|
||||
*/
|
||||
@Getter
|
||||
private final String objectName;
|
||||
|
||||
/**
|
||||
* Reference to the main plugin instance
|
||||
*/
|
||||
@Getter(AccessLevel.PROTECTED)
|
||||
private final LuckPermsPlugin plugin;
|
||||
|
||||
/**
|
||||
* If false, only permissions specific to the server are applied
|
||||
*/
|
||||
@Setter
|
||||
private boolean includeGlobalPermissions;
|
||||
|
||||
/**
|
||||
* The user/group's permissions
|
||||
*/
|
||||
private Map<String, Boolean> nodes = new HashMap<>();
|
||||
|
||||
protected PermissionObject(LuckPermsPlugin plugin, String objectName) {
|
||||
this.objectName = objectName;
|
||||
this.plugin = plugin;
|
||||
this.includeGlobalPermissions = plugin.getConfiguration().getIncludeGlobalPerms();
|
||||
}
|
||||
@Getter
|
||||
private Map<String, Boolean> nodes = new ConcurrentHashMap<>();
|
||||
|
||||
public void setNodes(Map<String, Boolean> nodes) {
|
||||
this.nodes = nodes;
|
||||
this.nodes.clear();
|
||||
this.nodes.putAll(nodes);
|
||||
auditTemporaryPermissions();
|
||||
}
|
||||
|
||||
@@ -81,7 +75,7 @@ public abstract class PermissionObject {
|
||||
*/
|
||||
public boolean hasPermission(String node, boolean b) {
|
||||
if (node.startsWith("global/")) node = node.replace("global/", "");
|
||||
return hasPermission(getNodes(), node, b);
|
||||
return hasPermission(this.nodes, node, b);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -156,7 +150,7 @@ public abstract class PermissionObject {
|
||||
if (hasPermission(node, value)) {
|
||||
throw new ObjectAlreadyHasException();
|
||||
}
|
||||
getNodes().put(node, value);
|
||||
this.nodes.put(node, value);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -201,26 +195,21 @@ public abstract class PermissionObject {
|
||||
*/
|
||||
public void unsetPermission(String node, boolean temporary) throws ObjectLacksException {
|
||||
if (node.startsWith("global/")) node = node.replace("global/", "");
|
||||
String match = null;
|
||||
final String fNode = node;
|
||||
Optional<String> match = Optional.empty();
|
||||
|
||||
if (!temporary) {
|
||||
if (getNodes().containsKey(node)) {
|
||||
match = node;
|
||||
}
|
||||
if (temporary) {
|
||||
match = this.nodes.keySet().stream()
|
||||
.filter(n -> n.contains("$")).filter(n -> Patterns.TEMP_SPLIT.split(n)[0].equalsIgnoreCase(fNode))
|
||||
.findFirst();
|
||||
} else {
|
||||
for (String n : getNodes().keySet()) {
|
||||
if (n.contains("$")) {
|
||||
String[] parts = Patterns.TEMP_SPLIT.split(n);
|
||||
if (parts[0].equalsIgnoreCase(node)) {
|
||||
match = n;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (this.nodes.containsKey(fNode)) {
|
||||
match = Optional.of(fNode);
|
||||
}
|
||||
}
|
||||
|
||||
if (match != null) {
|
||||
getNodes().remove(match);
|
||||
if (match.isPresent()) {
|
||||
this.nodes.remove(match.get());
|
||||
} else {
|
||||
throw new ObjectLacksException();
|
||||
}
|
||||
@@ -263,7 +252,7 @@ public abstract class PermissionObject {
|
||||
* @return a {@link Map} of the permissions
|
||||
*/
|
||||
public Map<String, Boolean> getLocalPermissions(String server, List<String> excludedGroups) {
|
||||
return getPermissions(server, excludedGroups, includeGlobalPermissions);
|
||||
return getPermissions(server, excludedGroups, plugin.getConfiguration().getIncludeGlobalPerms());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -271,19 +260,12 @@ public abstract class PermissionObject {
|
||||
* @return a map of temporary nodes
|
||||
*/
|
||||
public Map<Map.Entry<String, Boolean>, Long> getTemporaryNodes() {
|
||||
Map<Map.Entry<String, Boolean>, Long> temps = new HashMap<>();
|
||||
|
||||
for (Map.Entry<String, Boolean> e : getNodes().entrySet()) {
|
||||
if (!e.getKey().contains("$")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String[] parts = Patterns.TEMP_SPLIT.split(e.getKey());
|
||||
return this.nodes.entrySet().stream().filter(e -> e.getKey().contains("$")).map(e -> {
|
||||
final String[] parts = Patterns.TEMP_SPLIT.split(e.getKey());
|
||||
final long expiry = Long.parseLong(parts[1]);
|
||||
temps.put(new AbstractMap.SimpleEntry<>(parts[0], e.getValue()), expiry);
|
||||
}
|
||||
return new AbstractMap.SimpleEntry<Map.Entry<String, Boolean>, Long>(new AbstractMap.SimpleEntry<>(parts[0], e.getValue()), expiry);
|
||||
|
||||
return temps;
|
||||
}).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -291,37 +273,18 @@ public abstract class PermissionObject {
|
||||
* @return a map of permanent nodes
|
||||
*/
|
||||
public Map<String, Boolean> getPermanentNodes() {
|
||||
Map<String, Boolean> permas = new HashMap<>();
|
||||
|
||||
for (Map.Entry<String, Boolean> e : getNodes().entrySet()) {
|
||||
if (e.getKey().contains("$")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
permas.put(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
return permas;
|
||||
return this.nodes.entrySet().stream().filter(e -> !e.getKey().contains("$"))
|
||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes temporary permissions that have expired
|
||||
* @return true if permissions had expired and had to be removed
|
||||
*/
|
||||
public boolean auditTemporaryPermissions() {
|
||||
Set<String> toRemove = getNodes().keySet().stream()
|
||||
public void auditTemporaryPermissions() {
|
||||
this.nodes.keySet().stream()
|
||||
.filter(s -> s.contains("$"))
|
||||
.filter(s -> DateUtil.shouldExpire(Long.parseLong(Patterns.TEMP_SPLIT.split(s)[1])))
|
||||
.collect(Collectors.toSet());
|
||||
toRemove.forEach(s -> getNodes().remove(s));
|
||||
return !toRemove.isEmpty();
|
||||
}
|
||||
|
||||
private String stripTime(String s) {
|
||||
if (s.contains("$")) {
|
||||
return Patterns.TEMP_SPLIT.split(s)[0];
|
||||
}
|
||||
return s;
|
||||
.forEach(s -> this.nodes.remove(s));
|
||||
}
|
||||
|
||||
protected Map<String, Boolean> convertTemporaryPerms() {
|
||||
@@ -330,7 +293,7 @@ public abstract class PermissionObject {
|
||||
Map<String, Boolean> nodes = new HashMap<>();
|
||||
Map<String, Boolean> tempNodes = new HashMap<>();
|
||||
|
||||
for (Map.Entry<String, Boolean> e : getNodes().entrySet()) {
|
||||
for (Map.Entry<String, Boolean> e : this.nodes.entrySet()) {
|
||||
if (e.getKey().contains("$")) {
|
||||
tempNodes.put(e.getKey(), e.getValue());
|
||||
} else {
|
||||
@@ -469,4 +432,11 @@ public abstract class PermissionObject {
|
||||
|
||||
return perms;
|
||||
}
|
||||
|
||||
private static String stripTime(String s) {
|
||||
if (s.contains("$")) {
|
||||
return Patterns.TEMP_SPLIT.split(s)[0];
|
||||
}
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user