Reverse order of wildcard permission check, respect explicit values (#85)

This commit is contained in:
Jikoo 2018-05-26 06:58:56 -04:00
parent f5dce23f18
commit 8ce6e2f8d3

View file

@ -25,9 +25,9 @@ public enum Permissions {
EXEMPT("exempt"),
CROSSWORLD("crossworld"),
SILENT("silent"),
SILENT_DEFAULT("silent", "default"),
SILENT_DEFAULT("silent.default"),
ANYCHEST("anychest"),
ANY_DEFAULT("any", "default"),
ANY_DEFAULT("any.default"),
ENDERCHEST("openender"),
ENDERCHEST_ALL("openenderall"),
SEARCH("search"),
@ -35,31 +35,35 @@ public enum Permissions {
EDITENDER("editender"),
OPENSELF("openself");
private final String[] permission;
private final String permission;
Permissions(String... permissions) {
this.permission = new String[permissions.length + 1];
this.permission[0] = "OpenInv";
System.arraycopy(permissions, 0, this.permission, 1, permissions.length);
Permissions(String permission) {
this.permission = "OpenInv." + permission;
}
public boolean hasPermission(Permissible permissible) {
StringBuilder permissionBuilder = new StringBuilder();
// Support wildcard nodes.
for (int i = 0; i < permission.length; i++) {
if (permissible.hasPermission(permissionBuilder.toString() + "*")) {
return true;
}
permissionBuilder.append(permission[i]).append('.');
boolean hasPermission = permissible.hasPermission(permission);
if (hasPermission || permissible.isPermissionSet(permission)) {
return hasPermission;
}
// Delete trailing period.
if (permissionBuilder.length() > 0) {
permissionBuilder.deleteCharAt(permissionBuilder.length() - 1);
StringBuilder permissionDestroyer = new StringBuilder(permission);
for (int lastPeriod = permissionDestroyer.lastIndexOf("."); lastPeriod > 0;
lastPeriod = permissionDestroyer.lastIndexOf(".")) {
permissionDestroyer.delete(lastPeriod + 1, permissionDestroyer.length()).append('*');
hasPermission = permissible.hasPermission(permissionDestroyer.toString());
if (hasPermission || permissible.isPermissionSet(permissionDestroyer.toString())) {
return hasPermission;
}
return permissible.hasPermission(permissionBuilder.toString());
permissionDestroyer.delete(lastPeriod, permissionDestroyer.length());
}
return permissible.hasPermission("*");
}
}