Account permission wildcards

This commit is contained in:
AnimeGitB 2022-05-07 13:30:55 +09:30 committed by Melledy
parent e23b72f298
commit d03f1ca5d1

View File

@ -1,6 +1,7 @@
package emu.grasscutter.game;
import dev.morphia.annotations.*;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.utils.Crypto;
import emu.grasscutter.utils.Utils;
@ -107,11 +108,41 @@ public class Account {
this.permissions.add(permission); return true;
}
public static boolean permissionMatchesWildcard(String wildcard, String[] permissionParts) {
String[] wildcardParts = wildcard.split("\\.");
if (permissionParts.length < wildcardParts.length) { // A longer wildcard can never match a shorter permission
return false;
}
for (int i=0; i<wildcardParts.length; i++) {
switch (wildcardParts[i]) {
case "**": // Recursing match
return true;
case "*": // Match only one layer
if (i >= (permissionParts.length-1)) {
return true;
}
break;
default: // This layer isn't a wildcard, it needs to match exactly
if (!wildcardParts[i].equals(permissionParts[i])) {
return false;
}
}
}
// At this point the wildcard will have matched every layer, but if it is shorter then the permission then this is not a match at this point (no **).
return (wildcardParts.length == permissionParts.length);
}
public boolean hasPermission(String permission) {
return this.permissions.contains(permission) ||
this.permissions.contains("*") ||
(this.permissions.contains("player") || this.permissions.contains("player.*")) && permission.startsWith("player.") ||
(this.permissions.contains("server") || this.permissions.contains("server.*")) && permission.startsWith("server.");
if (this.permissions.contains(permission) || this.permissions.contains("*")) {
return true;
}
String[] permissionParts = permission.split("\\.");
for (String p : this.permissions) {
if (permissionMatchesWildcard(p, permissionParts)) {
return true;
}
}
return false;
}
public boolean removePermission(String permission) {