#432 Inject in commands: DataSource / AntiBot / PasswordSecurity / PlayerCache

- Inject the services instead of passing them through the command service
This commit is contained in:
ljacqu
2016-05-02 18:52:34 +02:00
parent e6dacd6951
commit 9af596327a
23 changed files with 211 additions and 243 deletions
+1 -1
View File
@@ -256,11 +256,11 @@ public class AuthMe extends JavaPlugin {
initializer.register(NewSetting.class, newSettings);
initializer.register(Messages.class, messages);
initializer.register(DataSource.class, database);
initializer.provide(BaseCommands.class, CommandInitializer.buildCommands(initializer));
// Some statically injected things
initializer.register(PlayerCache.class, PlayerCache.getInstance());
initializer.register(LimboCache.class, LimboCache.getInstance());
initializer.provide(BaseCommands.class, CommandInitializer.buildCommands(initializer));
permsMan = initializer.get(PermissionsManager.class);
bukkitService = initializer.get(BukkitService.class);
@@ -1,10 +1,7 @@
package fr.xephi.authme.command;
import fr.xephi.authme.AntiBot;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.command.help.HelpProvider;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.hooks.PluginHooks;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages;
@@ -48,8 +45,6 @@ public class CommandService {
@Inject
private SpawnLoader spawnLoader;
@Inject
private AntiBot antiBot;
@Inject
private ValidationService validationService;
@Inject
private BukkitService bukkitService;
@@ -95,15 +90,6 @@ public class CommandService {
authMe.getServer().getScheduler().runTaskAsynchronously(authMe, task);
}
/**
* Return the AuthMe data source.
*
* @return The used data source
*/
public DataSource getDataSource() {
return authMe.getDataSource();
}
/**
* Return the AuthMe instance for further manipulation. Use only if other methods from
* the command service cannot be used.
@@ -114,15 +100,6 @@ public class CommandService {
return authMe;
}
/**
* Return the PasswordSecurity instance.
*
* @return The password security instance
*/
public PasswordSecurity getPasswordSecurity() {
return passwordSecurity;
}
/**
* Output the help for a given command.
*
@@ -185,10 +162,6 @@ public class CommandService {
return settings;
}
public PlayerCache getPlayerCache() {
return PlayerCache.getInstance();
}
public PluginHooks getPluginHooks() {
return pluginHooks;
}
@@ -197,10 +170,6 @@ public class CommandService {
return spawnLoader;
}
public AntiBot getAntiBot() {
return antiBot;
}
/**
* Verifies whether a password is valid according to the plugin settings.
*
@@ -2,13 +2,16 @@ package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword;
import org.bukkit.command.CommandSender;
import javax.inject.Inject;
import java.util.List;
/**
@@ -16,6 +19,15 @@ import java.util.List;
*/
public class ChangePasswordAdminCommand implements ExecutableCommand {
@Inject
private PasswordSecurity passwordSecurity;
@Inject
private PlayerCache playerCache;
@Inject
private DataSource dataSource;
@Override
public void executeCommand(final CommandSender sender, List<String> arguments,
final CommandService commandService) {
@@ -36,10 +48,9 @@ public class ChangePasswordAdminCommand implements ExecutableCommand {
@Override
public void run() {
DataSource dataSource = commandService.getDataSource();
PlayerAuth auth = null;
if (commandService.getPlayerCache().isAuthenticated(playerNameLowerCase)) {
auth = commandService.getPlayerCache().getAuth(playerNameLowerCase);
if (playerCache.isAuthenticated(playerNameLowerCase)) {
auth = playerCache.getAuth(playerNameLowerCase);
} else if (dataSource.isAuthAvailable(playerNameLowerCase)) {
auth = dataSource.getAuth(playerNameLowerCase);
}
@@ -48,8 +59,7 @@ public class ChangePasswordAdminCommand implements ExecutableCommand {
return;
}
HashedPassword hashedPassword = commandService.getPasswordSecurity()
.computeHash(playerPass, playerNameLowerCase);
HashedPassword hashedPassword = passwordSecurity.computeHash(playerPass, playerNameLowerCase);
auth.setPassword(hashedPassword);
if (!dataSource.updatePassword(auth)) {
@@ -3,9 +3,11 @@ package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import org.bukkit.command.CommandSender;
import javax.inject.Inject;
import java.util.List;
/**
@@ -13,11 +15,14 @@ import java.util.List;
*/
public class GetEmailCommand implements ExecutableCommand {
@Inject
private DataSource dataSource;
@Override
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) {
String playerName = arguments.isEmpty() ? sender.getName() : arguments.get(0);
PlayerAuth auth = commandService.getDataSource().getAuth(playerName);
PlayerAuth auth = dataSource.getAuth(playerName);
if (auth == null) {
commandService.send(sender, MessageKey.UNKNOWN_USER);
} else {
@@ -3,9 +3,11 @@ package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import org.bukkit.command.CommandSender;
import javax.inject.Inject;
import java.util.Date;
import java.util.List;
@@ -14,12 +16,15 @@ import java.util.List;
*/
public class LastLoginCommand implements ExecutableCommand {
@Inject
private DataSource dataSource;
@Override
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) {
// Get the player
String playerName = (arguments.size() >= 1) ? arguments.get(0) : sender.getName();
PlayerAuth auth = commandService.getDataSource().getAuth(playerName);
PlayerAuth auth = dataSource.getAuth(playerName);
if (auth == null) {
commandService.send(sender, MessageKey.USER_NOT_REGISTERED);
return;
@@ -3,10 +3,12 @@ package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.settings.properties.PurgeSettings;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
@@ -16,6 +18,9 @@ import java.util.List;
*/
public class PurgeBannedPlayersCommand implements ExecutableCommand {
@Inject
private DataSource dataSource;
@Override
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) {
// AuthMe plugin instance
@@ -23,12 +28,12 @@ public class PurgeBannedPlayersCommand implements ExecutableCommand {
// Get the list of banned players
List<String> bannedPlayers = new ArrayList<>();
for (OfflinePlayer offlinePlayer : plugin.getServer().getBannedPlayers()) {
for (OfflinePlayer offlinePlayer : commandService.getBukkitService().getBannedPlayers()) {
bannedPlayers.add(offlinePlayer.getName().toLowerCase());
}
// Purge the banned players
commandService.getDataSource().purgeBanned(bannedPlayers);
dataSource.purgeBanned(bannedPlayers);
if (commandService.getProperty(PurgeSettings.REMOVE_ESSENTIALS_FILES)
&& commandService.getPluginHooks().isEssentialsAvailable())
plugin.dataManager.purgeEssentials(bannedPlayers);
@@ -3,10 +3,12 @@ package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.settings.properties.PurgeSettings;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import javax.inject.Inject;
import java.util.Calendar;
import java.util.List;
@@ -18,6 +20,9 @@ public class PurgeCommand implements ExecutableCommand {
private static final int MINIMUM_LAST_SEEN_DAYS = 30;
@Inject
private DataSource dataSource;
@Override
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) {
// Get the days parameter
@@ -45,7 +50,7 @@ public class PurgeCommand implements ExecutableCommand {
long until = calendar.getTimeInMillis();
// Purge the data, get the purged values
List<String> purged = commandService.getDataSource().autoPurgeDatabase(until);
List<String> purged = dataSource.autoPurgeDatabase(until);
// Show a status message
sender.sendMessage(ChatColor.GOLD + "Deleted " + purged.size() + " user accounts");
@@ -3,9 +3,11 @@ package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import org.bukkit.command.CommandSender;
import javax.inject.Inject;
import java.util.List;
/**
@@ -13,26 +15,29 @@ import java.util.List;
*/
public class PurgeLastPositionCommand implements ExecutableCommand {
@Inject
private DataSource dataSource;
@Override
public void executeCommand(final CommandSender sender, List<String> arguments, CommandService commandService) {
String playerName = arguments.isEmpty() ? sender.getName() : arguments.get(0);
if ("*".equals(playerName)) {
for (PlayerAuth auth : commandService.getDataSource().getAllAuths()) {
for (PlayerAuth auth : dataSource.getAllAuths()) {
resetLastPosition(auth);
commandService.getDataSource().updateQuitLoc(auth);
dataSource.updateQuitLoc(auth);
}
sender.sendMessage("All players last position locations are now reset");
} else {
// Get the user auth and make sure the user exists
PlayerAuth auth = commandService.getDataSource().getAuth(playerName);
PlayerAuth auth = dataSource.getAuth(playerName);
if (auth == null) {
commandService.send(sender, MessageKey.UNKNOWN_USER);
return;
}
resetLastPosition(auth);
commandService.getDataSource().updateQuitLoc(auth);
dataSource.updateQuitLoc(auth);
sender.sendMessage(playerName + "'s last position location is now reset");
}
}
@@ -4,11 +4,14 @@ import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import javax.inject.Inject;
import java.util.List;
/**
@@ -16,6 +19,12 @@ import java.util.List;
*/
public class RegisterAdminCommand implements ExecutableCommand {
@Inject
private PasswordSecurity passwordSecurity;
@Inject
private DataSource dataSource;
@Override
public void executeCommand(final CommandSender sender, List<String> arguments,
final CommandService commandService) {
@@ -35,23 +44,22 @@ public class RegisterAdminCommand implements ExecutableCommand {
@Override
public void run() {
if (commandService.getDataSource().isAuthAvailable(playerNameLowerCase)) {
if (dataSource.isAuthAvailable(playerNameLowerCase)) {
commandService.send(sender, MessageKey.NAME_ALREADY_REGISTERED);
return;
}
HashedPassword hashedPassword = commandService.getPasswordSecurity()
.computeHash(playerPass, playerNameLowerCase);
HashedPassword hashedPassword = passwordSecurity.computeHash(playerPass, playerNameLowerCase);
PlayerAuth auth = PlayerAuth.builder()
.name(playerNameLowerCase)
.realName(playerName)
.password(hashedPassword)
.build();
if (!commandService.getDataSource().saveAuth(auth)) {
if (!dataSource.saveAuth(auth)) {
commandService.send(sender, MessageKey.ERROR);
return;
}
commandService.getDataSource().setUnlogged(playerNameLowerCase);
dataSource.setUnlogged(playerNameLowerCase);
commandService.send(sender, MessageKey.REGISTER_SUCCESS);
ConsoleLogger.info(sender.getName() + " registered " + playerName);
@@ -8,6 +8,7 @@ import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import org.bukkit.command.CommandSender;
import javax.inject.Inject;
import java.util.List;
/**
@@ -15,6 +16,12 @@ import java.util.List;
*/
public class SetEmailCommand implements ExecutableCommand {
@Inject
private DataSource dataSource;
@Inject
private PlayerCache playerCache;
@Override
public void executeCommand(final CommandSender sender, List<String> arguments,
final CommandService commandService) {
@@ -32,7 +39,6 @@ public class SetEmailCommand implements ExecutableCommand {
@Override
public void run() {
// Validate the user
DataSource dataSource = commandService.getDataSource();
PlayerAuth auth = dataSource.getAuth(playerName);
if (auth == null) {
commandService.send(sender, MessageKey.UNKNOWN_USER);
@@ -50,8 +56,8 @@ public class SetEmailCommand implements ExecutableCommand {
}
// Update the player cache
if (PlayerCache.getInstance().getAuth(playerName) != null) {
PlayerCache.getInstance().updatePlayer(auth);
if (playerCache.getAuth(playerName) != null) {
playerCache.updatePlayer(auth);
}
// Show a status message
@@ -8,6 +8,7 @@ import fr.xephi.authme.command.help.HelpProvider;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import javax.inject.Inject;
import java.util.Arrays;
import java.util.List;
@@ -16,9 +17,11 @@ import java.util.List;
*/
public class SwitchAntiBotCommand implements ExecutableCommand {
@Inject
private AntiBot antiBot;
@Override
public void executeCommand(final CommandSender sender, List<String> arguments, CommandService commandService) {
AntiBot antiBot = commandService.getAntiBot();
if (arguments.isEmpty()) {
sender.sendMessage("[AuthMe] AntiBot status: " + antiBot.getAntiBotStatus().name());
return;
@@ -6,6 +6,7 @@ import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.cache.limbo.LimboCache;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
@@ -19,6 +20,7 @@ import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitTask;
import javax.inject.Inject;
import java.util.List;
import static fr.xephi.authme.util.BukkitService.TICKS_PER_SECOND;
@@ -28,6 +30,12 @@ import static fr.xephi.authme.util.BukkitService.TICKS_PER_SECOND;
*/
public class UnregisterAdminCommand implements ExecutableCommand {
@Inject
private DataSource dataSource;
@Inject
private PlayerCache playerCache;
@Override
public void executeCommand(final CommandSender sender, List<String> arguments, CommandService commandService) {
// Get the player name
@@ -35,20 +43,20 @@ public class UnregisterAdminCommand implements ExecutableCommand {
String playerNameLowerCase = playerName.toLowerCase();
// Make sure the user is valid
if (!commandService.getDataSource().isAuthAvailable(playerNameLowerCase)) {
if (!dataSource.isAuthAvailable(playerNameLowerCase)) {
commandService.send(sender, MessageKey.UNKNOWN_USER);
return;
}
// Remove the player
if (!commandService.getDataSource().removeAuth(playerNameLowerCase)) {
if (!dataSource.removeAuth(playerNameLowerCase)) {
commandService.send(sender, MessageKey.ERROR);
return;
}
// Unregister the player
Player target = commandService.getPlayer(playerNameLowerCase);
PlayerCache.getInstance().removePlayer(playerNameLowerCase);
playerCache.removePlayer(playerNameLowerCase);
Utils.setGroup(target, Utils.GroupType.UNREGISTERED);
if (target != null && target.isOnline()) {
if (commandService.getProperty(RegistrationSettings.FORCE)) {
@@ -8,6 +8,7 @@ import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.task.ChangePasswordTask;
import org.bukkit.entity.Player;
import javax.inject.Inject;
import java.util.List;
/**
@@ -15,13 +16,15 @@ import java.util.List;
*/
public class ChangePasswordCommand extends PlayerCommand {
@Inject
private PlayerCache playerCache;
@Override
public void runCommand(Player player, List<String> arguments, CommandService commandService) {
String oldPassword = arguments.get(0);
String newPassword = arguments.get(1);
String name = player.getName().toLowerCase();
final PlayerCache playerCache = commandService.getPlayerCache();
if (!playerCache.isAuthenticated(name)) {
commandService.send(player, MessageKey.NOT_LOGGED_IN);
return;
@@ -8,16 +8,27 @@ import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.RandomString;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.settings.properties.EmailSettings;
import fr.xephi.authme.util.StringUtils;
import org.bukkit.entity.Player;
import javax.inject.Inject;
import java.util.List;
public class RecoverEmailCommand extends PlayerCommand {
@Inject
private PasswordSecurity passwordSecurity;
@Inject
private DataSource dataSource;
@Inject
private PlayerCache playerCache;
@Override
public void runCommand(Player player, List<String> arguments, CommandService commandService) {
final String playerMail = arguments.get(0);
@@ -29,7 +40,6 @@ public class RecoverEmailCommand extends PlayerCommand {
commandService.send(player, MessageKey.ERROR);
return;
}
DataSource dataSource = commandService.getDataSource();
if (dataSource.isAuthAvailable(playerName)) {
if (PlayerCache.getInstance().isAuthenticated(playerName)) {
commandService.send(player, MessageKey.ALREADY_LOGGED_IN_ERROR);
@@ -37,10 +47,10 @@ public class RecoverEmailCommand extends PlayerCommand {
}
String thePass = RandomString.generate(commandService.getProperty(EmailSettings.RECOVERY_PASSWORD_LENGTH));
HashedPassword hashNew = commandService.getPasswordSecurity().computeHash(thePass, playerName);
HashedPassword hashNew = passwordSecurity.computeHash(thePass, playerName);
PlayerAuth auth;
if (PlayerCache.getInstance().isAuthenticated(playerName)) {
auth = PlayerCache.getInstance().getAuth(playerName);
if (playerCache.isAuthenticated(playerName)) {
auth = playerCache.getAuth(playerName);
} else if (dataSource.isAuthAvailable(playerName)) {
auth = dataSource.getAuth(playerName);
} else {