#707 Convert async processes as services

(work in progress - rough, untested changes)
This commit is contained in:
ljacqu
2016-05-17 19:49:06 +02:00
parent 3ad76b8ec5
commit f5c89e897f
14 changed files with 359 additions and 412 deletions
@@ -6,7 +6,7 @@ import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.permission.PlayerStatePermission;
import fr.xephi.authme.process.Process;
import fr.xephi.authme.process.NewProcess;
import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.security.HashAlgorithm;
import fr.xephi.authme.security.crypts.HashedPassword;
@@ -18,42 +18,30 @@ import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.util.StringUtils;
import fr.xephi.authme.util.Utils;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import javax.inject.Inject;
import java.util.List;
/**
*/
public class AsyncRegister implements Process {
public class AsyncRegister implements NewProcess {
private final Player player;
private final String name;
private final String password;
private final String ip;
private final String email;
private final AuthMe plugin;
private final DataSource database;
private final PlayerCache playerCache;
private final ProcessService service;
private final boolean autoLogin;
@Inject
private AuthMe plugin;
public AsyncRegister(Player player, String password, String email, AuthMe plugin, DataSource data,
PlayerCache playerCache, ProcessService service, boolean autoLogin) {
this.player = player;
this.password = password;
this.name = player.getName().toLowerCase();
this.email = email;
this.plugin = plugin;
this.database = data;
this.ip = Utils.getPlayerIp(player);
this.playerCache = playerCache;
this.service = service;
this.autoLogin = autoLogin;
}
@Inject
private DataSource database;
private boolean preRegisterCheck() {
@Inject
private PlayerCache playerCache;
@Inject
private ProcessService service;
AsyncRegister() { }
private boolean preRegisterCheck(Player player, String password) {
final String name = player.getName().toLowerCase();
if (playerCache.isAuthenticated(name)) {
service.send(player, MessageKey.ALREADY_LOGGED_IN_ERROR);
return false;
@@ -78,6 +66,7 @@ public class AsyncRegister implements Process {
}
final int maxRegPerIp = service.getProperty(RestrictionSettings.MAX_REGISTRATION_PER_IP);
final String ip = Utils.getPlayerIp(player);
if (maxRegPerIp > 0
&& !"127.0.0.1".equalsIgnoreCase(ip)
&& !"localhost".equalsIgnoreCase(ip)
@@ -92,18 +81,18 @@ public class AsyncRegister implements Process {
return true;
}
@Override
public void run() {
if (preRegisterCheck()) {
public void register(Player player, String password, String email, boolean autoLogin) {
if (preRegisterCheck(player, password)) {
if (!StringUtils.isEmpty(email)) {
emailRegister();
emailRegister(player, password, email);
} else {
passwordRegister();
passwordRegister(player, password, autoLogin);
}
}
}
private void emailRegister() {
private void emailRegister(Player player, String password, String email) {
final String name = player.getName().toLowerCase();
final int maxRegPerEmail = service.getProperty(EmailSettings.MAX_REG_PER_EMAIL);
if (maxRegPerEmail > 0
&& !plugin.getPermissionsManager().hasPermission(player, PlayerStatePermission.ALLOW_MULTIPLE_ACCOUNTS)) {
@@ -116,6 +105,7 @@ public class AsyncRegister implements Process {
}
final HashedPassword hashedPassword = service.computeHash(password, name);
final String ip = Utils.getPlayerIp(player);
PlayerAuth auth = PlayerAuth.builder()
.name(name)
.realName(player.getName())
@@ -137,7 +127,9 @@ public class AsyncRegister implements Process {
}
private void passwordRegister() {
private void passwordRegister(Player player, String password, boolean autoLogin) {
final String name = player.getName().toLowerCase();
final String ip = Utils.getPlayerIp(player);
final HashedPassword hashedPassword = service.computeHash(password, name);
PlayerAuth auth = PlayerAuth.builder()
.name(name)