Create util method to check if email is empty

- Create method to check if email is empty or the default AuthMe email (avoids repetition)
- Check that input email has '@' inside text (relates to #1105)
This commit is contained in:
ljacqu 2017-04-18 21:24:33 +02:00
parent b99cc3bada
commit b0c05afaa7
9 changed files with 40 additions and 14 deletions

View File

@ -5,6 +5,7 @@ import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.datasource.DataSourceResult;
import fr.xephi.authme.mail.SendMailSsl;
import fr.xephi.authme.util.StringUtils;
import fr.xephi.authme.util.Utils;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.bukkit.ChatColor;
@ -69,7 +70,7 @@ class TestEmailSender implements DebugSection {
return null;
}
final String email = emailResult.getValue();
if (email == null || "your@email.com".equals(email)) {
if (Utils.isEmailEmpty(email)) {
sender.sendMessage(ChatColor.RED + "No email set for your account!"
+ " Please use /authme debug mail <email>");
return null;

View File

@ -10,6 +10,7 @@ import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.PasswordRecoveryService;
import fr.xephi.authme.service.RecoveryCodeService;
import fr.xephi.authme.util.Utils;
import org.bukkit.entity.Player;
import javax.inject.Inject;
@ -60,7 +61,7 @@ public class RecoverEmailCommand extends PlayerCommand {
}
final String email = emailResult.getValue();
if (email == null || !email.equalsIgnoreCase(playerMail) || "your@email.com".equalsIgnoreCase(email)) {
if (Utils.isEmailEmpty(email) || !email.equalsIgnoreCase(playerMail)) {
commonService.send(player, MessageKey.INVALID_EMAIL);
return;
}

View File

@ -5,6 +5,7 @@ import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.util.Utils;
import org.bukkit.entity.Player;
import javax.inject.Inject;
@ -24,7 +25,7 @@ public class ShowEmailCommand extends PlayerCommand {
@Override
public void runCommand(Player player, List<String> arguments) {
PlayerAuth auth = playerCache.getAuth(player.getName());
if (auth != null && auth.getEmail() != null && !"your@email.com".equalsIgnoreCase(auth.getEmail())) {
if (auth != null && !Utils.isEmailEmpty(auth.getEmail())) {
commonService.send(player, MessageKey.EMAIL_SHOW, auth.getEmail());
} else {
commonService.send(player, MessageKey.SHOW_NO_EMAIL);

View File

@ -7,20 +7,20 @@ 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.output.ConsoleFilter;
import fr.xephi.authme.output.Log4JFilter;
import fr.xephi.authme.security.HashAlgorithm;
import fr.xephi.authme.security.crypts.description.Recommendation;
import fr.xephi.authme.security.crypts.description.Usage;
import org.bstats.Metrics;
import fr.xephi.authme.output.ConsoleFilter;
import fr.xephi.authme.output.Log4JFilter;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.DatabaseSettings;
import fr.xephi.authme.settings.properties.EmailSettings;
import fr.xephi.authme.settings.properties.PluginSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.util.StringUtils;
import fr.xephi.authme.util.Utils;
import org.apache.logging.log4j.LogManager;
import org.bstats.Metrics;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
@ -112,7 +112,7 @@ public class OnStartupTasks {
public void run() {
for (PlayerAuth auth : dataSource.getLoggedPlayers()) {
String email = auth.getEmail();
if (StringUtils.isEmpty(email) || "your@email.com".equalsIgnoreCase(email)) {
if (Utils.isEmailEmpty(email)) {
Player player = bukkitService.getPlayerExact(auth.getRealName());
if (player != null) {
messages.send(player, MessageKey.ADD_EMAIL_MESSAGE);

View File

@ -8,6 +8,7 @@ import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.AsynchronousProcess;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.util.Utils;
import org.bukkit.entity.Player;
import javax.inject.Inject;
@ -38,7 +39,7 @@ public class AsyncAddEmail implements AsynchronousProcess {
PlayerAuth auth = playerCache.getAuth(playerName);
final String currentEmail = auth.getEmail();
if (currentEmail != null && !"your@email.com".equals(currentEmail)) {
if (!Utils.isEmailEmpty(currentEmail)) {
service.send(player, MessageKey.USAGE_CHANGE_EMAIL);
} else if (!validationService.validateEmail(email)) {
service.send(player, MessageKey.INVALID_EMAIL);

View File

@ -25,7 +25,7 @@ import fr.xephi.authme.settings.properties.HooksSettings;
import fr.xephi.authme.settings.properties.PluginSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.util.PlayerUtils;
import fr.xephi.authme.util.StringUtils;
import fr.xephi.authme.util.Utils;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
@ -228,8 +228,7 @@ public class AsynchronousLogin implements AsynchronousProcess {
displayOtherAccounts(auths, player);
final String email = auth.getEmail();
if (service.getProperty(EmailSettings.RECALL_PLAYERS)
&& (StringUtils.isEmpty(email) || "your@email.com".equalsIgnoreCase(email))) {
if (service.getProperty(EmailSettings.RECALL_PLAYERS) && Utils.isEmailEmpty(email)) {
service.send(player, MessageKey.ADD_EMAIL_MESSAGE);
}

View File

@ -15,6 +15,7 @@ import fr.xephi.authme.settings.properties.ProtectionSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.util.PlayerUtils;
import fr.xephi.authme.util.StringUtils;
import fr.xephi.authme.util.Utils;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -90,7 +91,7 @@ public class ValidationService implements Reloadable {
* @return true if the email is valid, false otherwise
*/
public boolean validateEmail(String email) {
if (!email.contains("@") || email.endsWith("@") || "your@email.com".equalsIgnoreCase(email)) {
if (Utils.isEmailEmpty(email) || !StringUtils.isInsideString('@', email)) {
return false;
}
final String emailDomain = email.split("@")[1];

View File

@ -88,4 +88,14 @@ public final class Utils {
return Runtime.getRuntime().availableProcessors();
}
/**
* Returns whether the given email is empty or equal to the standard "undefined" email address.
*
* @param email the email to check
*
* @return true if the email is empty
*/
public static boolean isEmailEmpty(String email) {
return StringUtils.isEmpty(email) || "your@email.com".equalsIgnoreCase(email);
}
}

View File

@ -22,7 +22,7 @@ import static org.mockito.Mockito.verifyZeroInteractions;
public class UtilsTest {
@BeforeClass
public static void setAuthmeInstance() {
public static void initLogger() {
TestHelper.setupLogger();
}
@ -105,4 +105,16 @@ public class UtilsTest {
assertThat(Utils.isClassLoaded("org.bukkit.event.player.PlayerFishEvent"), equalTo(true));
assertThat(Utils.isClassLoaded("com.someclass.doesnot.exist"), equalTo(false));
}
@Test
public void shouldDetectIfEmailIsEmpty() {
// given / when / then
assertThat(Utils.isEmailEmpty(""), equalTo(true));
assertThat(Utils.isEmailEmpty(null), equalTo(true));
assertThat(Utils.isEmailEmpty("your@email.com"), equalTo(true));
assertThat(Utils.isEmailEmpty("Your@Email.com"), equalTo(true));
assertThat(Utils.isEmailEmpty("my@example.org"), equalTo(false));
assertThat(Utils.isEmailEmpty("hey"), equalTo(false));
}
}