Null verifier wildcard fix (#1849)

* fix hasPermission(String, PermissionVerifier) Behavior

* add test cases
This commit is contained in:
Hydrogen 2023-05-10 01:51:06 +03:00 committed by GitHub
parent 1d6ff874f8
commit 2c3e38b178
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 7 deletions

View File

@ -112,15 +112,17 @@ public interface PermissionHandler {
* @return true if the handler has the permission, false otherwise
*/
default boolean hasPermission(@NotNull String permissionName, @Nullable PermissionVerifier permissionVerifier) {
final Permission permission = getPermission(permissionName);
Permission permission = getPermission(permissionName);
if (permission != null) {
// If no permission verifier, hand off to no-verifier hasPermission for wildcard support
if(permissionVerifier == null) { return hasPermission(permission); }
// Verify using the permission verifier
return permissionVerifier == null || permissionVerifier.isValid(permission.getNBTData());
if (permission == null && permissionVerifier == null) {
permission = new Permission(permissionName, null);
} else if (permission == null) {
return false;
}
return false;
// If no permission verifier, hand off to no-verifier hasPermission for wildcard support
if(permissionVerifier == null) { return hasPermission(permission); }
// Verify using the permission verifier
return permissionVerifier.isValid(permission.getNBTData());
}
/**

View File

@ -85,6 +85,8 @@ public class TestPermissions {
Permission permission = new Permission("foo.b*r.baz");
Permission match = new Permission("foo.baaar.baz");
Permission match2 = new Permission("foo.br.baz");
String match3 = "foo.br.baz";
String match4 = "foo.baaar.baz";
Permission nomatch = new Permission("foo.br.bz");
Permission nomatch2 = new Permission("foo.b.baz");
assertFalse(player.hasPermission(match));
@ -96,6 +98,8 @@ public class TestPermissions {
assertTrue(player.hasPermission(match));
assertTrue(player.hasPermission(match2));
assertTrue(player.hasPermission(match3));
assertTrue(player.hasPermission(match4));
assertFalse(player.hasPermission(nomatch));
assertFalse(player.hasPermission(nomatch2));
}
@ -105,6 +109,8 @@ public class TestPermissions {
Permission permission = new Permission("foo.b*");
Permission match = new Permission("foo.baaar.baz");
Permission match2 = new Permission("foo.b");
String match3 = "foo.b";
String match4 = "foo.baaar.baz";
Permission nomatch = new Permission("foo.");
Permission nomatch2 = new Permission("foo/b");
assertFalse(player.hasPermission(match));
@ -116,6 +122,8 @@ public class TestPermissions {
assertTrue(player.hasPermission(match));
assertTrue(player.hasPermission(match2));
assertTrue(player.hasPermission(match3));
assertTrue(player.hasPermission(match4));
assertFalse(player.hasPermission(nomatch));
assertFalse(player.hasPermission(nomatch2));
}