#427 Define registration type with two options - one configuring the 2nd argument

- Split sole setting to two: one defining the registration type, and one defining what the register command should take as second argument
- Contains ugly code that will be fixed with a later issue
This commit is contained in:
ljacqu
2016-12-31 15:34:40 +01:00
parent d298e1c6f1
commit 0b4d7273f6
17 changed files with 264 additions and 106 deletions
@@ -5,23 +5,25 @@ 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.process.register.RegisterSecondaryArgument;
import fr.xephi.authme.process.register.RegistrationType;
import fr.xephi.authme.process.register.executors.RegistrationExecutorProvider;
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.RegistrationArgumentType;
import fr.xephi.authme.settings.properties.RegistrationArgumentType.Execution;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import org.bukkit.entity.Player;
import javax.inject.Inject;
import java.util.List;
import static fr.xephi.authme.settings.properties.RegistrationArgumentType.EMAIL_WITH_CONFIRMATION;
import static fr.xephi.authme.settings.properties.RegistrationArgumentType.PASSWORD_WITH_CONFIRMATION;
import static fr.xephi.authme.settings.properties.RegistrationArgumentType.PASSWORD_WITH_EMAIL;
import static fr.xephi.authme.settings.properties.RegistrationSettings.REGISTRATION_TYPE;
import static fr.xephi.authme.process.register.RegisterSecondaryArgument.CONFIRMATION;
import static fr.xephi.authme.process.register.RegisterSecondaryArgument.EMAIL_MANDATORY;
import static fr.xephi.authme.process.register.RegisterSecondaryArgument.EMAIL_OPTIONAL;
import static fr.xephi.authme.process.register.RegisterSecondaryArgument.NONE;
import static fr.xephi.authme.settings.properties.RegistrationSettings.REGISTER_SECOND_ARGUMENT;
/**
* Command for /register.
@@ -50,19 +52,18 @@ public class RegisterCommand extends PlayerCommand {
management.performRegister(player,
registrationExecutorProvider.getTwoFactorRegisterExecutor(player));
return;
}
// Ensure that there is 1 argument, or 2 if confirmation is required
RegistrationArgumentType registerType = commonService.getProperty(REGISTRATION_TYPE);
if (registerType.getRequiredNumberOfArgs() > arguments.size()) {
} else if (arguments.size() < 1) {
commonService.send(player, MessageKey.USAGE_REGISTER);
return;
}
if (registerType.getExecution() == Execution.EMAIL) {
RegistrationType registrationType = commonService.getProperty(RegistrationSettings.REGISTRATION_TYPE);
if (registrationType == RegistrationType.PASSWORD) {
handlePasswordRegistration(player, arguments);
} else if (registrationType == RegistrationType.EMAIL) {
handleEmailRegistration(player, arguments);
} else {
handlePasswordRegistration(player, arguments);
throw new IllegalStateException("Unknown registration type '" + registrationType + "'");
}
}
@@ -72,23 +73,51 @@ public class RegisterCommand extends PlayerCommand {
}
private void handlePasswordRegistration(Player player, List<String> arguments) {
RegistrationArgumentType registrationType = commonService.getProperty(REGISTRATION_TYPE);
if (registrationType == PASSWORD_WITH_CONFIRMATION && !arguments.get(0).equals(arguments.get(1))) {
commonService.send(player, MessageKey.PASSWORD_MATCH_ERROR);
} else if (registrationType == PASSWORD_WITH_EMAIL && arguments.size() > 1) {
handlePasswordWithEmailRegistration(player, arguments);
} else {
management.performRegister(player, registrationExecutorProvider
.getPasswordRegisterExecutor(player, arguments.get(0)));
if (isSecondArgValidForPasswordRegistration(player, arguments)) {
final String password = arguments.get(0);
final String email = getEmailIfAvailable(arguments);
management.performRegister(
player, registrationExecutorProvider.getPasswordRegisterExecutor(player, password, email));
}
}
private void handlePasswordWithEmailRegistration(Player player, List<String> arguments) {
if (validationService.validateEmail(arguments.get(1))) {
management.performRegister(player, registrationExecutorProvider
.getPasswordRegisterExecutor(player, arguments.get(0), arguments.get(1)));
private String getEmailIfAvailable(List<String> arguments) {
if (arguments.size() >= 2) {
RegisterSecondaryArgument secondArgType = commonService.getProperty(REGISTER_SECOND_ARGUMENT);
if (secondArgType == EMAIL_MANDATORY || secondArgType == EMAIL_OPTIONAL) {
return arguments.get(1);
}
}
return null;
}
private boolean isSecondArgValidForPasswordRegistration(Player player, List<String> arguments) {
RegisterSecondaryArgument secondArgType = commonService.getProperty(REGISTER_SECOND_ARGUMENT);
// cases where args.size < 2
if (secondArgType == NONE || secondArgType == EMAIL_OPTIONAL && arguments.size() < 2) {
return true;
} else if (arguments.size() < 2) {
commonService.send(player, MessageKey.USAGE_REGISTER);
return false;
}
if (secondArgType == CONFIRMATION) {
if (arguments.get(0).equals(arguments.get(1))) {
return true;
} else {
commonService.send(player, MessageKey.PASSWORD_MATCH_ERROR);
return false;
}
} else if (secondArgType == EMAIL_MANDATORY || secondArgType == EMAIL_OPTIONAL) {
if (validationService.validateEmail(arguments.get(1))) {
return true;
} else {
commonService.send(player, MessageKey.INVALID_EMAIL);
return false;
}
} else {
commonService.send(player, MessageKey.INVALID_EMAIL);
throw new IllegalStateException("Unknown secondary argument type '" + secondArgType + "'");
}
}
@@ -103,10 +132,30 @@ public class RegisterCommand extends PlayerCommand {
final String email = arguments.get(0);
if (!validationService.validateEmail(email)) {
commonService.send(player, MessageKey.INVALID_EMAIL);
} else if (commonService.getProperty(REGISTRATION_TYPE) == EMAIL_WITH_CONFIRMATION && !email.equals(arguments.get(1))) {
commonService.send(player, MessageKey.USAGE_REGISTER);
} else {
} else if (isSecondArgValidForEmailRegistration(player, arguments)) {
management.performRegister(player, registrationExecutorProvider.getEmailRegisterExecutor(player, email));
}
}
private boolean isSecondArgValidForEmailRegistration(Player player, List<String> arguments) {
RegisterSecondaryArgument secondArgType = commonService.getProperty(REGISTER_SECOND_ARGUMENT);
// cases where args.size < 2
if (secondArgType == NONE || secondArgType == EMAIL_OPTIONAL && arguments.size() < 2) {
return true;
} else if (arguments.size() < 2) {
commonService.send(player, MessageKey.USAGE_REGISTER);
return false;
}
if (secondArgType == EMAIL_OPTIONAL || secondArgType == EMAIL_MANDATORY || secondArgType == CONFIRMATION) {
if (arguments.get(0).equals(arguments.get(1))) {
return true;
} else {
commonService.send(player, MessageKey.USAGE_REGISTER);
return false;
}
} else {
throw new IllegalStateException("Unknown secondary argument type '" + secondArgType + "'");
}
}
}
@@ -1,9 +1,5 @@
package fr.xephi.authme.process.email;
import javax.inject.Inject;
import org.bukkit.entity.Player;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
@@ -13,6 +9,10 @@ import fr.xephi.authme.process.AsynchronousProcess;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.util.Utils;
import org.bukkit.entity.Player;
import javax.inject.Inject;
/**
* Async task to add an email to an account.
@@ -65,7 +65,10 @@ public class AsyncAddEmail implements AsynchronousProcess {
if (dataSource.isAuthAvailable(player.getName())) {
service.send(player, MessageKey.LOGIN_MESSAGE);
} else {
service.send(player, service.getProperty(RegistrationSettings.REGISTRATION_TYPE).getMessageKey());
MessageKey registerMessage = Utils.getRegisterMessage(
service.getProperty(RegistrationSettings.REGISTRATION_TYPE),
service.getProperty(RegistrationSettings.REGISTER_SECOND_ARGUMENT));
service.send(player, registerMessage);
}
}
@@ -1,9 +1,5 @@
package fr.xephi.authme.process.email;
import javax.inject.Inject;
import org.bukkit.entity.Player;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
@@ -12,6 +8,10 @@ import fr.xephi.authme.process.AsynchronousProcess;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.util.Utils;
import org.bukkit.entity.Player;
import javax.inject.Inject;
/**
* Async task for changing the email.
@@ -68,7 +68,10 @@ public class AsyncChangeEmail implements AsynchronousProcess {
if (dataSource.isAuthAvailable(player.getName())) {
service.send(player, MessageKey.LOGIN_MESSAGE);
} else {
service.send(player, service.getProperty(RegistrationSettings.REGISTRATION_TYPE).getMessageKey());
MessageKey registerMessage = Utils.getRegisterMessage(
service.getProperty(RegistrationSettings.REGISTRATION_TYPE),
service.getProperty(RegistrationSettings.REGISTER_SECOND_ARGUMENT));
service.send(player, registerMessage);
}
}
}
@@ -0,0 +1,20 @@
package fr.xephi.authme.process.register;
/**
* Type of the second argument of the {@code /register} command.
*/
public enum RegisterSecondaryArgument {
/** No second argument. */
NONE,
/** Confirmation of the first argument. */
CONFIRMATION,
/** For password registration, mandatory secondary argument is email. */
EMAIL_MANDATORY,
/** For password registration, optional secondary argument is email. */
EMAIL_OPTIONAL
}
@@ -0,0 +1,19 @@
package fr.xephi.authme.process.register;
/**
* Registration type.
*/
public enum RegistrationType {
/**
* Password registration: account is registered with a password supplied by the player.
*/
PASSWORD,
/**
* Email registration: account is registered with an email supplied by the player. A password
* is generated and sent to the email address.
*/
EMAIL
}
@@ -18,10 +18,6 @@ public class RegistrationExecutorProvider {
RegistrationExecutorProvider() {
}
public RegistrationExecutor getPasswordRegisterExecutor(Player player, String password) {
return getPasswordRegisterExecutor(player, password, null);
}
public RegistrationExecutor getPasswordRegisterExecutor(Player player, String password, String email) {
return passwordRegisterExecutorProvider.new PasswordRegisterExecutor(player, password, email);
}
@@ -8,8 +8,9 @@ import com.google.common.base.Objects;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.initialization.DataFolder;
import fr.xephi.authme.output.LogLevel;
import fr.xephi.authme.process.register.RegisterSecondaryArgument;
import fr.xephi.authme.process.register.RegistrationType;
import fr.xephi.authme.settings.properties.PluginSettings;
import fr.xephi.authme.settings.properties.RegistrationArgumentType;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
@@ -232,25 +233,20 @@ public class SettingsMigrationService extends PlainMigrationService {
}
boolean useEmail = newProperty("settings.registration.enableEmailRegistrationSystem", false).getValue(resource);
RegistrationType registrationType = useEmail ? RegistrationType.EMAIL : RegistrationType.PASSWORD;
String useConfirmationPath = useEmail
? "settings.registration.doubleEmailCheck"
: "settings.restrictions.enablePasswordConfirmation";
boolean hasConfirmation = newProperty(useConfirmationPath, false).getValue(resource);
RegistrationArgumentType registerType;
if (useEmail) {
registerType = hasConfirmation
? RegistrationArgumentType.EMAIL_WITH_CONFIRMATION
: RegistrationArgumentType.EMAIL;
} else {
registerType = hasConfirmation
? RegistrationArgumentType.PASSWORD_WITH_CONFIRMATION
: RegistrationArgumentType.PASSWORD;
}
RegisterSecondaryArgument secondaryArgument = hasConfirmation
? RegisterSecondaryArgument.CONFIRMATION
: RegisterSecondaryArgument.NONE;
ConsoleLogger.warning("Merging old registration settings into '"
+ RegistrationSettings.REGISTRATION_TYPE.getPath() + "'");
resource.setValue(RegistrationSettings.REGISTRATION_TYPE.getPath(), registerType);
resource.setValue(RegistrationSettings.REGISTRATION_TYPE.getPath(), registrationType);
resource.setValue(RegistrationSettings.REGISTER_SECOND_ARGUMENT.getPath(), secondaryArgument);
return true;
}
@@ -3,6 +3,8 @@ package fr.xephi.authme.settings.properties;
import ch.jalu.configme.Comment;
import ch.jalu.configme.SettingsHolder;
import ch.jalu.configme.properties.Property;
import fr.xephi.authme.process.register.RegisterSecondaryArgument;
import fr.xephi.authme.process.register.RegistrationType;
import static ch.jalu.configme.properties.PropertyInitializer.newProperty;
@@ -25,11 +27,21 @@ public class RegistrationSettings implements SettingsHolder {
newProperty("settings.registration.force", true);
@Comment({
"Type of registration: PASSWORD, PASSWORD_WITH_CONFIRMATION, EMAIL",
"EMAIL_WITH_CONFIRMATION, PASSWORD_WITH_EMAIL"
"Type of registration: PASSWORD or EMAIL",
"Password = account is registered with a password supplied by the user;",
"Email = password is generated and sent to the email provided by the user."
})
public static final Property<RegistrationArgumentType> REGISTRATION_TYPE =
newProperty(RegistrationArgumentType.class, "settings.registration.type", RegistrationArgumentType.PASSWORD_WITH_CONFIRMATION);
public static final Property<RegistrationType> REGISTRATION_TYPE =
newProperty(RegistrationType.class, "settings.registration.type", RegistrationType.PASSWORD);
@Comment({
"Second argument the /register command should take: NONE = no 2nd argument",
"CONFIRMATION = must repeat first argument (pass or email)",
"EMAIL_OPTIONAL = for password register: 2nd argument can be empty or have email address",
"EMAIL_MANDATORY = for password register: 2nd argument MUST be an email address"
})
public static final Property<RegisterSecondaryArgument> REGISTER_SECOND_ARGUMENT =
newProperty(RegisterSecondaryArgument.class, "settings.registration.secondArg", RegisterSecondaryArgument.CONFIRMATION);
@Comment({
"Do we force kick a player after a successful registration?",
@@ -1,13 +1,5 @@
package fr.xephi.authme.task;
import static fr.xephi.authme.service.BukkitService.TICKS_PER_SECOND;
import javax.inject.Inject;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.data.limbo.LimboCache;
@@ -18,6 +10,14 @@ import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.util.Utils;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import javax.inject.Inject;
import static fr.xephi.authme.service.BukkitService.TICKS_PER_SECOND;
/**
* Registers tasks associated with a PlayerData.
@@ -96,7 +96,9 @@ public class LimboPlayerTaskManager {
if (isRegistered) {
return MessageKey.LOGIN_MESSAGE;
} else {
return settings.getProperty(RegistrationSettings.REGISTRATION_TYPE).getMessageKey();
return Utils.getRegisterMessage(
settings.getProperty(RegistrationSettings.REGISTRATION_TYPE),
settings.getProperty(RegistrationSettings.REGISTER_SECOND_ARGUMENT));
}
}
@@ -1,6 +1,10 @@
package fr.xephi.authme.util;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.register.RegisterSecondaryArgument;
import fr.xephi.authme.process.register.RegistrationType;
import java.util.regex.Pattern;
/**
@@ -57,4 +61,31 @@ public final class Utils {
public static int getCoreCount() {
return Runtime.getRuntime().availableProcessors();
}
/**
* Returns the proper message key for the given registration types.
*
* @param registrationType the registration type
* @param secondaryArgType secondary argument type for the register command
* @return the message key
*/
// TODO #1037: Remove this method
public static MessageKey getRegisterMessage(RegistrationType registrationType,
RegisterSecondaryArgument secondaryArgType) {
if (registrationType == RegistrationType.PASSWORD) {
if (secondaryArgType == RegisterSecondaryArgument.CONFIRMATION) {
return MessageKey.REGISTER_MESSAGE;
} else if (secondaryArgType == RegisterSecondaryArgument.NONE) {
return MessageKey.REGISTER_NO_REPEAT_MESSAGE;
} else { /* EMAIL_MANDATORY || EMAIL_OPTIONAL */
return MessageKey.REGISTER_PASSWORD_EMAIL_MESSAGE;
}
} else { /* registrationType == EMAIL */
if (secondaryArgType == RegisterSecondaryArgument.NONE) {
return MessageKey.REGISTER_EMAIL_NO_REPEAT_MESSAGE;
} else { /* CONFIRMATION || EMAIL_MANDATORY || EMAIL_OPTIONAL */
return MessageKey.REGISTER_EMAIL_MESSAGE;
}
}
}
}