#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

View File

@ -1,9 +1,5 @@
package fr.xephi.authme.process; package fr.xephi.authme.process;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.hooks.PluginHooks;
import fr.xephi.authme.process.email.AsyncAddEmail; import fr.xephi.authme.process.email.AsyncAddEmail;
import fr.xephi.authme.process.email.AsyncChangeEmail; import fr.xephi.authme.process.email.AsyncChangeEmail;
import fr.xephi.authme.process.join.AsynchronousJoin; import fr.xephi.authme.process.join.AsynchronousJoin;
@ -12,63 +8,110 @@ import fr.xephi.authme.process.logout.AsynchronousLogout;
import fr.xephi.authme.process.quit.AsynchronousQuit; import fr.xephi.authme.process.quit.AsynchronousQuit;
import fr.xephi.authme.process.register.AsyncRegister; import fr.xephi.authme.process.register.AsyncRegister;
import fr.xephi.authme.process.unregister.AsynchronousUnregister; import fr.xephi.authme.process.unregister.AsynchronousUnregister;
import fr.xephi.authme.util.BukkitService;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitScheduler;
import javax.inject.Inject; import javax.inject.Inject;
/**
*/
public class Management { public class Management {
@Inject @Inject
private AuthMe plugin; private BukkitService bukkitService;
// Processes
@Inject @Inject
private BukkitScheduler sched; private AsyncAddEmail asyncAddEmail;
@Inject @Inject
private ProcessService processService; private AsyncChangeEmail asyncChangeEmail;
@Inject @Inject
private DataSource dataSource; private AsynchronousLogout asynchronousLogout;
@Inject @Inject
private PlayerCache playerCache; private AsynchronousQuit asynchronousQuit;
@Inject @Inject
private PluginHooks pluginHooks; private AsynchronousJoin asynchronousJoin;
@Inject
private AsyncRegister asyncRegister;
@Inject
private AsynchronousLogin asynchronousLogin;
@Inject
private AsynchronousUnregister asynchronousUnregister;
Management() { } Management() { }
public void performLogin(final Player player, final String password, final boolean forceLogin) { public void performLogin(final Player player, final String password, final boolean forceLogin) {
runTask(new AsynchronousLogin(player, password, forceLogin, plugin, dataSource, processService)); runTask(new Runnable() {
@Override
public void run() {
asynchronousLogin.login(player, password, forceLogin);
}
});
} }
public void performLogout(final Player player) { public void performLogout(final Player player) {
runTask(new AsynchronousLogout(player, plugin, dataSource, processService)); runTask(new Runnable() {
@Override
public void run() {
asynchronousLogout.logout(player);
}
});
} }
public void performRegister(final Player player, final String password, final String email, final boolean autoLogin) { public void performRegister(final Player player, final String password, final String email, final boolean autoLogin) {
runTask(new AsyncRegister(player, password, email, plugin, dataSource, playerCache, processService, autoLogin)); runTask(new Runnable() {
@Override
public void run() {
asyncRegister.register(player, password, email, autoLogin);
}
});
} }
public void performUnregister(final Player player, final String password, final boolean force) { public void performUnregister(final Player player, final String password, final boolean isForce) {
runTask(new AsynchronousUnregister(player, password, force, plugin, processService)); runTask(new Runnable() {
@Override
public void run() {
asynchronousUnregister.unregister(player, password, isForce);
}
});
} }
public void performJoin(final Player player) { public void performJoin(final Player player) {
runTask(new AsynchronousJoin(player, plugin, dataSource, playerCache, pluginHooks, processService)); runTask(new Runnable() {
@Override
public void run() {
asynchronousJoin.processJoin(player);
}
});
} }
public void performQuit(final Player player, final boolean isKick) { public void performQuit(final Player player, final boolean isKick) {
runTask(new AsynchronousQuit(player, plugin, dataSource, isKick, processService)); runTask(new Runnable() {
@Override
public void run() {
asynchronousQuit.processQuit(player, isKick);
}
});
} }
public void performAddEmail(final Player player, final String newEmail) { public void performAddEmail(final Player player, final String newEmail) {
runTask(new AsyncAddEmail(player, newEmail, dataSource, playerCache, processService)); runTask(new Runnable() {
@Override
public void run() {
asyncAddEmail.addEmail(player, newEmail);
}
});
} }
public void performChangeEmail(final Player player, final String oldEmail, final String newEmail) { public void performChangeEmail(final Player player, final String oldEmail, final String newEmail) {
runTask(new AsyncChangeEmail(player, oldEmail, newEmail, dataSource, playerCache, processService)); runTask(new Runnable() {
@Override
public void run() {
asyncChangeEmail.changeEmail(player, oldEmail, newEmail);
}
});
} }
private void runTask(Process process) { private void runTask(Runnable runnable) {
sched.runTaskAsynchronously(plugin, process); bukkitService.runTaskAsynchronously(runnable);
} }
} }

View File

@ -0,0 +1,7 @@
package fr.xephi.authme.process;
/**
* Marker interface for AuthMe processes.
*/
public interface NewProcess {
}

View File

