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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -6,7 +6,7 @@ import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.AsynchronousProcess;
import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword;
import org.bukkit.entity.Player;
@ -19,7 +19,7 @@ public class AsyncChangePassword implements AsynchronousProcess {
private DataSource dataSource;
@Inject
private ProcessService processService;
private CommonService commonService;
@Inject
private PasswordSecurity passwordSecurity;
@ -39,15 +39,15 @@ public class AsyncChangePassword implements AsynchronousProcess {
auth.setPassword(hashedPassword);
if (!dataSource.updatePassword(auth)) {
processService.send(player, MessageKey.ERROR);
commonService.send(player, MessageKey.ERROR);
return;
}
playerCache.updatePlayer(auth);
processService.send(player, MessageKey.PASSWORD_CHANGED_SUCCESS);
commonService.send(player, MessageKey.PASSWORD_CHANGED_SUCCESS);
ConsoleLogger.info(player.getName() + " changed his password");
} else {
processService.send(player, MessageKey.WRONG_PASSWORD);
commonService.send(player, MessageKey.WRONG_PASSWORD);
}
}
}

View File

@ -6,7 +6,8 @@ import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.AsynchronousProcess;
import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import org.bukkit.entity.Player;
@ -18,7 +19,7 @@ import javax.inject.Inject;
public class AsyncAddEmail implements AsynchronousProcess {
@Inject
private ProcessService service;
private CommonService service;
@Inject
private DataSource dataSource;
@ -26,6 +27,9 @@ public class AsyncAddEmail implements AsynchronousProcess {
@Inject
private PlayerCache playerCache;
@Inject
private ValidationService validationService;
AsyncAddEmail() { }
public void addEmail(Player player, String email) {
@ -37,9 +41,9 @@ public class AsyncAddEmail implements AsynchronousProcess {
if (currentEmail != null && !"your@email.com".equals(currentEmail)) {
service.send(player, MessageKey.USAGE_CHANGE_EMAIL);
} else if (!service.validateEmail(email)) {
} else if (!validationService.validateEmail(email)) {
service.send(player, MessageKey.INVALID_EMAIL);
} else if (!service.isEmailFreeForRegistration(email, player)) {
} else if (!validationService.isEmailFreeForRegistration(email, player)) {
service.send(player, MessageKey.EMAIL_ALREADY_USED_ERROR);
} else {
auth.setEmail(email);

View File

@ -5,7 +5,8 @@ import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.AsynchronousProcess;
import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import org.bukkit.entity.Player;
@ -17,7 +18,7 @@ import javax.inject.Inject;
public class AsyncChangeEmail implements AsynchronousProcess {
@Inject
private ProcessService service;
private CommonService service;
@Inject
private PlayerCache playerCache;
@ -25,6 +26,9 @@ public class AsyncChangeEmail implements AsynchronousProcess {
@Inject
private DataSource dataSource;
@Inject
private ValidationService validationService;
AsyncChangeEmail() { }
public void changeEmail(Player player, String oldEmail, String newEmail) {
@ -35,11 +39,11 @@ public class AsyncChangeEmail implements AsynchronousProcess {
if (currentEmail == null) {
service.send(player, MessageKey.USAGE_ADD_EMAIL);
} else if (newEmail == null || !service.validateEmail(newEmail)) {
} else if (newEmail == null || !validationService.validateEmail(newEmail)) {
service.send(player, MessageKey.INVALID_NEW_EMAIL);
} else if (!oldEmail.equals(currentEmail)) {
service.send(player, MessageKey.INVALID_OLD_EMAIL);
} else if (!service.isEmailFreeForRegistration(newEmail, player)) {
} else if (!validationService.isEmailFreeForRegistration(newEmail, player)) {
service.send(player, MessageKey.EMAIL_ALREADY_USED_ERROR);
} else {
saveNewEmail(auth, player, newEmail);

View File

@ -13,7 +13,7 @@ import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.permission.AuthGroupType;
import fr.xephi.authme.permission.PlayerStatePermission;
import fr.xephi.authme.process.AsynchronousProcess;
import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.process.login.AsynchronousLogin;
import fr.xephi.authme.settings.commandconfig.CommandManager;
import fr.xephi.authme.settings.properties.HooksSettings;
@ -45,7 +45,7 @@ public class AsynchronousJoin implements AsynchronousProcess {
private DataSource database;
@Inject
private ProcessService service;
private CommonService service;
@Inject
private PlayerCache playerCache;

View File

@ -16,7 +16,7 @@ import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.permission.PlayerPermission;
import fr.xephi.authme.permission.PlayerStatePermission;
import fr.xephi.authme.process.AsynchronousProcess;
import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.process.SyncProcessManager;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.settings.properties.DatabaseSettings;
@ -44,7 +44,7 @@ public class AsynchronousLogin implements AsynchronousProcess {
private DataSource dataSource;
@Inject
private ProcessService service;
private CommonService service;
@Inject
private PermissionsManager permissionsManager;

View File

@ -8,13 +8,13 @@ import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.events.LoginEvent;
import fr.xephi.authme.events.RestoreInventoryEvent;
import fr.xephi.authme.listener.PlayerListener;
import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.process.SynchronousProcess;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.BungeeService;
import fr.xephi.authme.service.TeleportationService;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.commandconfig.CommandManager;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.TeleportationService;
import fr.xephi.authme.util.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
@ -33,9 +33,6 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess {
@Inject
private BungeeService bungeeService;
@Inject
private ProcessService service;
@Inject
private LimboCache limboCache;
@ -54,6 +51,9 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess {
@Inject
private CommandManager commandManager;
@Inject
private Settings settings;
ProcessSyncPlayerLogin() {
}
@ -77,7 +77,7 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess {
// because LimboCache#restoreData teleport player to last location.
}
if (service.getProperty(PROTECT_INVENTORY_BEFORE_LOGIN)) {
if (settings.getProperty(PROTECT_INVENTORY_BEFORE_LOGIN)) {
restoreInventory(player);
}
@ -94,7 +94,7 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess {
}
}
if (service.getProperty(RegistrationSettings.APPLY_BLIND_EFFECT)) {
if (settings.getProperty(RegistrationSettings.APPLY_BLIND_EFFECT)) {
player.removePotionEffect(PotionEffectType.BLINDNESS);
}
@ -103,13 +103,13 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess {
player.saveData();
// Login is done, display welcome message
if (service.getProperty(RegistrationSettings.USE_WELCOME_MESSAGE)) {
if (service.getProperty(RegistrationSettings.BROADCAST_WELCOME_MESSAGE)) {
for (String s : service.getSettings().getWelcomeMessage()) {
if (settings.getProperty(RegistrationSettings.USE_WELCOME_MESSAGE)) {
if (settings.getProperty(RegistrationSettings.BROADCAST_WELCOME_MESSAGE)) {
for (String s : settings.getWelcomeMessage()) {
Bukkit.getServer().broadcastMessage(plugin.replaceAllInfo(s, player));
}
} else {
for (String s : service.getSettings().getWelcomeMessage()) {
for (String s : settings.getWelcomeMessage()) {
player.sendMessage(plugin.replaceAllInfo(s, player));
}
}

View File

@ -6,7 +6,7 @@ import fr.xephi.authme.data.limbo.LimboCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.AsynchronousProcess;
import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.process.SyncProcessManager;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import org.bukkit.entity.Player;
@ -19,7 +19,7 @@ public class AsynchronousLogout implements AsynchronousProcess {
private DataSource database;
@Inject
private ProcessService service;
private CommonService service;
@Inject
private PlayerCache playerCache;

View File

@ -6,7 +6,7 @@ import fr.xephi.authme.events.LogoutEvent;
import fr.xephi.authme.listener.protocollib.ProtocolLibService;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.permission.AuthGroupType;
import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.process.SynchronousProcess;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
@ -25,7 +25,7 @@ import static fr.xephi.authme.service.BukkitService.TICKS_PER_SECOND;
public class ProcessSynchronousPlayerLogout implements SynchronousProcess {
@Inject
private ProcessService service;
private CommonService service;
@Inject
private BukkitService bukkitService;

View File

@ -7,7 +7,7 @@ import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.CacheDataSource;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.process.AsynchronousProcess;
import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.process.SyncProcessManager;
import fr.xephi.authme.settings.SpawnLoader;
import fr.xephi.authme.settings.properties.RestrictionSettings;
@ -27,7 +27,7 @@ public class AsynchronousQuit implements AsynchronousProcess {
private DataSource database;
@Inject
private ProcessService service;
private CommonService service;
@Inject
private PlayerCache playerCache;

View File

@ -7,7 +7,7 @@ import fr.xephi.authme.mail.SendMailSSL;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.process.AsynchronousProcess;
import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.process.SyncProcessManager;
import fr.xephi.authme.process.login.AsynchronousLogin;
import fr.xephi.authme.security.HashAlgorithm;
@ -51,7 +51,7 @@ public class AsyncRegister implements AsynchronousProcess {
@Inject
private PasswordSecurity passwordSecurity;
@Inject
private ProcessService service;
private CommonService service;
@Inject
private SyncProcessManager syncProcessManager;
@Inject

View File

@ -3,7 +3,7 @@ package fr.xephi.authme.process.register;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.permission.AuthGroupType;
import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.process.SynchronousProcess;
import fr.xephi.authme.settings.properties.HooksSettings;
import fr.xephi.authme.task.LimboPlayerTaskManager;
@ -16,7 +16,7 @@ import javax.inject.Inject;
public class ProcessSyncEmailRegister implements SynchronousProcess {
@Inject
private ProcessService service;
private CommonService service;
@Inject
private LimboPlayerTaskManager limboPlayerTaskManager;

View File

@ -4,7 +4,7 @@ import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.data.limbo.LimboCache;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.permission.AuthGroupType;
import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.process.SynchronousProcess;
import fr.xephi.authme.service.BungeeService;
import fr.xephi.authme.settings.commandconfig.CommandManager;
@ -25,7 +25,7 @@ public class ProcessSyncPasswordRegister implements SynchronousProcess {
private BungeeService bungeeService;
@Inject
private ProcessService service;
private CommonService service;
@Inject
private LimboCache limboCache;

View File

@ -9,7 +9,7 @@ import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.permission.AuthGroupHandler;
import fr.xephi.authme.permission.AuthGroupType;
import fr.xephi.authme.process.AsynchronousProcess;
import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
@ -31,7 +31,7 @@ public class AsynchronousUnregister implements AsynchronousProcess {
private DataSource dataSource;
@Inject
private ProcessService service;
private CommonService service;
@Inject
private PasswordSecurity passwordSecurity;

View File

@ -1,4 +1,4 @@
package fr.xephi.authme.process;
package fr.xephi.authme.service;
import com.github.authme.configme.properties.Property;
import fr.xephi.authme.message.MessageKey;
@ -8,16 +8,15 @@ import fr.xephi.authme.permission.AuthGroupType;
import fr.xephi.authme.permission.PermissionNode;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.service.ValidationService;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import javax.inject.Inject;
/**
* Service for asynchronous and synchronous processes.
* Service for the most common operations regarding settings, messages and permissions.
*/
public class ProcessService {
public class CommonService {
@Inject
private Settings settings;
@ -25,17 +24,17 @@ public class ProcessService {
@Inject
private Messages messages;
@Inject
private ValidationService validationService;
@Inject
private PermissionsManager permissionsManager;
@Inject
private AuthGroupHandler authGroupHandler;
CommonService() {
}
/**
* Retrieve a property's value.
* Retrieves a property's value.
*
* @param property the property to retrieve
* @param <T> the property type
@ -46,16 +45,7 @@ public class ProcessService {
}
/**
* Return the settings manager.
*
* @return settings manager
*/
public Settings getSettings() {
return settings;
}
/**
* Send a message to the command sender.
* Sends a message to the command sender.
*
* @param sender the command sender
* @param key the message key
@ -65,7 +55,7 @@ public class ProcessService {
}
/**
* Send a message to the command sender with the given replacements.
* Sends a message to the command sender with the given replacements.
*
* @param sender the command sender
* @param key the message key
@ -76,7 +66,7 @@ public class ProcessService {
}
/**
* Retrieve a message.
* Retrieves a message.
*
* @param key the key of the message
* @return the message, split by line
@ -86,7 +76,7 @@ public class ProcessService {
}
/**
* Retrieve a message as one piece.
* Retrieves a message in one piece.
*
* @param key the key of the message
* @return the message
@ -95,18 +85,24 @@ public class ProcessService {
return messages.retrieveSingle(key);
}
public boolean validateEmail(String email) {
return validationService.validateEmail(email);
}
public boolean isEmailFreeForRegistration(String email, CommandSender sender) {
return validationService.isEmailFreeForRegistration(email, sender);
}
/**
* Checks whether the player has the given permission.
*
* @param player the player
* @param node the permission node to check
* @return true if player has permission, false otherwise
*/
public boolean hasPermission(Player player, PermissionNode node) {
return permissionsManager.hasPermission(player, node);
}
/**
* Sets the permission group of the given player.
*
* @param player the player to process
* @param group the group to add the player to
* @return true on success, false otherwise
*/
public boolean setGroup(Player player, AuthGroupType group) {
return authGroupHandler.setGroup(player, group);
}

View File

@ -1,130 +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.settings.properties.SecuritySettings;
import fr.xephi.authme.service.ValidationService;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* Test for {@link CommandService}.
*/
@RunWith(MockitoJUnitRunner.class)
public class CommandServiceTest {
@InjectMocks
private CommandService commandService;
@Mock
private Messages messages;
@Mock
private Settings settings;
@Mock
private ValidationService validationService;
@Test
public void shouldSendMessage() {
// given
CommandSender sender = mock(CommandSender.class);
// when
commandService.send(sender, MessageKey.INVALID_EMAIL);
// then
verify(messages).send(sender, MessageKey.INVALID_EMAIL);
}
@Test
public void shouldSendMessageWithReplacements() {
// given
CommandSender sender = mock(Player.class);
// when
commandService.send(sender, MessageKey.ANTIBOT_AUTO_ENABLED_MESSAGE, "10");
// then
verify(messages).send(sender, MessageKey.ANTIBOT_AUTO_ENABLED_MESSAGE, "10");
}
@Test
public void shouldRetrieveMessage() {
// given
MessageKey key = MessageKey.USAGE_CAPTCHA;
String[] givenMessages = new String[]{"Lorem ipsum...", "Test line test"};
given(messages.retrieve(key)).willReturn(givenMessages);
// when
String[] result = commandService.retrieveMessage(key);
// then
assertThat(result, equalTo(givenMessages));
verify(messages).retrieve(key);
}
@Test
public void shouldRetrieveProperty() {
// given
Property<Integer> property = SecuritySettings.CAPTCHA_LENGTH;
given(settings.getProperty(property)).willReturn(7);
// when
int result = commandService.getProperty(property);
// then
assertThat(result, equalTo(7));
verify(settings).getProperty(property);
}
@Test
public void shouldReturnSettings() {
// given/when
Settings result = commandService.getSettings();
// then
assertThat(result, equalTo(settings));
}
@Test
public void shouldValidateEmail() {
// given
String email = "test@example.tld";
given(validationService.validateEmail(email)).willReturn(true);
// when
boolean result = commandService.validateEmail(email);
// then
assertThat(result, equalTo(true));
verify(validationService).validateEmail(email);
}
@Test
public void shouldCheckIfEmailCanBeUsed() {
// given
String email = "mail@example.com";
CommandSender sender = mock(CommandSender.class);
given(validationService.isEmailFreeForRegistration(email, sender))
.willReturn(true);
// when
boolean result = commandService.isEmailFreeForRegistration(email, sender);
// then
assertThat(result, equalTo(true));
verify(validationService).isEmailFreeForRegistration(email, sender);
}
}

View File

@ -1,10 +1,10 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.command.CommandService;
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 org.junit.Test;
import org.junit.runner.RunWith;
@ -36,7 +36,7 @@ public class AccountsCommandTest {
@InjectMocks
private AccountsCommand command;
@Mock
private CommandService service;
private CommonService service;
@Mock
private DataSource dataSource;
@Mock

View File

@ -1,7 +1,6 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
@ -9,6 +8,7 @@ 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;
@ -39,7 +39,7 @@ public class ChangePasswordAdminCommandTest {
private ChangePasswordAdminCommand command;
@Mock
private CommandService service;
private CommonService service;
@Mock
private PasswordSecurity passwordSecurity;

View File

@ -2,10 +2,10 @@ package fr.xephi.authme.command.executable.authme;
import ch.jalu.injector.Injector;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.datasource.converter.Converter;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.util.StringUtils;
import org.bukkit.command.CommandSender;
import org.junit.BeforeClass;
@ -42,7 +42,7 @@ public class ConverterCommandTest {
private ConverterCommand command;
@Mock
private CommandService commandService;
private CommonService commandService;
@Mock
private BukkitService bukkitService;

View File

@ -1,9 +1,9 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.command.CommandService;
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 org.junit.Test;
import org.junit.runner.RunWith;
@ -32,7 +32,7 @@ public class GetEmailCommandTest {
private DataSource dataSource;
@Mock
private CommandService service;
private CommonService service;
@Test
public void shouldReportUnknownUser() {

View File

@ -1,9 +1,9 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.service.CommonService;
import org.bukkit.command.CommandSender;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -39,7 +39,7 @@ public class LastLoginCommandTest {
private DataSource dataSource;
@Mock
private CommandService service;
private CommonService service;
@Test

View File

@ -1,9 +1,9 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.command.CommandService;
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 org.junit.Test;
import org.junit.runner.RunWith;
@ -33,7 +33,7 @@ public class PurgeLastPositionCommandTest {
private DataSource dataSource;
@Mock
private CommandService service;
private CommonService service;
@Test

View File

@ -1,7 +1,6 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.limbo.LimboCache;
import fr.xephi.authme.datasource.DataSource;
@ -9,6 +8,7 @@ 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;
@ -51,7 +51,7 @@ public class RegisterAdminCommandTest {
private BukkitService bukkitService;
@Mock
private CommandService commandService;
private CommonService commandService;
@Mock
private ValidationService validationService;
@ -164,7 +164,7 @@ public class RegisterAdminCommandTest {
Player player = mock(Player.class);
given(bukkitService.getPlayerExact(user)).willReturn(player);
String kickForAdminRegister = "Admin registered you -- log in again";
given(commandService.retrieveSingle(MessageKey.KICK_FOR_ADMIN_REGISTER)).willReturn(kickForAdminRegister);
given(commandService.retrieveSingleMessage(MessageKey.KICK_FOR_ADMIN_REGISTER)).willReturn(kickForAdminRegister);
CommandSender sender = mock(CommandSender.class);
// when

View File

@ -3,13 +3,13 @@ package fr.xephi.authme.command.executable.authme;
import ch.jalu.injector.Injector;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.datasource.DataSourceType;
import fr.xephi.authme.initialization.Reloadable;
import fr.xephi.authme.initialization.SettingsDependent;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.output.LogLevel;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.DatabaseSettings;
import fr.xephi.authme.settings.properties.PluginSettings;
@ -59,7 +59,7 @@ public class ReloadCommandTest {
private DataSource dataSource;
@Mock
private CommandService commandService;
private CommonService commandService;
@BeforeClass
public static void setUpLogger() {

View File

@ -1,11 +1,12 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
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 org.junit.Test;
import org.junit.runner.RunWith;
@ -37,7 +38,7 @@ public class SetEmailCommandTest {
private DataSource dataSource;
@Mock
private CommandService commandService;
private CommonService commandService;
@Mock
private PlayerCache playerCache;
@ -45,19 +46,22 @@ public class SetEmailCommandTest {
@Mock
private BukkitService bukkitService;
@Mock
private ValidationService validationService;
@Test
public void shouldRejectInvalidMail() {
// given
String user = "somebody";
String email = "some.test@example.org";
given(commandService.validateEmail(email)).willReturn(false);
given(validationService.validateEmail(email)).willReturn(false);
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Arrays.asList(user, email));
// then
verify(commandService).validateEmail(email);
verify(validationService).validateEmail(email);
verify(commandService).send(sender, MessageKey.INVALID_EMAIL);
verifyZeroInteractions(dataSource);
}
@ -67,7 +71,7 @@ public class SetEmailCommandTest {
// given
String user = "nonexistent";
String email = "mail@example.com";
given(commandService.validateEmail(email)).willReturn(true);
given(validationService.validateEmail(email)).willReturn(true);
given(dataSource.getAuth(user)).willReturn(null);
CommandSender sender = mock(CommandSender.class);
@ -76,7 +80,7 @@ public class SetEmailCommandTest {
runOptionallyAsyncTask(bukkitService);
// then
verify(commandService).validateEmail(email);
verify(validationService).validateEmail(email);
verify(dataSource).getAuth(user);
verify(commandService).send(sender, MessageKey.UNKNOWN_USER);
verifyNoMoreInteractions(dataSource);
@ -87,20 +91,20 @@ public class SetEmailCommandTest {
// given
String user = "someone";
String email = "mail@example.com";
given(commandService.validateEmail(email)).willReturn(true);
given(validationService.validateEmail(email)).willReturn(true);
PlayerAuth auth = mock(PlayerAuth.class);
given(dataSource.getAuth(user)).willReturn(auth);
CommandSender sender = mock(CommandSender.class);
given(commandService.isEmailFreeForRegistration(email, sender)).willReturn(false);
given(validationService.isEmailFreeForRegistration(email, sender)).willReturn(false);
// when
command.executeCommand(sender, Arrays.asList(user, email));
runOptionallyAsyncTask(bukkitService);
// then
verify(commandService).validateEmail(email);
verify(validationService).validateEmail(email);
verify(dataSource).getAuth(user);
verify(commandService).isEmailFreeForRegistration(email, sender);
verify(validationService).isEmailFreeForRegistration(email, sender);
verify(commandService).send(sender, MessageKey.EMAIL_ALREADY_USED_ERROR);
verifyNoMoreInteractions(dataSource);
verifyZeroInteractions(auth);
@ -111,11 +115,11 @@ public class SetEmailCommandTest {
// given
String user = "Bobby";
String email = "new-addr@example.org";
given(commandService.validateEmail(email)).willReturn(true);
given(validationService.validateEmail(email)).willReturn(true);
PlayerAuth auth = mock(PlayerAuth.class);
given(dataSource.getAuth(user)).willReturn(auth);
CommandSender sender = mock(CommandSender.class);
given(commandService.isEmailFreeForRegistration(email, sender)).willReturn(true);
given(validationService.isEmailFreeForRegistration(email, sender)).willReturn(true);
given(dataSource.updateEmail(auth)).willReturn(false);
// when
@ -123,9 +127,9 @@ public class SetEmailCommandTest {
runOptionallyAsyncTask(bukkitService);
// then
verify(commandService).validateEmail(email);
verify(validationService).validateEmail(email);
verify(dataSource).getAuth(user);
verify(commandService).isEmailFreeForRegistration(email, sender);
verify(validationService).isEmailFreeForRegistration(email, sender);
verify(commandService).send(sender, MessageKey.ERROR);
verify(dataSource).updateEmail(auth);
verifyNoMoreInteractions(dataSource);
@ -136,11 +140,11 @@ public class SetEmailCommandTest {
// given
String user = "Bobby";
String email = "new-addr@example.org";
given(commandService.validateEmail(email)).willReturn(true);
given(validationService.validateEmail(email)).willReturn(true);
PlayerAuth auth = mock(PlayerAuth.class);
given(dataSource.getAuth(user)).willReturn(auth);
CommandSender sender = mock(CommandSender.class);
given(commandService.isEmailFreeForRegistration(email, sender)).willReturn(true);
given(validationService.isEmailFreeForRegistration(email, sender)).willReturn(true);
given(dataSource.updateEmail(auth)).willReturn(true);
given(playerCache.getAuth(user)).willReturn(null);
@ -149,9 +153,9 @@ public class SetEmailCommandTest {
runOptionallyAsyncTask(bukkitService);
// then
verify(commandService).validateEmail(email);
verify(validationService).validateEmail(email);
verify(dataSource).getAuth(user);
verify(commandService).isEmailFreeForRegistration(email, sender);
verify(validationService).isEmailFreeForRegistration(email, sender);
verify(commandService).send(sender, MessageKey.EMAIL_CHANGED_SUCCESS);
verify(dataSource).updateEmail(auth);
verify(playerCache, never()).updatePlayer(any(PlayerAuth.class));
@ -163,11 +167,11 @@ public class SetEmailCommandTest {
// given
String user = "Bobby";
String email = "new-addr@example.org";
given(commandService.validateEmail(email)).willReturn(true);
given(validationService.validateEmail(email)).willReturn(true);
PlayerAuth auth = mock(PlayerAuth.class);
given(dataSource.getAuth(user)).willReturn(auth);
CommandSender sender = mock(CommandSender.class);
given(commandService.isEmailFreeForRegistration(email, sender)).willReturn(true);
given(validationService.isEmailFreeForRegistration(email, sender)).willReturn(true);
given(dataSource.updateEmail(auth)).willReturn(true);
given(playerCache.getAuth(user)).willReturn(mock(PlayerAuth.class));
@ -176,9 +180,9 @@ public class SetEmailCommandTest {
runOptionallyAsyncTask(bukkitService);
// then
verify(commandService).validateEmail(email);
verify(validationService).validateEmail(email);
verify(dataSource).getAuth(user);
verify(commandService).isEmailFreeForRegistration(email, sender);
verify(validationService).isEmailFreeForRegistration(email, sender);
verify(commandService).send(sender, MessageKey.EMAIL_CHANGED_SUCCESS);
verify(dataSource).updateEmail(auth);
verify(playerCache).updatePlayer(auth);

View File

@ -1,10 +1,10 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.command.CommandService;
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;
import org.junit.Test;
@ -33,7 +33,7 @@ public class UnregisterAdminCommandTest {
private DataSource dataSource;
@Mock
private CommandService commandService;
private CommonService commandService;
@Mock
private BukkitService bukkitService;

View File

@ -2,8 +2,8 @@ package fr.xephi.authme.command.executable.captcha;
import fr.xephi.authme.data.CaptchaManager;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.service.CommonService;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -34,7 +34,7 @@ public class CaptchaCommandTest {
private PlayerCache playerCache;
@Mock
private CommandService commandService;
private CommonService commandService;
@Test
public void shouldDetectIfPlayerIsLoggedIn() {

View File

@ -1,9 +1,9 @@
package fr.xephi.authme.command.executable.changepassword;
import fr.xephi.authme.command.CommandService;
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.command.BlockCommandSender;
@ -38,7 +38,7 @@ public class ChangePasswordCommandTest {
private ChangePasswordCommand command;
@Mock
private CommandService commandService;
private CommonService commandService;
@Mock
private PlayerCache playerCache;

View File

@ -1,8 +1,8 @@
package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.service.CommonService;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -29,7 +29,7 @@ public class AddEmailCommandTest {
private AddEmailCommand command;
@Mock
private CommandService commandService;
private CommonService commandService;
@Mock
private Management management;

View File

@ -1,7 +1,6 @@
package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
@ -9,6 +8,7 @@ import fr.xephi.authme.mail.SendMailSSL;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.RecoveryCodeService;
import fr.xephi.authme.settings.properties.EmailSettings;
import org.bukkit.entity.Player;
@ -50,7 +50,7 @@ public class RecoverEmailCommandTest {
private PasswordSecurity passwordSecurity;
@Mock
private CommandService commandService;
private CommonService commandService;
@Mock
private DataSource dataSource;

View File

@ -1,9 +1,9 @@
package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.command.CommandService;
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 org.junit.Test;
import org.junit.runner.RunWith;
@ -30,7 +30,7 @@ public class ShowEmailCommandTest {
private ShowEmailCommand command;
@Mock
private CommandService commandService;
private CommonService commandService;
@Mock
private PlayerCache playerCache;

View File

@ -1,11 +1,12 @@
package fr.xephi.authme.command.executable.register;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.command.CommandService;
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.service.CommonService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.settings.properties.EmailSettings;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
@ -21,7 +22,6 @@ import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@ -44,7 +44,7 @@ public class RegisterCommandTest {
private RegisterCommand command;
@Mock
private CommandService commandService;
private CommonService commandService;
@Mock
private Management management;
@ -52,6 +52,9 @@ public class RegisterCommandTest {
@Mock
private SendMailSSL sendMailSsl;
@Mock
private ValidationService validationService;
@BeforeClass
public static void setup() {
TestHelper.setupLogger();
@ -70,7 +73,7 @@ public class RegisterCommandTest {
CommandSender sender = mock(BlockCommandSender.class);
// when
command.executeCommand(sender, new ArrayList<String>());
command.executeCommand(sender, Collections.emptyList());
// then
verify(sender).sendMessage(argThat(containsString("Player only!")));
@ -84,7 +87,7 @@ public class RegisterCommandTest {
Player player = mock(Player.class);
// when
command.executeCommand(player, Collections.<String>emptyList());
command.executeCommand(player, Collections.emptyList());
// then
verify(management).performRegister(player, "", "", true);
@ -97,7 +100,7 @@ public class RegisterCommandTest {
Player player = mock(Player.class);
// when
command.executeCommand(player, Collections.<String>emptyList());
command.executeCommand(player, Collections.emptyList());
// then
verify(commandService).send(player, MessageKey.USAGE_REGISTER);
@ -154,7 +157,7 @@ public class RegisterCommandTest {
public void shouldRejectInvalidEmail() {
// given
String playerMail = "player@example.org";
given(commandService.validateEmail(playerMail)).willReturn(false);
given(validationService.validateEmail(playerMail)).willReturn(false);
given(commandService.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)).willReturn(true);
given(commandService.getProperty(RegistrationSettings.ENABLE_CONFIRM_EMAIL)).willReturn(true);
@ -165,7 +168,7 @@ public class RegisterCommandTest {
command.executeCommand(player, Arrays.asList(playerMail, playerMail));
// then
verify(commandService).validateEmail(playerMail);
verify(validationService).validateEmail(playerMail);
verify(commandService).send(player, MessageKey.INVALID_EMAIL);
verifyZeroInteractions(management);
}
@ -174,7 +177,7 @@ public class RegisterCommandTest {
public void shouldRejectInvalidEmailConfirmation() {
// given
String playerMail = "bobber@bobby.org";
given(commandService.validateEmail(playerMail)).willReturn(true);
given(validationService.validateEmail(playerMail)).willReturn(true);
given(commandService.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)).willReturn(true);
given(commandService.getProperty(RegistrationSettings.ENABLE_CONFIRM_EMAIL)).willReturn(true);
@ -194,7 +197,7 @@ public class RegisterCommandTest {
public void shouldPerformEmailRegistration() {
// given
String playerMail = "asfd@lakjgre.lds";
given(commandService.validateEmail(playerMail)).willReturn(true);
given(validationService.validateEmail(playerMail)).willReturn(true);
int passLength = 7;
given(commandService.getProperty(EmailSettings.RECOVERY_PASSWORD_LENGTH)).willReturn(passLength);
@ -207,7 +210,7 @@ public class RegisterCommandTest {
command.executeCommand(player, Arrays.asList(playerMail, playerMail));
// then
verify(commandService).validateEmail(playerMail);
verify(validationService).validateEmail(playerMail);
verify(sendMailSsl).hasAllInformation();
verify(management).performRegister(eq(player), argThat(stringWithLength(passLength)), eq(playerMail), eq(true));
}

View File

@ -1,9 +1,9 @@
package fr.xephi.authme.command.executable.unregister;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.service.CommonService;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -31,7 +31,7 @@ public class UnregisterCommandTest {
private Management management;
@Mock
private CommandService commandService;
private CommonService commandService;
@Mock
private PlayerCache playerCache;

View File

@ -5,7 +5,8 @@ import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import org.bukkit.entity.Player;
import org.junit.BeforeClass;
@ -40,7 +41,10 @@ public class AsyncAddEmailTest {
private PlayerCache playerCache;
@Mock
private ProcessService service;
private CommonService service;
@Mock
private ValidationService validationService;
@BeforeClass
public static void setUp() {
@ -57,8 +61,8 @@ public class AsyncAddEmailTest {
given(auth.getEmail()).willReturn(null);
given(playerCache.getAuth("tester")).willReturn(auth);
given(dataSource.updateEmail(any(PlayerAuth.class))).willReturn(true);
given(service.validateEmail(email)).willReturn(true);
given(service.isEmailFreeForRegistration(email, player)).willReturn(true);
given(validationService.validateEmail(email)).willReturn(true);
given(validationService.isEmailFreeForRegistration(email, player)).willReturn(true);
// when
asyncAddEmail.addEmail(player, email);
@ -80,8 +84,8 @@ public class AsyncAddEmailTest {
given(auth.getEmail()).willReturn(null);
given(playerCache.getAuth("tester")).willReturn(auth);
given(dataSource.updateEmail(any(PlayerAuth.class))).willReturn(false);
given(service.validateEmail(email)).willReturn(true);
given(service.isEmailFreeForRegistration(email, player)).willReturn(true);
given(validationService.validateEmail(email)).willReturn(true);
given(validationService.isEmailFreeForRegistration(email, player)).willReturn(true);
// when
asyncAddEmail.addEmail(player, email);
@ -117,7 +121,7 @@ public class AsyncAddEmailTest {
PlayerAuth auth = mock(PlayerAuth.class);
given(auth.getEmail()).willReturn(null);
given(playerCache.getAuth("my_player")).willReturn(auth);
given(service.validateEmail(email)).willReturn(false);
given(validationService.validateEmail(email)).willReturn(false);
// when
asyncAddEmail.addEmail(player, email);
@ -136,8 +140,8 @@ public class AsyncAddEmailTest {
PlayerAuth auth = mock(PlayerAuth.class);
given(auth.getEmail()).willReturn(null);
given(playerCache.getAuth("testname")).willReturn(auth);
given(service.validateEmail(email)).willReturn(true);
given(service.isEmailFreeForRegistration(email, player)).willReturn(false);
given(validationService.validateEmail(email)).willReturn(true);
given(validationService.isEmailFreeForRegistration(email, player)).willReturn(false);
// when
asyncAddEmail.addEmail(player, email);

View File

@ -4,7 +4,8 @@ import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import org.bukkit.entity.Player;
import org.junit.Test;
@ -39,7 +40,10 @@ public class AsyncChangeEmailTest {
private DataSource dataSource;
@Mock
private ProcessService service;
private CommonService service;
@Mock
private ValidationService validationService;
@Test
public void shouldAddEmail() {
@ -50,8 +54,8 @@ public class AsyncChangeEmailTest {
PlayerAuth auth = authWithMail("old@mail.tld");
given(playerCache.getAuth("bobby")).willReturn(auth);
given(dataSource.updateEmail(auth)).willReturn(true);
given(service.validateEmail(newEmail)).willReturn(true);
given(service.isEmailFreeForRegistration(newEmail, player)).willReturn(true);
given(validationService.validateEmail(newEmail)).willReturn(true);
given(validationService.isEmailFreeForRegistration(newEmail, player)).willReturn(true);
// when
process.changeEmail(player, "old@mail.tld", newEmail);
@ -71,8 +75,8 @@ public class AsyncChangeEmailTest {
PlayerAuth auth = authWithMail("old@mail.tld");
given(playerCache.getAuth("bobby")).willReturn(auth);
given(dataSource.updateEmail(auth)).willReturn(false);
given(service.validateEmail(newEmail)).willReturn(true);
given(service.isEmailFreeForRegistration(newEmail, player)).willReturn(true);
given(validationService.validateEmail(newEmail)).willReturn(true);
given(validationService.isEmailFreeForRegistration(newEmail, player)).willReturn(true);
// when
process.changeEmail(player, "old@mail.tld", newEmail);
@ -108,7 +112,7 @@ public class AsyncChangeEmailTest {
given(playerCache.isAuthenticated("bobby")).willReturn(true);
PlayerAuth auth = authWithMail("old@mail.tld");
given(playerCache.getAuth("bobby")).willReturn(auth);
given(service.validateEmail(newEmail)).willReturn(false);
given(validationService.validateEmail(newEmail)).willReturn(false);
// when
process.changeEmail(player, "old@mail.tld", newEmail);
@ -127,7 +131,7 @@ public class AsyncChangeEmailTest {
given(playerCache.isAuthenticated("bobby")).willReturn(true);
PlayerAuth auth = authWithMail("other@address.email");
given(playerCache.getAuth("bobby")).willReturn(auth);
given(service.validateEmail(newEmail)).willReturn(true);
given(validationService.validateEmail(newEmail)).willReturn(true);
// when
process.changeEmail(player, "old@mail.tld", newEmail);
@ -146,8 +150,8 @@ public class AsyncChangeEmailTest {
given(playerCache.isAuthenticated("username")).willReturn(true);
PlayerAuth auth = authWithMail("old@example.com");
given(playerCache.getAuth("username")).willReturn(auth);
given(service.validateEmail(newEmail)).willReturn(true);
given(service.isEmailFreeForRegistration(newEmail, player)).willReturn(false);
given(validationService.validateEmail(newEmail)).willReturn(true);
given(validationService.isEmailFreeForRegistration(newEmail, player)).willReturn(false);
// when
process.changeEmail(player, "old@example.com", newEmail);

View File

@ -8,7 +8,7 @@ import fr.xephi.authme.events.AuthMeAsyncPreLoginEvent;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.permission.PlayerStatePermission;
import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.settings.properties.DatabaseSettings;
import fr.xephi.authme.settings.properties.HooksSettings;
@ -56,7 +56,7 @@ public class AsynchronousLoginTest {
@Mock
private PlayerCache playerCache;
@Mock
private ProcessService processService;
private CommonService commonService;
@Mock
private LimboPlayerTaskManager limboPlayerTaskManager;
@Mock
@ -81,7 +81,7 @@ public class AsynchronousLoginTest {
// then
verify(playerCache, only()).isAuthenticated(name);
verify(processService).send(player, MessageKey.ALREADY_LOGGED_IN_ERROR);
verify(commonService).send(player, MessageKey.ALREADY_LOGGED_IN_ERROR);
verifyZeroInteractions(dataSource);
}
@ -98,7 +98,7 @@ public class AsynchronousLoginTest {
// then
verify(playerCache, only()).isAuthenticated(name);
verify(processService).send(player, MessageKey.USER_NOT_REGISTERED);
verify(commonService).send(player, MessageKey.USER_NOT_REGISTERED);
verify(dataSource, only()).getAuth(name);
}
@ -111,15 +111,15 @@ public class AsynchronousLoginTest {
int groupId = 13;
PlayerAuth auth = PlayerAuth.builder().name(name).groupId(groupId).build();
given(dataSource.getAuth(name)).willReturn(auth);
given(processService.getProperty(DatabaseSettings.MYSQL_COL_GROUP)).willReturn("group");
given(processService.getProperty(HooksSettings.NON_ACTIVATED_USERS_GROUP)).willReturn(groupId);
given(commonService.getProperty(DatabaseSettings.MYSQL_COL_GROUP)).willReturn("group");
given(commonService.getProperty(HooksSettings.NON_ACTIVATED_USERS_GROUP)).willReturn(groupId);
// when
asynchronousLogin.forceLogin(player);
// then
verify(playerCache, only()).isAuthenticated(name);
verify(processService).send(player, MessageKey.ACCOUNT_NOT_ACTIVATED);
verify(commonService).send(player, MessageKey.ACCOUNT_NOT_ACTIVATED);
verify(dataSource, only()).getAuth(name);
}
@ -133,7 +133,7 @@ public class AsynchronousLoginTest {
given(playerCache.isAuthenticated(name)).willReturn(false);
PlayerAuth auth = PlayerAuth.builder().name(name).build();
given(dataSource.getAuth(name)).willReturn(auth);
given(processService.getProperty(DatabaseSettings.MYSQL_COL_GROUP)).willReturn("");
given(commonService.getProperty(DatabaseSettings.MYSQL_COL_GROUP)).willReturn("");
doReturn(true).when(asynchronousLogin).hasReachedMaxLoggedInPlayersForIp(any(Player.class), anyString());
// when
@ -141,7 +141,7 @@ public class AsynchronousLoginTest {
// then
verify(playerCache, only()).isAuthenticated(name);
verify(processService).send(player, MessageKey.ALREADY_LOGGED_IN_ERROR);
verify(commonService).send(player, MessageKey.ALREADY_LOGGED_IN_ERROR);
verify(dataSource, only()).getAuth(name);
verify(asynchronousLogin).hasReachedMaxLoggedInPlayersForIp(player, ip);
}
@ -156,8 +156,8 @@ public class AsynchronousLoginTest {
given(playerCache.isAuthenticated(name)).willReturn(false);
PlayerAuth auth = PlayerAuth.builder().name(name).build();
given(dataSource.getAuth(name)).willReturn(auth);
given(processService.getProperty(DatabaseSettings.MYSQL_COL_GROUP)).willReturn("");
given(processService.getProperty(PluginSettings.USE_ASYNC_TASKS)).willReturn(true);
given(commonService.getProperty(DatabaseSettings.MYSQL_COL_GROUP)).willReturn("");
given(commonService.getProperty(PluginSettings.USE_ASYNC_TASKS)).willReturn(true);
doReturn(false).when(asynchronousLogin).hasReachedMaxLoggedInPlayersForIp(any(Player.class), anyString());
doAnswer(new Answer<Void>() {
@Override
@ -181,7 +181,7 @@ public class AsynchronousLoginTest {
public void shouldPassMaxLoginPerIpCheck() {
// given
Player player = mockPlayer("Carl");
given(processService.getProperty(RestrictionSettings.MAX_LOGIN_PER_IP)).willReturn(2);
given(commonService.getProperty(RestrictionSettings.MAX_LOGIN_PER_IP)).willReturn(2);
given(permissionsManager.hasPermission(player, PlayerStatePermission.ALLOW_MULTIPLE_ACCOUNTS)).willReturn(false);
mockOnlinePlayersInBukkitService();
@ -198,7 +198,7 @@ public class AsynchronousLoginTest {
public void shouldSkipIpCheckForZeroThreshold() {
// given
Player player = mockPlayer("Fiona");
given(processService.getProperty(RestrictionSettings.MAX_LOGIN_PER_IP)).willReturn(0);
given(commonService.getProperty(RestrictionSettings.MAX_LOGIN_PER_IP)).willReturn(0);
// when
boolean result = asynchronousLogin.hasReachedMaxLoggedInPlayersForIp(player, "192.168.0.1");
@ -212,7 +212,7 @@ public class AsynchronousLoginTest {
public void shouldSkipIpCheckForPlayerWithMultipleAccountsPermission() {
// given
Player player = mockPlayer("Frank");
given(processService.getProperty(RestrictionSettings.MAX_LOGIN_PER_IP)).willReturn(1);
given(commonService.getProperty(RestrictionSettings.MAX_LOGIN_PER_IP)).willReturn(1);
given(permissionsManager.hasPermission(player, PlayerStatePermission.ALLOW_MULTIPLE_ACCOUNTS)).willReturn(true);
// when
@ -228,7 +228,7 @@ public class AsynchronousLoginTest {
public void shouldFailIpCheckForIpWithTooManyPlayersOnline() {
// given
Player player = mockPlayer("Ian");
given(processService.getProperty(RestrictionSettings.MAX_LOGIN_PER_IP)).willReturn(2);
given(commonService.getProperty(RestrictionSettings.MAX_LOGIN_PER_IP)).willReturn(2);
given(permissionsManager.hasPermission(player, PlayerStatePermission.ALLOW_MULTIPLE_ACCOUNTS)).willReturn(false);
mockOnlinePlayersInBukkitService();

View File

@ -8,7 +8,7 @@ import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.permission.AuthGroupHandler;
import fr.xephi.authme.permission.AuthGroupType;
import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.service.BukkitService;
@ -45,7 +45,7 @@ public class AsynchronousUnregisterTest {
@Mock
private DataSource dataSource;
@Mock
private ProcessService service;
private CommonService service;
@Mock
private PasswordSecurity passwordSecurity;
@Mock

View File

@ -1,4 +1,4 @@
package fr.xephi.authme.process;
package fr.xephi.authme.service;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.message.Messages;
@ -9,7 +9,6 @@ import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.permission.PlayerPermission;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.service.ValidationService;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.Test;
@ -25,16 +24,13 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* Test for {@link ProcessService}.
* Test for {@link CommonService}.
*/
@RunWith(MockitoJUnitRunner.class)
public class ProcessServiceTest {
public class CommonServiceTest {
@InjectMocks
private ProcessService processService;
@Mock
private ValidationService validationService;
private CommonService commonService;
@Mock
private Settings settings;
@ -54,22 +50,13 @@ public class ProcessServiceTest {
given(settings.getProperty(SecuritySettings.CAPTCHA_LENGTH)).willReturn(8);
// when
int result = processService.getProperty(SecuritySettings.CAPTCHA_LENGTH);
int result = commonService.getProperty(SecuritySettings.CAPTCHA_LENGTH);
// then
verify(settings).getProperty(SecuritySettings.CAPTCHA_LENGTH);
assertThat(result, equalTo(8));
}
@Test
public void shouldReturnSettings() {
// given/when
Settings result = processService.getSettings();
// then
assertThat(result, equalTo(settings));
}
@Test
public void shouldSendMessageToPlayer() {
// given
@ -77,7 +64,7 @@ public class ProcessServiceTest {
MessageKey key = MessageKey.ACCOUNT_NOT_ACTIVATED;
// when
processService.send(sender, key);
commonService.send(sender, key);
// then
verify(messages).send(sender, key);
@ -91,7 +78,7 @@ public class ProcessServiceTest {
String[] replacements = new String[]{"test", "toast"};
// when
processService.send(sender, key, replacements);
commonService.send(sender, key, replacements);
// then
verify(messages).send(sender, key, replacements);
@ -105,7 +92,7 @@ public class ProcessServiceTest {
given(messages.retrieve(key)).willReturn(lines);
// when
String[] result = processService.retrieveMessage(key);
String[] result = commonService.retrieveMessage(key);
// then
assertThat(result, equalTo(lines));
@ -120,43 +107,13 @@ public class ProcessServiceTest {
given(messages.retrieveSingle(key)).willReturn(text);
// when
String result = processService.retrieveSingleMessage(key);
String result = commonService.retrieveSingleMessage(key);
// then
assertThat(result, equalTo(text));
verify(messages).retrieveSingle(key);
}
@Test
public void shouldValidateEmail() {
// given
String email = "test@example.tld";
given(validationService.validateEmail(email)).willReturn(true);
// when
boolean result = processService.validateEmail(email);
// then
assertThat(result, equalTo(true));
verify(validationService).validateEmail(email);
}
@Test
public void shouldCheckIfEmailCanBeUsed() {
// given
String email = "mail@example.com";
CommandSender sender = mock(CommandSender.class);
given(validationService.isEmailFreeForRegistration(email, sender))
.willReturn(true);
// when
boolean result = processService.isEmailFreeForRegistration(email, sender);
// then
assertThat(result, equalTo(true));
verify(validationService).isEmailFreeForRegistration(email, sender);
}
@Test
public void shouldCheckPermission() {
// given
@ -165,7 +122,7 @@ public class ProcessServiceTest {
given(permissionsManager.hasPermission(player, permission)).willReturn(true);
// when
boolean result = processService.hasPermission(player, permission);
boolean result = commonService.hasPermission(player, permission);
// then
verify(permissionsManager).hasPermission(player, permission);
@ -180,7 +137,7 @@ public class ProcessServiceTest {
given(authGroupHandler.setGroup(player, type)).willReturn(true);
// when
boolean result = processService.setGroup(player, type);
boolean result = commonService.setGroup(player, type);
// then
verify(authGroupHandler).setGroup(player, type);