From 9653354135e6792693692caab4d2fc41bed09f87 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Sun, 31 Jan 2016 18:50:57 +0100 Subject: [PATCH] #450 Move Settings#getRestrictedIp() --- .../authme/process/join/AsynchronousJoin.java | 38 ++++++++++++++++--- .../fr/xephi/authme/settings/NewSetting.java | 4 +- .../fr/xephi/authme/settings/Settings.java | 37 ------------------ .../settings/SettingsMigrationService.java | 12 +++--- .../settings/NewSettingIntegrationTest.java | 1 - 5 files changed, 39 insertions(+), 53 deletions(-) diff --git a/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java b/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java index cbdac41eb..2feff435c 100644 --- a/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java +++ b/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java @@ -49,14 +49,14 @@ public class AsynchronousJoin { } public void process() { - if (Settings.checkVeryGames) { - plugin.getVerygamesIp(player); - } - if (Utils.isUnrestricted(player)) { return; } + if (Settings.checkVeryGames) { + plugin.getVerygamesIp(player); + } + if (plugin.ess != null && Settings.disableSocialSpy) { plugin.ess.getUser(player).setSocialSpyEnabled(false); } @@ -64,13 +64,13 @@ public class AsynchronousJoin { final String ip = plugin.getIP(player); - if (Settings.isAllowRestrictedIp && !Settings.getRestrictedIp(name, ip, player.getAddress().getHostName())) { + if (Settings.isAllowRestrictedIp && !isNameRestricted(name, ip, player.getAddress().getHostName())) { sched.scheduleSyncDelayedTask(plugin, new Runnable() { @Override public void run() { AuthMePlayerListener.causeByAuthMe.putIfAbsent(name, true); - player.kickPlayer("You are not the Owner of this account, please try another name!"); + player.kickPlayer("You are not the owner of this account. Please try another name!"); if (Settings.banUnsafeIp) plugin.getServer().banIP(ip); } @@ -282,4 +282,30 @@ public class AsynchronousJoin { }); } + /** + * Return whether the name is restricted based on the restriction setting. + * + * @param name The name to check + * @param ip The IP address of the player + * @param domain The hostname of the IP address + * @return True if the name is restricted (IP/domain is not allowed for the given name), + * false if the restrictions are met or if the name has no restrictions to it + */ + private static boolean isNameRestricted(String name, String ip, String domain) { + boolean nameFound = false; + for (String entry : Settings.getRestrictedIp) { + String[] args = entry.split(";"); + String testName = args[0]; + String testIp = args[1]; + if (testName.equalsIgnoreCase(name)) { + nameFound = true; + if ((ip != null && testIp.equals(ip)) + || (domain != null && testIp.equalsIgnoreCase(domain))) { + return false; + } + } + } + return nameFound; + } + } diff --git a/src/main/java/fr/xephi/authme/settings/NewSetting.java b/src/main/java/fr/xephi/authme/settings/NewSetting.java index 5f393cb96..20ed0952d 100644 --- a/src/main/java/fr/xephi/authme/settings/NewSetting.java +++ b/src/main/java/fr/xephi/authme/settings/NewSetting.java @@ -47,7 +47,7 @@ public class NewSetting { messagesFile = buildMessagesFile(); PropertyMap propertyMap = SettingsFieldRetriever.getAllPropertyFields(); - if (SettingsMigrationService.checkAndMigrate(configuration, propertyMap)) { + if (SettingsMigrationService.checkAndMigrate(configuration, propertyMap, pluginFolder)) { ConsoleLogger.info("Merged new config options"); ConsoleLogger.info("Please check your config.yml file for new settings!"); save(propertyMap); @@ -67,7 +67,7 @@ public class NewSetting { this.configFile = configFile; this.pluginFolder = new File(""); - if (propertyMap != null && SettingsMigrationService.checkAndMigrate(configuration, propertyMap)) { + if (propertyMap != null && SettingsMigrationService.checkAndMigrate(configuration, propertyMap, pluginFolder)) { save(propertyMap); } } diff --git a/src/main/java/fr/xephi/authme/settings/Settings.java b/src/main/java/fr/xephi/authme/settings/Settings.java index 14794965d..7e6b13da4 100644 --- a/src/main/java/fr/xephi/authme/settings/Settings.java +++ b/src/main/java/fr/xephi/authme/settings/Settings.java @@ -340,43 +340,6 @@ public final class Settings { } } - /** - * Config option for setting and check restricted user by username;ip , - * return false if ip and name doesn't match with player that join the - * server, so player has a restricted access - * - * @param name String - * @param ip String - * @param domain String - * - * @return boolean - */ - public static boolean getRestrictedIp(String name, String ip, String domain) { - - Iterator iterator = getRestrictedIp.iterator(); - boolean trueOnce = false; - boolean nameFound = false; - while (iterator.hasNext()) { - String[] args = iterator.next().split(";"); - String testName = args[0]; - String testIp = args[1]; - if (testName.equalsIgnoreCase(name)) { - nameFound = true; - if (ip != null) { - if (testIp.equalsIgnoreCase(ip)) { - trueOnce = true; - } - } - if (domain != null) { - if (testIp.equalsIgnoreCase(domain)) { - trueOnce = true; - } - } - } - } - return !nameFound || trueOnce; - } - /** * Saves the configuration to disk * diff --git a/src/main/java/fr/xephi/authme/settings/SettingsMigrationService.java b/src/main/java/fr/xephi/authme/settings/SettingsMigrationService.java index 0691609be..1e010f4c5 100644 --- a/src/main/java/fr/xephi/authme/settings/SettingsMigrationService.java +++ b/src/main/java/fr/xephi/authme/settings/SettingsMigrationService.java @@ -5,7 +5,6 @@ import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.settings.domain.Property; import fr.xephi.authme.settings.propertymap.PropertyMap; import fr.xephi.authme.util.StringUtils; -import fr.xephi.authme.util.Wrapper; import org.bukkit.configuration.file.FileConfiguration; import java.io.File; @@ -27,22 +26,21 @@ public final class SettingsMigrationService { * * @param configuration The file configuration to check and migrate * @param propertyMap The property map of all existing properties + * @param pluginFolder The plugin folder * @return True if there is a change and the config must be saved, false if the config is up-to-date */ - public static boolean checkAndMigrate(FileConfiguration configuration, PropertyMap propertyMap) { - return performMigrations(configuration) || hasDeprecatedProperties(configuration) + public static boolean checkAndMigrate(FileConfiguration configuration, PropertyMap propertyMap, File pluginFolder) { + return performMigrations(configuration, pluginFolder) || hasDeprecatedProperties(configuration) || !containsAllSettings(configuration, propertyMap); } - private static boolean performMigrations(FileConfiguration configuration) { + private static boolean performMigrations(FileConfiguration configuration, File pluginFolder) { boolean changes = false; if ("[a-zA-Z0-9_?]*".equals(configuration.getString(ALLOWED_NICKNAME_CHARACTERS.getPath()))) { configuration.set(ALLOWED_NICKNAME_CHARACTERS.getPath(), "[a-zA-Z0-9_]*"); changes = true; } - // TODO #450: Don't get the data folder statically - Wrapper w = Wrapper.getInstance(); - changes = changes || performMailTextToFileMigration(configuration, w.getDataFolder()); + changes = changes || performMailTextToFileMigration(configuration, pluginFolder); return changes; } diff --git a/src/test/java/fr/xephi/authme/settings/NewSettingIntegrationTest.java b/src/test/java/fr/xephi/authme/settings/NewSettingIntegrationTest.java index d01902b57..646c18456 100644 --- a/src/test/java/fr/xephi/authme/settings/NewSettingIntegrationTest.java +++ b/src/test/java/fr/xephi/authme/settings/NewSettingIntegrationTest.java @@ -42,7 +42,6 @@ public class NewSettingIntegrationTest { // given YamlConfiguration configuration = YamlConfiguration.loadConfiguration(getConfigFile(COMPLETE_FILE)); File file = new File("unused"); - assumeThat(file.exists(), equalTo(false)); // when / then NewSetting settings = new NewSetting(configuration, file, propertyMap);