#449 Migrate remaining non-group legacy Settings

This commit is contained in:
ljacqu 2016-07-23 14:44:52 +02:00
parent 476e0c197d
commit 18a9fbaa26
14 changed files with 119 additions and 127 deletions

View File

@ -30,11 +30,9 @@ import fr.xephi.authme.output.ConsoleFilter;
import fr.xephi.authme.output.Log4JFilter; import fr.xephi.authme.output.Log4JFilter;
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.permission.AuthGroupHandler;
import fr.xephi.authme.permission.PermissionsManager; import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.permission.PermissionsSystemType; import fr.xephi.authme.permission.PermissionsSystemType;
import fr.xephi.authme.process.Management; import fr.xephi.authme.process.Management;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.SHA256; import fr.xephi.authme.security.crypts.SHA256;
import fr.xephi.authme.settings.NewSetting; import fr.xephi.authme.settings.NewSetting;
import fr.xephi.authme.settings.Settings; 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.MigrationService;
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 fr.xephi.authme.util.ValidationService;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
@ -71,6 +70,7 @@ import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.scheduler.BukkitWorker; import org.bukkit.scheduler.BukkitWorker;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
@ -112,6 +112,7 @@ public class AuthMe extends JavaPlugin {
private BukkitService bukkitService; private BukkitService bukkitService;
private Injector injector; private Injector injector;
private GeoLiteAPI geoLiteApi; private GeoLiteAPI geoLiteApi;
private PlayerCache playerCache;
/** /**
* Constructor. * Constructor.
@ -281,18 +282,20 @@ public class AuthMe extends JavaPlugin {
} }
protected void instantiateServices(Injector injector) { protected void instantiateServices(Injector injector) {
// Some statically injected things // PlayerCache is still injected statically sometimes
injector.register(PlayerCache.class, PlayerCache.getInstance()); playerCache = PlayerCache.getInstance();
injector.register(PlayerCache.class, playerCache);
messages = injector.getSingleton(Messages.class); messages = injector.getSingleton(Messages.class);
permsMan = injector.getSingleton(PermissionsManager.class); permsMan = injector.getSingleton(PermissionsManager.class);
bukkitService = injector.getSingleton(BukkitService.class); bukkitService = injector.getSingleton(BukkitService.class);
pluginHooks = injector.getSingleton(PluginHooks.class); pluginHooks = injector.getSingleton(PluginHooks.class);
injector.getSingleton(PasswordSecurity.class);
spawnLoader = injector.getSingleton(SpawnLoader.class); spawnLoader = injector.getSingleton(SpawnLoader.class);
commandHandler = injector.getSingleton(CommandHandler.class); commandHandler = injector.getSingleton(CommandHandler.class);
management = injector.getSingleton(Management.class); management = injector.getSingleton(Management.class);
geoLiteApi = injector.getSingleton(GeoLiteAPI.class); geoLiteApi = injector.getSingleton(GeoLiteAPI.class);
// Trigger construction of API classes; they will keep track of the singleton
injector.getSingleton(NewAPI.class); injector.getSingleton(NewAPI.class);
injector.getSingleton(API.class); injector.getSingleton(API.class);
} }
@ -346,14 +349,13 @@ public class AuthMe extends JavaPlugin {
int playersOnline = bukkitService.getOnlinePlayers().size(); int playersOnline = bukkitService.getOnlinePlayers().size();
if (playersOnline < 1) { if (playersOnline < 1) {
database.purgeLogged(); database.purgeLogged();
} else if (Settings.reloadSupport) { } else if (newSettings.getProperty(SecuritySettings.USE_RELOAD_COMMAND_SUPPORT)) {
for (PlayerAuth auth : database.getLoggedPlayers()) { for (PlayerAuth auth : database.getLoggedPlayers()) {
if (auth == null) { if (auth != null) {
continue; 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 // Save player data
BukkitService bukkitService = injector.getIfAvailable(BukkitService.class); BukkitService bukkitService = injector.getIfAvailable(BukkitService.class);
LimboCache limboCache = injector.getIfAvailable(LimboCache.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<? extends Player> players = bukkitService.getOnlinePlayers(); Collection<? extends Player> players = bukkitService.getOnlinePlayers();
for (Player player : players) { 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 // Stop/unload the server/plugin as defined in the configuration
public void stopOrUnload() { 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!"); ConsoleLogger.warning("THE SERVER IS GOING TO SHUT DOWN AS DEFINED IN THE CONFIGURATION!");
getServer().shutdown(); getServer().shutdown();
} else { } else {
@ -507,9 +509,10 @@ public class AuthMe extends JavaPlugin {
* *
* @throws ClassNotFoundException if no driver could be found for the datasource * @throws ClassNotFoundException if no driver could be found for the datasource
* @throws SQLException when initialization of a SQL datasource failed * @throws SQLException when initialization of a SQL datasource failed
* @throws IOException if flat file cannot be read
* @see AuthMe#database * @see AuthMe#database
*/ */
public void setupDatabase(NewSetting settings) throws ClassNotFoundException, SQLException { public void setupDatabase(NewSetting settings) throws ClassNotFoundException, SQLException, IOException {
if (this.database != null) { if (this.database != null) {
this.database.close(); this.database.close();
} }
@ -518,7 +521,7 @@ public class AuthMe extends JavaPlugin {
DataSource dataSource; DataSource dataSource;
switch (dataSourceType) { switch (dataSourceType) {
case FILE: case FILE:
dataSource = new FlatFile(); dataSource = new FlatFile(this);
break; break;
case MYSQL: case MYSQL:
dataSource = new MySQL(settings); dataSource = new MySQL(settings);
@ -530,10 +533,10 @@ public class AuthMe extends JavaPlugin {
throw new UnsupportedOperationException("Unknown data source type '" + dataSourceType + "'"); 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; dataSource = convertedSource == null ? dataSource : convertedSource;
if (newSettings.getProperty(DatabaseSettings.USE_CACHING)) { if (settings.getProperty(DatabaseSettings.USE_CACHING)) {
dataSource = new CacheDataSource(dataSource); dataSource = new CacheDataSource(dataSource);
} }
@ -560,11 +563,11 @@ public class AuthMe extends JavaPlugin {
} }
// Save Player Data // Save Player Data
private void savePlayer(Player player, LimboCache limboCache, AuthGroupHandler authGroupHandler) { private void savePlayer(Player player, LimboCache limboCache, ValidationService validationService) {
if (safeIsNpc(player) || Utils.isUnrestricted(player)) { final String name = player.getName().toLowerCase();
if (safeIsNpc(player) || validationService.isUnrestricted(name)) {
return; return;
} }
String name = player.getName().toLowerCase();
if (limboCache.hasPlayerData(name)) { if (limboCache.hasPlayerData(name)) {
limboCache.restoreData(player); limboCache.restoreData(player);
limboCache.removeFromCache(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) { private boolean safeIsNpc(Player player) {
@ -622,7 +625,7 @@ public class AuthMe extends JavaPlugin {
.replace("{ONLINE}", playersOnline) .replace("{ONLINE}", playersOnline)
.replace("{MAXPLAYERS}", Integer.toString(server.getMaxPlayers())) .replace("{MAXPLAYERS}", Integer.toString(server.getMaxPlayers()))
.replace("{IP}", ipAddress) .replace("{IP}", ipAddress)
.replace("{LOGINS}", Integer.toString(PlayerCache.getInstance().getLogged())) .replace("{LOGINS}", Integer.toString(playerCache.getLogged()))
.replace("{WORLD}", player.getWorld().getName()) .replace("{WORLD}", player.getWorld().getName())
.replace("{SERVER}", server.getServerName()) .replace("{SERVER}", server.getServerName())
.replace("{VERSION}", server.getBukkitVersion()) .replace("{VERSION}", server.getBukkitVersion())

View File

@ -8,7 +8,7 @@ import fr.xephi.authme.hooks.PluginHooks;
import fr.xephi.authme.process.Management; import fr.xephi.authme.process.Management;
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.util.Utils; import fr.xephi.authme.util.ValidationService;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -29,18 +29,20 @@ public class API {
private static PasswordSecurity passwordSecurity; private static PasswordSecurity passwordSecurity;
private static Management management; private static Management management;
private static PluginHooks pluginHooks; private static PluginHooks pluginHooks;
private static ValidationService validationService;
/* /*
* Constructor. * Constructor.
*/ */
@Inject @Inject
API(AuthMe instance, DataSource dataSource, PasswordSecurity passwordSecurity, Management management, API(AuthMe instance, DataSource dataSource, PasswordSecurity passwordSecurity, Management management,
PluginHooks pluginHooks) { PluginHooks pluginHooks, ValidationService validationService) {
API.instance = instance; API.instance = instance;
API.dataSource = dataSource; API.dataSource = dataSource;
API.passwordSecurity = passwordSecurity; API.passwordSecurity = passwordSecurity;
API.management = management; API.management = management;
API.pluginHooks = pluginHooks; API.pluginHooks = pluginHooks;
API.validationService = validationService;
} }
/** /**
@ -77,7 +79,7 @@ public class API {
* @return true if the player is unrestricted * @return true if the player is unrestricted
*/ */
public static boolean isUnrestricted(Player player) { public static boolean isUnrestricted(Player player) {
return Utils.isUnrestricted(player); return validationService.isUnrestricted(player.getName());
} }
public static Location getLastLocation(Player player) { public static Location getLastLocation(Player player) {

View File

@ -8,7 +8,7 @@ import fr.xephi.authme.hooks.PluginHooks;
import fr.xephi.authme.process.Management; import fr.xephi.authme.process.Management;
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.util.Utils; import fr.xephi.authme.util.ValidationService;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -29,18 +29,20 @@ public class NewAPI {
private final DataSource dataSource; private final DataSource dataSource;
private final PasswordSecurity passwordSecurity; private final PasswordSecurity passwordSecurity;
private final Management management; private final Management management;
private final ValidationService validationService;
/* /*
* Constructor for NewAPI. * Constructor for NewAPI.
*/ */
@Inject @Inject
NewAPI(AuthMe plugin, PluginHooks pluginHooks, DataSource dataSource, PasswordSecurity passwordSecurity, NewAPI(AuthMe plugin, PluginHooks pluginHooks, DataSource dataSource, PasswordSecurity passwordSecurity,
Management management) { Management management, ValidationService validationService) {
this.plugin = plugin; this.plugin = plugin;
this.pluginHooks = pluginHooks; this.pluginHooks = pluginHooks;
this.dataSource = dataSource; this.dataSource = dataSource;
this.passwordSecurity = passwordSecurity; this.passwordSecurity = passwordSecurity;
this.management = management; this.management = management;
this.validationService = validationService;
NewAPI.singleton = this; NewAPI.singleton = this;
} }
@ -106,7 +108,7 @@ public class NewAPI {
* @see fr.xephi.authme.settings.properties.RestrictionSettings#UNRESTRICTED_NAMES * @see fr.xephi.authme.settings.properties.RestrictionSettings#UNRESTRICTED_NAMES
*/ */
public boolean isUnrestricted(Player player) { public boolean isUnrestricted(Player player) {
return Utils.isUnrestricted(player); return validationService.isUnrestricted(player.getName());
} }
/** /**

View File

@ -6,7 +6,6 @@ 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.security.crypts.HashedPassword; import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.settings.Settings;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.BufferedWriter; import java.io.BufferedWriter;
@ -42,21 +41,11 @@ public class FlatFile implements DataSource {
*/ */
private final File source; private final File source;
public FlatFile() { public FlatFile(AuthMe authMe) throws IOException {
AuthMe instance = AuthMe.getInstance(); source = new File(authMe.getDataFolder(), "auths.db");
boolean createResult = source.createNewFile();
source = new File(instance.getDataFolder(), "auths.db"); if (!createResult) {
try { throw new IOException("Could not create file '" + source.getPath() + "'");
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);
}
} }
} }

View File

@ -13,7 +13,7 @@ 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.util.BukkitService; import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.util.TeleportationService; 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.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -75,6 +75,8 @@ public class AuthMePlayerListener implements Listener {
private ListenerService listenerService; private ListenerService listenerService;
@Inject @Inject
private TeleportationService teleportationService; private TeleportationService teleportationService;
@Inject
private ValidationService validationService;
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST) @EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) { public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
@ -206,7 +208,7 @@ public class AuthMePlayerListener implements Listener {
@EventHandler(priority = EventPriority.LOWEST) @EventHandler(priority = EventPriority.LOWEST)
public void onPlayerLogin(PlayerLoginEvent event) { public void onPlayerLogin(PlayerLoginEvent event) {
final Player player = event.getPlayer(); final Player player = event.getPlayer();
if (Utils.isUnrestricted(player)) { if (validationService.isUnrestricted(player.getName())) {
return; return;
} else if (onJoinVerifier.refusePlayerForFullServer(event)) { } else if (onJoinVerifier.refusePlayerForFullServer(event)) {
return; return;

View File

@ -6,15 +6,13 @@ import fr.xephi.authme.hooks.PluginHooks;
import fr.xephi.authme.initialization.SettingsDependent; import fr.xephi.authme.initialization.SettingsDependent;
import fr.xephi.authme.settings.NewSetting; import fr.xephi.authme.settings.NewSetting;
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.util.ValidationService;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityEvent; import org.bukkit.event.entity.EntityEvent;
import org.bukkit.event.player.PlayerEvent; import org.bukkit.event.player.PlayerEvent;
import javax.inject.Inject; 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. * 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 DataSource dataSource;
private final PluginHooks pluginHooks; private final PluginHooks pluginHooks;
private final PlayerCache playerCache; private final PlayerCache playerCache;
private final ValidationService validationService;
private boolean isRegistrationForced; private boolean isRegistrationForced;
private Set<String> unrestrictedNames;
@Inject @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.dataSource = dataSource;
this.pluginHooks = pluginHooks; this.pluginHooks = pluginHooks;
this.playerCache = playerCache; this.playerCache = playerCache;
this.validationService = validationService;
reload(settings); reload(settings);
} }
@ -76,8 +76,6 @@ class ListenerService implements SettingsDependent {
@Override @Override
public void reload(NewSetting settings) { public void reload(NewSetting settings) {
isRegistrationForced = settings.getProperty(RegistrationSettings.FORCE); 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 * @return true if the player may play, false otherwise
*/ */
private boolean checkAuth(String name) { private boolean checkAuth(String name) {
if (isUnrestricted(name) || playerCache.isAuthenticated(name)) { if (validationService.isUnrestricted(name) || playerCache.isAuthenticated(name)) {
return true; return true;
} }
if (!isRegistrationForced && !dataSource.isAuthAvailable(name)) { if (!isRegistrationForced && !dataSource.isAuthAvailable(name)) {
@ -96,14 +94,4 @@ class ListenerService implements SettingsDependent {
} }
return false; 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());
}
} }

View File

@ -3,6 +3,7 @@ package fr.xephi.authme.permission;
import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.limbo.LimboCache; import fr.xephi.authme.cache.limbo.LimboCache;
import fr.xephi.authme.cache.limbo.PlayerData; import fr.xephi.authme.cache.limbo.PlayerData;
import fr.xephi.authme.initialization.Reloadable;
import fr.xephi.authme.settings.NewSetting; import fr.xephi.authme.settings.NewSetting;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.PluginSettings; 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. * Changes the permission group according to the auth status of the player and the configuration.
*/ */
public class AuthGroupHandler { public class AuthGroupHandler implements Reloadable {
@Inject @Inject
private PermissionsManager permissionsManager; private PermissionsManager permissionsManager;
@ -26,6 +27,10 @@ public class AuthGroupHandler {
@Inject @Inject
private LimboCache limboCache; private LimboCache limboCache;
private String unloggedInGroup;
private String unregisteredGroup;
private String registeredGroup;
AuthGroupHandler() { } AuthGroupHandler() { }
/** /**
@ -52,18 +57,18 @@ public class AuthGroupHandler {
switch (group) { switch (group) {
case UNREGISTERED: case UNREGISTERED:
// Remove the other group type groups, set the current group // Remove the other group type groups, set the current group
permissionsManager.removeGroups(player, Arrays.asList(Settings.getRegisteredGroup, settings.getProperty(SecuritySettings.UNLOGGEDIN_GROUP))); permissionsManager.removeGroups(player, Arrays.asList(registeredGroup, unloggedInGroup));
return permissionsManager.addGroup(player, Settings.unRegisteredGroup); return permissionsManager.addGroup(player, unregisteredGroup);
case REGISTERED: case REGISTERED:
// Remove the other group type groups, set the current group // Remove the other group type groups, set the current group
permissionsManager.removeGroups(player, Arrays.asList(Settings.unRegisteredGroup, settings.getProperty(SecuritySettings.UNLOGGEDIN_GROUP))); permissionsManager.removeGroups(player, Arrays.asList(unregisteredGroup, unloggedInGroup));
return permissionsManager.addGroup(player, Settings.getRegisteredGroup); return permissionsManager.addGroup(player, registeredGroup);
case NOT_LOGGED_IN: case NOT_LOGGED_IN:
// Remove the other group type groups, set the current group // Remove the other group type groups, set the current group
permissionsManager.removeGroups(player, Arrays.asList(Settings.unRegisteredGroup, Settings.getRegisteredGroup)); permissionsManager.removeGroups(player, Arrays.asList(unregisteredGroup, registeredGroup));
return permissionsManager.addGroup(player, settings.getProperty(SecuritySettings.UNLOGGEDIN_GROUP)); return permissionsManager.addGroup(player, unloggedInGroup);
case LOGGED_IN: case LOGGED_IN:
// Get the player data // Get the player data
@ -77,7 +82,7 @@ public class AuthGroupHandler {
// Remove the other group types groups, set the real group // Remove the other group types groups, set the real group
permissionsManager.removeGroups(player, permissionsManager.removeGroups(player,
Arrays.asList(Settings.unRegisteredGroup, Settings.getRegisteredGroup, settings.getProperty(SecuritySettings.UNLOGGEDIN_GROUP)) Arrays.asList(unregisteredGroup, registeredGroup, unloggedInGroup)
); );
return permissionsManager.addGroup(player, realGroup); return permissionsManager.addGroup(player, realGroup);
default: default:
@ -102,11 +107,17 @@ public class AuthGroupHandler {
} }
// Remove old groups // Remove old groups
permissionsManager.removeGroups(player, Arrays.asList(Settings.unRegisteredGroup, permissionsManager.removeGroups(player, Arrays.asList(unregisteredGroup, registeredGroup, unloggedInGroup));
Settings.getRegisteredGroup, Settings.getUnloggedinGroup));
// Add the normal group, return the result // Add the normal group, return the result
return permissionsManager.addGroup(player, group); return permissionsManager.addGroup(player, group);
} }
@Override
public void reload() {
unloggedInGroup = settings.getProperty(SecuritySettings.UNLOGGEDIN_GROUP);
unregisteredGroup = Settings.unRegisteredGroup;
registeredGroup = Settings.getRegisteredGroup;
}
} }

View File

@ -18,9 +18,9 @@ import fr.xephi.authme.process.AsynchronousProcess;
import fr.xephi.authme.process.ProcessService; import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.process.SyncProcessManager; import fr.xephi.authme.process.SyncProcessManager;
import fr.xephi.authme.security.PasswordSecurity; 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.DatabaseSettings;
import fr.xephi.authme.settings.properties.EmailSettings; 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.settings.properties.RestrictionSettings;
import fr.xephi.authme.task.PlayerDataTaskManager; import fr.xephi.authme.task.PlayerDataTaskManager;
import fr.xephi.authme.util.BukkitService; import fr.xephi.authme.util.BukkitService;
@ -94,7 +94,8 @@ public class AsynchronousLogin implements AsynchronousProcess {
return null; 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); service.send(player, MessageKey.ACCOUNT_NOT_ACTIVATED);
return null; return null;
} }

View File

@ -12,6 +12,7 @@ import fr.xephi.authme.process.SyncProcessManager;
import fr.xephi.authme.settings.SpawnLoader; import fr.xephi.authme.settings.SpawnLoader;
import fr.xephi.authme.settings.properties.RestrictionSettings; import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.util.Utils; import fr.xephi.authme.util.Utils;
import fr.xephi.authme.util.ValidationService;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -40,12 +41,15 @@ public class AsynchronousQuit implements AsynchronousProcess {
@Inject @Inject
private SpawnLoader spawnLoader; private SpawnLoader spawnLoader;
@Inject
private ValidationService validationService;
AsynchronousQuit() { AsynchronousQuit() {
} }
public void processQuit(Player player) { public void processQuit(Player player) {
if (player == null || Utils.isUnrestricted(player)) { if (player == null || validationService.isUnrestricted(player.getName())) {
return; return;
} }
final String name = player.getName().toLowerCase(); final String name = player.getName().toLowerCase();

View File

@ -1,28 +1,16 @@
package fr.xephi.authme.settings; package fr.xephi.authme.settings;
import fr.xephi.authme.AuthMe; 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 org.bukkit.configuration.file.FileConfiguration;
import java.util.List;
/** /**
* Old settings manager. See {@link NewSetting} for the new manager. * Old settings manager. See {@link NewSetting} for the new manager.
*/ */
@Deprecated @Deprecated
public final class Settings { public final class Settings {
public static List<String> getUnrestrictedName;
public static boolean isAllowRestrictedIp;
public static boolean isStopEnabled;
public static boolean reloadSupport;
public static String getUnloggedinGroup;
public static String unRegisteredGroup; public static String unRegisteredGroup;
public static String getRegisteredGroup; public static String getRegisteredGroup;
public static int getNonActivatedGroup;
private static FileConfiguration configFile;
/** /**
* Constructor for Settings. * Constructor for Settings.
@ -30,29 +18,8 @@ public final class Settings {
* @param pl AuthMe * @param pl AuthMe
*/ */
public Settings(AuthMe pl) { public Settings(AuthMe pl) {
configFile = pl.getConfig(); FileConfiguration 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);
unRegisteredGroup = configFile.getString("GroupOptions.UnregisteredPlayerGroup", ""); unRegisteredGroup = configFile.getString("GroupOptions.UnregisteredPlayerGroup", "");
getUnrestrictedName = load(RestrictionSettings.UNRESTRICTED_NAMES);
getRegisteredGroup = configFile.getString("GroupOptions.RegisteredPlayerGroup", ""); 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 <T> The property type
* @return The config value of the property
*/
private static <T> T load(Property<T> property) {
return property.getFromFile(configFile);
} }
} }

View File

@ -1,7 +1,6 @@
package fr.xephi.authme.util; package fr.xephi.authme.util;
import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.settings.Settings;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -15,14 +14,6 @@ public final class Utils {
private 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. * Get player's UUID if can, name otherwise.
* *

View File

@ -16,7 +16,9 @@ import org.bukkit.command.CommandSender;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.inject.Inject; import javax.inject.Inject;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.regex.Pattern; import java.util.regex.Pattern;
/** /**
@ -34,6 +36,7 @@ public class ValidationService implements Reloadable {
private GeoLiteAPI geoLiteApi; private GeoLiteAPI geoLiteApi;
private Pattern passwordRegex; private Pattern passwordRegex;
private Set<String> unrestrictedNames;
ValidationService() { } ValidationService() { }
@ -41,6 +44,8 @@ public class ValidationService implements Reloadable {
@Override @Override
public void reload() { public void reload() {
passwordRegex = Utils.safePatternCompile(settings.getProperty(RestrictionSettings.ALLOWED_PASSWORD_REGEX)); 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); 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. * 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 * Whitelist has precedence over blacklist: if a whitelist is set, the value is rejected if not present

View File

@ -8,7 +8,7 @@ import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.hooks.PluginHooks; import fr.xephi.authme.hooks.PluginHooks;
import fr.xephi.authme.settings.NewSetting; import fr.xephi.authme.settings.NewSetting;
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.util.ValidationService;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
@ -18,8 +18,6 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import java.util.Arrays;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
@ -48,11 +46,12 @@ public class ListenerServiceTest {
@Mock @Mock
private PlayerCache playerCache; private PlayerCache playerCache;
@Mock
private ValidationService validationService;
@BeforeInjecting @BeforeInjecting
public void initializeDefaultSettings() { public void initializeDefaultSettings() {
given(settings.getProperty(RegistrationSettings.FORCE)).willReturn(true); given(settings.getProperty(RegistrationSettings.FORCE)).willReturn(true);
given(settings.getProperty(RestrictionSettings.UNRESTRICTED_NAMES)).willReturn(
Arrays.asList("npc1", "npc2", "npc3"));
} }
@Test @Test
@ -145,6 +144,7 @@ public class ListenerServiceTest {
Player player = mockPlayerWithName(playerName); Player player = mockPlayerWithName(playerName);
EntityEvent event = mock(EntityEvent.class); EntityEvent event = mock(EntityEvent.class);
given(event.getEntity()).willReturn(player); given(event.getEntity()).willReturn(player);
given(validationService.isUnrestricted(playerName)).willReturn(true);
// when // when
boolean result = listenerService.shouldCancelEvent(event); boolean result = listenerService.shouldCancelEvent(event);

View File

@ -51,6 +51,7 @@ public class ValidationServiceTest {
given(settings.getProperty(SecuritySettings.UNSAFE_PASSWORDS)) given(settings.getProperty(SecuritySettings.UNSAFE_PASSWORDS))
.willReturn(Arrays.asList("unsafe", "other-unsafe")); .willReturn(Arrays.asList("unsafe", "other-unsafe"));
given(settings.getProperty(EmailSettings.MAX_REG_PER_EMAIL)).willReturn(3); given(settings.getProperty(EmailSettings.MAX_REG_PER_EMAIL)).willReturn(3);
given(settings.getProperty(RestrictionSettings.UNRESTRICTED_NAMES)).willReturn(Arrays.asList("name01", "npc"));
} }
@Test @Test
@ -189,6 +190,7 @@ public class ValidationServiceTest {
assertThat(validationService.validateEmail("your@email.com"), equalTo(false)); assertThat(validationService.validateEmail("your@email.com"), equalTo(false));
} }
@Test
public void shouldAllowRegistration() { public void shouldAllowRegistration() {
// given // given
CommandSender sender = mock(CommandSender.class); CommandSender sender = mock(CommandSender.class);
@ -204,6 +206,7 @@ public class ValidationServiceTest {
assertThat(result, equalTo(true)); assertThat(result, equalTo(true));
} }
@Test
public void shouldRejectEmailWithTooManyAccounts() { public void shouldRejectEmailWithTooManyAccounts() {
// given // given
CommandSender sender = mock(CommandSender.class); CommandSender sender = mock(CommandSender.class);
@ -219,6 +222,7 @@ public class ValidationServiceTest {
assertThat(result, equalTo(false)); assertThat(result, equalTo(false));
} }
@Test
public void shouldAllowBypassForPresentPermission() { public void shouldAllowBypassForPresentPermission() {
// given // given
CommandSender sender = mock(CommandSender.class); CommandSender sender = mock(CommandSender.class);
@ -234,6 +238,19 @@ public class ValidationServiceTest {
assertThat(result, equalTo(true)); 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) { private static void assertErrorEquals(ValidationResult validationResult, MessageKey messageKey, String... args) {
assertThat(validationResult.hasError(), equalTo(true)); assertThat(validationResult.hasError(), equalTo(true));
assertThat(validationResult.getMessageKey(), equalTo(messageKey)); assertThat(validationResult.getMessageKey(), equalTo(messageKey));