@ -1,14 +1,11 @@
package fr.xephi.authme.process; package fr.xephi.authme.process;
import fr.xephi.authme.AuthMe; import fr.xephi.authme.AuthMe;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.hooks.PluginHooks;
import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages; import fr.xephi.authme.output.Messages;
import fr.xephi.authme.security.PasswordSecurity; import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword; import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.settings.NewSetting; import fr.xephi.authme.settings.NewSetting;
import fr.xephi.authme.settings.SpawnLoader;
import fr.xephi.authme.settings.domain.Property; import fr.xephi.authme.settings.domain.Property;
import fr.xephi.authme.util.BukkitService; import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.util.ValidationService; import fr.xephi.authme.util.ValidationService;
@ -32,14 +29,8 @@ public class ProcessService {
@Inject @Inject
private AuthMe authMe; private AuthMe authMe;
@Inject @Inject
private DataSource dataSource;
@Inject
private PasswordSecurity passwordSecurity; private PasswordSecurity passwordSecurity;
@Inject @Inject
private PluginHooks pluginHooks;
@Inject
private SpawnLoader spawnLoader;
@Inject
private ValidationService validationService; private ValidationService validationService;
@Inject @Inject
private BukkitService bukkitService; private BukkitService bukkitService;
@ -165,33 +156,6 @@ public class ProcessService {
return passwordSecurity.computeHash(password, username); return passwordSecurity.computeHash(password, username);
} }
/**
* Return the PluginHooks manager.
*
* @return PluginHooks instance
*/
public PluginHooks getPluginHooks() {
return pluginHooks;
}
/**
* Return the spawn manager.
*
* @return SpawnLoader instance
*/
public SpawnLoader getSpawnLoader() {
return spawnLoader;
}
/**
* Return the plugin's datasource.
*
* @return the datasource
*/
public DataSource getDataSource() {
return dataSource;
}
/** /**
* Verifies whether a password is valid according to the plugin settings. * Verifies whether a password is valid according to the plugin settings.
* *

View File

@ -5,33 +5,30 @@ import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.process.Process; import fr.xephi.authme.process.NewProcess;
import fr.xephi.authme.process.ProcessService; import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.settings.properties.RegistrationSettings; import fr.xephi.authme.settings.properties.RegistrationSettings;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import javax.inject.Inject;
/** /**
* Async task to add an email to an account. * Async task to add an email to an account.
*/ */
public class AsyncAddEmail implements Process { public class AsyncAddEmail implements NewProcess {
private final Player player; @Inject
private final String email; private ProcessService service;
private final ProcessService service;
private final DataSource dataSource;
private final PlayerCache playerCache;
public AsyncAddEmail(Player player, String email, DataSource dataSource, PlayerCache playerCache, @Inject
ProcessService service) { private DataSource dataSource;
this.player = player;
this.email = email;
this.dataSource = dataSource;
this.playerCache = playerCache;
this.service = service;
}
@Override @Inject
public void run() { private PlayerCache playerCache;
AsyncAddEmail() { }
public void addEmail(Player player, String email) {
String playerName = player.getName().toLowerCase(); String playerName = player.getName().toLowerCase();
if (playerCache.isAuthenticated(playerName)) { if (playerCache.isAuthenticated(playerName)) {
@ -55,11 +52,11 @@ public class AsyncAddEmail implements Process {
} }
} }
} else { } else {
sendUnloggedMessage(dataSource); sendUnloggedMessage(player);
} }
} }
private void sendUnloggedMessage(DataSource dataSource) { private void sendUnloggedMessage(Player player) {
if (dataSource.isAuthAvailable(player.getName())) { if (dataSource.isAuthAvailable(player.getName())) {
service.send(player, MessageKey.LOGIN_MESSAGE); service.send(player, MessageKey.LOGIN_MESSAGE);
} else if (service.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)) { } else if (service.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)) {

View File

@ -4,35 +4,30 @@ import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.process.Process; import fr.xephi.authme.process.NewProcess;
import fr.xephi.authme.process.ProcessService; import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.settings.properties.RegistrationSettings; import fr.xephi.authme.settings.properties.RegistrationSettings;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import javax.inject.Inject;
/** /**
* Async task for changing the email. * Async task for changing the email.
*/ */
public class AsyncChangeEmail implements Process { public class AsyncChangeEmail implements NewProcess {
private final Player player; @Inject
private final String oldEmail; private ProcessService service;
private final String newEmail;
private final ProcessService service;
private final PlayerCache playerCache;
private final DataSource dataSource;
public AsyncChangeEmail(Player player, String oldEmail, String newEmail, DataSource dataSource, @Inject
PlayerCache playerCache, ProcessService service) { private PlayerCache playerCache;
this.player = player;
this.oldEmail = oldEmail;
this.newEmail = newEmail;
this.playerCache = playerCache;
this.dataSource = dataSource;
this.service = service;
}
@Override @Inject
public void run() { private DataSource dataSource;
AsyncChangeEmail() { }
public void changeEmail(Player player, String oldEmail, String newEmail) {
String playerName = player.getName().toLowerCase(); String playerName = player.getName().toLowerCase();
if (playerCache.isAuthenticated(playerName)) { if (playerCache.isAuthenticated(playerName)) {
PlayerAuth auth = playerCache.getAuth(playerName); PlayerAuth auth = playerCache.getAuth(playerName);
@ -47,14 +42,14 @@ public class AsyncChangeEmail implements Process {
} else if (!service.isEmailFreeForRegistration(newEmail, player)) { } else if (!service.isEmailFreeForRegistration(newEmail, player)) {
service.send(player, MessageKey.EMAIL_ALREADY_USED_ERROR); service.send(player, MessageKey.EMAIL_ALREADY_USED_ERROR);
} else { } else {
saveNewEmail(auth); saveNewEmail(auth, player, newEmail);
} }
} else { } else {
outputUnloggedMessage(); outputUnloggedMessage(player);
} }
} }
private void saveNewEmail(PlayerAuth auth) { private void saveNewEmail(PlayerAuth auth, Player player, String newEmail) {
auth.setEmail(newEmail); auth.setEmail(newEmail);
if (dataSource.updateEmail(auth)) { if (dataSource.updateEmail(auth)) {
playerCache.updatePlayer(auth); playerCache.updatePlayer(auth);
@ -64,7 +59,7 @@ public class AsyncChangeEmail implements Process {
} }
} }
private void outputUnloggedMessage() { private void outputUnloggedMessage(Player player) {
if (dataSource.isAuthAvailable(player.getName())) { if (dataSource.isAuthAvailable(player.getName())) {
service.send(player, MessageKey.LOGIN_MESSAGE); service.send(player, MessageKey.LOGIN_MESSAGE);
} else if (service.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)) { } else if (service.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)) {

View File

@ -14,9 +14,10 @@ import fr.xephi.authme.hooks.PluginHooks;
import fr.xephi.authme.listener.AuthMePlayerListener; import fr.xephi.authme.listener.AuthMePlayerListener;
import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.permission.PlayerStatePermission; 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.process.ProcessService;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.SpawnLoader;
import fr.xephi.authme.settings.properties.HooksSettings; import fr.xephi.authme.settings.properties.HooksSettings;
import fr.xephi.authme.settings.properties.PluginSettings; import fr.xephi.authme.settings.properties.PluginSettings;
import fr.xephi.authme.settings.properties.RegistrationSettings; import fr.xephi.authme.settings.properties.RegistrationSettings;
@ -35,42 +36,48 @@ import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
import javax.inject.Inject;
import static fr.xephi.authme.settings.properties.RestrictionSettings.PROTECT_INVENTORY_BEFORE_LOGIN; import static fr.xephi.authme.settings.properties.RestrictionSettings.PROTECT_INVENTORY_BEFORE_LOGIN;
/** /**
*/ */
public class AsynchronousJoin implements Process { public class AsynchronousJoin implements NewProcess {
private final AuthMe plugin; @Inject
private final Player player; private AuthMe plugin;
private final DataSource database;
private final String name;
private final ProcessService service;
private final PlayerCache playerCache;
private final PluginHooks pluginHooks;
private final boolean disableCollisions = MethodUtils @Inject
private DataSource database;
@Inject
private ProcessService service;
@Inject
private PlayerCache playerCache;
@Inject
private LimboCache limboCache;
@Inject
private PluginHooks pluginHooks;
@Inject
private SpawnLoader spawnLoader;
private static final boolean DISABLE_COLLISIONS = MethodUtils
.getAccessibleMethod(LivingEntity.class, "setCollidable", new Class[]{}) != null; .getAccessibleMethod(LivingEntity.class, "setCollidable", new Class[]{}) != null;
public AsynchronousJoin(Player player, AuthMe plugin, DataSource database, PlayerCache playerCache, AsynchronousJoin() { }
PluginHooks pluginHooks, ProcessService service) {
this.player = player;
this.plugin = plugin;
this.database = database;
this.name = player.getName().toLowerCase();
this.service = service;
this.playerCache = playerCache;
this.pluginHooks = pluginHooks;
}
@Override public void processJoin(final Player player) {
public void run() {
if (Utils.isUnrestricted(player)) { if (Utils.isUnrestricted(player)) {
return; return;
} }
final String name = player.getName().toLowerCase();
if (service.getProperty(HooksSettings.DISABLE_SOCIAL_SPY)) { if (service.getProperty(HooksSettings.DISABLE_SOCIAL_SPY)) {
service.getPluginHooks().setEssentialsSocialSpyStatus(player, false); pluginHooks.setEssentialsSocialSpyStatus(player, false);
} }
final String ip = Utils.getPlayerIp(player); final String ip = Utils.getPlayerIp(player);
@ -101,10 +108,10 @@ public class AsynchronousJoin implements Process {
return; return;
} }
// Prevent player collisions in 1.9 // Prevent player collisions in 1.9
if (disableCollisions) { if (DISABLE_COLLISIONS) {
((LivingEntity) player).setCollidable(false); ((LivingEntity) player).setCollidable(false);
} }
final Location spawnLoc = service.getSpawnLoader().getSpawnLocation(player); final Location spawnLoc = spawnLoader.getSpawnLocation(player);
final boolean isAuthAvailable = database.isAuthAvailable(name); final boolean isAuthAvailable = database.isAuthAvailable(name);
if (isAuthAvailable) { if (isAuthAvailable) {
if (!service.getProperty(RestrictionSettings.NO_TELEPORT)) { if (!service.getProperty(RestrictionSettings.NO_TELEPORT)) {
@ -123,7 +130,7 @@ public class AsynchronousJoin implements Process {
} }
} }
placePlayerSafely(player, spawnLoc); placePlayerSafely(player, spawnLoc);
LimboCache.getInstance().updateLimboPlayer(player); limboCache.updateLimboPlayer(player);
// protect inventory // protect inventory
if (service.getProperty(PROTECT_INVENTORY_BEFORE_LOGIN) && plugin.inventoryProtector != null) { if (service.getProperty(PROTECT_INVENTORY_BEFORE_LOGIN) && plugin.inventoryProtector != null) {
@ -144,7 +151,7 @@ public class AsynchronousJoin implements Process {
} }
PlayerAuth auth = database.getAuth(name); PlayerAuth auth = database.getAuth(name);
database.setUnlogged(name); database.setUnlogged(name);
PlayerCache.getInstance().removePlayer(name); playerCache.removePlayer(name);
if (auth != null && auth.getIp().equals(ip)) { if (auth != null && auth.getIp().equals(ip)) {
service.send(player, MessageKey.SESSION_RECONNECTION); service.send(player, MessageKey.SESSION_RECONNECTION);
plugin.getManagement().performLogin(player, "dontneed", true); plugin.getManagement().performLogin(player, "dontneed", true);
@ -161,12 +168,12 @@ public class AsynchronousJoin implements Process {
return; return;
} }
if (!Settings.noTeleport && !needFirstSpawn() && Settings.isTeleportToSpawnEnabled if (!Settings.noTeleport && !needFirstSpawn(player) && Settings.isTeleportToSpawnEnabled
|| (Settings.isForceSpawnLocOnJoinEnabled && Settings.getForcedWorlds.contains(player.getWorld().getName()))) { || (Settings.isForceSpawnLocOnJoinEnabled && Settings.getForcedWorlds.contains(player.getWorld().getName()))) {
service.scheduleSyncDelayedTask(new Runnable() { service.scheduleSyncDelayedTask(new Runnable() {
@Override @Override
public void run() { public void run() {
SpawnTeleportEvent tpEvent = new SpawnTeleportEvent(player, player.getLocation(), spawnLoc, PlayerCache.getInstance().isAuthenticated(name)); SpawnTeleportEvent tpEvent = new SpawnTeleportEvent(player, player.getLocation(), spawnLoc, playerCache.isAuthenticated(name));
service.callEvent(tpEvent); service.callEvent(tpEvent);
if (!tpEvent.isCancelled() && player.isOnline() && tpEvent.getTo() != null if (!tpEvent.isCancelled() && player.isOnline() && tpEvent.getTo() != null
&& tpEvent.getTo().getWorld() != null) { && tpEvent.getTo().getWorld() != null) {
@ -177,8 +184,8 @@ public class AsynchronousJoin implements Process {
} }
} }
if (!LimboCache.getInstance().hasLimboPlayer(name)) { if (!limboCache.hasLimboPlayer(name)) {
LimboCache.getInstance().addLimboPlayer(player); limboCache.addLimboPlayer(player);
} }
Utils.setGroup(player, isAuthAvailable ? GroupType.NOTLOGGEDIN : GroupType.UNREGISTERED); Utils.setGroup(player, isAuthAvailable ? GroupType.NOTLOGGEDIN : GroupType.UNREGISTERED);
@ -209,7 +216,7 @@ public class AsynchronousJoin implements Process {
int msgInterval = service.getProperty(RegistrationSettings.MESSAGE_INTERVAL); int msgInterval = service.getProperty(RegistrationSettings.MESSAGE_INTERVAL);
if (registrationTimeout > 0) { if (registrationTimeout > 0) {
BukkitTask id = service.runTaskLater(new TimeoutTask(plugin, name, player), registrationTimeout); BukkitTask id = service.runTaskLater(new TimeoutTask(plugin, name, player), registrationTimeout);
LimboPlayer limboPlayer = LimboCache.getInstance().getLimboPlayer(name); LimboPlayer limboPlayer = limboCache.getLimboPlayer(name);
if (limboPlayer != null) { if (limboPlayer != null) {
limboPlayer.setTimeoutTask(id); limboPlayer.setTimeoutTask(id);
} }
@ -223,21 +230,21 @@ public class AsynchronousJoin implements Process {
? MessageKey.REGISTER_EMAIL_MESSAGE ? MessageKey.REGISTER_EMAIL_MESSAGE
: MessageKey.REGISTER_MESSAGE; : MessageKey.REGISTER_MESSAGE;
} }
if (msgInterval > 0 && LimboCache.getInstance().getLimboPlayer(name) != null) { if (msgInterval > 0 && limboCache.getLimboPlayer(name) != null) {
BukkitTask msgTask = service.runTask(new MessageTask(service.getBukkitService(), plugin.getMessages(), BukkitTask msgTask = service.runTask(new MessageTask(service.getBukkitService(), plugin.getMessages(),
name, msg, msgInterval)); name, msg, msgInterval));
LimboPlayer limboPlayer = LimboCache.getInstance().getLimboPlayer(name); LimboPlayer limboPlayer = limboCache.getLimboPlayer(name);
if (limboPlayer != null) { if (limboPlayer != null) {
limboPlayer.setMessageTask(msgTask); limboPlayer.setMessageTask(msgTask);
} }
} }
} }
private boolean needFirstSpawn() { private boolean needFirstSpawn(final Player player) {
if (player.hasPlayedBefore()) { if (player.hasPlayedBefore()) {
return false; return false;
} }
Location firstSpawn = service.getSpawnLoader().getFirstSpawn(); Location firstSpawn = spawnLoader.getFirstSpawn();
if (firstSpawn == null) { if (firstSpawn == null) {
return false; return false;
} }

View File

@ -10,9 +10,10 @@ import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.events.AuthMeAsyncPreLoginEvent; import fr.xephi.authme.events.AuthMeAsyncPreLoginEvent;
import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.permission.AdminPermission; import fr.xephi.authme.permission.AdminPermission;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.permission.PlayerPermission; import fr.xephi.authme.permission.PlayerPermission;
import fr.xephi.authme.permission.PlayerStatePermission; 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.process.ProcessService;
import fr.xephi.authme.security.RandomString; import fr.xephi.authme.security.RandomString;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
@ -22,43 +23,41 @@ import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings; import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.settings.properties.SecuritySettings; import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.task.MessageTask; import fr.xephi.authme.task.MessageTask;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.util.StringUtils; import fr.xephi.authme.util.StringUtils;
import fr.xephi.authme.util.Utils; import fr.xephi.authme.util.Utils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
import javax.inject.Inject;
import java.util.List; import java.util.List;
/** /**
*/ */
public class AsynchronousLogin implements Process { public class AsynchronousLogin implements NewProcess {
private final Player player; @Inject
private final String name; private AuthMe plugin;
private final String realName;
private final String password;
private final boolean forceLogin;
private final AuthMe plugin;
private final DataSource database;
private final String ip;
private final ProcessService service;
public AsynchronousLogin(Player player, String password, boolean forceLogin, AuthMe plugin, DataSource data, @Inject
ProcessService service) { private DataSource database;
this.player = player;
this.name = player.getName().toLowerCase();
this.password = password;
this.realName = player.getName();
this.forceLogin = forceLogin;
this.plugin = plugin;
this.database = data;
this.ip = Utils.getPlayerIp(player);
this.service = service;
}
private boolean needsCaptcha() { @Inject
private ProcessService service;
@Inject
private PermissionsManager permissionsManager;
@Inject
private PlayerCache playerCache;
@Inject
private LimboCache limboCache;
AsynchronousLogin() { }
private boolean needsCaptcha(Player player) {
final String name = player.getName().toLowerCase();
if (service.getProperty(SecuritySettings.USE_CAPTCHA)) { if (service.getProperty(SecuritySettings.USE_CAPTCHA)) {
if (!plugin.captcha.containsKey(name)) { if (!plugin.captcha.containsKey(name)) {
plugin.captcha.putIfAbsent(name, 1); plugin.captcha.putIfAbsent(name, 1);
@ -82,8 +81,9 @@ public class AsynchronousLogin implements Process {
* *
* @return PlayerAuth * @return PlayerAuth
*/ */
private PlayerAuth preAuth() { private PlayerAuth preAuth(Player player) {
if (PlayerCache.getInstance().isAuthenticated(name)) { final String name = player.getName().toLowerCase();
if (playerCache.isAuthenticated(name)) {
service.send(player, MessageKey.ALREADY_LOGGED_IN_ERROR); service.send(player, MessageKey.ALREADY_LOGGED_IN_ERROR);
return null; return null;
} }
@ -92,7 +92,7 @@ public class AsynchronousLogin implements Process {
if (pAuth == null) { if (pAuth == null) {
service.send(player, MessageKey.USER_NOT_REGISTERED); service.send(player, MessageKey.USER_NOT_REGISTERED);
LimboPlayer limboPlayer = LimboCache.getInstance().getLimboPlayer(name); LimboPlayer limboPlayer = limboCache.getLimboPlayer(name);
if (limboPlayer != null) { if (limboPlayer != null) {
limboPlayer.getMessageTask().cancel(); limboPlayer.getMessageTask().cancel();
String[] msg = service.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION) String[] msg = service.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)
@ -110,9 +110,10 @@ public class AsynchronousLogin implements Process {
return null; return null;
} }
final String ip = Utils.getPlayerIp(player);
if (Settings.getMaxLoginPerIp > 0 if (Settings.getMaxLoginPerIp > 0
&& !plugin.getPermissionsManager().hasPermission(player, PlayerStatePermission.ALLOW_MULTIPLE_ACCOUNTS) && !permissionsManager.hasPermission(player, PlayerStatePermission.ALLOW_MULTIPLE_ACCOUNTS)
&& !ip.equalsIgnoreCase("127.0.0.1") && !ip.equalsIgnoreCase("localhost")) { && !"127.0.0.1".equalsIgnoreCase(ip) && !"localhost".equalsIgnoreCase(ip)) {
if (plugin.isLoggedIp(name, ip)) { if (plugin.isLoggedIp(name, ip)) {
service.send(player, MessageKey.ALREADY_LOGGED_IN_ERROR); service.send(player, MessageKey.ALREADY_LOGGED_IN_ERROR);
return null; return null;
@ -127,13 +128,13 @@ public class AsynchronousLogin implements Process {
return pAuth; return pAuth;
} }
@Override public void login(final Player player, String password, boolean forceLogin) {
public void run() { PlayerAuth pAuth = preAuth(player);
PlayerAuth pAuth = preAuth(); if (pAuth == null || needsCaptcha(player)) {
if (pAuth == null || needsCaptcha()) {
return; return;
} }
final String ip = Utils.getPlayerIp(player);
if ("127.0.0.1".equals(pAuth.getIp()) && !pAuth.getIp().equals(ip)) { if ("127.0.0.1".equals(pAuth.getIp()) && !pAuth.getIp().equals(ip)) {
pAuth.setIp(ip); pAuth.setIp(ip);
database.updateIp(pAuth.getNickname(), ip); database.updateIp(pAuth.getNickname(), ip);
@ -141,12 +142,13 @@ public class AsynchronousLogin implements Process {
String email = pAuth.getEmail(); String email = pAuth.getEmail();
boolean passwordVerified = forceLogin || plugin.getPasswordSecurity() boolean passwordVerified = forceLogin || plugin.getPasswordSecurity()
.comparePassword(password, pAuth.getPassword(), realName); .comparePassword(password, pAuth.getPassword(), player.getName());
final String name = player.getName().toLowerCase();
if (passwordVerified && player.isOnline()) { if (passwordVerified && player.isOnline()) {
PlayerAuth auth = PlayerAuth.builder() PlayerAuth auth = PlayerAuth.builder()
.name(name) .name(name)
.realName(realName) .realName(player.getName())
.ip(ip) .ip(ip)
.email(email) .email(email)
.password(pAuth.getPassword()) .password(pAuth.getPassword())
@ -166,7 +168,7 @@ public class AsynchronousLogin implements Process {
if (!forceLogin) if (!forceLogin)
service.send(player, MessageKey.LOGIN_SUCCESS); service.send(player, MessageKey.LOGIN_SUCCESS);
displayOtherAccounts(auth); displayOtherAccounts(auth, player);
if (service.getProperty(EmailSettings.RECALL_PLAYERS) if (service.getProperty(EmailSettings.RECALL_PLAYERS)
&& (StringUtils.isEmpty(email) || "your@email.com".equalsIgnoreCase(email))) { && (StringUtils.isEmpty(email) || "your@email.com".equalsIgnoreCase(email))) {
@ -174,11 +176,11 @@ public class AsynchronousLogin implements Process {
} }
if (!service.getProperty(SecuritySettings.REMOVE_SPAM_FROM_CONSOLE)) { if (!service.getProperty(SecuritySettings.REMOVE_SPAM_FROM_CONSOLE)) {
ConsoleLogger.info(realName + " logged in!"); ConsoleLogger.info(player.getName() + " logged in!");
} }
// makes player isLoggedin via API // makes player isLoggedin via API
PlayerCache.getInstance().addPlayer(auth); playerCache.addPlayer(auth);
database.setLogged(name); database.setLogged(name);
// As the scheduling executes the Task most likely after the current // As the scheduling executes the Task most likely after the current
@ -197,7 +199,7 @@ public class AsynchronousLogin implements Process {
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, syncPlayerLogin); Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, syncPlayerLogin);
} else if (player.isOnline()) { } else if (player.isOnline()) {
if (!service.getProperty(SecuritySettings.REMOVE_SPAM_FROM_CONSOLE)) { if (!service.getProperty(SecuritySettings.REMOVE_SPAM_FROM_CONSOLE)) {
ConsoleLogger.info(realName + " used the wrong password"); ConsoleLogger.info(player.getName() + " used the wrong password");
} }
if (service.getProperty(RestrictionSettings.KICK_ON_WRONG_PASSWORD)) { if (service.getProperty(RestrictionSettings.KICK_ON_WRONG_PASSWORD)) {
service.scheduleSyncDelayedTask(new Runnable() { service.scheduleSyncDelayedTask(new Runnable() {
@ -214,29 +216,30 @@ public class AsynchronousLogin implements Process {
} }
} }
// TODO: allow translation! // TODO #423: allow translation!
private void displayOtherAccounts(PlayerAuth auth) { private void displayOtherAccounts(PlayerAuth auth, Player player) {
if (!service.getProperty(RestrictionSettings.DISPLAY_OTHER_ACCOUNTS) || auth == null) { if (!service.getProperty(RestrictionSettings.DISPLAY_OTHER_ACCOUNTS) || auth == null) {
return; return;
} }
List<String> auths = this.database.getAllAuthsByIp(auth.getIp()); List<String> auths = database.getAllAuthsByIp(auth.getIp());
if (auths.size() < 2) { if (auths.size() < 2) {
return; return;
} }
// TODO: color player names with green if the account is online // TODO #423: color player names with green if the account is online
String message = StringUtils.join(", ", auths) + "."; String message = StringUtils.join(", ", auths) + ".";
ConsoleLogger.info("The user " + player.getName() + " has " + auths.size() + " accounts:"); ConsoleLogger.info("The user " + player.getName() + " has " + auths.size() + " accounts:");
ConsoleLogger.info(message); ConsoleLogger.info(message);
for (Player player : service.getOnlinePlayers()) { for (Player onlinePlayer : service.getOnlinePlayers()) {
if ((player.getName().equalsIgnoreCase(this.player.getName()) && plugin.getPermissionsManager().hasPermission(player, PlayerPermission.SEE_OWN_ACCOUNTS))) { if ((onlinePlayer.getName().equalsIgnoreCase(onlinePlayer.getName())
player.sendMessage("You own " + auths.size() + " accounts:"); && permissionsManager.hasPermission(onlinePlayer, PlayerPermission.SEE_OWN_ACCOUNTS))) {
player.sendMessage(message); onlinePlayer.sendMessage("You own " + auths.size() + " accounts:");
} else if (plugin.getPermissionsManager().hasPermission(player, AdminPermission.SEE_OTHER_ACCOUNTS)) { onlinePlayer.sendMessage(message);
player.sendMessage("The user " + player.getName() + " has " + auths.size() + " accounts:"); } else if (permissionsManager.hasPermission(onlinePlayer, AdminPermission.SEE_OTHER_ACCOUNTS)) {
player.sendMessage(message); onlinePlayer.sendMessage("The user " + onlinePlayer.getName() + " has " + auths.size() + " accounts:");
onlinePlayer.sendMessage(message);
} }
} }
} }

View File

@ -6,50 +6,37 @@ import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.cache.limbo.LimboCache; import fr.xephi.authme.cache.limbo.LimboCache;
import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.process.Process; import fr.xephi.authme.process.NewProcess;
import fr.xephi.authme.process.ProcessService; import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.util.Utils; import fr.xephi.authme.util.Utils;
import fr.xephi.authme.util.Utils.GroupType; import fr.xephi.authme.util.Utils.GroupType;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
/** import javax.inject.Inject;
*/
public class AsynchronousLogout implements Process {
private final Player player; public class AsynchronousLogout implements NewProcess {
private final String name;
private final AuthMe plugin;
private final DataSource database;
private boolean canLogout = true;
private final ProcessService service;
/** @Inject
* Constructor for AsynchronousLogout. private AuthMe plugin;
*
* @param player Player
* @param plugin AuthMe
* @param database DataSource
* @param service The process service
*/
public AsynchronousLogout(Player player, AuthMe plugin, DataSource database, ProcessService service) {
this.player = player;
this.plugin = plugin;
this.database = database;
this.name = player.getName().toLowerCase();
this.service = service;
}
private void preLogout() { @Inject
if (!PlayerCache.getInstance().isAuthenticated(name)) { private DataSource database;
@Inject
private ProcessService service;
@Inject
private PlayerCache playerCache;
@Inject
private LimboCache limboCache;
AsynchronousLogout() { }
public void logout(Player player) {
final String name = player.getName().toLowerCase();
if (!playerCache.isAuthenticated(name)) {
service.send(player, MessageKey.NOT_LOGGED_IN); service.send(player, MessageKey.NOT_LOGGED_IN);
canLogout = false;
}
}
@Override
public void run() {
preLogout();
if (!canLogout) {
return; return;
} }
final Player p = player; final Player p = player;
@ -61,7 +48,7 @@ public class AsynchronousLogout implements Process {
auth.setWorld(p.getWorld().getName()); auth.setWorld(p.getWorld().getName());
database.updateQuitLoc(auth); database.updateQuitLoc(auth);
PlayerCache.getInstance().removePlayer(name); playerCache.removePlayer(name);
database.setUnlogged(name); database.setUnlogged(name);
service.scheduleSyncDelayedTask(new Runnable() { service.scheduleSyncDelayedTask(new Runnable() {
@Override @Override
@ -69,10 +56,10 @@ public class AsynchronousLogout implements Process {
Utils.teleportToSpawn(p); Utils.teleportToSpawn(p);
} }
}); });
if (LimboCache.getInstance().hasLimboPlayer(name)) { if (limboCache.hasLimboPlayer(name)) {
LimboCache.getInstance().deleteLimboPlayer(name); limboCache.deleteLimboPlayer(name);
} }
LimboCache.getInstance().addLimboPlayer(player); limboCache.addLimboPlayer(player);
Utils.setGroup(player, GroupType.NOTLOGGEDIN); Utils.setGroup(player, GroupType.NOTLOGGEDIN);
service.scheduleSyncDelayedTask(new ProcessSynchronousPlayerLogout(p, plugin, service)); service.scheduleSyncDelayedTask(new ProcessSynchronousPlayerLogout(p, plugin, service));
} }

View File

@ -7,7 +7,7 @@ import fr.xephi.authme.cache.limbo.LimboCache;
import fr.xephi.authme.cache.limbo.LimboPlayer; import fr.xephi.authme.cache.limbo.LimboPlayer;
import fr.xephi.authme.datasource.CacheDataSource; import fr.xephi.authme.datasource.CacheDataSource;
import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.process.Process; import fr.xephi.authme.process.NewProcess;
import fr.xephi.authme.process.ProcessService; import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.RestrictionSettings; import fr.xephi.authme.settings.properties.RestrictionSettings;
@ -17,35 +17,37 @@ import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
public class AsynchronousQuit implements Process { import javax.inject.Inject;
private final AuthMe plugin; public class AsynchronousQuit implements NewProcess {
private final DataSource database;
private final Player player;
private final String name;
private boolean isOp = false;
private boolean needToChange = false;
private final boolean isKick;
private final ProcessService service;
public AsynchronousQuit(Player p, AuthMe plugin, DataSource database, boolean isKick, ProcessService service) { @Inject
this.player = p; private AuthMe plugin;
this.plugin = plugin;
this.database = database;
this.name = p.getName().toLowerCase();
this.isKick = isKick;
this.service = service;
}
@Override @Inject
public void run() { private DataSource database;
@Inject
private ProcessService service;
@Inject
private PlayerCache playerCache;
@Inject
private LimboCache limboCache;
AsynchronousQuit() { }
public void processQuit(Player player, boolean isKick) {
if (player == null || Utils.isUnrestricted(player)) { if (player == null || Utils.isUnrestricted(player)) {
return; return;
} }
final String name = player.getName().toLowerCase();
String ip = Utils.getPlayerIp(player); String ip = Utils.getPlayerIp(player);
if (PlayerCache.getInstance().isAuthenticated(name)) { if (playerCache.isAuthenticated(name)) {
if (service.getProperty(RestrictionSettings.SAVE_QUIT_LOCATION)) { if (service.getProperty(RestrictionSettings.SAVE_QUIT_LOCATION)) {
Location loc = player.getLocation(); Location loc = player.getLocation();
PlayerAuth auth = PlayerAuth.builder() PlayerAuth auth = PlayerAuth.builder()
@ -62,14 +64,17 @@ public class AsynchronousQuit implements Process {
database.updateSession(auth); database.updateSession(auth);
} }
LimboPlayer limbo = LimboCache.getInstance().getLimboPlayer(name); boolean needToChange = false;
boolean isOp = false;
LimboPlayer limbo = limboCache.getLimboPlayer(name);
if (limbo != null) { if (limbo != null) {
if (!StringUtils.isEmpty(limbo.getGroup())) { if (!StringUtils.isEmpty(limbo.getGroup())) {
Utils.addNormal(player, limbo.getGroup()); Utils.addNormal(player, limbo.getGroup());
} }
needToChange = true; needToChange = true;
isOp = limbo.isOperator(); isOp = limbo.isOperator();
LimboCache.getInstance().deleteLimboPlayer(name); limboCache.deleteLimboPlayer(name);
} }
if (Settings.isSessionsEnabled && !isKick) { if (Settings.isSessionsEnabled && !isKick) {
if (Settings.getSessionTimeout != 0) { if (Settings.getSessionTimeout != 0) {
@ -78,7 +83,7 @@ public class AsynchronousQuit implements Process {
@Override @Override
public void run() { public void run() {
postLogout(); postLogout(name);
} }
}, Settings.getSessionTimeout * 20 * 60); }, Settings.getSessionTimeout * 20 * 60);
@ -86,11 +91,11 @@ public class AsynchronousQuit implements Process {
plugin.sessions.put(name, task); plugin.sessions.put(name, task);
} else { } else {
//plugin is disabled; we cannot schedule more tasks so run it directly here //plugin is disabled; we cannot schedule more tasks so run it directly here
postLogout(); postLogout(name);
} }
} }
} else { } else {
PlayerCache.getInstance().removePlayer(name); playerCache.removePlayer(name);
database.setUnlogged(name); database.setUnlogged(name);
} }
@ -103,7 +108,7 @@ public class AsynchronousQuit implements Process {
} }
} }
private void postLogout() { private void postLogout(String name) {
PlayerCache.getInstance().removePlayer(name); PlayerCache.getInstance().removePlayer(name);
database.setUnlogged(name); database.setUnlogged(name);
plugin.sessions.remove(name); plugin.sessions.remove(name);

View File

@ -6,7 +6,7 @@ import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.permission.PlayerStatePermission; 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.process.ProcessService;
import fr.xephi.authme.security.HashAlgorithm; import fr.xephi.authme.security.HashAlgorithm;
import fr.xephi.authme.security.crypts.HashedPassword; 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.settings.properties.SecuritySettings;
import fr.xephi.authme.util.StringUtils; import fr.xephi.authme.util.StringUtils;
import fr.xephi.authme.util.Utils; import fr.xephi.authme.util.Utils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import javax.inject.Inject;
import java.util.List; import java.util.List;
/** public class AsyncRegister implements NewProcess {
*/
public class AsyncRegister implements Process {
private final Player player; @Inject
private final String name; private AuthMe plugin;
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;
public AsyncRegister(Player player, String password, String email, AuthMe plugin, DataSource data, @Inject
PlayerCache playerCache, ProcessService service, boolean autoLogin) { private DataSource database;
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;
}
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)) { if (playerCache.isAuthenticated(name)) {
service.send(player, MessageKey.ALREADY_LOGGED_IN_ERROR); service.send(player, MessageKey.ALREADY_LOGGED_IN_ERROR);
return false; return false;
@ -78,6 +66,7 @@ public class AsyncRegister implements Process {
} }
final int maxRegPerIp = service.getProperty(RestrictionSettings.MAX_REGISTRATION_PER_IP); final int maxRegPerIp = service.getProperty(RestrictionSettings.MAX_REGISTRATION_PER_IP);
final String ip = Utils.getPlayerIp(player);
if (maxRegPerIp > 0 if (maxRegPerIp > 0
&& !"127.0.0.1".equalsIgnoreCase(ip) && !"127.0.0.1".equalsIgnoreCase(ip)
&& !"localhost".equalsIgnoreCase(ip) && !"localhost".equalsIgnoreCase(ip)
@ -92,18 +81,18 @@ public class AsyncRegister implements Process {
return true; return true;
} }
@Override public void register(Player player, String password, String email, boolean autoLogin) {
public void run() { if (preRegisterCheck(player, password)) {
if (preRegisterCheck()) {
if (!StringUtils.isEmpty(email)) { if (!StringUtils.isEmpty(email)) {
emailRegister(); emailRegister(player, password, email);
} else { } 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); final int maxRegPerEmail = service.getProperty(EmailSettings.MAX_REG_PER_EMAIL);
if (maxRegPerEmail > 0 if (maxRegPerEmail > 0
&& !plugin.getPermissionsManager().hasPermission(player, PlayerStatePermission.ALLOW_MULTIPLE_ACCOUNTS)) { && !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 HashedPassword hashedPassword = service.computeHash(password, name);
final String ip = Utils.getPlayerIp(player);
PlayerAuth auth = PlayerAuth.builder() PlayerAuth auth = PlayerAuth.builder()
.name(name) .name(name)
.realName(player.getName()) .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); final HashedPassword hashedPassword = service.computeHash(password, name);
PlayerAuth auth = PlayerAuth.builder() PlayerAuth auth = PlayerAuth.builder()
.name(name) .name(name)

View File

@ -4,12 +4,13 @@ import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.cache.backup.JsonCache;
import fr.xephi.authme.cache.limbo.LimboCache; import fr.xephi.authme.cache.limbo.LimboCache;
import fr.xephi.authme.cache.limbo.LimboPlayer; import fr.xephi.authme.cache.limbo.LimboPlayer;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.process.Process; import fr.xephi.authme.process.NewProcess;
import fr.xephi.authme.process.ProcessService; import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.RegistrationSettings; import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings; import fr.xephi.authme.settings.properties.RestrictionSettings;
@ -22,55 +23,50 @@ import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
public class AsynchronousUnregister implements Process { import javax.inject.Inject;
private final Player player; import static fr.xephi.authme.util.BukkitService.TICKS_PER_SECOND;
private final String name;
private final String password;
private final boolean force;
private final AuthMe plugin;
private final JsonCache playerCache;
private final ProcessService service;
/** public class AsynchronousUnregister implements NewProcess {
* Constructor.
*
* @param player The player to perform the action for
* @param password The password
* @param force True to bypass password validation
* @param plugin The plugin instance
* @param service The process service
*/
public AsynchronousUnregister(Player player, String password, boolean force, AuthMe plugin,
ProcessService service) {
this.player = player;
this.name = player.getName().toLowerCase();
this.password = password;
this.force = force;
this.plugin = plugin;
this.playerCache = new JsonCache();
this.service = service;
}
@Override @Inject
public void run() { private AuthMe plugin;
PlayerAuth cachedAuth = PlayerCache.getInstance().getAuth(name);
if (force || plugin.getPasswordSecurity().comparePassword( @Inject
password, cachedAuth.getPassword(), player.getName())) { private DataSource dataSource;
if (!service.getDataSource().removeAuth(name)) {
@Inject
private ProcessService service;
@Inject
private PasswordSecurity passwordSecurity;
@Inject
private PlayerCache playerCache;
@Inject
private LimboCache limboCache;
AsynchronousUnregister() { }
public void unregister(Player player, String password, boolean force) {
final String name = player.getName().toLowerCase();
PlayerAuth cachedAuth = playerCache.getAuth(name);
if (force || passwordSecurity.comparePassword(password, cachedAuth.getPassword(), player.getName())) {
if (!dataSource.removeAuth(name)) {
service.send(player, MessageKey.ERROR); service.send(player, MessageKey.ERROR);
return; return;
} }
int timeOut = service.getProperty(RestrictionSettings.TIMEOUT) * 20; int timeOut = service.getProperty(RestrictionSettings.TIMEOUT) * TICKS_PER_SECOND;
if (Settings.isForcedRegistrationEnabled) { if (Settings.isForcedRegistrationEnabled) {
Utils.teleportToSpawn(player); Utils.teleportToSpawn(player);
player.saveData(); player.saveData();
PlayerCache.getInstance().removePlayer(player.getName().toLowerCase()); playerCache.removePlayer(player.getName().toLowerCase());
if (!Settings.getRegisteredGroup.isEmpty()) { if (!Settings.getRegisteredGroup.isEmpty()) {
Utils.setGroup(player, GroupType.UNREGISTERED); Utils.setGroup(player, GroupType.UNREGISTERED);
} }
LimboCache.getInstance().addLimboPlayer(player); limboCache.addLimboPlayer(player);
LimboPlayer limboPlayer = LimboCache.getInstance().getLimboPlayer(name); LimboPlayer limboPlayer = limboCache.getLimboPlayer(name);
int interval = service.getProperty(RegistrationSettings.MESSAGE_INTERVAL); int interval = service.getProperty(RegistrationSettings.MESSAGE_INTERVAL);
if (timeOut != 0) { if (timeOut != 0) {
BukkitTask id = service.runTaskLater(new TimeoutTask(plugin, name, player), timeOut); BukkitTask id = service.runTaskLater(new TimeoutTask(plugin, name, player), timeOut);
@ -85,12 +81,8 @@ public class AsynchronousUnregister implements Process {
if (!Settings.unRegisteredGroup.isEmpty()) { if (!Settings.unRegisteredGroup.isEmpty()) {
Utils.setGroup(player, Utils.GroupType.UNREGISTERED); Utils.setGroup(player, Utils.GroupType.UNREGISTERED);
} }
PlayerCache.getInstance().removePlayer(name); playerCache.removePlayer(name);
// check if Player cache File Exist and delete it, preventing
// duplication of items
if (playerCache.doesCacheExist(player)) {
playerCache.removeCache(player);
}
// Apply blind effect // Apply blind effect
if (service.getProperty(RegistrationSettings.APPLY_BLIND_EFFECT)) { if (service.getProperty(RegistrationSettings.APPLY_BLIND_EFFECT)) {
player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, timeOut, 2)); player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, timeOut, 2));

View File

@ -140,33 +140,6 @@ public class ProcessServiceTest {
assertThat(result, equalTo(authMe)); assertThat(result, equalTo(authMe));
} }
@Test
public void shouldReturnPluginHooks() {
// given / when
PluginHooks result = processService.getPluginHooks();
// then
assertThat(result, equalTo(pluginHooks));
}
@Test
public void shouldReturnSpawnLoader() {
// given / when
SpawnLoader result = processService.getSpawnLoader();
// then
assertThat(result, equalTo(spawnLoader));
}
@Test
public void shouldReturnDatasource() {
// given / when
DataSource result = processService.getDataSource();
// then
assertThat(result, equalTo(dataSource));
}
@Test @Test
public void shouldComputeHash() { public void shouldComputeHash() {
// given // given

View File

@ -11,6 +11,7 @@ import org.bukkit.entity.Player;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
@ -26,12 +27,18 @@ import static org.mockito.Mockito.verify;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class AsyncAddEmailTest { public class AsyncAddEmailTest {
@InjectMocks
private AsyncAddEmail asyncAddEmail;
@Mock @Mock
private Player player; private Player player;
@Mock @Mock
private DataSource dataSource; private DataSource dataSource;
@Mock @Mock
private PlayerCache playerCache; private PlayerCache playerCache;
@Mock @Mock
private ProcessService service; private ProcessService service;
@ -44,7 +51,6 @@ public class AsyncAddEmailTest {
public void shouldAddEmail() { public void shouldAddEmail() {
// given // given
String email = "my.mail@example.org"; String email = "my.mail@example.org";
AsyncAddEmail process = createProcess(email);
given(player.getName()).willReturn("testEr"); given(player.getName()).willReturn("testEr");
given(playerCache.isAuthenticated("tester")).willReturn(true); given(playerCache.isAuthenticated("tester")).willReturn(true);
PlayerAuth auth = mock(PlayerAuth.class); PlayerAuth auth = mock(PlayerAuth.class);
@ -55,7 +61,7 @@ public class AsyncAddEmailTest {
given(service.isEmailFreeForRegistration(email, player)).willReturn(true); given(service.isEmailFreeForRegistration(email, player)).willReturn(true);
// when // when
process.run(); asyncAddEmail.addEmail(player, email);
// then // then
verify(dataSource).updateEmail(auth); verify(dataSource).updateEmail(auth);
@ -68,7 +74,6 @@ public class AsyncAddEmailTest {
public void shouldReturnErrorWhenMailCannotBeSaved() { public void shouldReturnErrorWhenMailCannotBeSaved() {
// given // given
String email = "my.mail@example.org"; String email = "my.mail@example.org";
AsyncAddEmail process = createProcess(email);
given(player.getName()).willReturn("testEr"); given(player.getName()).willReturn("testEr");
given(playerCache.isAuthenticated("tester")).willReturn(true); given(playerCache.isAuthenticated("tester")).willReturn(true);
PlayerAuth auth = mock(PlayerAuth.class); PlayerAuth auth = mock(PlayerAuth.class);
@ -80,7 +85,7 @@ public class AsyncAddEmailTest {
given(service.isEmailFreeForRegistration(email, player)).willReturn(true); given(service.isEmailFreeForRegistration(email, player)).willReturn(true);
// when // when
process.run(); asyncAddEmail.addEmail(player, email);
// then // then
verify(dataSource).updateEmail(auth); verify(dataSource).updateEmail(auth);
@ -90,7 +95,6 @@ public class AsyncAddEmailTest {
@Test @Test
public void shouldNotAddMailIfPlayerAlreadyHasEmail() { public void shouldNotAddMailIfPlayerAlreadyHasEmail() {
// given // given
AsyncAddEmail process = createProcess("some.mail@example.org");
given(player.getName()).willReturn("my_Player"); given(player.getName()).willReturn("my_Player");
given(playerCache.isAuthenticated("my_player")).willReturn(true); given(playerCache.isAuthenticated("my_player")).willReturn(true);
PlayerAuth auth = mock(PlayerAuth.class); PlayerAuth auth = mock(PlayerAuth.class);
@ -98,7 +102,7 @@ public class AsyncAddEmailTest {
given(playerCache.getAuth("my_player")).willReturn(auth); given(playerCache.getAuth("my_player")).willReturn(auth);
// when // when
process.run(); asyncAddEmail.addEmail(player, "some.mail@example.org");
// then // then
verify(service).send(player, MessageKey.USAGE_CHANGE_EMAIL); verify(service).send(player, MessageKey.USAGE_CHANGE_EMAIL);
@ -109,7 +113,6 @@ public class AsyncAddEmailTest {
public void shouldNotAddMailIfItIsInvalid() { public void shouldNotAddMailIfItIsInvalid() {
// given // given
String email = "invalid_mail"; String email = "invalid_mail";
AsyncAddEmail process = createProcess(email);
given(player.getName()).willReturn("my_Player"); given(player.getName()).willReturn("my_Player");
given(playerCache.isAuthenticated("my_player")).willReturn(true); given(playerCache.isAuthenticated("my_player")).willReturn(true);
PlayerAuth auth = mock(PlayerAuth.class); PlayerAuth auth = mock(PlayerAuth.class);
@ -118,7 +121,7 @@ public class AsyncAddEmailTest {
given(service.validateEmail(email)).willReturn(false); given(service.validateEmail(email)).willReturn(false);
// when // when
process.run(); asyncAddEmail.addEmail(player, email);
// then // then
verify(service).send(player, MessageKey.INVALID_EMAIL); verify(service).send(player, MessageKey.INVALID_EMAIL);
@ -129,7 +132,6 @@ public class AsyncAddEmailTest {
public void shouldNotAddMailIfAlreadyUsed() { public void shouldNotAddMailIfAlreadyUsed() {
// given // given
String email = "player@mail.tld"; String email = "player@mail.tld";
AsyncAddEmail process = createProcess(email);
given(player.getName()).willReturn("TestName"); given(player.getName()).willReturn("TestName");
given(playerCache.isAuthenticated("testname")).willReturn(true); given(playerCache.isAuthenticated("testname")).willReturn(true);
PlayerAuth auth = mock(PlayerAuth.class); PlayerAuth auth = mock(PlayerAuth.class);
@ -139,7 +141,7 @@ public class AsyncAddEmailTest {
given(service.isEmailFreeForRegistration(email, player)).willReturn(false); given(service.isEmailFreeForRegistration(email, player)).willReturn(false);
// when // when
process.run(); asyncAddEmail.addEmail(player, email);
// then // then
verify(service).send(player, MessageKey.EMAIL_ALREADY_USED_ERROR); verify(service).send(player, MessageKey.EMAIL_ALREADY_USED_ERROR);
@ -149,13 +151,12 @@ public class AsyncAddEmailTest {
@Test @Test
public void shouldShowLoginMessage() { public void shouldShowLoginMessage() {
// given // given
AsyncAddEmail process = createProcess("test@mail.com");
given(player.getName()).willReturn("Username12"); given(player.getName()).willReturn("Username12");
given(playerCache.isAuthenticated("username12")).willReturn(false); given(playerCache.isAuthenticated("username12")).willReturn(false);
given(dataSource.isAuthAvailable("Username12")).willReturn(true); given(dataSource.isAuthAvailable("Username12")).willReturn(true);
// when // when
process.run(); asyncAddEmail.addEmail(player, "test@mail.com");
// then // then
verify(service).send(player, MessageKey.LOGIN_MESSAGE); verify(service).send(player, MessageKey.LOGIN_MESSAGE);
@ -165,14 +166,13 @@ public class AsyncAddEmailTest {
@Test @Test
public void shouldShowEmailRegisterMessage() { public void shouldShowEmailRegisterMessage() {
// given // given
AsyncAddEmail process = createProcess("test@mail.com");
given(player.getName()).willReturn("user"); given(player.getName()).willReturn("user");
given(playerCache.isAuthenticated("user")).willReturn(false); given(playerCache.isAuthenticated("user")).willReturn(false);
given(dataSource.isAuthAvailable("user")).willReturn(false); given(dataSource.isAuthAvailable("user")).willReturn(false);
given(service.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)).willReturn(true); given(service.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)).willReturn(true);
// when // when
process.run(); asyncAddEmail.addEmail(player, "test@mail.com");
// then // then
verify(service).send(player, MessageKey.REGISTER_EMAIL_MESSAGE); verify(service).send(player, MessageKey.REGISTER_EMAIL_MESSAGE);
@ -182,28 +182,17 @@ public class AsyncAddEmailTest {
@Test @Test
public void shouldShowRegularRegisterMessage() { public void shouldShowRegularRegisterMessage() {
// given // given
AsyncAddEmail process = createProcess("test@mail.com");
given(player.getName()).willReturn("user"); given(player.getName()).willReturn("user");
given(playerCache.isAuthenticated("user")).willReturn(false); given(playerCache.isAuthenticated("user")).willReturn(false);
given(dataSource.isAuthAvailable("user")).willReturn(false); given(dataSource.isAuthAvailable("user")).willReturn(false);
given(service.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)).willReturn(false); given(service.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)).willReturn(false);
// when // when
process.run(); asyncAddEmail.addEmail(player, "test@mail.com");
// then // then
verify(service).send(player, MessageKey.REGISTER_MESSAGE); verify(service).send(player, MessageKey.REGISTER_MESSAGE);
verify(playerCache, never()).updatePlayer(any(PlayerAuth.class)); verify(playerCache, never()).updatePlayer(any(PlayerAuth.class));
} }
/**
* Create an instance of {@link AsyncAddEmail} with the class' mocks.
*
* @param email The email to use
* @return The created process
*/
private AsyncAddEmail createProcess(String email) {
return new AsyncAddEmail(player, email, dataSource, playerCache, service);
}
} }

View File

@ -5,11 +5,11 @@ import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.process.ProcessService; import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.settings.properties.EmailSettings;
import fr.xephi.authme.settings.properties.RegistrationSettings; import fr.xephi.authme.settings.properties.RegistrationSettings;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
@ -26,12 +26,18 @@ import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class AsyncChangeEmailTest { public class AsyncChangeEmailTest {
@InjectMocks
private AsyncChangeEmail process;
@Mock @Mock
private Player player; private Player player;
@Mock @Mock
private PlayerCache playerCache; private PlayerCache playerCache;
@Mock @Mock
private DataSource dataSource; private DataSource dataSource;
@Mock @Mock
private ProcessService service; private ProcessService service;
@ -39,7 +45,6 @@ public class AsyncChangeEmailTest {
public void shouldAddEmail() { public void shouldAddEmail() {
// given // given
String newEmail = "new@mail.tld"; String newEmail = "new@mail.tld";
AsyncChangeEmail process = createProcess("old@mail.tld", newEmail);
given(player.getName()).willReturn("Bobby"); given(player.getName()).willReturn("Bobby");
given(playerCache.isAuthenticated("bobby")).willReturn(true); given(playerCache.isAuthenticated("bobby")).willReturn(true);
PlayerAuth auth = authWithMail("old@mail.tld"); PlayerAuth auth = authWithMail("old@mail.tld");
@ -49,7 +54,7 @@ public class AsyncChangeEmailTest {
given(service.isEmailFreeForRegistration(newEmail, player)).willReturn(true); given(service.isEmailFreeForRegistration(newEmail, player)).willReturn(true);
// when // when
process.run(); process.changeEmail(player, "old@mail.tld", newEmail);
// then // then
verify(dataSource).updateEmail(auth); verify(dataSource).updateEmail(auth);
@ -61,7 +66,6 @@ public class AsyncChangeEmailTest {
public void shouldShowErrorIfSaveFails() { public void shouldShowErrorIfSaveFails() {
// given // given
String newEmail = "new@mail.tld"; String newEmail = "new@mail.tld";
AsyncChangeEmail process = createProcess("old@mail.tld", newEmail);
given(player.getName()).willReturn("Bobby"); given(player.getName()).willReturn("Bobby");
given(playerCache.isAuthenticated("bobby")).willReturn(true); given(playerCache.isAuthenticated("bobby")).willReturn(true);
PlayerAuth auth = authWithMail("old@mail.tld"); PlayerAuth auth = authWithMail("old@mail.tld");
@ -71,7 +75,7 @@ public class AsyncChangeEmailTest {
given(service.isEmailFreeForRegistration(newEmail, player)).willReturn(true); given(service.isEmailFreeForRegistration(newEmail, player)).willReturn(true);
// when // when
process.run(); process.changeEmail(player, "old@mail.tld", newEmail);
// then // then
verify(dataSource).updateEmail(auth); verify(dataSource).updateEmail(auth);
@ -82,14 +86,13 @@ public class AsyncChangeEmailTest {
@Test @Test
public void shouldShowAddEmailUsage() { public void shouldShowAddEmailUsage() {
// given // given
AsyncChangeEmail process = createProcess("old@mail.tld", "new@mail.tld");
given(player.getName()).willReturn("Bobby"); given(player.getName()).willReturn("Bobby");
given(playerCache.isAuthenticated("bobby")).willReturn(true); given(playerCache.isAuthenticated("bobby")).willReturn(true);
PlayerAuth auth = authWithMail(null); PlayerAuth auth = authWithMail(null);
given(playerCache.getAuth("bobby")).willReturn(auth); given(playerCache.getAuth("bobby")).willReturn(auth);
// when // when
process.run(); process.changeEmail(player, "old@mail.tld", "new@mailt.tld");
// then // then
verify(dataSource, never()).updateEmail(any(PlayerAuth.class)); verify(dataSource, never()).updateEmail(any(PlayerAuth.class));
@ -101,7 +104,6 @@ public class AsyncChangeEmailTest {
public void shouldRejectInvalidNewMail() { public void shouldRejectInvalidNewMail() {
// given // given
String newEmail = "bogus"; String newEmail = "bogus";
AsyncChangeEmail process = createProcess("old@mail.tld", newEmail);
given(player.getName()).willReturn("Bobby"); given(player.getName()).willReturn("Bobby");
given(playerCache.isAuthenticated("bobby")).willReturn(true); given(playerCache.isAuthenticated("bobby")).willReturn(true);
PlayerAuth auth = authWithMail("old@mail.tld"); PlayerAuth auth = authWithMail("old@mail.tld");
@ -109,7 +111,7 @@ public class AsyncChangeEmailTest {
given(service.validateEmail(newEmail)).willReturn(false); given(service.validateEmail(newEmail)).willReturn(false);
// when // when
process.run(); process.changeEmail(player, "old@mail.tld", newEmail);
// then // then
verify(dataSource, never()).updateEmail(any(PlayerAuth.class)); verify(dataSource, never()).updateEmail(any(PlayerAuth.class));
@ -121,7 +123,6 @@ public class AsyncChangeEmailTest {
public void shouldRejectInvalidOldEmail() { public void shouldRejectInvalidOldEmail() {
// given // given
String newEmail = "new@mail.tld"; String newEmail = "new@mail.tld";
AsyncChangeEmail process = createProcess("old@mail.tld", newEmail);
given(player.getName()).willReturn("Bobby"); given(player.getName()).willReturn("Bobby");
given(playerCache.isAuthenticated("bobby")).willReturn(true); given(playerCache.isAuthenticated("bobby")).willReturn(true);
PlayerAuth auth = authWithMail("other@address.email"); PlayerAuth auth = authWithMail("other@address.email");
@ -131,7 +132,7 @@ public class AsyncChangeEmailTest {
// when // when
process.run(); process.changeEmail(player, "old@mail.tld", newEmail);
// then // then
verify(dataSource, never()).updateEmail(any(PlayerAuth.class)); verify(dataSource, never()).updateEmail(any(PlayerAuth.class));
@ -143,7 +144,6 @@ public class AsyncChangeEmailTest {
public void shouldRejectAlreadyUsedEmail() { public void shouldRejectAlreadyUsedEmail() {
// given // given
String newEmail = "new@example.com"; String newEmail = "new@example.com";
AsyncChangeEmail process = createProcess("old@example.com", newEmail);
given(player.getName()).willReturn("Username"); given(player.getName()).willReturn("Username");
given(playerCache.isAuthenticated("username")).willReturn(true); given(playerCache.isAuthenticated("username")).willReturn(true);
PlayerAuth auth = authWithMail("old@example.com"); PlayerAuth auth = authWithMail("old@example.com");
@ -152,7 +152,7 @@ public class AsyncChangeEmailTest {
given(service.isEmailFreeForRegistration(newEmail, player)).willReturn(false); given(service.isEmailFreeForRegistration(newEmail, player)).willReturn(false);
// when // when
process.run(); process.changeEmail(player, "old@example.com", newEmail);
// then // then
verify(dataSource, never()).updateEmail(any(PlayerAuth.class)); verify(dataSource, never()).updateEmail(any(PlayerAuth.class));
@ -163,13 +163,12 @@ public class AsyncChangeEmailTest {
@Test @Test
public void shouldSendLoginMessage() { public void shouldSendLoginMessage() {
// given // given
AsyncChangeEmail process = createProcess("old@mail.tld", "new@mail.tld");
given(player.getName()).willReturn("Bobby"); given(player.getName()).willReturn("Bobby");
given(playerCache.isAuthenticated("bobby")).willReturn(false); given(playerCache.isAuthenticated("bobby")).willReturn(false);
given(dataSource.isAuthAvailable("Bobby")).willReturn(true); given(dataSource.isAuthAvailable("Bobby")).willReturn(true);
// when // when
process.run(); process.changeEmail(player, "old@mail.tld", "new@mail.tld");
// then // then
verify(dataSource, never()).updateEmail(any(PlayerAuth.class)); verify(dataSource, never()).updateEmail(any(PlayerAuth.class));
@ -180,14 +179,13 @@ public class AsyncChangeEmailTest {
@Test @Test
public void shouldShowEmailRegistrationMessage() { public void shouldShowEmailRegistrationMessage() {
// given // given
AsyncChangeEmail process = createProcess("old@mail.tld", "new@mail.tld");
given(player.getName()).willReturn("Bobby"); given(player.getName()).willReturn("Bobby");
given(playerCache.isAuthenticated("bobby")).willReturn(false); given(playerCache.isAuthenticated("bobby")).willReturn(false);
given(dataSource.isAuthAvailable("Bobby")).willReturn(false); given(dataSource.isAuthAvailable("Bobby")).willReturn(false);
given(service.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)).willReturn(true); given(service.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)).willReturn(true);
// when // when
process.run(); process.changeEmail(player, "old@mail.tld", "new@mail.tld");
// then // then
verify(dataSource, never()).updateEmail(any(PlayerAuth.class)); verify(dataSource, never()).updateEmail(any(PlayerAuth.class));
@ -198,14 +196,13 @@ public class AsyncChangeEmailTest {
@Test @Test
public void shouldShowRegistrationMessage() { public void shouldShowRegistrationMessage() {
// given // given
AsyncChangeEmail process = createProcess("old@mail.tld", "new@mail.tld");
given(player.getName()).willReturn("Bobby"); given(player.getName()).willReturn("Bobby");
given(playerCache.isAuthenticated("bobby")).willReturn(false); given(playerCache.isAuthenticated("bobby")).willReturn(false);
given(dataSource.isAuthAvailable("Bobby")).willReturn(false); given(dataSource.isAuthAvailable("Bobby")).willReturn(false);
given(service.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)).willReturn(false); given(service.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)).willReturn(false);
// when // when
process.run(); process.changeEmail(player, "old@mail.tld", "new@mail.tld");
// then // then
verify(dataSource, never()).updateEmail(any(PlayerAuth.class)); verify(dataSource, never()).updateEmail(any(PlayerAuth.class));
@ -219,8 +216,4 @@ public class AsyncChangeEmailTest {
return auth; return auth;
} }
private AsyncChangeEmail createProcess(String oldEmail, String newEmail) {
given(service.getProperty(EmailSettings.MAX_REG_PER_EMAIL)).willReturn(5);
return new AsyncChangeEmail(player, oldEmail, newEmail, dataSource, playerCache, service);
}
} }