Don't purge users if unable to load permission data

This commit is contained in:
Gabriele C 2018-04-19 11:45:21 +02:00
parent ba4ed7bdd9
commit 6e16abc34e
7 changed files with 85 additions and 34 deletions

View File

@ -1,11 +1,13 @@
package fr.xephi.authme.listener; package fr.xephi.authme.listener;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.data.QuickCommandsProtectionManager; import fr.xephi.authme.data.QuickCommandsProtectionManager;
import fr.xephi.authme.data.auth.PlayerAuth; import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey; import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.message.Messages; import fr.xephi.authme.message.Messages;
import fr.xephi.authme.permission.PermissionsManager; import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.permission.handlers.PermissionLoadUserException;
import fr.xephi.authme.process.Management; import fr.xephi.authme.process.Management;
import fr.xephi.authme.service.AntiBotService; import fr.xephi.authme.service.AntiBotService;
import fr.xephi.authme.service.BukkitService; import fr.xephi.authme.service.BukkitService;
@ -259,11 +261,15 @@ public class PlayerListener implements Listener {
} }
// Keep pre-UUID compatibility // Keep pre-UUID compatibility
try {
try { try {
permissionsManager.loadUserData(event.getUniqueId()); permissionsManager.loadUserData(event.getUniqueId());
} catch (NoSuchMethodError e) { } catch (NoSuchMethodError e) {
permissionsManager.loadUserData(name); permissionsManager.loadUserData(name);
} }
} catch (PermissionLoadUserException e) {
ConsoleLogger.logException("Unable to load the permission data of user " + name, e);
}
try { try {
runOnJoinChecks(JoiningPlayer.fromName(name), event.getAddress().getHostAddress()); runOnJoinChecks(JoiningPlayer.fromName(name), event.getAddress().getHostAddress());

View File

@ -8,6 +8,7 @@ import fr.xephi.authme.permission.handlers.BPermissionsHandler;
import fr.xephi.authme.permission.handlers.LuckPermsHandler; import fr.xephi.authme.permission.handlers.LuckPermsHandler;
import fr.xephi.authme.permission.handlers.PermissionHandler; import fr.xephi.authme.permission.handlers.PermissionHandler;
import fr.xephi.authme.permission.handlers.PermissionHandlerException; import fr.xephi.authme.permission.handlers.PermissionHandlerException;
import fr.xephi.authme.permission.handlers.PermissionLoadUserException;
import fr.xephi.authme.permission.handlers.PermissionsExHandler; import fr.xephi.authme.permission.handlers.PermissionsExHandler;
import fr.xephi.authme.permission.handlers.VaultHandler; import fr.xephi.authme.permission.handlers.VaultHandler;
import fr.xephi.authme.permission.handlers.ZPermissionsHandler; import fr.xephi.authme.permission.handlers.ZPermissionsHandler;
@ -110,7 +111,9 @@ public class PermissionsManager implements Reloadable {
* Creates a permission handler for the provided permission systems if possible. * Creates a permission handler for the provided permission systems if possible.
* *
* @param type the permission systems type for which to create a corresponding permission handler * @param type the permission systems type for which to create a corresponding permission handler
*
* @return the permission handler, or {@code null} if not possible * @return the permission handler, or {@code null} if not possible
*
* @throws PermissionHandlerException during initialization of the permission handler * @throws PermissionHandlerException during initialization of the permission handler
*/ */
private PermissionHandler createPermissionHandler(PermissionsSystemType type) throws PermissionHandlerException { private PermissionHandler createPermissionHandler(PermissionsSystemType type) throws PermissionHandlerException {
@ -230,6 +233,7 @@ public class PermissionsManager implements Reloadable {
* *
* @param joiningPlayer The player to check * @param joiningPlayer The player to check
* @param permissionNode The permission node to verify * @param permissionNode The permission node to verify
*
* @return true if the player has permission, false otherwise * @return true if the player has permission, false otherwise
*/ */
public boolean hasPermission(JoiningPlayer joiningPlayer, PermissionNode permissionNode) { public boolean hasPermission(JoiningPlayer joiningPlayer, PermissionNode permissionNode) {
@ -443,14 +447,46 @@ public class PermissionsManager implements Reloadable {
return removeGroups(player, groupNames); return removeGroups(player, groupNames);
} }
public void loadUserData(UUID uuid) { /**
* Loads the permission data of the given player.
*
* @param offlinePlayer the offline player.
* @return true if the load was successful.
*/
public boolean loadUserData(OfflinePlayer offlinePlayer) {
try {
try {
loadUserData(offlinePlayer.getUniqueId());
} catch (NoSuchMethodError e) {
loadUserData(offlinePlayer.getName());
}
} catch (PermissionLoadUserException e) {
ConsoleLogger.logException("Unable to load the permission data of user " + offlinePlayer.getName(), e);
return false;
}
return true;
}
/**
* Loads the permission data of the given player unique identifier.
*
* @param uuid the {@link UUID} of the player.
* @throws PermissionLoadUserException if the action failed.
*/
public void loadUserData(UUID uuid) throws PermissionLoadUserException {
if (!isEnabled()) { if (!isEnabled()) {
return; return;
} }
handler.loadUserData(uuid); handler.loadUserData(uuid);
} }
public void loadUserData(String name) { /**
* Loads the permission data of the given player name.
*
* @param name the name of the player.
* @throws PermissionLoadUserException if the action failed.
*/
public void loadUserData(String name) throws PermissionLoadUserException {
if (!isEnabled()) { if (!isEnabled()) {
return; return;
} }

View File

@ -189,22 +189,21 @@ public class LuckPermsHandler implements PermissionHandler {
} }
@Override @Override
public void loadUserData(UUID uuid) { public void loadUserData(UUID uuid) throws PermissionLoadUserException {
try { try {
luckPermsApi.getUserManager().loadUser(uuid).get(5, TimeUnit.SECONDS); luckPermsApi.getUserManager().loadUser(uuid).get(5, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) { } catch (InterruptedException | ExecutionException | TimeoutException e) {
e.printStackTrace(); throw new PermissionLoadUserException("Unable to load the permission data of the user " + uuid, e);
} }
} }
@Override @Override
public void loadUserData(String name) { public void loadUserData(String name) throws PermissionLoadUserException {
try { try {
UUID uuid = luckPermsApi.getStorage().getUUID(name).get(5, TimeUnit.SECONDS); UUID uuid = luckPermsApi.getStorage().getUUID(name).get(5, TimeUnit.SECONDS);
loadUserData(uuid); loadUserData(uuid);
} catch (InterruptedException | ExecutionException | TimeoutException e) { } catch (InterruptedException | ExecutionException | TimeoutException e) {
e.printStackTrace(); throw new PermissionLoadUserException("Unable to load the permission data of the user " + name, e);
} }
} }
} }

View File

@ -107,10 +107,9 @@ public interface PermissionHandler {
*/ */
PermissionsSystemType getPermissionSystem(); PermissionsSystemType getPermissionSystem();
default void loadUserData(UUID uuid) { default void loadUserData(UUID uuid) throws PermissionLoadUserException {
} }
default void loadUserData(String name) { default void loadUserData(String name) throws PermissionLoadUserException {
} }
} }

View File

@ -0,0 +1,13 @@
package fr.xephi.authme.permission.handlers;
import java.util.UUID;
/**
* Exception thrown when a {@link PermissionHandler#loadUserData(UUID uuid)} request fails.
*/
public class PermissionLoadUserException extends Exception {
public PermissionLoadUserException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -212,15 +212,13 @@ public class PurgeExecutor {
} }
for (OfflinePlayer offlinePlayer : cleared) { for (OfflinePlayer offlinePlayer : cleared) {
try { if (!permissionsManager.loadUserData(offlinePlayer)) {
permissionsManager.loadUserData(offlinePlayer.getUniqueId()); ConsoleLogger.warning("Unable to purge the permissions of user " + offlinePlayer + "!");
} catch (NoSuchMethodError e) { continue;
permissionsManager.loadUserData(offlinePlayer.getName());
} }
permissionsManager.removeAllGroups(offlinePlayer); permissionsManager.removeAllGroups(offlinePlayer);
} }
ConsoleLogger.info("AutoPurge: Removed permissions from " + cleared.size() + " player(s)."); ConsoleLogger.info("AutoPurge: Removed permissions from " + cleared.size() + " player(s).");
} }
} }

View File

@ -3,6 +3,7 @@ package fr.xephi.authme.task.purge;
import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.permission.PermissionsManager; import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.permission.PlayerStatePermission; import fr.xephi.authme.permission.PlayerStatePermission;
import fr.xephi.authme.permission.handlers.PermissionLoadUserException;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
@ -73,10 +74,9 @@ class PurgeTask extends BukkitRunnable {
OfflinePlayer offlinePlayer = offlinePlayers[nextPosition]; OfflinePlayer offlinePlayer = offlinePlayers[nextPosition];
if (offlinePlayer.getName() != null && toPurge.remove(offlinePlayer.getName().toLowerCase())) { if (offlinePlayer.getName() != null && toPurge.remove(offlinePlayer.getName().toLowerCase())) {
try { if(!permissionsManager.loadUserData(offlinePlayer)) {
permissionsManager.loadUserData(offlinePlayer.getUniqueId()); ConsoleLogger.warning("Unable to check if the user " + offlinePlayer.getName() + " can be purged!");
} catch (NoSuchMethodError e) { continue;
permissionsManager.loadUserData(offlinePlayer.getName());
} }
if (!permissionsManager.hasPermissionOffline(offlinePlayer, PlayerStatePermission.BYPASS_PURGE)) { if (!permissionsManager.hasPermissionOffline(offlinePlayer, PlayerStatePermission.BYPASS_PURGE)) {
playerPortion.add(offlinePlayer); playerPortion.add(offlinePlayer);