From 61420429962cecbaec748888c5a5d236983901ee Mon Sep 17 00:00:00 2001 From: ljacqu Date: Mon, 21 May 2018 08:45:18 +0200 Subject: [PATCH] #1417 Add permission node to allow chat before login --- docs/permission_nodes.md | 5 ++-- .../xephi/authme/listener/PlayerListener.java | 25 ++++++++++++------ .../permission/PlayerStatePermission.java | 9 +++++-- src/main/resources/plugin.yml | 3 +++ .../authme/listener/PlayerListenerTest.java | 26 +++++++++++++++++++ 5 files changed, 56 insertions(+), 12 deletions(-) diff --git a/docs/permission_nodes.md b/docs/permission_nodes.md index 828d23b53..643a327b0 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. @@ -30,6 +30,7 @@ The following are the permission nodes that are currently supported by the lates - **authme.admin.switchantibot** – Administrator command to toggle the AntiBot protection status. - **authme.admin.unregister** – Administrator command to unregister an existing user. - **authme.admin.updatemessages** – Permission to use the update messages command. +- **authme.allowchatbeforelogin** – Permission to send chat messages before being logged in. - **authme.allowmultipleaccounts** – Permission to be able to register multiple accounts. - **authme.bypassantibot** – Permission node to bypass AntiBot protection. - **authme.bypasscountrycheck** – Permission to bypass the GeoIp country code check. @@ -69,4 +70,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 Sun Apr 22 11:00:13 CEST 2018 +This page was automatically generated on the [AuthMe/AuthMeReloaded repository](https://github.com/AuthMe/AuthMeReloaded/tree/master/docs/) on Mon May 21 08:43:08 CEST 2018 diff --git a/src/main/java/fr/xephi/authme/listener/PlayerListener.java b/src/main/java/fr/xephi/authme/listener/PlayerListener.java index 5b6327127..660119ed8 100644 --- a/src/main/java/fr/xephi/authme/listener/PlayerListener.java +++ b/src/main/java/fr/xephi/authme/listener/PlayerListener.java @@ -7,6 +7,7 @@ import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.message.MessageKey; import fr.xephi.authme.message.Messages; import fr.xephi.authme.permission.PermissionsManager; +import fr.xephi.authme.permission.PlayerStatePermission; import fr.xephi.authme.permission.handlers.PermissionLoadUserException; import fr.xephi.authme.process.Management; import fr.xephi.authme.service.AntiBotService; @@ -65,7 +66,7 @@ public class PlayerListener implements Listener { @Inject private Settings settings; @Inject - private Messages m; + private Messages messages; @Inject private DataSource dataSource; @Inject @@ -107,12 +108,12 @@ public class PlayerListener implements Listener { final Player player = event.getPlayer(); if (!quickCommandsProtectionManager.isAllowed(player.getName())) { event.setCancelled(true); - player.kickPlayer(m.retrieveSingle(player, MessageKey.QUICK_COMMAND_PROTECTION_KICK)); + player.kickPlayer(messages.retrieveSingle(player, MessageKey.QUICK_COMMAND_PROTECTION_KICK)); return; } if (listenerService.shouldCancelEvent(player)) { event.setCancelled(true); - m.send(player, MessageKey.DENIED_COMMAND); + messages.send(player, MessageKey.DENIED_COMMAND); } } @@ -123,10 +124,18 @@ public class PlayerListener implements Listener { } final Player player = event.getPlayer(); - if (listenerService.shouldCancelEvent(player)) { + final boolean mayPlayerSendChat = !listenerService.shouldCancelEvent(player) + || permissionsManager.hasPermission(player, PlayerStatePermission.ALLOW_CHAT_BEFORE_LOGIN); + if (mayPlayerSendChat) { + removeUnauthorizedRecipients(event); + } else { event.setCancelled(true); - m.send(player, MessageKey.DENIED_CHAT); - } else if (settings.getProperty(RestrictionSettings.HIDE_CHAT)) { + messages.send(player, MessageKey.DENIED_CHAT); + } + } + + private void removeUnauthorizedRecipients(AsyncPlayerChatEvent event) { + if (settings.getProperty(RestrictionSettings.HIDE_CHAT)) { event.getRecipients().removeIf(listenerService::shouldCancelEvent); if (event.getRecipients().isEmpty()) { event.setCancelled(true); @@ -274,7 +283,7 @@ public class PlayerListener implements Listener { try { runOnJoinChecks(JoiningPlayer.fromName(name), event.getAddress().getHostAddress()); } catch (FailedVerificationException e) { - event.setKickMessage(m.retrieveSingle(name, e.getReason(), e.getArgs())); + event.setKickMessage(messages.retrieveSingle(name, e.getReason(), e.getArgs())); event.setLoginResult(AsyncPlayerPreLoginEvent.Result.KICK_OTHER); } } @@ -302,7 +311,7 @@ public class PlayerListener implements Listener { try { runOnJoinChecks(JoiningPlayer.fromPlayerObject(player), event.getAddress().getHostAddress()); } catch (FailedVerificationException e) { - event.setKickMessage(m.retrieveSingle(player, e.getReason(), e.getArgs())); + event.setKickMessage(messages.retrieveSingle(player, 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 2a0517225..667b55d56 100644 --- a/src/main/java/fr/xephi/authme/permission/PlayerStatePermission.java +++ b/src/main/java/fr/xephi/authme/permission/PlayerStatePermission.java @@ -34,7 +34,12 @@ public enum PlayerStatePermission implements PermissionNode { /** * Permission to bypass the GeoIp country code check. */ - BYPASS_COUNTRY_CHECK("authme.bypasscountrycheck", DefaultPermission.NOT_ALLOWED); + BYPASS_COUNTRY_CHECK("authme.bypasscountrycheck", DefaultPermission.NOT_ALLOWED), + + /** + * Permission to send chat messages before being logged in. + */ + ALLOW_CHAT_BEFORE_LOGIN("authme.allowchatbeforelogin", DefaultPermission.NOT_ALLOWED); /** * The permission node. @@ -42,7 +47,7 @@ public enum PlayerStatePermission implements PermissionNode { private String node; /** - * The default permission level + * The default permission level. */ private DefaultPermission defaultPermission; diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 1be18288c..90e31c921 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -161,6 +161,9 @@ permissions: authme.admin.updatemessages: description: Permission to use the update messages command. default: op + authme.allowchatbeforelogin: + description: Permission to send chat messages before being logged in. + default: false authme.allowmultipleaccounts: description: Permission to be able to register multiple accounts. default: op diff --git a/src/test/java/fr/xephi/authme/listener/PlayerListenerTest.java b/src/test/java/fr/xephi/authme/listener/PlayerListenerTest.java index 42d14e463..59d837a9c 100644 --- a/src/test/java/fr/xephi/authme/listener/PlayerListenerTest.java +++ b/src/test/java/fr/xephi/authme/listener/PlayerListenerTest.java @@ -6,6 +6,8 @@ import fr.xephi.authme.data.auth.PlayerAuth; import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.message.MessageKey; import fr.xephi.authme.message.Messages; +import fr.xephi.authme.permission.PermissionsManager; +import fr.xephi.authme.permission.PlayerStatePermission; import fr.xephi.authme.process.Management; import fr.xephi.authme.service.AntiBotService; import fr.xephi.authme.service.BukkitService; @@ -60,6 +62,7 @@ import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceTo import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.nullValue; import static org.junit.Assert.assertThat; import static org.mockito.ArgumentMatchers.any; @@ -113,6 +116,8 @@ public class PlayerListenerTest { private JoinMessageService joinMessageService; @Mock private QuickCommandsProtectionManager quickCommandsProtectionManager; + @Mock + private PermissionsManager permissionsManager; /** * #831: If a player is kicked because of "logged in from another location", the kick @@ -289,12 +294,14 @@ public class PlayerListenerTest { given(settings.getProperty(RestrictionSettings.ALLOW_CHAT)).willReturn(false); AsyncPlayerChatEvent event = newAsyncChatEvent(); given(listenerService.shouldCancelEvent(event.getPlayer())).willReturn(true); + given(permissionsManager.hasPermission(event.getPlayer(), PlayerStatePermission.ALLOW_CHAT_BEFORE_LOGIN)).willReturn(false); // when listener.onPlayerChat(event); // then verify(listenerService).shouldCancelEvent(event.getPlayer()); + verify(permissionsManager).hasPermission(event.getPlayer(), PlayerStatePermission.ALLOW_CHAT_BEFORE_LOGIN); verify(event).setCancelled(true); verify(messages).send(event.getPlayer(), MessageKey.DENIED_CHAT); } @@ -356,6 +363,25 @@ public class PlayerListenerTest { assertThat(event.getRecipients(), empty()); } + @Test + public void shouldAllowChatForBypassPermission() { + // given + given(settings.getProperty(RestrictionSettings.ALLOW_CHAT)).willReturn(false); + AsyncPlayerChatEvent event = newAsyncChatEvent(); + given(listenerService.shouldCancelEvent(event.getPlayer())).willReturn(true); + given(permissionsManager.hasPermission(event.getPlayer(), PlayerStatePermission.ALLOW_CHAT_BEFORE_LOGIN)).willReturn(true); + given(settings.getProperty(RestrictionSettings.HIDE_CHAT)).willReturn(false); + + // when + listener.onPlayerChat(event); + + // then + assertThat(event.isCancelled(), equalTo(false)); + verify(listenerService).shouldCancelEvent(event.getPlayer()); + verify(permissionsManager).hasPermission(event.getPlayer(), PlayerStatePermission.ALLOW_CHAT_BEFORE_LOGIN); + assertThat(event.getRecipients(), hasSize(3)); + } + @Test public void shouldAllowUnlimitedMovement() { // given