From 2fbdd469196b04474ed1c21a2e1beba39138bbfa Mon Sep 17 00:00:00 2001 From: Gabriele C Date: Sun, 3 Sep 2017 02:07:00 +0200 Subject: [PATCH] #1298 Make permission manager accept OfflinePlayers + fix permission purge --- .../authme/permission/PermissionsManager.java | 18 +++++++-------- .../handlers/BPermissionsHandler.java | 22 +++++++++---------- .../handlers/PermissionHandler.java | 13 ++++++----- .../handlers/PermissionsExHandler.java | 21 +++++++++--------- .../permission/handlers/VaultHandler.java | 14 ++++++------ .../handlers/ZPermissionsHandler.java | 20 ++++++----------- .../authme/task/purge/PurgeExecutor.java | 4 +--- 7 files changed, 53 insertions(+), 59 deletions(-) diff --git a/src/main/java/fr/xephi/authme/permission/PermissionsManager.java b/src/main/java/fr/xephi/authme/permission/PermissionsManager.java index 8cfaf907d..2f443ccac 100644 --- a/src/main/java/fr/xephi/authme/permission/PermissionsManager.java +++ b/src/main/java/fr/xephi/authme/permission/PermissionsManager.java @@ -256,7 +256,7 @@ public class PermissionsManager implements Reloadable { * * @return Permission groups, or an empty collection if this feature is not supported. */ - public Collection getGroups(Player player) { + public Collection getGroups(OfflinePlayer player) { return isEnabled() ? handler.getGroups(player) : Collections.emptyList(); } @@ -267,7 +267,7 @@ public class PermissionsManager implements Reloadable { * * @return The name of the primary permission group. Or null. */ - public String getPrimaryGroup(Player player) { + public String getPrimaryGroup(OfflinePlayer player) { return isEnabled() ? handler.getPrimaryGroup(player) : null; } @@ -280,7 +280,7 @@ public class PermissionsManager implements Reloadable { * @return True if the player is in the specified group, false otherwise. * False is also returned if groups aren't supported by the used permissions system. */ - public boolean isInGroup(Player player, String groupName) { + public boolean isInGroup(OfflinePlayer player, String groupName) { return isEnabled() && handler.isInGroup(player, groupName); } @@ -293,7 +293,7 @@ public class PermissionsManager implements Reloadable { * @return True if succeed, false otherwise. * False is also returned if this feature isn't supported for the current permissions system. */ - public boolean addGroup(Player player, String groupName) { + public boolean addGroup(OfflinePlayer player, String groupName) { if (!isEnabled() || StringUtils.isEmpty(groupName)) { return false; } @@ -309,7 +309,7 @@ public class PermissionsManager implements Reloadable { * @return True if at least one group was added, false otherwise. * False is also returned if this feature isn't supported for the current permissions system. */ - public boolean addGroups(Player player, Collection groupNames) { + public boolean addGroups(OfflinePlayer player, Collection groupNames) { // If no permissions system is used, return false if (!isEnabled()) { return false; @@ -336,7 +336,7 @@ public class PermissionsManager implements Reloadable { * @return True if succeed, false otherwise. * False is also returned if this feature isn't supported for the current permissions system. */ - public boolean removeGroup(Player player, String groupName) { + public boolean removeGroup(OfflinePlayer player, String groupName) { return isEnabled() && handler.removeFromGroup(player, groupName); } @@ -349,7 +349,7 @@ public class PermissionsManager implements Reloadable { * @return True if at least one group was removed, false otherwise. * False is also returned if this feature isn't supported for the current permissions system. */ - public boolean removeGroups(Player player, Collection groupNames) { + public boolean removeGroups(OfflinePlayer player, Collection groupNames) { // If no permissions system is used, return false if (!isEnabled()) { return false; @@ -377,7 +377,7 @@ public class PermissionsManager implements Reloadable { * @return True if succeed, false otherwise. * False is also returned if this feature isn't supported for the current permissions system. */ - public boolean setGroup(Player player, String groupName) { + public boolean setGroup(OfflinePlayer player, String groupName) { return isEnabled() && handler.setGroup(player, groupName); } @@ -391,7 +391,7 @@ public class PermissionsManager implements Reloadable { * @return True if succeed, false otherwise. * False will also be returned if this feature isn't supported for the used permissions system. */ - public boolean removeAllGroups(Player player) { + public boolean removeAllGroups(OfflinePlayer player) { // If no permissions system is used, return false if (!isEnabled()) { return false; diff --git a/src/main/java/fr/xephi/authme/permission/handlers/BPermissionsHandler.java b/src/main/java/fr/xephi/authme/permission/handlers/BPermissionsHandler.java index 849ecd657..ecf167ac0 100644 --- a/src/main/java/fr/xephi/authme/permission/handlers/BPermissionsHandler.java +++ b/src/main/java/fr/xephi/authme/permission/handlers/BPermissionsHandler.java @@ -4,7 +4,7 @@ import de.bananaco.bpermissions.api.ApiLayer; import de.bananaco.bpermissions.api.CalculableType; import fr.xephi.authme.permission.PermissionNode; import fr.xephi.authme.permission.PermissionsSystemType; -import org.bukkit.entity.Player; +import org.bukkit.OfflinePlayer; import java.util.Arrays; import java.util.List; @@ -18,8 +18,8 @@ import java.util.List; public class BPermissionsHandler implements PermissionHandler { @Override - public boolean addToGroup(Player player, String group) { - ApiLayer.addGroup(player.getWorld().getName(), CalculableType.USER, player.getName(), group); + public boolean addToGroup(OfflinePlayer player, String group) { + ApiLayer.addGroup(null, CalculableType.USER, player.getName(), group); return true; } @@ -34,25 +34,25 @@ public class BPermissionsHandler implements PermissionHandler { } @Override - public boolean isInGroup(Player player, String group) { - return ApiLayer.hasGroup(player.getWorld().getName(), CalculableType.USER, player.getName(), group); + public boolean isInGroup(OfflinePlayer player, String group) { + return ApiLayer.hasGroup(null, CalculableType.USER, player.getName(), group); } @Override - public boolean removeFromGroup(Player player, String group) { - ApiLayer.removeGroup(player.getWorld().getName(), CalculableType.USER, player.getName(), group); + public boolean removeFromGroup(OfflinePlayer player, String group) { + ApiLayer.removeGroup(null, CalculableType.USER, player.getName(), group); return true; } @Override - public boolean setGroup(Player player, String group) { - ApiLayer.setGroup(player.getWorld().getName(), CalculableType.USER, player.getName(), group); + public boolean setGroup(OfflinePlayer player, String group) { + ApiLayer.setGroup(null, CalculableType.USER, player.getName(), group); return true; } @Override - public List getGroups(Player player) { - return Arrays.asList(ApiLayer.getGroups(player.getWorld().getName(), CalculableType.USER, player.getName())); + public List getGroups(OfflinePlayer player) { + return Arrays.asList(ApiLayer.getGroups(null, CalculableType.USER, player.getName())); } @Override diff --git a/src/main/java/fr/xephi/authme/permission/handlers/PermissionHandler.java b/src/main/java/fr/xephi/authme/permission/handlers/PermissionHandler.java index 50816ce01..48a2f9700 100644 --- a/src/main/java/fr/xephi/authme/permission/handlers/PermissionHandler.java +++ b/src/main/java/fr/xephi/authme/permission/handlers/PermissionHandler.java @@ -3,6 +3,7 @@ package fr.xephi.authme.permission.handlers; import fr.xephi.authme.permission.PermissionNode; import fr.xephi.authme.permission.PermissionsSystemType; import fr.xephi.authme.util.Utils; +import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; import java.util.Collection; @@ -19,7 +20,7 @@ public interface PermissionHandler { * @return True if succeed, false otherwise. * False is also returned if this feature isn't supported for the current permissions system. */ - boolean addToGroup(Player player, String group); + boolean addToGroup(OfflinePlayer player, String group); /** * Check whether the current permissions system has group support. @@ -49,7 +50,7 @@ public interface PermissionHandler { * @return True if the player is in the specified group, false otherwise. * False is also returned if groups aren't supported by the used permissions system. */ - default boolean isInGroup(Player player, String group) { + default boolean isInGroup(OfflinePlayer player, String group) { return getGroups(player).contains(group); } @@ -62,7 +63,7 @@ public interface PermissionHandler { * @return True if succeed, false otherwise. * False is also returned if this feature isn't supported for the current permissions system. */ - boolean removeFromGroup(Player player, String group); + boolean removeFromGroup(OfflinePlayer player, String group); /** * Set the permission group of a player, if supported. @@ -74,7 +75,7 @@ public interface PermissionHandler { * @return True if succeed, false otherwise. * False is also returned if this feature isn't supported for the current permissions system. */ - boolean setGroup(Player player, String group); + boolean setGroup(OfflinePlayer player, String group); /** * Get the permission groups of a player, if available. @@ -83,7 +84,7 @@ public interface PermissionHandler { * * @return Permission groups, or an empty list if this feature is not supported. */ - Collection getGroups(Player player); + Collection getGroups(OfflinePlayer player); /** * Get the primary group of a player, if available. @@ -92,7 +93,7 @@ public interface PermissionHandler { * * @return The name of the primary permission group. Or null. */ - default String getPrimaryGroup(Player player) { + default String getPrimaryGroup(OfflinePlayer player) { Collection groups = getGroups(player); if (Utils.isCollectionEmpty(groups)) { return null; diff --git a/src/main/java/fr/xephi/authme/permission/handlers/PermissionsExHandler.java b/src/main/java/fr/xephi/authme/permission/handlers/PermissionsExHandler.java index 9b4550d84..bc6b57f8c 100644 --- a/src/main/java/fr/xephi/authme/permission/handlers/PermissionsExHandler.java +++ b/src/main/java/fr/xephi/authme/permission/handlers/PermissionsExHandler.java @@ -2,6 +2,7 @@ package fr.xephi.authme.permission.handlers; import fr.xephi.authme.permission.PermissionNode; import fr.xephi.authme.permission.PermissionsSystemType; +import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; import ru.tehkode.permissions.PermissionManager; import ru.tehkode.permissions.PermissionUser; @@ -28,12 +29,12 @@ public class PermissionsExHandler implements PermissionHandler { } @Override - public boolean addToGroup(Player player, String group) { + public boolean addToGroup(OfflinePlayer player, String group) { if (!PermissionsEx.getPermissionManager().getGroupNames().contains(group)) { return false; } - PermissionUser user = PermissionsEx.getUser(player); + PermissionUser user = PermissionsEx.getUser(player.getName()); user.addGroup(group); return true; } @@ -50,31 +51,31 @@ public class PermissionsExHandler implements PermissionHandler { } @Override - public boolean isInGroup(Player player, String group) { - PermissionUser user = permissionManager.getUser(player); + public boolean isInGroup(OfflinePlayer player, String group) { + PermissionUser user = permissionManager.getUser(player.getName()); return user.inGroup(group); } @Override - public boolean removeFromGroup(Player player, String group) { - PermissionUser user = permissionManager.getUser(player); + public boolean removeFromGroup(OfflinePlayer player, String group) { + PermissionUser user = permissionManager.getUser(player.getName()); user.removeGroup(group); return true; } @Override - public boolean setGroup(Player player, String group) { + public boolean setGroup(OfflinePlayer player, String group) { List groups = new ArrayList<>(); groups.add(group); - PermissionUser user = permissionManager.getUser(player); + PermissionUser user = permissionManager.getUser(player.getName()); user.setParentsIdentifier(groups); return true; } @Override - public List getGroups(Player player) { - PermissionUser user = permissionManager.getUser(player); + public List getGroups(OfflinePlayer player) { + PermissionUser user = permissionManager.getUser(player.getName()); return user.getParentIdentifiers(null); } diff --git a/src/main/java/fr/xephi/authme/permission/handlers/VaultHandler.java b/src/main/java/fr/xephi/authme/permission/handlers/VaultHandler.java index 72b323f98..6e6815fe8 100644 --- a/src/main/java/fr/xephi/authme/permission/handlers/VaultHandler.java +++ b/src/main/java/fr/xephi/authme/permission/handlers/VaultHandler.java @@ -3,8 +3,8 @@ package fr.xephi.authme.permission.handlers; import fr.xephi.authme.permission.PermissionNode; import fr.xephi.authme.permission.PermissionsSystemType; import net.milkbowl.vault.permission.Permission; +import org.bukkit.OfflinePlayer; import org.bukkit.Server; -import org.bukkit.entity.Player; import org.bukkit.plugin.RegisteredServiceProvider; import java.util.Arrays; @@ -41,7 +41,7 @@ public class VaultHandler implements PermissionHandler { } @Override - public boolean addToGroup(Player player, String group) { + public boolean addToGroup(OfflinePlayer player, String group) { return vaultProvider.playerAddGroup(null, player, group); } @@ -56,17 +56,17 @@ public class VaultHandler implements PermissionHandler { } @Override - public boolean isInGroup(Player player, String group) { + public boolean isInGroup(OfflinePlayer player, String group) { return vaultProvider.playerInGroup(null, player, group); } @Override - public boolean removeFromGroup(Player player, String group) { + public boolean removeFromGroup(OfflinePlayer player, String group) { return vaultProvider.playerRemoveGroup(null, player, group); } @Override - public boolean setGroup(Player player, String group) { + public boolean setGroup(OfflinePlayer player, String group) { for (String groupName : getGroups(player)) { removeFromGroup(player, groupName); } @@ -75,12 +75,12 @@ public class VaultHandler implements PermissionHandler { } @Override - public List getGroups(Player player) { + public List getGroups(OfflinePlayer player) { return Arrays.asList(vaultProvider.getPlayerGroups(null, player)); } @Override - public String getPrimaryGroup(Player player) { + public String getPrimaryGroup(OfflinePlayer player) { return vaultProvider.getPrimaryGroup(null, player); } diff --git a/src/main/java/fr/xephi/authme/permission/handlers/ZPermissionsHandler.java b/src/main/java/fr/xephi/authme/permission/handlers/ZPermissionsHandler.java index c86863f78..dc3901237 100644 --- a/src/main/java/fr/xephi/authme/permission/handlers/ZPermissionsHandler.java +++ b/src/main/java/fr/xephi/authme/permission/handlers/ZPermissionsHandler.java @@ -3,7 +3,7 @@ package fr.xephi.authme.permission.handlers; import fr.xephi.authme.permission.PermissionNode; import fr.xephi.authme.permission.PermissionsSystemType; import org.bukkit.Bukkit; -import org.bukkit.entity.Player; +import org.bukkit.OfflinePlayer; import org.tyrannyofheaven.bukkit.zPermissions.ZPermissionsService; import java.util.Collection; @@ -29,7 +29,7 @@ public class ZPermissionsHandler implements PermissionHandler { } @Override - public boolean addToGroup(Player player, String group) { + public boolean addToGroup(OfflinePlayer player, String group) { return Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "permissions player " + player.getName() + " addgroup " + group); } @@ -42,34 +42,28 @@ public class ZPermissionsHandler implements PermissionHandler { @Override public boolean hasPermissionOffline(String name, PermissionNode node) { Map perms = zPermissionsService.getPlayerPermissions(null, null, name); - if (perms.containsKey(node.getNode())) { - return perms.get(node.getNode()); - } else { - return false; - } + return perms.getOrDefault(node.getNode(), false); } @Override - public boolean removeFromGroup(Player player, String group) { + public boolean removeFromGroup(OfflinePlayer player, String group) { return Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "permissions player " + player.getName() + " removegroup " + group); } @Override - public boolean setGroup(Player player, String group) { + public boolean setGroup(OfflinePlayer player, String group) { return Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "permissions player " + player.getName() + " setgroup " + group); } @Override - public Collection getGroups(Player player) { - // TODO Gnat008 20160631: Use UUID not name? + public Collection getGroups(OfflinePlayer player) { return zPermissionsService.getPlayerGroups(player.getName()); } @Override - public String getPrimaryGroup(Player player) { - // TODO Gnat008 20160631: Use UUID not name? + public String getPrimaryGroup(OfflinePlayer player) { return zPermissionsService.getPlayerPrimaryGroup(player.getName()); } diff --git a/src/main/java/fr/xephi/authme/task/purge/PurgeExecutor.java b/src/main/java/fr/xephi/authme/task/purge/PurgeExecutor.java index 16fd154d7..52b9f9dc7 100644 --- a/src/main/java/fr/xephi/authme/task/purge/PurgeExecutor.java +++ b/src/main/java/fr/xephi/authme/task/purge/PurgeExecutor.java @@ -191,15 +191,13 @@ public class PurgeExecutor { ConsoleLogger.info("AutoPurge: Removed " + deletedFiles + " EssentialsFiles"); } - // TODO #676: What is this method for? Is it correct? synchronized void purgePermissions(Collection cleared) { if (!settings.getProperty(PurgeSettings.REMOVE_PERMISSIONS)) { return; } for (OfflinePlayer offlinePlayer : cleared) { - String name = offlinePlayer.getName(); - permissionsManager.removeAllGroups(bukkitService.getPlayerExact(name)); + permissionsManager.removeAllGroups(offlinePlayer); } ConsoleLogger.info("AutoPurge: Removed permissions from " + cleared.size() + " player(s).");