From 2c3e38b178b45ed8e331f3d16c4b33114e7323be Mon Sep 17 00:00:00 2001 From: Hydrogen <96733109+dev-hydrogen@users.noreply.github.com> Date: Wed, 10 May 2023 01:51:06 +0300 Subject: [PATCH] Null verifier wildcard fix (#1849) * fix hasPermission(String, PermissionVerifier) Behavior * add test cases --- .../server/permission/PermissionHandler.java | 16 +++++++++------- .../server/permission/TestPermissions.java | 8 ++++++++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/main/java/net/minestom/server/permission/PermissionHandler.java b/src/main/java/net/minestom/server/permission/PermissionHandler.java index 50ce3fac8..0683e3497 100644 --- a/src/main/java/net/minestom/server/permission/PermissionHandler.java +++ b/src/main/java/net/minestom/server/permission/PermissionHandler.java @@ -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()); } /** diff --git a/src/test/java/net/minestom/server/permission/TestPermissions.java b/src/test/java/net/minestom/server/permission/TestPermissions.java index 30840900a..c5604540d 100644 --- a/src/test/java/net/minestom/server/permission/TestPermissions.java +++ b/src/test/java/net/minestom/server/permission/TestPermissions.java @@ -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)); }