Add permission to bypass country check (#1323)

* Add permission to bypass country check

#1321 Still need to fix unit tests

* Fix test
This commit is contained in:
Gabriele C 2017-08-31 19:36:57 +02:00 committed by GitHub
parent b96ae61697
commit 6b875a9ba4
7 changed files with 39 additions and 25 deletions

View File

@ -1,5 +1,5 @@
<!-- AUTO-GENERATED FILE! Do not edit this directly --> <!-- AUTO-GENERATED FILE! Do not edit this directly -->
<!-- File auto-generated on Sat Aug 12 13:42:15 CEST 2017. See docs/permissions/permission_nodes.tpl.md --> <!-- File auto-generated on Thu Aug 31 12:11:53 CEST 2017. See docs/permissions/permission_nodes.tpl.md -->
## AuthMe Permission Nodes ## AuthMe Permission Nodes
The following are the permission nodes that are currently supported by the latest dev builds. 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.admin.updatemessages** Permission to use the update messages command.
- **authme.allowmultipleaccounts** Permission to be able to register multiple accounts. - **authme.allowmultipleaccounts** Permission to be able to register multiple accounts.
- **authme.bypassantibot** Permission node to bypass AntiBot protection. - **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.bypassforcesurvival** Permission for users to bypass force-survival mode.
- **authme.bypasspurge** Permission to bypass the purging process. - **authme.bypasspurge** Permission to bypass the purging process.
- **authme.debug.command** General permission to use the /authme debug command. - **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

View File

@ -168,15 +168,15 @@ public class OnJoinVerifier implements Reloadable {
/** /**
* Checks that the player's country is admitted. * Checks that the player's country is admitted.
* *
* @param player the player
* @param isAuthAvailable whether or not the user is registered * @param isAuthAvailable whether or not the user is registered
* @param playerIp the ip address of the player
* @throws FailedVerificationException if the verification fails * @throws FailedVerificationException if the verification fails
*/ */
public void checkPlayerCountry(boolean isAuthAvailable, public void checkPlayerCountry(Player player, boolean isAuthAvailable) throws FailedVerificationException {
String playerIp) throws FailedVerificationException {
if ((!isAuthAvailable || settings.getProperty(ProtectionSettings.ENABLE_PROTECTION_REGISTERED)) if ((!isAuthAvailable || settings.getProperty(ProtectionSettings.ENABLE_PROTECTION_REGISTERED))
&& settings.getProperty(ProtectionSettings.ENABLE_PROTECTION) && 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); throw new FailedVerificationException(MessageKey.COUNTRY_BANNED_ERROR);
} }
} }

View File

@ -222,7 +222,7 @@ public class PlayerListener implements Listener {
onJoinVerifier.checkKickNonRegistered(isAuthAvailable); onJoinVerifier.checkKickNonRegistered(isAuthAvailable);
onJoinVerifier.checkAntibot(player, isAuthAvailable); onJoinVerifier.checkAntibot(player, isAuthAvailable);
onJoinVerifier.checkNameCasing(player, auth); onJoinVerifier.checkNameCasing(player, auth);
onJoinVerifier.checkPlayerCountry(isAuthAvailable, event.getAddress().getHostAddress()); onJoinVerifier.checkPlayerCountry(player, isAuthAvailable);
} catch (FailedVerificationException e) { } catch (FailedVerificationException e) {
event.setKickMessage(m.retrieveSingle(e.getReason(), e.getArgs())); event.setKickMessage(m.retrieveSingle(e.getReason(), e.getArgs()));
event.setResult(PlayerLoginEvent.Result.KICK_OTHER); event.setResult(PlayerLoginEvent.Result.KICK_OTHER);

View File

@ -29,7 +29,12 @@ public enum PlayerStatePermission implements PermissionNode {
/** /**
* Permission to bypass the purging process. * 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. * The permission node.

View File

@ -154,6 +154,9 @@ permissions:
authme.bypassantibot: authme.bypassantibot:
description: Permission node to bypass AntiBot protection. description: Permission node to bypass AntiBot protection.
default: op default: op
authme.bypasscountrycheck:
description: Permission to use to see own other accounts.
default: false
authme.bypassforcesurvival: authme.bypassforcesurvival:
description: Permission for users to bypass force-survival mode. description: Permission for users to bypass force-survival mode.
default: op default: op

View File

@ -29,6 +29,7 @@ import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import java.net.InetSocketAddress;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
@ -438,15 +439,17 @@ public class OnJoinVerifierTest {
*/ */
@Test @Test
public void shouldNotCheckCountry() throws FailedVerificationException { public void shouldNotCheckCountry() throws FailedVerificationException {
Player player = newPlayerWithAddress("127.0.0.1");
// protection setting disabled // protection setting disabled
given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION)).willReturn(false); given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION)).willReturn(false);
given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION_REGISTERED)).willReturn(true); given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION_REGISTERED)).willReturn(true);
onJoinVerifier.checkPlayerCountry(false, "127.0.0.1"); onJoinVerifier.checkPlayerCountry(player, false);
verifyZeroInteractions(validationService); verifyZeroInteractions(validationService);
// protection for registered players disabled // protection for registered players disabled
given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION_REGISTERED)).willReturn(false); given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION_REGISTERED)).willReturn(false);
onJoinVerifier.checkPlayerCountry(true, "127.0.0.1"); onJoinVerifier.checkPlayerCountry(player, true);
verifyZeroInteractions(validationService); verifyZeroInteractions(validationService);
} }
@ -454,11 +457,12 @@ public class OnJoinVerifierTest {
public void shouldCheckAndAcceptUnregisteredPlayerCountry() throws FailedVerificationException { public void shouldCheckAndAcceptUnregisteredPlayerCountry() throws FailedVerificationException {
// given // given
String ip = "192.168.0.1"; String ip = "192.168.0.1";
Player player = newPlayerWithAddress(ip);
given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION)).willReturn(true); given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION)).willReturn(true);
given(validationService.isCountryAdmitted(ip)).willReturn(true); given(validationService.isCountryAdmitted(ip)).willReturn(true);
// when // when
onJoinVerifier.checkPlayerCountry(false, ip); onJoinVerifier.checkPlayerCountry(player, false);
// then // then
verify(validationService).isCountryAdmitted(ip); verify(validationService).isCountryAdmitted(ip);
@ -468,12 +472,13 @@ public class OnJoinVerifierTest {
public void shouldCheckAndAcceptRegisteredPlayerCountry() throws FailedVerificationException { public void shouldCheckAndAcceptRegisteredPlayerCountry() throws FailedVerificationException {
// given // given
String ip = "192.168.10.24"; String ip = "192.168.10.24";
Player player = newPlayerWithAddress(ip);
given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION)).willReturn(true); given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION)).willReturn(true);
given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION_REGISTERED)).willReturn(true); given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION_REGISTERED)).willReturn(true);
given(validationService.isCountryAdmitted(ip)).willReturn(true); given(validationService.isCountryAdmitted(ip)).willReturn(true);
// when // when
onJoinVerifier.checkPlayerCountry(true, ip); onJoinVerifier.checkPlayerCountry(player, true);
// then // then
verify(validationService).isCountryAdmitted(ip); verify(validationService).isCountryAdmitted(ip);
@ -483,6 +488,7 @@ public class OnJoinVerifierTest {
public void shouldThrowForBannedCountry() throws FailedVerificationException { public void shouldThrowForBannedCountry() throws FailedVerificationException {
// given // given
String ip = "192.168.40.0"; String ip = "192.168.40.0";
Player player = newPlayerWithAddress(ip);
given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION)).willReturn(true); given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION)).willReturn(true);
given(validationService.isCountryAdmitted(ip)).willReturn(false); given(validationService.isCountryAdmitted(ip)).willReturn(false);
@ -490,7 +496,7 @@ public class OnJoinVerifierTest {
expectValidationExceptionWith(MessageKey.COUNTRY_BANNED_ERROR); expectValidationExceptionWith(MessageKey.COUNTRY_BANNED_ERROR);
// when // when
onJoinVerifier.checkPlayerCountry(false, ip); onJoinVerifier.checkPlayerCountry(player, false);
} }
private static Player newPlayerWithName(String name) { private static Player newPlayerWithName(String name) {
@ -499,6 +505,12 @@ public class OnJoinVerifierTest {
return player; 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" }) @SuppressWarnings({ "unchecked", "rawtypes" })
private void returnOnlineListFromBukkitServer(Collection<Player> onlineList) { private void returnOnlineListFromBukkitServer(Collection<Player> onlineList) {
// Note ljacqu 20160529: The compiler gets lost in generics because Collection<? extends Player> is returned // Note ljacqu 20160529: The compiler gets lost in generics because Collection<? extends Player> is returned

View File

@ -47,7 +47,7 @@ import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import java.net.InetAddress; import java.net.UnknownHostException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
@ -554,12 +554,12 @@ public class PlayerListenerTest {
} }
@Test @Test
public void shouldPerformAllJoinVerificationsSuccessfully() throws FailedVerificationException { public void shouldPerformAllJoinVerificationsSuccessfully() throws FailedVerificationException, UnknownHostException {
// given // given
String name = "someone"; String name = "someone";
Player player = mockPlayerWithName(name); 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(validationService.isUnrestricted(name)).willReturn(false);
given(onJoinVerifier.refusePlayerForFullServer(event)).willReturn(false); given(onJoinVerifier.refusePlayerForFullServer(event)).willReturn(false);
PlayerAuth auth = PlayerAuth.builder().name(name).build(); PlayerAuth auth = PlayerAuth.builder().name(name).build();
@ -576,7 +576,7 @@ public class PlayerListenerTest {
verify(onJoinVerifier).checkAntibot(player, true); verify(onJoinVerifier).checkAntibot(player, true);
verify(onJoinVerifier).checkKickNonRegistered(true); verify(onJoinVerifier).checkKickNonRegistered(true);
verify(onJoinVerifier).checkNameCasing(player, auth); verify(onJoinVerifier).checkNameCasing(player, auth);
verify(onJoinVerifier).checkPlayerCountry(true, ip); verify(onJoinVerifier).checkPlayerCountry(player, true);
verify(teleportationService).teleportOnJoin(player); verify(teleportationService).teleportOnJoin(player);
verifyNoModifyingCalls(event); verifyNoModifyingCalls(event);
} }
@ -883,11 +883,4 @@ public class PlayerListenerTest {
verify(event, atLeast(0)).getAddress(); verify(event, atLeast(0)).getAddress();
verifyNoMoreInteractions(event); verifyNoMoreInteractions(event);
} }
private static InetAddress mockAddrWithIp(String ip) {
InetAddress addr = mock(InetAddress.class);
given(addr.getHostAddress()).willReturn(ip);
return addr;
}
} }