diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index 1d7d3568..49285b05 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -30,11 +30,9 @@ import fr.xephi.authme.output.ConsoleFilter; import fr.xephi.authme.output.Log4JFilter; import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.Messages; -import fr.xephi.authme.permission.AuthGroupHandler; import fr.xephi.authme.permission.PermissionsManager; import fr.xephi.authme.permission.PermissionsSystemType; import fr.xephi.authme.process.Management; -import fr.xephi.authme.security.PasswordSecurity; import fr.xephi.authme.security.crypts.SHA256; import fr.xephi.authme.settings.NewSetting; import fr.xephi.authme.settings.Settings; @@ -55,6 +53,7 @@ import fr.xephi.authme.util.GeoLiteAPI; import fr.xephi.authme.util.MigrationService; import fr.xephi.authme.util.StringUtils; import fr.xephi.authme.util.Utils; +import fr.xephi.authme.util.ValidationService; import org.apache.logging.log4j.LogManager; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -71,6 +70,7 @@ import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitWorker; import java.io.File; +import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import java.util.Collection; @@ -112,6 +112,7 @@ public class AuthMe extends JavaPlugin { private BukkitService bukkitService; private Injector injector; private GeoLiteAPI geoLiteApi; + private PlayerCache playerCache; /** * Constructor. @@ -281,18 +282,20 @@ public class AuthMe extends JavaPlugin { } protected void instantiateServices(Injector injector) { - // Some statically injected things - injector.register(PlayerCache.class, PlayerCache.getInstance()); + // PlayerCache is still injected statically sometimes + playerCache = PlayerCache.getInstance(); + injector.register(PlayerCache.class, playerCache); messages = injector.getSingleton(Messages.class); permsMan = injector.getSingleton(PermissionsManager.class); bukkitService = injector.getSingleton(BukkitService.class); pluginHooks = injector.getSingleton(PluginHooks.class); - injector.getSingleton(PasswordSecurity.class); spawnLoader = injector.getSingleton(SpawnLoader.class); commandHandler = injector.getSingleton(CommandHandler.class); management = injector.getSingleton(Management.class); geoLiteApi = injector.getSingleton(GeoLiteAPI.class); + + // Trigger construction of API classes; they will keep track of the singleton injector.getSingleton(NewAPI.class); injector.getSingleton(API.class); } @@ -346,14 +349,13 @@ public class AuthMe extends JavaPlugin { int playersOnline = bukkitService.getOnlinePlayers().size(); if (playersOnline < 1) { database.purgeLogged(); - } else if (Settings.reloadSupport) { + } else if (newSettings.getProperty(SecuritySettings.USE_RELOAD_COMMAND_SUPPORT)) { for (PlayerAuth auth : database.getLoggedPlayers()) { - if (auth == null) { - continue; + if (auth != null) { + auth.setLastLogin(new Date().getTime()); + database.updateSession(auth); + playerCache.addPlayer(auth); } - auth.setLastLogin(new Date().getTime()); - database.updateSession(auth); - PlayerCache.getInstance().addPlayer(auth); } } } @@ -422,12 +424,12 @@ public class AuthMe extends JavaPlugin { // Save player data BukkitService bukkitService = injector.getIfAvailable(BukkitService.class); LimboCache limboCache = injector.getIfAvailable(LimboCache.class); - AuthGroupHandler authGroupHandler = injector.getIfAvailable(AuthGroupHandler.class); + ValidationService validationService = injector.getIfAvailable(ValidationService.class); - if (bukkitService != null && limboCache != null) { + if (bukkitService != null && limboCache != null && validationService != null) { Collection players = bukkitService.getOnlinePlayers(); for (Player player : players) { - savePlayer(player, limboCache, authGroupHandler); + savePlayer(player, limboCache, validationService); } } @@ -492,7 +494,7 @@ public class AuthMe extends JavaPlugin { // Stop/unload the server/plugin as defined in the configuration public void stopOrUnload() { - if (Settings.isStopEnabled) { + if (newSettings == null || newSettings.getProperty(SecuritySettings.STOP_SERVER_ON_PROBLEM)) { ConsoleLogger.warning("THE SERVER IS GOING TO SHUT DOWN AS DEFINED IN THE CONFIGURATION!"); getServer().shutdown(); } else { @@ -507,9 +509,10 @@ public class AuthMe extends JavaPlugin { * * @throws ClassNotFoundException if no driver could be found for the datasource * @throws SQLException when initialization of a SQL datasource failed + * @throws IOException if flat file cannot be read * @see AuthMe#database */ - public void setupDatabase(NewSetting settings) throws ClassNotFoundException, SQLException { + public void setupDatabase(NewSetting settings) throws ClassNotFoundException, SQLException, IOException { if (this.database != null) { this.database.close(); } @@ -518,7 +521,7 @@ public class AuthMe extends JavaPlugin { DataSource dataSource; switch (dataSourceType) { case FILE: - dataSource = new FlatFile(); + dataSource = new FlatFile(this); break; case MYSQL: dataSource = new MySQL(settings); @@ -530,10 +533,10 @@ public class AuthMe extends JavaPlugin { throw new UnsupportedOperationException("Unknown data source type '" + dataSourceType + "'"); } - DataSource convertedSource = MigrationService.convertFlatfileToSqlite(newSettings, dataSource); + DataSource convertedSource = MigrationService.convertFlatfileToSqlite(settings, dataSource); dataSource = convertedSource == null ? dataSource : convertedSource; - if (newSettings.getProperty(DatabaseSettings.USE_CACHING)) { + if (settings.getProperty(DatabaseSettings.USE_CACHING)) { dataSource = new CacheDataSource(dataSource); } @@ -560,11 +563,11 @@ public class AuthMe extends JavaPlugin { } // Save Player Data - private void savePlayer(Player player, LimboCache limboCache, AuthGroupHandler authGroupHandler) { - if (safeIsNpc(player) || Utils.isUnrestricted(player)) { + private void savePlayer(Player player, LimboCache limboCache, ValidationService validationService) { + final String name = player.getName().toLowerCase(); + if (safeIsNpc(player) || validationService.isUnrestricted(name)) { return; } - String name = player.getName().toLowerCase(); if (limboCache.hasPlayerData(name)) { limboCache.restoreData(player); limboCache.removeFromCache(player); @@ -585,7 +588,7 @@ public class AuthMe extends JavaPlugin { } } } - PlayerCache.getInstance().removePlayer(name); + playerCache.removePlayer(name); } private boolean safeIsNpc(Player player) { @@ -622,7 +625,7 @@ public class AuthMe extends JavaPlugin { .replace("{ONLINE}", playersOnline) .replace("{MAXPLAYERS}", Integer.toString(server.getMaxPlayers())) .replace("{IP}", ipAddress) - .replace("{LOGINS}", Integer.toString(PlayerCache.getInstance().getLogged())) + .replace("{LOGINS}", Integer.toString(playerCache.getLogged())) .replace("{WORLD}", player.getWorld().getName()) .replace("{SERVER}", server.getServerName()) .replace("{VERSION}", server.getBukkitVersion()) diff --git a/src/main/java/fr/xephi/authme/api/API.java b/src/main/java/fr/xephi/authme/api/API.java index 937d4f12..8757bcd5 100644 --- a/src/main/java/fr/xephi/authme/api/API.java +++ b/src/main/java/fr/xephi/authme/api/API.java @@ -8,7 +8,7 @@ import fr.xephi.authme.hooks.PluginHooks; import fr.xephi.authme.process.Management; import fr.xephi.authme.security.PasswordSecurity; import fr.xephi.authme.security.crypts.HashedPassword; -import fr.xephi.authme.util.Utils; +import fr.xephi.authme.util.ValidationService; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.entity.Player; @@ -29,18 +29,20 @@ public class API { private static PasswordSecurity passwordSecurity; private static Management management; private static PluginHooks pluginHooks; + private static ValidationService validationService; /* * Constructor. */ @Inject API(AuthMe instance, DataSource dataSource, PasswordSecurity passwordSecurity, Management management, - PluginHooks pluginHooks) { + PluginHooks pluginHooks, ValidationService validationService) { API.instance = instance; API.dataSource = dataSource; API.passwordSecurity = passwordSecurity; API.management = management; API.pluginHooks = pluginHooks; + API.validationService = validationService; } /** @@ -77,7 +79,7 @@ public class API { * @return true if the player is unrestricted */ public static boolean isUnrestricted(Player player) { - return Utils.isUnrestricted(player); + return validationService.isUnrestricted(player.getName()); } public static Location getLastLocation(Player player) { diff --git a/src/main/java/fr/xephi/authme/api/NewAPI.java b/src/main/java/fr/xephi/authme/api/NewAPI.java index 2343007b..7acff099 100644 --- a/src/main/java/fr/xephi/authme/api/NewAPI.java +++ b/src/main/java/fr/xephi/authme/api/NewAPI.java @@ -8,7 +8,7 @@ import fr.xephi.authme.hooks.PluginHooks; import fr.xephi.authme.process.Management; import fr.xephi.authme.security.PasswordSecurity; import fr.xephi.authme.security.crypts.HashedPassword; -import fr.xephi.authme.util.Utils; +import fr.xephi.authme.util.ValidationService; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.entity.Player; @@ -29,18 +29,20 @@ public class NewAPI { private final DataSource dataSource; private final PasswordSecurity passwordSecurity; private final Management management; + private final ValidationService validationService; /* * Constructor for NewAPI. */ @Inject NewAPI(AuthMe plugin, PluginHooks pluginHooks, DataSource dataSource, PasswordSecurity passwordSecurity, - Management management) { + Management management, ValidationService validationService) { this.plugin = plugin; this.pluginHooks = pluginHooks; this.dataSource = dataSource; this.passwordSecurity = passwordSecurity; this.management = management; + this.validationService = validationService; NewAPI.singleton = this; } @@ -106,7 +108,7 @@ public class NewAPI { * @see fr.xephi.authme.settings.properties.RestrictionSettings#UNRESTRICTED_NAMES */ public boolean isUnrestricted(Player player) { - return Utils.isUnrestricted(player); + return validationService.isUnrestricted(player.getName()); } /** diff --git a/src/main/java/fr/xephi/authme/datasource/FlatFile.java b/src/main/java/fr/xephi/authme/datasource/FlatFile.java index 60a49fe0..907d8573 100644 --- a/src/main/java/fr/xephi/authme/datasource/FlatFile.java +++ b/src/main/java/fr/xephi/authme/datasource/FlatFile.java @@ -6,7 +6,6 @@ import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.security.crypts.HashedPassword; -import fr.xephi.authme.settings.Settings; import java.io.BufferedReader; import java.io.BufferedWriter; @@ -42,21 +41,11 @@ public class FlatFile implements DataSource { */ private final File source; - public FlatFile() { - AuthMe instance = AuthMe.getInstance(); - - source = new File(instance.getDataFolder(), "auths.db"); - try { - source.createNewFile(); - } catch (IOException e) { - ConsoleLogger.logException("Cannot open flatfile", e); - if (Settings.isStopEnabled) { - ConsoleLogger.warning("Can't use FLAT FILE... SHUTDOWN..."); - instance.getServer().shutdown(); - } - if (!Settings.isStopEnabled) { - instance.getServer().getPluginManager().disablePlugin(instance); - } + public FlatFile(AuthMe authMe) throws IOException { + source = new File(authMe.getDataFolder(), "auths.db"); + boolean createResult = source.createNewFile(); + if (!createResult) { + throw new IOException("Could not create file '" + source.getPath() + "'"); } } diff --git a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java index 281ee5b9..11323ada 100644 --- a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java +++ b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java @@ -13,7 +13,7 @@ import fr.xephi.authme.settings.properties.RegistrationSettings; import fr.xephi.authme.settings.properties.RestrictionSettings; import fr.xephi.authme.util.BukkitService; import fr.xephi.authme.util.TeleportationService; -import fr.xephi.authme.util.Utils; +import fr.xephi.authme.util.ValidationService; import org.bukkit.Location; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -75,6 +75,8 @@ public class AuthMePlayerListener implements Listener { private ListenerService listenerService; @Inject private TeleportationService teleportationService; + @Inject + private ValidationService validationService; @EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST) public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) { @@ -206,7 +208,7 @@ public class AuthMePlayerListener implements Listener { @EventHandler(priority = EventPriority.LOWEST) public void onPlayerLogin(PlayerLoginEvent event) { final Player player = event.getPlayer(); - if (Utils.isUnrestricted(player)) { + if (validationService.isUnrestricted(player.getName())) { return; } else if (onJoinVerifier.refusePlayerForFullServer(event)) { return; diff --git a/src/main/java/fr/xephi/authme/listener/ListenerService.java b/src/main/java/fr/xephi/authme/listener/ListenerService.java index a7ae0896..fbaac456 100644 --- a/src/main/java/fr/xephi/authme/listener/ListenerService.java +++ b/src/main/java/fr/xephi/authme/listener/ListenerService.java @@ -6,15 +6,13 @@ import fr.xephi.authme.hooks.PluginHooks; import fr.xephi.authme.initialization.SettingsDependent; import fr.xephi.authme.settings.NewSetting; import fr.xephi.authme.settings.properties.RegistrationSettings; -import fr.xephi.authme.settings.properties.RestrictionSettings; +import fr.xephi.authme.util.ValidationService; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.event.entity.EntityEvent; import org.bukkit.event.player.PlayerEvent; import javax.inject.Inject; -import java.util.HashSet; -import java.util.Set; /** * Service class for the AuthMe listeners to determine whether an event should be canceled. @@ -24,15 +22,17 @@ class ListenerService implements SettingsDependent { private final DataSource dataSource; private final PluginHooks pluginHooks; private final PlayerCache playerCache; + private final ValidationService validationService; private boolean isRegistrationForced; - private Set unrestrictedNames; @Inject - ListenerService(NewSetting settings, DataSource dataSource, PluginHooks pluginHooks, PlayerCache playerCache) { + ListenerService(NewSetting settings, DataSource dataSource, PluginHooks pluginHooks, + PlayerCache playerCache, ValidationService validationService) { this.dataSource = dataSource; this.pluginHooks = pluginHooks; this.playerCache = playerCache; + this.validationService = validationService; reload(settings); } @@ -76,8 +76,6 @@ class ListenerService implements SettingsDependent { @Override public void reload(NewSetting settings) { isRegistrationForced = settings.getProperty(RegistrationSettings.FORCE); - // Keep unrestricted names as Set for more efficient contains() - unrestrictedNames = new HashSet<>(settings.getProperty(RestrictionSettings.UNRESTRICTED_NAMES)); } /** @@ -88,7 +86,7 @@ class ListenerService implements SettingsDependent { * @return true if the player may play, false otherwise */ private boolean checkAuth(String name) { - if (isUnrestricted(name) || playerCache.isAuthenticated(name)) { + if (validationService.isUnrestricted(name) || playerCache.isAuthenticated(name)) { return true; } if (!isRegistrationForced && !dataSource.isAuthAvailable(name)) { @@ -96,14 +94,4 @@ class ListenerService implements SettingsDependent { } return false; } - - /** - * Checks if the name is unrestricted according to the configured settings. - * - * @param name the name to verify - * @return true if unrestricted, false otherwise - */ - private boolean isUnrestricted(String name) { - return unrestrictedNames.contains(name.toLowerCase()); - } } diff --git a/src/main/java/fr/xephi/authme/permission/AuthGroupHandler.java b/src/main/java/fr/xephi/authme/permission/AuthGroupHandler.java index 332d2137..508bb1ac 100644 --- a/src/main/java/fr/xephi/authme/permission/AuthGroupHandler.java +++ b/src/main/java/fr/xephi/authme/permission/AuthGroupHandler.java @@ -3,6 +3,7 @@ package fr.xephi.authme.permission; import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.cache.limbo.LimboCache; import fr.xephi.authme.cache.limbo.PlayerData; +import fr.xephi.authme.initialization.Reloadable; import fr.xephi.authme.settings.NewSetting; import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.properties.PluginSettings; @@ -15,7 +16,7 @@ import java.util.Arrays; /** * Changes the permission group according to the auth status of the player and the configuration. */ -public class AuthGroupHandler { +public class AuthGroupHandler implements Reloadable { @Inject private PermissionsManager permissionsManager; @@ -26,6 +27,10 @@ public class AuthGroupHandler { @Inject private LimboCache limboCache; + private String unloggedInGroup; + private String unregisteredGroup; + private String registeredGroup; + AuthGroupHandler() { } /** @@ -52,18 +57,18 @@ public class AuthGroupHandler { switch (group) { case UNREGISTERED: // Remove the other group type groups, set the current group - permissionsManager.removeGroups(player, Arrays.asList(Settings.getRegisteredGroup, settings.getProperty(SecuritySettings.UNLOGGEDIN_GROUP))); - return permissionsManager.addGroup(player, Settings.unRegisteredGroup); + permissionsManager.removeGroups(player, Arrays.asList(registeredGroup, unloggedInGroup)); + return permissionsManager.addGroup(player, unregisteredGroup); case REGISTERED: // Remove the other group type groups, set the current group - permissionsManager.removeGroups(player, Arrays.asList(Settings.unRegisteredGroup, settings.getProperty(SecuritySettings.UNLOGGEDIN_GROUP))); - return permissionsManager.addGroup(player, Settings.getRegisteredGroup); + permissionsManager.removeGroups(player, Arrays.asList(unregisteredGroup, unloggedInGroup)); + return permissionsManager.addGroup(player, registeredGroup); case NOT_LOGGED_IN: // Remove the other group type groups, set the current group - permissionsManager.removeGroups(player, Arrays.asList(Settings.unRegisteredGroup, Settings.getRegisteredGroup)); - return permissionsManager.addGroup(player, settings.getProperty(SecuritySettings.UNLOGGEDIN_GROUP)); + permissionsManager.removeGroups(player, Arrays.asList(unregisteredGroup, registeredGroup)); + return permissionsManager.addGroup(player, unloggedInGroup); case LOGGED_IN: // Get the player data @@ -77,7 +82,7 @@ public class AuthGroupHandler { // Remove the other group types groups, set the real group permissionsManager.removeGroups(player, - Arrays.asList(Settings.unRegisteredGroup, Settings.getRegisteredGroup, settings.getProperty(SecuritySettings.UNLOGGEDIN_GROUP)) + Arrays.asList(unregisteredGroup, registeredGroup, unloggedInGroup) ); return permissionsManager.addGroup(player, realGroup); default: @@ -102,11 +107,17 @@ public class AuthGroupHandler { } // Remove old groups - permissionsManager.removeGroups(player, Arrays.asList(Settings.unRegisteredGroup, - Settings.getRegisteredGroup, Settings.getUnloggedinGroup)); + permissionsManager.removeGroups(player, Arrays.asList(unregisteredGroup, registeredGroup, unloggedInGroup)); // Add the normal group, return the result return permissionsManager.addGroup(player, group); } + @Override + public void reload() { + unloggedInGroup = settings.getProperty(SecuritySettings.UNLOGGEDIN_GROUP); + unregisteredGroup = Settings.unRegisteredGroup; + registeredGroup = Settings.getRegisteredGroup; + } + } diff --git a/src/main/java/fr/xephi/authme/process/login/AsynchronousLogin.java b/src/main/java/fr/xephi/authme/process/login/AsynchronousLogin.java index 8e481f1a..cb4df02c 100644 --- a/src/main/java/fr/xephi/authme/process/login/AsynchronousLogin.java +++ b/src/main/java/fr/xephi/authme/process/login/AsynchronousLogin.java @@ -18,9 +18,9 @@ import fr.xephi.authme.process.AsynchronousProcess; import fr.xephi.authme.process.ProcessService; import fr.xephi.authme.process.SyncProcessManager; import fr.xephi.authme.security.PasswordSecurity; -import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.properties.DatabaseSettings; import fr.xephi.authme.settings.properties.EmailSettings; +import fr.xephi.authme.settings.properties.HooksSettings; import fr.xephi.authme.settings.properties.RestrictionSettings; import fr.xephi.authme.task.PlayerDataTaskManager; import fr.xephi.authme.util.BukkitService; @@ -94,7 +94,8 @@ public class AsynchronousLogin implements AsynchronousProcess { return null; } - if (!service.getProperty(DatabaseSettings.MYSQL_COL_GROUP).isEmpty() && pAuth.getGroupId() == Settings.getNonActivatedGroup) { + if (!service.getProperty(DatabaseSettings.MYSQL_COL_GROUP).isEmpty() + && pAuth.getGroupId() == service.getProperty(HooksSettings.NON_ACTIVATED_USERS_GROUP)) { service.send(player, MessageKey.ACCOUNT_NOT_ACTIVATED); return null; } diff --git a/src/main/java/fr/xephi/authme/process/quit/AsynchronousQuit.java b/src/main/java/fr/xephi/authme/process/quit/AsynchronousQuit.java index 4a2985dc..cc7eb60f 100644 --- a/src/main/java/fr/xephi/authme/process/quit/AsynchronousQuit.java +++ b/src/main/java/fr/xephi/authme/process/quit/AsynchronousQuit.java @@ -12,6 +12,7 @@ import fr.xephi.authme.process.SyncProcessManager; import fr.xephi.authme.settings.SpawnLoader; import fr.xephi.authme.settings.properties.RestrictionSettings; import fr.xephi.authme.util.Utils; +import fr.xephi.authme.util.ValidationService; import org.bukkit.Location; import org.bukkit.entity.Player; @@ -40,12 +41,15 @@ public class AsynchronousQuit implements AsynchronousProcess { @Inject private SpawnLoader spawnLoader; + @Inject + private ValidationService validationService; + AsynchronousQuit() { } public void processQuit(Player player) { - if (player == null || Utils.isUnrestricted(player)) { + if (player == null || validationService.isUnrestricted(player.getName())) { return; } final String name = player.getName().toLowerCase(); diff --git a/src/main/java/fr/xephi/authme/settings/Settings.java b/src/main/java/fr/xephi/authme/settings/Settings.java index 5682fae2..c35b2a8c 100644 --- a/src/main/java/fr/xephi/authme/settings/Settings.java +++ b/src/main/java/fr/xephi/authme/settings/Settings.java @@ -1,28 +1,16 @@ package fr.xephi.authme.settings; import fr.xephi.authme.AuthMe; -import fr.xephi.authme.settings.domain.Property; -import fr.xephi.authme.settings.properties.RestrictionSettings; -import fr.xephi.authme.settings.properties.SecuritySettings; import org.bukkit.configuration.file.FileConfiguration; -import java.util.List; - /** * Old settings manager. See {@link NewSetting} for the new manager. */ @Deprecated public final class Settings { - public static List getUnrestrictedName; - public static boolean isAllowRestrictedIp; - public static boolean isStopEnabled; - public static boolean reloadSupport; - public static String getUnloggedinGroup; public static String unRegisteredGroup; public static String getRegisteredGroup; - public static int getNonActivatedGroup; - private static FileConfiguration configFile; /** * Constructor for Settings. @@ -30,29 +18,8 @@ public final class Settings { * @param pl AuthMe */ public Settings(AuthMe pl) { - configFile = pl.getConfig(); - loadVariables(); - } - - private static void loadVariables() { - isAllowRestrictedIp = load(RestrictionSettings.ENABLE_RESTRICTED_USERS); - getUnloggedinGroup = load(SecuritySettings.UNLOGGEDIN_GROUP); - getNonActivatedGroup = configFile.getInt("ExternalBoardOptions.nonActivedUserGroup", -1); + FileConfiguration configFile = pl.getConfig(); unRegisteredGroup = configFile.getString("GroupOptions.UnregisteredPlayerGroup", ""); - getUnrestrictedName = load(RestrictionSettings.UNRESTRICTED_NAMES); getRegisteredGroup = configFile.getString("GroupOptions.RegisteredPlayerGroup", ""); - isStopEnabled = configFile.getBoolean("Security.SQLProblem.stopServer", true); - reloadSupport = configFile.getBoolean("Security.ReloadCommand.useReloadCommandSupport", true); - } - - /** - * Load the value via the new Property setup for temporary support within this old settings manager. - * - * @param property The property to load - * @param The property type - * @return The config value of the property - */ - private static T load(Property property) { - return property.getFromFile(configFile); } } diff --git a/src/main/java/fr/xephi/authme/util/Utils.java b/src/main/java/fr/xephi/authme/util/Utils.java index da05edb6..99008627 100644 --- a/src/main/java/fr/xephi/authme/util/Utils.java +++ b/src/main/java/fr/xephi/authme/util/Utils.java @@ -1,7 +1,6 @@ package fr.xephi.authme.util; import fr.xephi.authme.ConsoleLogger; -import fr.xephi.authme.settings.Settings; import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; @@ -15,14 +14,6 @@ public final class Utils { private Utils() { } - @Deprecated - public static boolean isUnrestricted(Player player) { - // TODO ljacqu 20160602: Checking for Settings.isAllowRestrictedIp is wrong! Nothing in the config suggests - // that this setting has anything to do with unrestricted names - return Settings.isAllowRestrictedIp - && Settings.getUnrestrictedName.contains(player.getName().toLowerCase()); - } - /** * Get player's UUID if can, name otherwise. * diff --git a/src/main/java/fr/xephi/authme/util/ValidationService.java b/src/main/java/fr/xephi/authme/util/ValidationService.java index f2f1b0e5..283a3eb1 100644 --- a/src/main/java/fr/xephi/authme/util/ValidationService.java +++ b/src/main/java/fr/xephi/authme/util/ValidationService.java @@ -16,7 +16,9 @@ import org.bukkit.command.CommandSender; import javax.annotation.PostConstruct; import javax.inject.Inject; import java.util.Collection; +import java.util.HashSet; import java.util.List; +import java.util.Set; import java.util.regex.Pattern; /** @@ -34,6 +36,7 @@ public class ValidationService implements Reloadable { private GeoLiteAPI geoLiteApi; private Pattern passwordRegex; + private Set unrestrictedNames; ValidationService() { } @@ -41,6 +44,8 @@ public class ValidationService implements Reloadable { @Override public void reload() { passwordRegex = Utils.safePatternCompile(settings.getProperty(RestrictionSettings.ALLOWED_PASSWORD_REGEX)); + // Use Set for more efficient contains() lookup + unrestrictedNames = new HashSet<>(settings.getProperty(RestrictionSettings.UNRESTRICTED_NAMES)); } /** @@ -113,6 +118,16 @@ public class ValidationService implements Reloadable { ProtectionSettings.COUNTRIES_BLACKLIST); } + /** + * Checks if the name is unrestricted according to the configured settings. + * + * @param name the name to verify + * @return true if unrestricted, false otherwise + */ + public boolean isUnrestricted(String name) { + return unrestrictedNames.contains(name.toLowerCase()); + } + /** * Verifies whether the given value is allowed according to the given whitelist and blacklist settings. * Whitelist has precedence over blacklist: if a whitelist is set, the value is rejected if not present diff --git a/src/test/java/fr/xephi/authme/listener/ListenerServiceTest.java b/src/test/java/fr/xephi/authme/listener/ListenerServiceTest.java index b2afbe23..942158e9 100644 --- a/src/test/java/fr/xephi/authme/listener/ListenerServiceTest.java +++ b/src/test/java/fr/xephi/authme/listener/ListenerServiceTest.java @@ -8,7 +8,7 @@ import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.hooks.PluginHooks; import fr.xephi.authme.settings.NewSetting; import fr.xephi.authme.settings.properties.RegistrationSettings; -import fr.xephi.authme.settings.properties.RestrictionSettings; +import fr.xephi.authme.util.ValidationService; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.event.HandlerList; @@ -18,8 +18,6 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; -import java.util.Arrays; - import static org.hamcrest.Matchers.equalTo; import static org.junit.Assert.assertThat; import static org.mockito.BDDMockito.given; @@ -48,11 +46,12 @@ public class ListenerServiceTest { @Mock private PlayerCache playerCache; + @Mock + private ValidationService validationService; + @BeforeInjecting public void initializeDefaultSettings() { given(settings.getProperty(RegistrationSettings.FORCE)).willReturn(true); - given(settings.getProperty(RestrictionSettings.UNRESTRICTED_NAMES)).willReturn( - Arrays.asList("npc1", "npc2", "npc3")); } @Test @@ -145,6 +144,7 @@ public class ListenerServiceTest { Player player = mockPlayerWithName(playerName); EntityEvent event = mock(EntityEvent.class); given(event.getEntity()).willReturn(player); + given(validationService.isUnrestricted(playerName)).willReturn(true); // when boolean result = listenerService.shouldCancelEvent(event); diff --git a/src/test/java/fr/xephi/authme/util/ValidationServiceTest.java b/src/test/java/fr/xephi/authme/util/ValidationServiceTest.java index 9b7d34f0..3c9fd5d2 100644 --- a/src/test/java/fr/xephi/authme/util/ValidationServiceTest.java +++ b/src/test/java/fr/xephi/authme/util/ValidationServiceTest.java @@ -51,6 +51,7 @@ public class ValidationServiceTest { given(settings.getProperty(SecuritySettings.UNSAFE_PASSWORDS)) .willReturn(Arrays.asList("unsafe", "other-unsafe")); given(settings.getProperty(EmailSettings.MAX_REG_PER_EMAIL)).willReturn(3); + given(settings.getProperty(RestrictionSettings.UNRESTRICTED_NAMES)).willReturn(Arrays.asList("name01", "npc")); } @Test @@ -189,6 +190,7 @@ public class ValidationServiceTest { assertThat(validationService.validateEmail("your@email.com"), equalTo(false)); } + @Test public void shouldAllowRegistration() { // given CommandSender sender = mock(CommandSender.class); @@ -204,6 +206,7 @@ public class ValidationServiceTest { assertThat(result, equalTo(true)); } + @Test public void shouldRejectEmailWithTooManyAccounts() { // given CommandSender sender = mock(CommandSender.class); @@ -219,6 +222,7 @@ public class ValidationServiceTest { assertThat(result, equalTo(false)); } + @Test public void shouldAllowBypassForPresentPermission() { // given CommandSender sender = mock(CommandSender.class); @@ -234,6 +238,19 @@ public class ValidationServiceTest { assertThat(result, equalTo(true)); } + @Test + public void shouldRecognizeUnrestrictedNames() { + assertThat(validationService.isUnrestricted("npc"), equalTo(true)); + assertThat(validationService.isUnrestricted("someplayer"), equalTo(false)); + assertThat(validationService.isUnrestricted("NAME01"), equalTo(true)); + + // Check reloading + given(settings.getProperty(RestrictionSettings.UNRESTRICTED_NAMES)).willReturn(Arrays.asList("new", "names")); + validationService.reload(); + assertThat(validationService.isUnrestricted("npc"), equalTo(false)); + assertThat(validationService.isUnrestricted("New"), equalTo(true)); + } + private static void assertErrorEquals(ValidationResult validationResult, MessageKey messageKey, String... args) { assertThat(validationResult.hasError(), equalTo(true)); assertThat(validationResult.getMessageKey(), equalTo(messageKey));