diff --git a/src/main/java/fr/xephi/authme/command/executable/email/AddEmailCommand.java b/src/main/java/fr/xephi/authme/command/executable/email/AddEmailCommand.java index 96d9aa7af..663372cda 100644 --- a/src/main/java/fr/xephi/authme/command/executable/email/AddEmailCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/email/AddEmailCommand.java @@ -2,6 +2,9 @@ package fr.xephi.authme.command.executable.email; import fr.xephi.authme.command.CommandService; import fr.xephi.authme.command.PlayerCommand; +import fr.xephi.authme.output.MessageKey; +import fr.xephi.authme.settings.Settings; +import fr.xephi.authme.util.StringUtils; import org.bukkit.entity.Player; import java.util.List; @@ -10,9 +13,15 @@ public class AddEmailCommand extends PlayerCommand { @Override public void runCommand(Player player, List arguments, CommandService commandService) { - String playerMail = arguments.get(0); - String playerMailVerify = arguments.get(1); + String email = arguments.get(0); + String emailConfirmation = arguments.get(1); - commandService.getManagement().performAddEmail(player, playerMail, playerMailVerify); + if (StringUtils.isEmpty(email) || "your@email.com".equals(email) || !Settings.isEmailCorrect(email)) { + commandService.send(player, MessageKey.INVALID_EMAIL); + } else if (email.equals(emailConfirmation)) { + commandService.getManagement().performAddEmail(player, email); + } else { + commandService.send(player, MessageKey.CONFIRM_EMAIL_MESSAGE); + } } } diff --git a/src/main/java/fr/xephi/authme/process/Management.java b/src/main/java/fr/xephi/authme/process/Management.java index 6d262308d..10ceb6989 100644 --- a/src/main/java/fr/xephi/authme/process/Management.java +++ b/src/main/java/fr/xephi/authme/process/Management.java @@ -1,6 +1,7 @@ package fr.xephi.authme.process; import fr.xephi.authme.AuthMe; +import fr.xephi.authme.process.email.AsyncAddEmail; import fr.xephi.authme.process.email.AsyncChangeEmail; import fr.xephi.authme.process.join.AsynchronousJoin; import fr.xephi.authme.process.login.AsynchronousLogin; @@ -90,11 +91,11 @@ public class Management { }); } - public void performAddEmail(final Player player, final String newEmail, final String newEmailVerify) { + public void performAddEmail(final Player player, final String newEmail) { sched.runTaskAsynchronously(plugin, new Runnable() { @Override public void run() { - new AsyncChangeEmail(player, plugin, null, newEmail, newEmailVerify).process(); + new AsyncAddEmail(plugin, player, newEmail).process(); } }); } diff --git a/src/main/java/fr/xephi/authme/process/email/AsyncAddEmail.java b/src/main/java/fr/xephi/authme/process/email/AsyncAddEmail.java new file mode 100644 index 000000000..23e02902f --- /dev/null +++ b/src/main/java/fr/xephi/authme/process/email/AsyncAddEmail.java @@ -0,0 +1,62 @@ +package fr.xephi.authme.process.email; + +import fr.xephi.authme.AuthMe; +import fr.xephi.authme.cache.auth.PlayerAuth; +import fr.xephi.authme.cache.auth.PlayerCache; +import fr.xephi.authme.datasource.DataSource; +import fr.xephi.authme.output.MessageKey; +import fr.xephi.authme.output.Messages; +import fr.xephi.authme.settings.Settings; +import fr.xephi.authme.util.StringUtils; +import org.bukkit.entity.Player; + +/** + * Async task to add an email to an account. + */ +public class AsyncAddEmail { + + private AuthMe plugin; + private Player player; + private String email; + private Messages messages; + + public AsyncAddEmail(AuthMe plugin, Player player, String email) { + this.plugin = plugin; + this.messages = plugin.getMessages(); + this.player = player; + this.email = email; + } + + public void process() { + String playerName = player.getName().toLowerCase(); + PlayerCache playerCache = PlayerCache.getInstance(); + + if (playerCache.isAuthenticated(playerName)) { + PlayerAuth auth = PlayerCache.getInstance().getAuth(playerName); + String currentEmail = auth.getEmail(); + + if (currentEmail == null) { + messages.send(player, MessageKey.USAGE_CHANGE_EMAIL); + } else if (StringUtils.isEmpty(email) || "your@email.com".equals(email) || Settings.isEmailCorrect(email)) { + messages.send(player, MessageKey.INVALID_EMAIL); + } else { + auth.setEmail(email); + playerCache.updatePlayer(auth); + messages.send(player, MessageKey.EMAIL_ADDED_SUCCESS); + } + } else { + sendUnloggedMessage(plugin.getDataSource()); + } + } + + private void sendUnloggedMessage(DataSource dataSource) { + if (dataSource.isAuthAvailable(player.getName())) { + messages.send(player, MessageKey.LOGIN_MESSAGE); + } else if (Settings.emailRegistration) { + messages.send(player, MessageKey.REGISTER_EMAIL_MESSAGE); + } else { + messages.send(player, MessageKey.REGISTER_MESSAGE); + } + } + +} diff --git a/src/main/java/fr/xephi/authme/process/email/AsyncChangeEmail.java b/src/main/java/fr/xephi/authme/process/email/AsyncChangeEmail.java index e59aaab89..3e438661f 100644 --- a/src/main/java/fr/xephi/authme/process/email/AsyncChangeEmail.java +++ b/src/main/java/fr/xephi/authme/process/email/AsyncChangeEmail.java @@ -3,6 +3,7 @@ package fr.xephi.authme.process.email; import fr.xephi.authme.AuthMe; import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerCache; +import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.Messages; import fr.xephi.authme.settings.Settings; @@ -17,74 +18,56 @@ public class AsyncChangeEmail { private final AuthMe plugin; private final String oldEmail; private final String newEmail; - private final String newEmailVerify; private final Messages m; - public AsyncChangeEmail(Player player, AuthMe plugin, String oldEmail, String newEmail, String newEmailVerify) { + public AsyncChangeEmail(Player player, AuthMe plugin, String oldEmail, String newEmail) { this.m = plugin.getMessages(); this.player = player; this.plugin = plugin; this.oldEmail = oldEmail; this.newEmail = newEmail; - this.newEmailVerify = newEmailVerify; - } - - public AsyncChangeEmail(Player player, AuthMe plugin, String oldEmail, String newEmail) { - this(player, plugin, oldEmail, newEmail, newEmail); } public void process() { String playerName = player.getName().toLowerCase(); if (PlayerCache.getInstance().isAuthenticated(playerName)) { - if (!newEmail.equals(newEmailVerify)) { - m.send(player, MessageKey.CONFIRM_EMAIL_MESSAGE); - return; - } PlayerAuth auth = PlayerCache.getInstance().getAuth(playerName); String currentEmail = auth.getEmail(); - if (oldEmail != null) { - if (StringUtils.isEmpty(currentEmail) || currentEmail.equals("your@email.com")) { - m.send(player, MessageKey.USAGE_ADD_EMAIL); - return; - } - if (!oldEmail.equals(currentEmail)) { - m.send(player, MessageKey.INVALID_OLD_EMAIL); - return; - } - } else { - if (!StringUtils.isEmpty(currentEmail) && !currentEmail.equals("your@email.com")) { - m.send(player, MessageKey.USAGE_CHANGE_EMAIL); - return; - } - } - if (!Settings.isEmailCorrect(newEmail)) { + + if (currentEmail == null) { + m.send(player, MessageKey.USAGE_ADD_EMAIL); + } else if (StringUtils.isEmpty(newEmail) || "your@email.com".equals(newEmail)) { + m.send(player, MessageKey.INVALID_EMAIL); + } else if (!oldEmail.equals(currentEmail)) { + m.send(player, MessageKey.INVALID_OLD_EMAIL); + } else if (Settings.isEmailCorrect(newEmail)) { m.send(player, MessageKey.INVALID_NEW_EMAIL); - return; - } - auth.setEmail(newEmail); - if (!plugin.getDataSource().updateEmail(auth)) { - m.send(player, MessageKey.ERROR); - auth.setEmail(currentEmail); - return; + } else { + saveNewEmail(auth); } + } else { + outputUnloggedMessage(); + } + } + + private void saveNewEmail(PlayerAuth auth) { + auth.setEmail(newEmail); + if (plugin.getDataSource().updateEmail(auth)) { PlayerCache.getInstance().updatePlayer(auth); - if (oldEmail == null) { - m.send(player, MessageKey.EMAIL_ADDED_SUCCESS); - player.sendMessage(auth.getEmail()); - return; - } m.send(player, MessageKey.EMAIL_CHANGED_SUCCESS); } else { - if (plugin.getDataSource().isAuthAvailable(playerName)) { - m.send(player, MessageKey.LOGIN_MESSAGE); - } else { - if (Settings.emailRegistration) { - m.send(player, MessageKey.REGISTER_EMAIL_MESSAGE); - } else { - m.send(player, MessageKey.REGISTER_MESSAGE); - } - } + m.send(player, MessageKey.ERROR); + auth.setEmail(newEmail); } + } + private void outputUnloggedMessage() { + if (plugin.getDataSource().isAuthAvailable(player.getName())) { + m.send(player, MessageKey.LOGIN_MESSAGE); + } else if (Settings.emailRegistration) { + m.send(player, MessageKey.REGISTER_EMAIL_MESSAGE); + } else { + m.send(player, MessageKey.REGISTER_MESSAGE); + } } } diff --git a/src/test/java/fr/xephi/authme/command/executable/email/AddEmailCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/email/AddEmailCommandTest.java index b255a5685..931c9c4eb 100644 --- a/src/test/java/fr/xephi/authme/command/executable/email/AddEmailCommandTest.java +++ b/src/test/java/fr/xephi/authme/command/executable/email/AddEmailCommandTest.java @@ -2,6 +2,7 @@ package fr.xephi.authme.command.executable.email; import fr.xephi.authme.command.CommandService; import fr.xephi.authme.process.Management; +import fr.xephi.authme.util.WrapperMock; import org.bukkit.command.BlockCommandSender; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -27,6 +28,7 @@ public class AddEmailCommandTest { @Before public void setUpMocks() { commandService = mock(CommandService.class); + WrapperMock.createInstance(); } @Test @@ -51,10 +53,10 @@ public class AddEmailCommandTest { given(commandService.getManagement()).willReturn(management); // when - command.executeCommand(sender, Arrays.asList("mail@example", "other_example"), commandService); + command.executeCommand(sender, Arrays.asList("mail@example", "mail@example"), commandService); // then - verify(management).performAddEmail(sender, "mail@example", "other_example"); + verify(management).performAddEmail(sender, "mail@example"); } }