#1417 Add permission node to allow chat before login

This commit is contained in:
ljacqu 2018-05-21 08:45:18 +02:00
parent 8e4288f911
commit 6142042996
5 changed files with 56 additions and 12 deletions

View File

@ -1,5 +1,5 @@
<!-- AUTO-GENERATED FILE! Do not edit this directly -->
<!-- File auto-generated on Sun Apr 22 11:00:13 CEST 2018. See docs/permissions/permission_nodes.tpl.md -->
<!-- File auto-generated on Mon May 21 08:43:08 CEST 2018. See docs/permissions/permission_nodes.tpl.md -->
## 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

View File

@ -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);
}
}

View File

@ -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;

View File

@ -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

View File

@ -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