From 2cd2b48a1a5b03d6ee0d103160a72ff6699caeb5 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Sat, 6 Feb 2016 21:56:08 +0100 Subject: [PATCH] #437 Email uniqueness in admin command; finalization - Check also in admin command that email is not already used - Misc bug fixing (logic errors, changes lost during large merge) - Use "email" and "setemail" as main labels for /authme subcommands --- .../xephi/authme/command/CommandInitializer.java | 4 ++-- .../command/executable/authme/SetEmailCommand.java | 3 +++ .../java/fr/xephi/authme/process/Management.java | 4 +++- .../xephi/authme/process/email/AsyncAddEmail.java | 14 ++++---------- .../authme/process/email/AsyncChangeEmail.java | 10 ++-------- .../authme/process/email/AsyncAddEmailTest.java | 2 +- 6 files changed, 15 insertions(+), 22 deletions(-) diff --git a/src/main/java/fr/xephi/authme/command/CommandInitializer.java b/src/main/java/fr/xephi/authme/command/CommandInitializer.java index 0a52c7008..0a29b1095 100644 --- a/src/main/java/fr/xephi/authme/command/CommandInitializer.java +++ b/src/main/java/fr/xephi/authme/command/CommandInitializer.java @@ -133,7 +133,7 @@ public final class CommandInitializer { // Register the getemail command CommandDescription.builder() .parent(AUTHME_BASE) - .labels("getemail", "getmail", "email", "mail") + .labels("email", "mail", "getemail", "getmail") .description("Display player's email") .detailedDescription("Display the email address of the specified player if set.") .withArgument("player", "Player name", true) @@ -144,7 +144,7 @@ public final class CommandInitializer { // Register the setemail command CommandDescription.builder() .parent(AUTHME_BASE) - .labels("chgemail", "chgmail", "setemail", "setmail") + .labels("setemail", "setmail", "chgemail", "chgmail") .description("Change player's email") .detailedDescription("Change the email address of the specified player.") .withArgument("player", "Player name", false) diff --git a/src/main/java/fr/xephi/authme/command/executable/authme/SetEmailCommand.java b/src/main/java/fr/xephi/authme/command/executable/authme/SetEmailCommand.java index ca794bb46..d19a2ce61 100644 --- a/src/main/java/fr/xephi/authme/command/executable/authme/SetEmailCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/authme/SetEmailCommand.java @@ -33,6 +33,9 @@ public class SetEmailCommand implements ExecutableCommand { if (auth == null) { commandService.send(sender, MessageKey.UNKNOWN_USER); return; + } else if (commandService.getDataSource().isEmailStored(playerEmail)) { + commandService.send(sender, MessageKey.EMAIL_ALREADY_USED_ERROR); + return; } // Set the email address diff --git a/src/main/java/fr/xephi/authme/process/Management.java b/src/main/java/fr/xephi/authme/process/Management.java index ec48328fb..ace554b7c 100644 --- a/src/main/java/fr/xephi/authme/process/Management.java +++ b/src/main/java/fr/xephi/authme/process/Management.java @@ -2,6 +2,7 @@ package fr.xephi.authme.process; import fr.xephi.authme.AuthMe; import fr.xephi.authme.cache.auth.PlayerCache; +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; @@ -99,7 +100,8 @@ public class Management { sched.runTaskAsynchronously(plugin, new Runnable() { @Override public void run() { - new AsyncChangeEmail(player, plugin, null, newEmail, plugin.getDataSource(), PlayerCache.getInstance(), settings).process(); + new AsyncAddEmail(player, plugin, newEmail, plugin.getDataSource(), + PlayerCache.getInstance(), settings).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 index 5f0ef8fc7..c7673f705 100644 --- a/src/main/java/fr/xephi/authme/process/email/AsyncAddEmail.java +++ b/src/main/java/fr/xephi/authme/process/email/AsyncAddEmail.java @@ -8,7 +8,6 @@ import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.Messages; import fr.xephi.authme.settings.NewSetting; import fr.xephi.authme.settings.Settings; -import fr.xephi.authme.util.StringUtils; import fr.xephi.authme.util.Utils; import org.bukkit.entity.Player; @@ -24,7 +23,7 @@ public class AsyncAddEmail { private final PlayerCache playerCache; private final NewSetting settings; - public AsyncAddEmail(AuthMe plugin, Player player, String email, DataSource dataSource, + public AsyncAddEmail(Player player, AuthMe plugin, String email, DataSource dataSource, PlayerCache playerCache, NewSetting settings) { this.messages = plugin.getMessages(); this.player = player; @@ -39,11 +38,11 @@ public class AsyncAddEmail { if (playerCache.isAuthenticated(playerName)) { PlayerAuth auth = playerCache.getAuth(playerName); - String currentEmail = auth.getEmail(); + final String currentEmail = auth.getEmail(); - if (currentEmail != null && !"your@mail.com".equals(currentEmail)) { + if (currentEmail != null && !"your@email.com".equals(currentEmail)) { messages.send(player, MessageKey.USAGE_CHANGE_EMAIL); - } else if (isEmailInvalid(email)) { + } else if (!Utils.isEmailCorrect(email, settings)) { messages.send(player, MessageKey.INVALID_EMAIL); } else if (dataSource.isEmailStored(email)) { messages.send(player, MessageKey.EMAIL_ALREADY_USED_ERROR); @@ -57,11 +56,6 @@ public class AsyncAddEmail { } } - private boolean isEmailInvalid(String email) { - return StringUtils.isEmpty(email) || "your@email.com".equals(email) - || !Utils.isEmailCorrect(email, settings); - } - private void sendUnloggedMessage(DataSource dataSource) { if (dataSource.isAuthAvailable(player.getName())) { messages.send(player, MessageKey.LOGIN_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 791802fc1..f9bbbd073 100644 --- a/src/main/java/fr/xephi/authme/process/email/AsyncChangeEmail.java +++ b/src/main/java/fr/xephi/authme/process/email/AsyncChangeEmail.java @@ -8,7 +8,6 @@ import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.Messages; import fr.xephi.authme.settings.NewSetting; import fr.xephi.authme.settings.Settings; -import fr.xephi.authme.util.StringUtils; import fr.xephi.authme.util.Utils; import org.bukkit.entity.Player; @@ -40,11 +39,11 @@ public class AsyncChangeEmail { String playerName = player.getName().toLowerCase(); if (playerCache.isAuthenticated(playerName)) { PlayerAuth auth = playerCache.getAuth(playerName); - String currentEmail = auth.getEmail(); + final String currentEmail = auth.getEmail(); if (currentEmail == null) { m.send(player, MessageKey.USAGE_ADD_EMAIL); - } else if (isEmailInvalid(newEmail)) { + } else if (newEmail == null || !Utils.isEmailCorrect(newEmail, settings)) { m.send(player, MessageKey.INVALID_NEW_EMAIL); } else if (!oldEmail.equals(currentEmail)) { m.send(player, MessageKey.INVALID_OLD_EMAIL); @@ -58,11 +57,6 @@ public class AsyncChangeEmail { } } - private boolean isEmailInvalid(String email) { - return StringUtils.isEmpty(email) || "your@email.com".equals(email) - || !Utils.isEmailCorrect(email, settings); - } - private void saveNewEmail(PlayerAuth auth) { auth.setEmail(newEmail); if (dataSource.updateEmail(auth)) { diff --git a/src/test/java/fr/xephi/authme/process/email/AsyncAddEmailTest.java b/src/test/java/fr/xephi/authme/process/email/AsyncAddEmailTest.java index 82eb77f13..4aea5e578 100644 --- a/src/test/java/fr/xephi/authme/process/email/AsyncAddEmailTest.java +++ b/src/test/java/fr/xephi/authme/process/email/AsyncAddEmailTest.java @@ -187,7 +187,7 @@ public class AsyncAddEmailTest { dataSource = mock(DataSource.class); playerCache = mock(PlayerCache.class); settings = mock(NewSetting.class); - return new AsyncAddEmail(authMe, player, email, dataSource, playerCache, settings); + return new AsyncAddEmail(player, authMe, email, dataSource, playerCache, settings); } }