Merge CommandService and ProcessService to CommonService

- Replace CommandService and ProcessService with CommonService: a service that offers our typical needs to work with settings, messages and permissions
- Remove validation methods from CommonService: inject ValidationService directly. Validation methods are not used very frequently and therefore don't belong in CommonService. Their presence was a relict from our architecture before injection was used.
This commit is contained in:
ljacqu
2016-12-03 12:10:30 +01:00
parent a38d3a25b8
commit c325d0db41
55 changed files with 317 additions and 553 deletions
@@ -1,94 +0,0 @@
package fr.xephi.authme.command;
import com.github.authme.configme.properties.Property;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.message.Messages;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.service.ValidationService;
import org.bukkit.command.CommandSender;
import javax.inject.Inject;
/**
* Service for implementations of {@link ExecutableCommand} to execute some common tasks.
* This service basically wraps calls, forwarding them to other classes.
*/
public class CommandService {
@Inject
private Messages messages;
@Inject
private Settings settings;
@Inject
private ValidationService validationService;
/**
* Send a message to a player.
*
* @param sender The command sender to send the message to
* @param messageKey The message key to send
*/
public void send(CommandSender sender, MessageKey messageKey) {
messages.send(sender, messageKey);
}
/**
* Send a message to a player.
*
* @param sender The command sender to send the message to
* @param messageKey The message key to send
* @param replacements The replacement arguments for the message key's tags
*/
public void send(CommandSender sender, MessageKey messageKey, String... replacements) {
messages.send(sender, messageKey, replacements);
}
/**
* Retrieve a message by its message key.
*
* @param key The message to retrieve
* @return The message
*/
public String[] retrieveMessage(MessageKey key) {
return messages.retrieve(key);
}
/**
* Retrieve a message as a single String by its message key.
*
* @param key The message to retrieve
* @return The message
*/
public String retrieveSingle(MessageKey key) {
return messages.retrieveSingle(key);
}
/**
* Retrieve the given property's value.
*
* @param property The property to retrieve
* @param <T> The type of the property
* @return The property's value
*/
public <T> T getProperty(Property<T> property) {
return settings.getProperty(property);
}
/**
* Return the settings manager.
*
* @return The settings manager
*/
public Settings getSettings() {
return settings;
}
public boolean validateEmail(String email) {
return validationService.validateEmail(email);
}
public boolean isEmailFreeForRegistration(String email, CommandSender sender) {
return validationService.isEmailFreeForRegistration(email, sender);
}
}
@@ -1,11 +1,11 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.CommonService;
import org.bukkit.command.CommandSender;
import javax.inject.Inject;
@@ -23,7 +23,7 @@ public class AccountsCommand implements ExecutableCommand {
private BukkitService bukkitService;
@Inject
private CommandService commandService;
private CommonService commonService;
@Override
public void executeCommand(final CommandSender sender, List<String> arguments) {
@@ -50,13 +50,13 @@ public class AccountsCommand implements ExecutableCommand {
public void run() {
PlayerAuth auth = dataSource.getAuth(playerName.toLowerCase());
if (auth == null) {
commandService.send(sender, MessageKey.UNKNOWN_USER);
commonService.send(sender, MessageKey.UNKNOWN_USER);
return;
}
List<String> accountList = dataSource.getAllAuthsByIp(auth.getIp());
if (accountList.isEmpty()) {
commandService.send(sender, MessageKey.USER_NOT_REGISTERED);
commonService.send(sender, MessageKey.USER_NOT_REGISTERED);
} else if (accountList.size() == 1) {
sender.sendMessage("[AuthMe] " + playerName + " is a single account player");
} else {
@@ -1,15 +1,15 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.service.ValidationService.ValidationResult;
import org.bukkit.command.CommandSender;
@@ -38,7 +38,7 @@ public class ChangePasswordAdminCommand implements ExecutableCommand {
private ValidationService validationService;
@Inject
private CommandService commandService;
private CommonService commonService;
@Override
public void executeCommand(final CommandSender sender, List<String> arguments) {
@@ -49,7 +49,7 @@ public class ChangePasswordAdminCommand implements ExecutableCommand {
// Validate the password
ValidationResult validationResult = validationService.validatePassword(playerPass, playerName);
if (validationResult.hasError()) {
commandService.send(sender, validationResult.getMessageKey(), validationResult.getArgs());
commonService.send(sender, validationResult.getMessageKey(), validationResult.getArgs());
return;
}
@@ -67,7 +67,7 @@ public class ChangePasswordAdminCommand implements ExecutableCommand {
private void changePassword(String nameLowercase, String password, CommandSender sender) {
PlayerAuth auth = getAuth(nameLowercase);
if (auth == null) {
commandService.send(sender, MessageKey.UNKNOWN_USER);
commonService.send(sender, MessageKey.UNKNOWN_USER);
return;
}
@@ -75,10 +75,10 @@ public class ChangePasswordAdminCommand implements ExecutableCommand {
auth.setPassword(hashedPassword);
if (dataSource.updatePassword(auth)) {
commandService.send(sender, MessageKey.PASSWORD_CHANGED_SUCCESS);
commonService.send(sender, MessageKey.PASSWORD_CHANGED_SUCCESS);
ConsoleLogger.info(sender.getName() + " changed password of " + nameLowercase);
} else {
commandService.send(sender, MessageKey.ERROR);
commonService.send(sender, MessageKey.ERROR);
}
}
@@ -4,7 +4,6 @@ import ch.jalu.injector.Injector;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableMap;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.datasource.converter.Converter;
import fr.xephi.authme.datasource.converter.CrazyLoginConverter;
@@ -16,6 +15,7 @@ import fr.xephi.authme.datasource.converter.vAuthConverter;
import fr.xephi.authme.datasource.converter.xAuthConverter;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.CommonService;
import org.bukkit.command.CommandSender;
import javax.inject.Inject;
@@ -31,7 +31,7 @@ public class ConverterCommand implements ExecutableCommand {
static final Map<String, Class<? extends Converter>> CONVERTERS = getConverters();
@Inject
private CommandService commandService;
private CommonService commonService;
@Inject
private BukkitService bukkitService;
@@ -61,7 +61,7 @@ public class ConverterCommand implements ExecutableCommand {
try {
converter.execute(sender);
} catch (Exception e) {
commandService.send(sender, MessageKey.ERROR);
commonService.send(sender, MessageKey.ERROR);
ConsoleLogger.logException("Error during conversion:", e);
}
}
@@ -1,10 +1,10 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.service.CommonService;
import org.bukkit.command.CommandSender;
import javax.inject.Inject;
@@ -19,7 +19,7 @@ public class GetEmailCommand implements ExecutableCommand {
private DataSource dataSource;
@Inject
private CommandService commandService;
private CommonService commonService;
@Override
public void executeCommand(CommandSender sender, List<String> arguments) {
@@ -27,7 +27,7 @@ public class GetEmailCommand implements ExecutableCommand {
PlayerAuth auth = dataSource.getAuth(playerName);
if (auth == null) {
commandService.send(sender, MessageKey.UNKNOWN_USER);
commonService.send(sender, MessageKey.UNKNOWN_USER);
} else {
sender.sendMessage("[AuthMe] " + playerName + "'s email: " + auth.getEmail());
}
@@ -1,10 +1,10 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.service.CommonService;
import org.bukkit.command.CommandSender;
import javax.inject.Inject;
@@ -20,7 +20,7 @@ public class LastLoginCommand implements ExecutableCommand {
private DataSource dataSource;
@Inject
private CommandService commandService;
private CommonService commonService;
@Override
public void executeCommand(CommandSender sender, List<String> arguments) {
@@ -29,7 +29,7 @@ public class LastLoginCommand implements ExecutableCommand {
PlayerAuth auth = dataSource.getAuth(playerName);
if (auth == null) {
commandService.send(sender, MessageKey.USER_NOT_REGISTERED);
commonService.send(sender, MessageKey.USER_NOT_REGISTERED);
return;
}
@@ -1,10 +1,10 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.service.CommonService;
import org.bukkit.command.CommandSender;
import javax.inject.Inject;
@@ -19,7 +19,7 @@ public class PurgeLastPositionCommand implements ExecutableCommand {
private DataSource dataSource;
@Inject
private CommandService commandService;
private CommonService commonService;
@Override
public void executeCommand(final CommandSender sender, List<String> arguments) {
@@ -35,7 +35,7 @@ public class PurgeLastPositionCommand implements ExecutableCommand {
// Get the user auth and make sure the user exists
PlayerAuth auth = dataSource.getAuth(playerName);
if (auth == null) {
commandService.send(sender, MessageKey.UNKNOWN_USER);
commonService.send(sender, MessageKey.UNKNOWN_USER);
return;
}
@@ -3,13 +3,13 @@ package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.limbo.LimboCache;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.service.ValidationService.ValidationResult;
import org.bukkit.command.CommandSender;
@@ -27,7 +27,7 @@ public class RegisterAdminCommand implements ExecutableCommand {
private PasswordSecurity passwordSecurity;
@Inject
private CommandService commandService;
private CommonService commonService;
@Inject
private DataSource dataSource;
@@ -51,7 +51,7 @@ public class RegisterAdminCommand implements ExecutableCommand {
// Command logic
ValidationResult passwordValidation = validationService.validatePassword(playerPass, playerName);
if (passwordValidation.hasError()) {
commandService.send(sender, passwordValidation.getMessageKey(), passwordValidation.getArgs());
commonService.send(sender, passwordValidation.getMessageKey(), passwordValidation.getArgs());
return;
}
@@ -60,7 +60,7 @@ public class RegisterAdminCommand implements ExecutableCommand {
@Override
public void run() {
if (dataSource.isAuthAvailable(playerNameLowerCase)) {
commandService.send(sender, MessageKey.NAME_ALREADY_REGISTERED);
commonService.send(sender, MessageKey.NAME_ALREADY_REGISTERED);
return;
}
HashedPassword hashedPassword = passwordSecurity.computeHash(playerPass, playerNameLowerCase);
@@ -71,12 +71,12 @@ public class RegisterAdminCommand implements ExecutableCommand {
.build();
if (!dataSource.saveAuth(auth)) {
commandService.send(sender, MessageKey.ERROR);
commonService.send(sender, MessageKey.ERROR);
return;
}
dataSource.setUnlogged(playerNameLowerCase);
commandService.send(sender, MessageKey.REGISTER_SUCCESS);
commonService.send(sender, MessageKey.REGISTER_SUCCESS);
ConsoleLogger.info(sender.getName() + " registered " + playerName);
final Player player = bukkitService.getPlayerExact(playerName);
if (player != null) {
@@ -84,7 +84,7 @@ public class RegisterAdminCommand implements ExecutableCommand {
@Override
public void run() {
limboCache.restoreData(player);
player.kickPlayer(commandService.retrieveSingle(MessageKey.KICK_FOR_ADMIN_REGISTER));
player.kickPlayer(commonService.retrieveSingleMessage(MessageKey.KICK_FOR_ADMIN_REGISTER));
}
});
}
@@ -3,12 +3,12 @@ package fr.xephi.authme.command.executable.authme;
import ch.jalu.injector.Injector;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.initialization.Reloadable;
import fr.xephi.authme.initialization.SettingsDependent;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.DatabaseSettings;
import org.bukkit.command.CommandSender;
@@ -35,7 +35,7 @@ public class ReloadCommand implements ExecutableCommand {
private DataSource dataSource;
@Inject
private CommandService commandService;
private CommonService commonService;
@Override
public void executeCommand(CommandSender sender, List<String> arguments) {
@@ -48,7 +48,7 @@ public class ReloadCommand implements ExecutableCommand {
sender.sendMessage("Note: cannot change database type during /authme reload");
}
performReloadOnServices();
commandService.send(sender, MessageKey.CONFIG_RELOAD_SUCCESS);
commonService.send(sender, MessageKey.CONFIG_RELOAD_SUCCESS);
} catch (Exception e) {
sender.sendMessage("Error occurred during reload of AuthMe: aborting");
ConsoleLogger.logException("Aborting! Encountered exception during reload of AuthMe:", e);
@@ -1,12 +1,13 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.ValidationService;
import org.bukkit.command.CommandSender;
import javax.inject.Inject;
@@ -21,7 +22,7 @@ public class SetEmailCommand implements ExecutableCommand {
private DataSource dataSource;
@Inject
private CommandService commandService;
private CommonService commonService;
@Inject
private PlayerCache playerCache;
@@ -29,6 +30,9 @@ public class SetEmailCommand implements ExecutableCommand {
@Inject
private BukkitService bukkitService;
@Inject
private ValidationService validationService;
@Override
public void executeCommand(final CommandSender sender, List<String> arguments) {
// Get the player name and email address
@@ -36,8 +40,8 @@ public class SetEmailCommand implements ExecutableCommand {
final String playerEmail = arguments.get(1);
// Validate the email address
if (!commandService.validateEmail(playerEmail)) {
commandService.send(sender, MessageKey.INVALID_EMAIL);
if (!validationService.validateEmail(playerEmail)) {
commonService.send(sender, MessageKey.INVALID_EMAIL);
return;
}
@@ -47,17 +51,17 @@ public class SetEmailCommand implements ExecutableCommand {
// Validate the user
PlayerAuth auth = dataSource.getAuth(playerName);
if (auth == null) {
commandService.send(sender, MessageKey.UNKNOWN_USER);
commonService.send(sender, MessageKey.UNKNOWN_USER);
return;
} else if (!commandService.isEmailFreeForRegistration(playerEmail, sender)) {
commandService.send(sender, MessageKey.EMAIL_ALREADY_USED_ERROR);
} else if (!validationService.isEmailFreeForRegistration(playerEmail, sender)) {
commonService.send(sender, MessageKey.EMAIL_ALREADY_USED_ERROR);
return;
}
// Set the email address
auth.setEmail(playerEmail);
if (!dataSource.updateEmail(auth)) {
commandService.send(sender, MessageKey.ERROR);
commonService.send(sender, MessageKey.ERROR);
return;
}
@@ -67,7 +71,7 @@ public class SetEmailCommand implements ExecutableCommand {
}
// Show a status message
commandService.send(sender, MessageKey.EMAIL_CHANGED_SUCCESS);
commonService.send(sender, MessageKey.EMAIL_CHANGED_SUCCESS);
}
});
}
@@ -1,11 +1,11 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.CommonService;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@@ -21,7 +21,7 @@ public class UnregisterAdminCommand implements ExecutableCommand {
private DataSource dataSource;
@Inject
private CommandService commandService;
private CommonService commonService;
@Inject
private BukkitService bukkitService;
@@ -38,7 +38,7 @@ public class UnregisterAdminCommand implements ExecutableCommand {
// Make sure the user exists
if (!dataSource.isAuthAvailable(playerName)) {
commandService.send(sender, MessageKey.UNKNOWN_USER);
commonService.send(sender, MessageKey.UNKNOWN_USER);
return;
}
@@ -1,10 +1,10 @@
package fr.xephi.authme.command.executable.captcha;
import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.data.CaptchaManager;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.service.CommonService;
import org.bukkit.entity.Player;
import javax.inject.Inject;
@@ -19,16 +19,16 @@ public class CaptchaCommand extends PlayerCommand {
private CaptchaManager captchaManager;
@Inject
private CommandService commandService;
private CommonService commonService;
@Override
public void runCommand(Player player, List<String> arguments) {
final String playerName = player.getName().toLowerCase();
if (playerCache.isAuthenticated(playerName)) {
commandService.send(player, MessageKey.ALREADY_LOGGED_IN_ERROR);
commonService.send(player, MessageKey.ALREADY_LOGGED_IN_ERROR);
} else if (!captchaManager.isCaptchaRequired(playerName)) {
commandService.send(player, MessageKey.USAGE_LOGIN);
commonService.send(player, MessageKey.USAGE_LOGIN);
} else {
checkCaptcha(player, arguments.get(0));
}
@@ -37,11 +37,11 @@ public class CaptchaCommand extends PlayerCommand {
private void checkCaptcha(Player player, String captchaCode) {
final boolean isCorrectCode = captchaManager.checkCode(player.getName(), captchaCode);
if (isCorrectCode) {
commandService.send(player, MessageKey.CAPTCHA_SUCCESS);
commandService.send(player, MessageKey.LOGIN_MESSAGE);
commonService.send(player, MessageKey.CAPTCHA_SUCCESS);
commonService.send(player, MessageKey.LOGIN_MESSAGE);
} else {
String newCode = captchaManager.generateCode(player.getName());
commandService.send(player, MessageKey.CAPTCHA_WRONG_ERROR, newCode);
commonService.send(player, MessageKey.CAPTCHA_WRONG_ERROR, newCode);
}
}
}
@@ -1,10 +1,10 @@
package fr.xephi.authme.command.executable.changepassword;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.service.ValidationService.ValidationResult;
import org.bukkit.entity.Player;
@@ -18,7 +18,7 @@ import java.util.List;
public class ChangePasswordCommand extends PlayerCommand {
@Inject
private CommandService commandService;
private CommonService commonService;
@Inject
private PlayerCache playerCache;
@@ -36,14 +36,14 @@ public class ChangePasswordCommand extends PlayerCommand {
String name = player.getName().toLowerCase();
if (!playerCache.isAuthenticated(name)) {
commandService.send(player, MessageKey.NOT_LOGGED_IN);
commonService.send(player, MessageKey.NOT_LOGGED_IN);
return;
}
// Make sure the password is allowed
ValidationResult passwordValidation = validationService.validatePassword(newPassword, name);
if (passwordValidation.hasError()) {
commandService.send(player, passwordValidation.getMessageKey(), passwordValidation.getArgs());
commonService.send(player, passwordValidation.getMessageKey(), passwordValidation.getArgs());
return;
}
@@ -1,9 +1,9 @@
package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.service.CommonService;
import org.bukkit.entity.Player;
import javax.inject.Inject;
@@ -18,7 +18,7 @@ public class AddEmailCommand extends PlayerCommand {
private Management management;
@Inject
private CommandService commandService;
private CommonService commonService;
@Override
public void runCommand(Player player, List<String> arguments) {
@@ -29,7 +29,7 @@ public class AddEmailCommand extends PlayerCommand {
// Closer inspection of the mail address handled by the async task
management.performAddEmail(player, email);
} else {
commandService.send(player, MessageKey.CONFIRM_EMAIL_MESSAGE);
commonService.send(player, MessageKey.CONFIRM_EMAIL_MESSAGE);
}
}
}
@@ -1,17 +1,17 @@
package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.mail.SendMailSSL;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.util.RandomStringUtils;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.RecoveryCodeService;
import fr.xephi.authme.util.RandomStringUtils;
import org.bukkit.entity.Player;
import javax.inject.Inject;
@@ -28,7 +28,7 @@ public class RecoverEmailCommand extends PlayerCommand {
private PasswordSecurity passwordSecurity;
@Inject
private CommandService commandService;
private CommonService commonService;
@Inject
private DataSource dataSource;
@@ -49,23 +49,23 @@ public class RecoverEmailCommand extends PlayerCommand {
if (!sendMailSsl.hasAllInformation()) {
ConsoleLogger.warning("Mail API is not set");
commandService.send(player, MessageKey.INCOMPLETE_EMAIL_SETTINGS);
commonService.send(player, MessageKey.INCOMPLETE_EMAIL_SETTINGS);
return;
}
if (playerCache.isAuthenticated(playerName)) {
commandService.send(player, MessageKey.ALREADY_LOGGED_IN_ERROR);
commonService.send(player, MessageKey.ALREADY_LOGGED_IN_ERROR);
return;
}
PlayerAuth auth = dataSource.getAuth(playerName); // TODO: Create method to get email only
if (auth == null) {
commandService.send(player, MessageKey.REGISTER_EMAIL_MESSAGE);
commonService.send(player, MessageKey.REGISTER_EMAIL_MESSAGE);
return;
}
final String email = auth.getEmail();
if (email == null || !email.equalsIgnoreCase(playerMail) || "your@email.com".equalsIgnoreCase(email)) {
commandService.send(player, MessageKey.INVALID_EMAIL);
commonService.send(player, MessageKey.INVALID_EMAIL);
return;
}
@@ -85,13 +85,13 @@ public class RecoverEmailCommand extends PlayerCommand {
private void createAndSendRecoveryCode(Player player, String email) {
String recoveryCode = recoveryCodeService.generateCode(player.getName());
sendMailSsl.sendRecoveryCode(player.getName(), email, recoveryCode);
commandService.send(player, MessageKey.RECOVERY_CODE_SENT);
commonService.send(player, MessageKey.RECOVERY_CODE_SENT);
}
private void processRecoveryCode(Player player, String code, String email) {
final String name = player.getName();
if (!recoveryCodeService.isCodeValid(name, code)) {
commandService.send(player, MessageKey.INCORRECT_RECOVERY_CODE);
commonService.send(player, MessageKey.INCORRECT_RECOVERY_CODE);
return;
}
@@ -101,11 +101,11 @@ public class RecoverEmailCommand extends PlayerCommand {
private void generateAndSendNewPassword(Player player, String email) {
String name = player.getName();
String thePass = RandomStringUtils.generate(commandService.getProperty(RECOVERY_PASSWORD_LENGTH));
String thePass = RandomStringUtils.generate(commonService.getProperty(RECOVERY_PASSWORD_LENGTH));
HashedPassword hashNew = passwordSecurity.computeHash(thePass, name);
dataSource.updatePassword(name, hashNew);
sendMailSsl.sendPasswordMail(name, email, thePass);
commandService.send(player, MessageKey.RECOVERY_EMAIL_SENT_MESSAGE);
commonService.send(player, MessageKey.RECOVERY_EMAIL_SENT_MESSAGE);
}
}
@@ -1,10 +1,10 @@
package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.PlayerCommand;
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 org.bukkit.entity.Player;
import javax.inject.Inject;
@@ -14,8 +14,9 @@ import java.util.List;
* Show email command.
*/
public class ShowEmailCommand extends PlayerCommand {
@Inject
private CommandService commandService;
private CommonService commonService;
@Inject
private PlayerCache playerCache;
@@ -24,9 +25,9 @@ public class ShowEmailCommand extends PlayerCommand {
public void runCommand(Player player, List<String> arguments) {
PlayerAuth auth = playerCache.getAuth(player.getName());
if (auth.getEmail() != null && !"your@email.com".equalsIgnoreCase(auth.getEmail())) {
commandService.send(player, MessageKey.EMAIL_SHOW, auth.getEmail());
commonService.send(player, MessageKey.EMAIL_SHOW, auth.getEmail());
} else {
commandService.send(player, MessageKey.SHOW_NO_EMAIL);
commonService.send(player, MessageKey.SHOW_NO_EMAIL);
}
}
}
@@ -1,15 +1,16 @@
package fr.xephi.authme.command.executable.register;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.mail.SendMailSSL;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.security.HashAlgorithm;
import fr.xephi.authme.util.RandomStringUtils;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.settings.properties.EmailSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.util.RandomStringUtils;
import org.bukkit.entity.Player;
import javax.inject.Inject;
@@ -20,20 +21,26 @@ import static fr.xephi.authme.settings.properties.RegistrationSettings.ENABLE_CO
import static fr.xephi.authme.settings.properties.RegistrationSettings.USE_EMAIL_REGISTRATION;
import static fr.xephi.authme.settings.properties.RestrictionSettings.ENABLE_PASSWORD_CONFIRMATION;
/**
* Command for /register.
*/
public class RegisterCommand extends PlayerCommand {
@Inject
private Management management;
@Inject
private CommandService commandService;
private CommonService commonService;
@Inject
private SendMailSSL sendMailSsl;
@Inject
private ValidationService validationService;
@Override
public void runCommand(Player player, List<String> arguments) {
if (commandService.getProperty(SecuritySettings.PASSWORD_HASH) == HashAlgorithm.TWO_FACTOR) {
if (commonService.getProperty(SecuritySettings.PASSWORD_HASH) == HashAlgorithm.TWO_FACTOR) {
//for two factor auth we don't need to check the usage
management.performRegister(player, "", "", true);
return;
@@ -42,11 +49,11 @@ public class RegisterCommand extends PlayerCommand {
// Ensure that there is 1 argument, or 2 if confirmation is required
final boolean useConfirmation = isConfirmationRequired();
if (arguments.isEmpty() || useConfirmation && arguments.size() < 2) {
commandService.send(player, MessageKey.USAGE_REGISTER);
commonService.send(player, MessageKey.USAGE_REGISTER);
return;
}
if (commandService.getProperty(USE_EMAIL_REGISTRATION)) {
if (commonService.getProperty(USE_EMAIL_REGISTRATION)) {
handleEmailRegistration(player, arguments);
} else {
handlePasswordRegistration(player, arguments);
@@ -59,8 +66,8 @@ public class RegisterCommand extends PlayerCommand {
}
private void handlePasswordRegistration(Player player, List<String> arguments) {
if (commandService.getProperty(ENABLE_PASSWORD_CONFIRMATION) && !arguments.get(0).equals(arguments.get(1))) {
commandService.send(player, MessageKey.PASSWORD_MATCH_ERROR);
if (commonService.getProperty(ENABLE_PASSWORD_CONFIRMATION) && !arguments.get(0).equals(arguments.get(1))) {
commonService.send(player, MessageKey.PASSWORD_MATCH_ERROR);
} else {
management.performRegister(player, arguments.get(0), "", true);
}
@@ -68,19 +75,19 @@ public class RegisterCommand extends PlayerCommand {
private void handleEmailRegistration(Player player, List<String> arguments) {
if (!sendMailSsl.hasAllInformation()) {
commandService.send(player, MessageKey.INCOMPLETE_EMAIL_SETTINGS);
commonService.send(player, MessageKey.INCOMPLETE_EMAIL_SETTINGS);
ConsoleLogger.warning("Cannot register player '" + player.getName() + "': no email or password is set "
+ "to send emails from. Please adjust your config at " + EmailSettings.MAIL_ACCOUNT.getPath());
return;
}
final String email = arguments.get(0);
if (!commandService.validateEmail(email)) {
commandService.send(player, MessageKey.INVALID_EMAIL);
} else if (commandService.getProperty(ENABLE_CONFIRM_EMAIL) && !email.equals(arguments.get(1))) {
commandService.send(player, MessageKey.USAGE_REGISTER);
if (!validationService.validateEmail(email)) {
commonService.send(player, MessageKey.INVALID_EMAIL);
} else if (commonService.getProperty(ENABLE_CONFIRM_EMAIL) && !email.equals(arguments.get(1))) {
commonService.send(player, MessageKey.USAGE_REGISTER);
} else {
String thePass = RandomStringUtils.generate(commandService.getProperty(RECOVERY_PASSWORD_LENGTH));
String thePass = RandomStringUtils.generate(commonService.getProperty(RECOVERY_PASSWORD_LENGTH));
management.performRegister(player, thePass, email, true);
}
}
@@ -91,8 +98,8 @@ public class RegisterCommand extends PlayerCommand {
* @return True if the confirmation is needed, false otherwise
*/
private boolean isConfirmationRequired() {
return commandService.getProperty(USE_EMAIL_REGISTRATION)
? commandService.getProperty(ENABLE_CONFIRM_EMAIL)
: commandService.getProperty(ENABLE_PASSWORD_CONFIRMATION);
return commonService.getProperty(USE_EMAIL_REGISTRATION)
? commonService.getProperty(ENABLE_CONFIRM_EMAIL)
: commonService.getProperty(ENABLE_PASSWORD_CONFIRMATION);
}
}
@@ -1,10 +1,10 @@
package fr.xephi.authme.command.executable.unregister;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.service.CommonService;
import org.bukkit.entity.Player;
import javax.inject.Inject;
@@ -19,7 +19,7 @@ public class UnregisterCommand extends PlayerCommand {
private Management management;
@Inject
private CommandService commandService;
private CommonService commonService;
@Inject
private PlayerCache playerCache;
@@ -31,7 +31,7 @@ public class UnregisterCommand extends PlayerCommand {
// Make sure the player is authenticated
if (!playerCache.isAuthenticated(playerName)) {
commandService.send(player, MessageKey.NOT_LOGGED_IN);
commonService.send(player, MessageKey.NOT_LOGGED_IN);
return;
}