From 6b875a9ba430c7f02998507e23e573327d561534 Mon Sep 17 00:00:00 2001 From: Gabriele C Date: Thu, 31 Aug 2017 19:36:57 +0200 Subject: [PATCH] Add permission to bypass country check (#1323) * Add permission to bypass country check #1321 Still need to fix unit tests * Fix test --- docs/permission_nodes.md | 5 +++-- .../xephi/authme/listener/OnJoinVerifier.java | 8 +++---- .../xephi/authme/listener/PlayerListener.java | 2 +- .../permission/PlayerStatePermission.java | 7 +++++- src/main/resources/plugin.yml | 3 +++ .../authme/listener/OnJoinVerifierTest.java | 22 ++++++++++++++----- .../authme/listener/PlayerListenerTest.java | 17 +++++--------- 7 files changed, 39 insertions(+), 25 deletions(-) diff --git a/docs/permission_nodes.md b/docs/permission_nodes.md index 9322ea50e..40eed22b5 100644 --- a/docs/permission_nodes.md +++ b/docs/permission_nodes.md @@ -1,5 +1,5 @@ - + ## AuthMe Permission Nodes The following are the permission nodes that are currently supported by the latest dev builds. @@ -31,6 +31,7 @@ The following are the permission nodes that are currently supported by the lates - **authme.admin.updatemessages** – Permission to use the update messages command. - **authme.allowmultipleaccounts** – Permission to be able to register multiple accounts. - **authme.bypassantibot** – Permission node to bypass AntiBot protection. +- **authme.bypasscountrycheck** – Permission to use to see own other accounts. - **authme.bypassforcesurvival** – Permission for users to bypass force-survival mode. - **authme.bypasspurge** – Permission to bypass the purging process. - **authme.debug.command** – General permission to use the /authme debug command. @@ -62,4 +63,4 @@ The following are the permission nodes that are currently supported by the lates --- -This page was automatically generated on the [AuthMe/AuthMeReloaded repository](https://github.com/AuthMe/AuthMeReloaded/tree/master/docs/) on Sat Aug 12 13:42:15 CEST 2017 +This page was automatically generated on the [AuthMe/AuthMeReloaded repository](https://github.com/AuthMe/AuthMeReloaded/tree/master/docs/) on Thu Aug 31 12:11:53 CEST 2017 diff --git a/src/main/java/fr/xephi/authme/listener/OnJoinVerifier.java b/src/main/java/fr/xephi/authme/listener/OnJoinVerifier.java index 78262d98c..922e87bcd 100644 --- a/src/main/java/fr/xephi/authme/listener/OnJoinVerifier.java +++ b/src/main/java/fr/xephi/authme/listener/OnJoinVerifier.java @@ -168,15 +168,15 @@ public class OnJoinVerifier implements Reloadable { /** * Checks that the player's country is admitted. * + * @param player the player * @param isAuthAvailable whether or not the user is registered - * @param playerIp the ip address of the player * @throws FailedVerificationException if the verification fails */ - public void checkPlayerCountry(boolean isAuthAvailable, - String playerIp) throws FailedVerificationException { + public void checkPlayerCountry(Player player, boolean isAuthAvailable) throws FailedVerificationException { if ((!isAuthAvailable || settings.getProperty(ProtectionSettings.ENABLE_PROTECTION_REGISTERED)) && settings.getProperty(ProtectionSettings.ENABLE_PROTECTION) - && !validationService.isCountryAdmitted(playerIp)) { + && !permissionsManager.hasPermission(player, PlayerStatePermission.BYPASS_COUNTRY_CHECK) + && !validationService.isCountryAdmitted(player.getAddress().getAddress().getHostAddress())) { throw new FailedVerificationException(MessageKey.COUNTRY_BANNED_ERROR); } } diff --git a/src/main/java/fr/xephi/authme/listener/PlayerListener.java b/src/main/java/fr/xephi/authme/listener/PlayerListener.java index bc31f4852..eebe5b0e6 100644 --- a/src/main/java/fr/xephi/authme/listener/PlayerListener.java +++ b/src/main/java/fr/xephi/authme/listener/PlayerListener.java @@ -222,7 +222,7 @@ public class PlayerListener implements Listener { onJoinVerifier.checkKickNonRegistered(isAuthAvailable); onJoinVerifier.checkAntibot(player, isAuthAvailable); onJoinVerifier.checkNameCasing(player, auth); - onJoinVerifier.checkPlayerCountry(isAuthAvailable, event.getAddress().getHostAddress()); + onJoinVerifier.checkPlayerCountry(player, isAuthAvailable); } catch (FailedVerificationException e) { event.setKickMessage(m.retrieveSingle(e.getReason(), e.getArgs())); event.setResult(PlayerLoginEvent.Result.KICK_OTHER); diff --git a/src/main/java/fr/xephi/authme/permission/PlayerStatePermission.java b/src/main/java/fr/xephi/authme/permission/PlayerStatePermission.java index aaeb0eea3..931c8642b 100644 --- a/src/main/java/fr/xephi/authme/permission/PlayerStatePermission.java +++ b/src/main/java/fr/xephi/authme/permission/PlayerStatePermission.java @@ -29,7 +29,12 @@ public enum PlayerStatePermission implements PermissionNode { /** * Permission to bypass the purging process. */ - BYPASS_PURGE("authme.bypasspurge", DefaultPermission.NOT_ALLOWED); + BYPASS_PURGE("authme.bypasspurge", DefaultPermission.NOT_ALLOWED), + + /** + * Permission to use to see own other accounts. + */ + BYPASS_COUNTRY_CHECK("authme.bypasscountrycheck", DefaultPermission.NOT_ALLOWED); /** * The permission node. diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 1ed7c6789..472faef6c 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -154,6 +154,9 @@ permissions: authme.bypassantibot: description: Permission node to bypass AntiBot protection. default: op + authme.bypasscountrycheck: + description: Permission to use to see own other accounts. + default: false authme.bypassforcesurvival: description: Permission for users to bypass force-survival mode. default: op diff --git a/src/test/java/fr/xephi/authme/listener/OnJoinVerifierTest.java b/src/test/java/fr/xephi/authme/listener/OnJoinVerifierTest.java index 766fb06ad..378c3de43 100644 --- a/src/test/java/fr/xephi/authme/listener/OnJoinVerifierTest.java +++ b/src/test/java/fr/xephi/authme/listener/OnJoinVerifierTest.java @@ -29,6 +29,7 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; +import java.net.InetSocketAddress; import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -438,15 +439,17 @@ public class OnJoinVerifierTest { */ @Test public void shouldNotCheckCountry() throws FailedVerificationException { + Player player = newPlayerWithAddress("127.0.0.1"); + // protection setting disabled given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION)).willReturn(false); given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION_REGISTERED)).willReturn(true); - onJoinVerifier.checkPlayerCountry(false, "127.0.0.1"); + onJoinVerifier.checkPlayerCountry(player, false); verifyZeroInteractions(validationService); // protection for registered players disabled given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION_REGISTERED)).willReturn(false); - onJoinVerifier.checkPlayerCountry(true, "127.0.0.1"); + onJoinVerifier.checkPlayerCountry(player, true); verifyZeroInteractions(validationService); } @@ -454,11 +457,12 @@ public class OnJoinVerifierTest { public void shouldCheckAndAcceptUnregisteredPlayerCountry() throws FailedVerificationException { // given String ip = "192.168.0.1"; + Player player = newPlayerWithAddress(ip); given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION)).willReturn(true); given(validationService.isCountryAdmitted(ip)).willReturn(true); // when - onJoinVerifier.checkPlayerCountry(false, ip); + onJoinVerifier.checkPlayerCountry(player, false); // then verify(validationService).isCountryAdmitted(ip); @@ -468,12 +472,13 @@ public class OnJoinVerifierTest { public void shouldCheckAndAcceptRegisteredPlayerCountry() throws FailedVerificationException { // given String ip = "192.168.10.24"; + Player player = newPlayerWithAddress(ip); given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION)).willReturn(true); given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION_REGISTERED)).willReturn(true); given(validationService.isCountryAdmitted(ip)).willReturn(true); // when - onJoinVerifier.checkPlayerCountry(true, ip); + onJoinVerifier.checkPlayerCountry(player, true); // then verify(validationService).isCountryAdmitted(ip); @@ -483,6 +488,7 @@ public class OnJoinVerifierTest { public void shouldThrowForBannedCountry() throws FailedVerificationException { // given String ip = "192.168.40.0"; + Player player = newPlayerWithAddress(ip); given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION)).willReturn(true); given(validationService.isCountryAdmitted(ip)).willReturn(false); @@ -490,7 +496,7 @@ public class OnJoinVerifierTest { expectValidationExceptionWith(MessageKey.COUNTRY_BANNED_ERROR); // when - onJoinVerifier.checkPlayerCountry(false, ip); + onJoinVerifier.checkPlayerCountry(player, false); } private static Player newPlayerWithName(String name) { @@ -499,6 +505,12 @@ public class OnJoinVerifierTest { return player; } + private static Player newPlayerWithAddress(String ip) { + Player player = mock(Player.class); + given(player.getAddress()).willReturn(new InetSocketAddress(ip, 80)); + return player; + } + @SuppressWarnings({ "unchecked", "rawtypes" }) private void returnOnlineListFromBukkitServer(Collection onlineList) { // Note ljacqu 20160529: The compiler gets lost in generics because Collection is returned diff --git a/src/test/java/fr/xephi/authme/listener/PlayerListenerTest.java b/src/test/java/fr/xephi/authme/listener/PlayerListenerTest.java index 312482d7a..36785c555 100644 --- a/src/test/java/fr/xephi/authme/listener/PlayerListenerTest.java +++ b/src/test/java/fr/xephi/authme/listener/PlayerListenerTest.java @@ -47,7 +47,7 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; -import java.net.InetAddress; +import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -554,12 +554,12 @@ public class PlayerListenerTest { } @Test - public void shouldPerformAllJoinVerificationsSuccessfully() throws FailedVerificationException { + public void shouldPerformAllJoinVerificationsSuccessfully() throws FailedVerificationException, UnknownHostException { // given String name = "someone"; Player player = mockPlayerWithName(name); - String ip = "12.34.56.78"; - PlayerLoginEvent event = spy(new PlayerLoginEvent(player, "", mockAddrWithIp(ip))); + + PlayerLoginEvent event = spy(new PlayerLoginEvent(player, "", null)); given(validationService.isUnrestricted(name)).willReturn(false); given(onJoinVerifier.refusePlayerForFullServer(event)).willReturn(false); PlayerAuth auth = PlayerAuth.builder().name(name).build(); @@ -576,7 +576,7 @@ public class PlayerListenerTest { verify(onJoinVerifier).checkAntibot(player, true); verify(onJoinVerifier).checkKickNonRegistered(true); verify(onJoinVerifier).checkNameCasing(player, auth); - verify(onJoinVerifier).checkPlayerCountry(true, ip); + verify(onJoinVerifier).checkPlayerCountry(player, true); verify(teleportationService).teleportOnJoin(player); verifyNoModifyingCalls(event); } @@ -883,11 +883,4 @@ public class PlayerListenerTest { verify(event, atLeast(0)).getAddress(); verifyNoMoreInteractions(event); } - - private static InetAddress mockAddrWithIp(String ip) { - InetAddress addr = mock(InetAddress.class); - given(addr.getHostAddress()).willReturn(ip); - return addr; - } - }