diff --git a/src/main/java/fr/xephi/authme/command/help/HelpProvider.java b/src/main/java/fr/xephi/authme/command/help/HelpProvider.java index 1d8b6bdb7..cadfd641f 100644 --- a/src/main/java/fr/xephi/authme/command/help/HelpProvider.java +++ b/src/main/java/fr/xephi/authme/command/help/HelpProvider.java @@ -48,7 +48,7 @@ public class HelpProvider implements SettingsDependent { private String helpHeader; @Inject - public HelpProvider(PermissionsManager permissionsManager, NewSetting settings) { + HelpProvider(PermissionsManager permissionsManager, NewSetting settings) { this.permissionsManager = permissionsManager; loadSettings(settings); } @@ -152,7 +152,7 @@ public class HelpProvider implements SettingsDependent { final DefaultPermission defaultPermission = permission.getDefaultPermission(); String addendum = ""; if (DefaultPermission.OP_ONLY.equals(defaultPermission)) { - addendum = PermissionsManager.evaluateDefaultPermission(defaultPermission, sender) + addendum = defaultPermission.evaluate(sender) ? " (You have permission)" : " (No permission)"; } diff --git a/src/main/java/fr/xephi/authme/permission/DefaultPermission.java b/src/main/java/fr/xephi/authme/permission/DefaultPermission.java index 9ad8aff13..3acbfa8c9 100644 --- a/src/main/java/fr/xephi/authme/permission/DefaultPermission.java +++ b/src/main/java/fr/xephi/authme/permission/DefaultPermission.java @@ -1,18 +1,35 @@ package fr.xephi.authme.permission; +import org.bukkit.command.CommandSender; + /** - * The default permission for a command if there is no support for permission nodes. + * The default permission to fall back to if there is no support for permission nodes. */ public enum DefaultPermission { - /** No one can execute the command. */ - NOT_ALLOWED("No permission"), + /** No one has permission. */ + NOT_ALLOWED("No permission") { + @Override + public boolean evaluate(CommandSender sender) { + return false; + } + }, - /** Only players with the OP status may execute the command. */ - OP_ONLY("OP's only"), + /** Only players with OP status have permission. */ + OP_ONLY("OP's only") { + @Override + public boolean evaluate(CommandSender sender) { + return sender.isOp(); + } + }, - /** The command can be executed by anyone. */ - ALLOWED("Everyone allowed"); + /** Everyone is granted permission. */ + ALLOWED("Everyone allowed") { + @Override + public boolean evaluate(CommandSender sender) { + return true; + } + }; /** Textual representation of the default permission. */ private final String title; @@ -26,9 +43,17 @@ public enum DefaultPermission { } /** - * Return the textual representation. - * - * @return The textual representation + * Evaluates whether permission is granted to the sender or not. + * + * @param sender the sender to process + * @return true if the sender has permission, false otherwise + */ + public abstract boolean evaluate(CommandSender sender); + + /** + * Return the textual representation. + * + * @return the textual representation */ public String getTitle() { return title; diff --git a/src/main/java/fr/xephi/authme/permission/PermissionsManager.java b/src/main/java/fr/xephi/authme/permission/PermissionsManager.java index 358bc5556..072e82c5c 100644 --- a/src/main/java/fr/xephi/authme/permission/PermissionsManager.java +++ b/src/main/java/fr/xephi/authme/permission/PermissionsManager.java @@ -1,13 +1,16 @@ package fr.xephi.authme.permission; -import de.bananaco.bpermissions.api.ApiLayer; -import de.bananaco.bpermissions.api.CalculableType; import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.command.CommandDescription; -import fr.xephi.authme.util.CollectionUtils; +import fr.xephi.authme.permission.handlers.BPermissionsHandler; +import fr.xephi.authme.permission.handlers.GroupManagerHandler; +import fr.xephi.authme.permission.handlers.PermissionHandler; +import fr.xephi.authme.permission.handlers.PermissionsBukkitHandler; +import fr.xephi.authme.permission.handlers.PermissionsExHandler; +import fr.xephi.authme.permission.handlers.VaultHandler; +import fr.xephi.authme.permission.handlers.ZPermissionsHandler; import net.milkbowl.vault.permission.Permission; import org.anjocaido.groupmanager.GroupManager; -import org.anjocaido.groupmanager.permissions.AnjoPermissionsHandler; import org.bukkit.Bukkit; import org.bukkit.Server; import org.bukkit.command.CommandSender; @@ -18,15 +21,12 @@ import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.RegisteredServiceProvider; import org.tyrannyofheaven.bukkit.zPermissions.ZPermissionsService; -import ru.tehkode.permissions.PermissionUser; import ru.tehkode.permissions.bukkit.PermissionsEx; import javax.annotation.PostConstruct; import javax.inject.Inject; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; -import java.util.Map; /** *

@@ -40,12 +40,8 @@ import java.util.Map; * @author Tim Visée, http://timvisee.com * @version 0.3 */ -public class PermissionsManager implements PermissionsService { +public class PermissionsManager { - /** - * Vault instance. - */ - public Permission vaultPerms = null; /** * Server instance. */ @@ -57,13 +53,10 @@ public class PermissionsManager implements PermissionsService { */ private PermissionsSystemType permsType = null; /** - * Essentials group manager instance. + * The permission handler that is currently in use. + * Null if no permission system is hooked. */ - private GroupManager groupManagerPerms; - /** - * zPermissions service instance. - */ - private ZPermissionsService zPermissionsService; + private PermissionHandler handler = null; /** * Constructor. @@ -83,7 +76,7 @@ public class PermissionsManager implements PermissionsService { * @return False if there isn't any permissions system used. */ public boolean isEnabled() { - return permsType != null; + return handler != null; } /** @@ -132,21 +125,23 @@ public class PermissionsManager implements PermissionsService { continue; } + handler = new PermissionsExHandler(PermissionsEx.getPermissionManager()); break; case ESSENTIALS_GROUP_MANAGER: // Set the plugin instance - groupManagerPerms = (GroupManager) plugin; + handler = new GroupManagerHandler((GroupManager) plugin); break; case Z_PERMISSIONS: // Set the zPermissions service and make sure it's valid - zPermissionsService = Bukkit.getServicesManager().load(ZPermissionsService.class); + ZPermissionsService zPermissionsService = Bukkit.getServicesManager().load(ZPermissionsService.class); if (zPermissionsService == null) { ConsoleLogger.info("Failed to hook into " + type.getName() + "!"); continue; } + handler = new ZPermissionsHandler(zPermissionsService); break; case VAULT: @@ -158,12 +153,21 @@ public class PermissionsManager implements PermissionsService { } // Get the Vault provider and make sure it's valid - vaultPerms = permissionProvider.getProvider(); + Permission vaultPerms = permissionProvider.getProvider(); if (vaultPerms == null) { ConsoleLogger.info("Not using " + type.getName() + " because it's disabled!"); continue; } + handler = new VaultHandler(vaultPerms); + break; + + case B_PERMISSIONS: + handler = new BPermissionsHandler(); + break; + + case PERMISSIONS_BUKKIT: + handler = new PermissionsBukkitHandler(); break; default: @@ -194,6 +198,7 @@ public class PermissionsManager implements PermissionsService { public void unhook() { // Reset the current used permissions system this.permsType = null; + this.handler = null; // Print a status message to the console ConsoleLogger.info("Unhooked from Permissions!"); @@ -219,7 +224,7 @@ public class PermissionsManager implements PermissionsService { * @param event Event instance. */ public void onPluginEnable(PluginEnableEvent event) { - // Get the plugin and it's name + // Get the plugin and its name Plugin plugin = event.getPlugin(); String pluginName = plugin.getName(); @@ -272,16 +277,7 @@ public class PermissionsManager implements PermissionsService { } Player player = (Player) sender; - return hasPermission(player, permissionNode.getNode(), def); - } - - public boolean hasPermission(Player player, Iterable nodes, boolean def) { - for (PermissionNode node : nodes) { - if (!hasPermission(player, node, def)) { - return false; - } - } - return true; + return hasPermission(player, permissionNode, def); } public boolean hasPermission(CommandSender sender, CommandDescription command) { @@ -290,75 +286,27 @@ public class PermissionsManager implements PermissionsService { } DefaultPermission defaultPermission = command.getPermission().getDefaultPermission(); - boolean def = evaluateDefaultPermission(defaultPermission, sender); + boolean def = defaultPermission.evaluate(sender); return (sender instanceof Player) - ? hasPermission((Player) sender, command.getPermission().getNode(), def) + ? hasPermission((Player) sender, command.getPermission(), def) : def; } - public static boolean evaluateDefaultPermission(DefaultPermission defaultPermission, CommandSender sender) { - switch (defaultPermission) { - case ALLOWED: - return true; - - case OP_ONLY: - return sender.isOp(); - - case NOT_ALLOWED: - default: - return false; - } - } - /** * Check if a player has permission. * * @param player The player. - * @param permsNode The permission node. + * @param node The permission node. * @param def Default returned if no permissions system is used. * * @return True if the player has permission. */ - private boolean hasPermission(Player player, String permsNode, boolean def) { + private boolean hasPermission(Player player, PermissionNode node, boolean def) { // If no permissions system is used, return the default value if (!isEnabled()) return def; - switch (this.permsType) { - case PERMISSIONS_EX: - // Permissions Ex - PermissionUser user = PermissionsEx.getUser(player); - return user.has(permsNode); - - case PERMISSIONS_BUKKIT: - // Permissions Bukkit - return player.hasPermission(permsNode); - - case B_PERMISSIONS: - // bPermissions - return ApiLayer.hasPermission(player.getWorld().getName(), CalculableType.USER, player.getName(), permsNode); - - case ESSENTIALS_GROUP_MANAGER: - // Essentials Group Manager - final AnjoPermissionsHandler handler = groupManagerPerms.getWorldsHolder().getWorldPermissions(player); - return handler != null && handler.has(player, permsNode); - - case Z_PERMISSIONS: - // zPermissions - Map perms = zPermissionsService.getPlayerPermissions(player.getWorld().getName(), null, player.getName()); - if (perms.containsKey(permsNode)) - return perms.get(permsNode); - else - return def; - - case VAULT: - // Vault - return vaultPerms.has(player, permsNode); - - default: - // Not hooked into any permissions system, return default - return def; - } + return handler.hasPermission(player, node); } /** @@ -372,22 +320,7 @@ public class PermissionsManager implements PermissionsService { if (!isEnabled()) return false; - switch (this.permsType) { - case PERMISSIONS_EX: - case PERMISSIONS_BUKKIT: - case B_PERMISSIONS: - case ESSENTIALS_GROUP_MANAGER: - case Z_PERMISSIONS: - return true; - - case VAULT: - // Vault - return vaultPerms.hasGroupSupport(); - - default: - // Not hooked into any permissions system, return false - return false; - } + return handler.hasGroupSupport(); } /** @@ -397,46 +330,12 @@ public class PermissionsManager implements PermissionsService { * * @return Permission groups, or an empty list if this feature is not supported. */ - @SuppressWarnings({"unchecked", "rawtypes", "deprecation"}) public List getGroups(Player player) { // If no permissions system is used, return an empty list if (!isEnabled()) return new ArrayList<>(); - switch (this.permsType) { - case PERMISSIONS_EX: - // Permissions Ex - PermissionUser user = PermissionsEx.getUser(player); - return user.getParentIdentifiers(null); - - case PERMISSIONS_BUKKIT: - // Permissions Bukkit - // FIXME: Add support for this! - return new ArrayList<>(); - - case B_PERMISSIONS: - // bPermissions - return Arrays.asList(ApiLayer.getGroups(player.getWorld().getName(), CalculableType.USER, player.getName())); - - case ESSENTIALS_GROUP_MANAGER: - // Essentials Group Manager - final AnjoPermissionsHandler handler = groupManagerPerms.getWorldsHolder().getWorldPermissions(player); - if (handler == null) - return new ArrayList<>(); - return Arrays.asList(handler.getGroups(player.getName())); - - case Z_PERMISSIONS: - //zPermissions - return new ArrayList(zPermissionsService.getPlayerGroups(player.getName())); - - case VAULT: - // Vault - return Arrays.asList(vaultPerms.getPlayerGroups(player)); - - default: - // Not hooked into any permissions system, return an empty list - return new ArrayList<>(); - } + return handler.getGroups(player); } /** @@ -451,39 +350,7 @@ public class PermissionsManager implements PermissionsService { if (!isEnabled()) return null; - switch (this.permsType) { - case PERMISSIONS_EX: - case PERMISSIONS_BUKKIT: - case B_PERMISSIONS: - // Get the groups of the player - List groups = getGroups(player); - - // Make sure there is any group available, or return null - if (groups.size() == 0) - return null; - - // Return the first group - return groups.get(0); - - case ESSENTIALS_GROUP_MANAGER: - // Essentials Group Manager - final AnjoPermissionsHandler handler = groupManagerPerms.getWorldsHolder().getWorldPermissions(player); - if (handler == null) - return null; - return handler.getGroup(player.getName()); - - case Z_PERMISSIONS: - //zPermissions - return zPermissionsService.getPlayerPrimaryGroup(player.getName()); - - case VAULT: - // Vault - return vaultPerms.getPrimaryGroup(player); - - default: - // Not hooked into any permissions system, return null - return null; - } + return handler.getPrimaryGroup(player); } /** @@ -500,40 +367,7 @@ public class PermissionsManager implements PermissionsService { if (!isEnabled()) return false; - switch (this.permsType) { - case PERMISSIONS_EX: - // Permissions Ex - PermissionUser user = PermissionsEx.getUser(player); - return user.inGroup(groupName); - - case PERMISSIONS_BUKKIT: - case Z_PERMISSIONS: - // Get the current list of groups - List groupNames = getGroups(player); - - // Check whether the list contains the group name, return the result - for (String entry : groupNames) - if (entry.equals(groupName)) - return true; - return false; - - case B_PERMISSIONS: - // bPermissions - return ApiLayer.hasGroup(player.getWorld().getName(), CalculableType.USER, player.getName(), groupName); - - case ESSENTIALS_GROUP_MANAGER: - // Essentials Group Manager - final AnjoPermissionsHandler handler = groupManagerPerms.getWorldsHolder().getWorldPermissions(player); - return handler != null && handler.inGroup(player.getName(), groupName); - - case VAULT: - // Vault - return vaultPerms.playerInGroup(player, groupName); - - default: - // Not hooked into any permissions system, return an empty list - return false; - } + return handler.isInGroup(player, groupName); } /** @@ -550,47 +384,7 @@ public class PermissionsManager implements PermissionsService { if (!isEnabled()) return false; - // Set the group the proper way - switch (this.permsType) { - case PERMISSIONS_EX: - // Permissions Ex - if(!PermissionsEx.getPermissionManager().getGroupNames().contains(groupName)) { - ConsoleLogger.showError("The plugin tried to set " + player + "'s group to " + groupName + ", but it doesn't exist!"); - return false; - } - PermissionUser user = PermissionsEx.getUser(player); - user.addGroup(groupName); - return true; - - case PERMISSIONS_BUKKIT: - // Permissions Bukkit - // Add the group to the user using a command - return Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "permissions player addgroup " + player.getName() + " " + groupName); - - case B_PERMISSIONS: - // bPermissions - ApiLayer.addGroup(player.getWorld().getName(), CalculableType.USER, player.getName(), groupName); - return true; - - case ESSENTIALS_GROUP_MANAGER: - // Essentials Group Manager - // Add the group to the user using a command - return Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "manuaddsub " + player.getName() + " " + groupName); - - case Z_PERMISSIONS: - // zPermissions - // Add the group to the user using a command - return Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "permissions player " + player.getName() + " addgroup " + groupName); - - case VAULT: - // Vault - vaultPerms.playerAddGroup(player, groupName); - return true; - - default: - // Not hooked into any permissions system, return false - return false; - } + return handler.addToGroup(player, groupName); } /** @@ -631,43 +425,7 @@ public class PermissionsManager implements PermissionsService { if (!isEnabled()) return false; - // Set the group the proper way - switch (this.permsType) { - case PERMISSIONS_EX: - // Permissions Ex - PermissionUser user = PermissionsEx.getUser(player); - user.removeGroup(groupName); - return true; - - case PERMISSIONS_BUKKIT: - // Permissions Bukkit - // Remove the group to the user using a command - return Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "permissions player removegroup " + player.getName() + " " + groupName); - - case B_PERMISSIONS: - // bPermissions - ApiLayer.removeGroup(player.getWorld().getName(), CalculableType.USER, player.getName(), groupName); - return true; - - case ESSENTIALS_GROUP_MANAGER: - // Essentials Group Manager - // Remove the group to the user using a command - return Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "manudelsub " + player.getName() + " " + groupName); - - case Z_PERMISSIONS: - // zPermissions - // Remove the group to the user using a command - return Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "permissions player " + player.getName() + " removegroup " + groupName); - - case VAULT: - // Vault - vaultPerms.playerRemoveGroup(player, groupName); - return true; - - default: - // Not hooked into any permissions system, return false - return false; - } + return handler.removeFromGroup(player, groupName); } /** @@ -709,50 +467,7 @@ public class PermissionsManager implements PermissionsService { if (!isEnabled()) return false; - // Create a list of group names - List groupNames = new ArrayList<>(); - groupNames.add(groupName); - - // Set the group the proper way - switch (this.permsType) { - case PERMISSIONS_EX: - // Permissions Ex - PermissionUser user = PermissionsEx.getUser(player); - user.setParentsIdentifier(groupNames); - return true; - - case PERMISSIONS_BUKKIT: - // Permissions Bukkit - // Set the user's group using a command - return Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "permissions player setgroup " + player.getName() + " " + groupName); - - case B_PERMISSIONS: - // bPermissions - ApiLayer.setGroup(player.getWorld().getName(), CalculableType.USER, player.getName(), groupName); - return true; - - case ESSENTIALS_GROUP_MANAGER: - // Essentials Group Manager - // Clear the list of groups, add the player to the specified group afterwards using a command - removeAllGroups(player); - return Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "manuadd " + player.getName() + " " + groupName); - - case Z_PERMISSIONS: - //zPermissions - // Set the players group through the plugin commands - return Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "permissions player " + player.getName() + " setgroup " + groupName); - - case VAULT: - // Vault - // Remove all current groups, add the player to the specified group afterwards - removeAllGroups(player); - vaultPerms.playerAddGroup(player, groupName); - return true; - - default: - // Not hooked into any permissions system, return false - return false; - } + return handler.setGroup(player, groupName); } /** @@ -792,7 +507,7 @@ public class PermissionsManager implements PermissionsService { /** * Remove all groups of the specified player, if supported. * Systems like Essentials GroupManager don't allow all groups to be removed from a player, thus the user will stay - * in it's primary group. All the subgroups are removed just fine. + * in its primary group. All the subgroups are removed just fine. * * @param player The player to remove all groups from. * diff --git a/src/main/java/fr/xephi/authme/permission/PermissionsService.java b/src/main/java/fr/xephi/authme/permission/PermissionsService.java deleted file mode 100644 index 9de4f68e9..000000000 --- a/src/main/java/fr/xephi/authme/permission/PermissionsService.java +++ /dev/null @@ -1,39 +0,0 @@ -package fr.xephi.authme.permission; - -import fr.xephi.authme.command.CommandDescription; -import org.bukkit.command.CommandSender; - -/** - * Interface for dealing with permissions. - */ -public interface PermissionsService { - - /** - * Check if the player has the given permission. - * - * @param sender The command sender - * @param permission The permission node to check - * @param def Default returned if no permissions system is used - * - * @return True if the player has permission - */ - boolean hasPermission(CommandSender sender, PermissionNode permission, boolean def); - - /** - * Check if the player has the permissions for the given command. - * - * @param sender The command sender - * @param command The command whose permissions should be checked - * - * @return True if the player may execute the command - */ - boolean hasPermission(CommandSender sender, CommandDescription command); - - /** - * Return the permission system the service is working with. - * - * @return The permission system AuthMe is hooked into - */ - PermissionsSystemType getSystem(); - -} diff --git a/src/main/java/fr/xephi/authme/permission/handlers/BPermissionsHandler.java b/src/main/java/fr/xephi/authme/permission/handlers/BPermissionsHandler.java new file mode 100644 index 000000000..455923232 --- /dev/null +++ b/src/main/java/fr/xephi/authme/permission/handlers/BPermissionsHandler.java @@ -0,0 +1,69 @@ +package fr.xephi.authme.permission.handlers; + +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 java.util.Arrays; +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); + return true; + } + + @Override + public boolean hasGroupSupport() { + return true; + } + + @Override + public boolean hasPermission(Player player, PermissionNode node) { + return ApiLayer.hasPermission(player.getWorld().getName(), CalculableType.USER, player.getName(), node.getNode()); + } + + @Override + public boolean isInGroup(Player player, String group) { + return ApiLayer.hasGroup(player.getWorld().getName(), CalculableType.USER, player.getName(), group); + } + + @Override + public boolean removeFromGroup(Player player, String group) { + ApiLayer.removeGroup(player.getWorld().getName(), 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); + return true; + } + + @Override + public List getGroups(Player player) { + return Arrays.asList(ApiLayer.getGroups(player.getWorld().getName(), CalculableType.USER, player.getName())); + } + + @Override + public String getPrimaryGroup(Player player) { + // Get the groups of the player + List groups = getGroups(player); + + // Make sure there is any group available, or return null + if (groups.size() == 0) + return null; + + // Return the first group + return groups.get(0); + } + + @Override + public PermissionsSystemType getPermissionSystem() { + return PermissionsSystemType.B_PERMISSIONS; + } +} diff --git a/src/main/java/fr/xephi/authme/permission/handlers/GroupManagerHandler.java b/src/main/java/fr/xephi/authme/permission/handlers/GroupManagerHandler.java new file mode 100644 index 000000000..fe0b0236f --- /dev/null +++ b/src/main/java/fr/xephi/authme/permission/handlers/GroupManagerHandler.java @@ -0,0 +1,79 @@ +package fr.xephi.authme.permission.handlers; + +import fr.xephi.authme.permission.PermissionNode; +import fr.xephi.authme.permission.PermissionsSystemType; +import org.anjocaido.groupmanager.GroupManager; +import org.anjocaido.groupmanager.permissions.AnjoPermissionsHandler; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class GroupManagerHandler implements PermissionHandler { + + private GroupManager groupManager; + + public GroupManagerHandler(GroupManager groupManager) { + this.groupManager = groupManager; + } + + @Override + public boolean addToGroup(Player player, String group) { + return Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "manuaddsub " + player.getName() + " " + group); + } + + @Override + public boolean hasGroupSupport() { + return true; + } + + @Override + public boolean hasPermission(Player player, PermissionNode node) { + final AnjoPermissionsHandler handler = groupManager.getWorldsHolder().getWorldPermissions(player); + return handler != null && handler.has(player, node.getNode()); + } + + @Override + public boolean isInGroup(Player player, String group) { + final AnjoPermissionsHandler handler = groupManager.getWorldsHolder().getWorldPermissions(player); + return handler != null && handler.inGroup(player.getName(), group); + } + + @Override + public boolean removeFromGroup(Player player, String group) { + return Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "manudelsub " + player.getName() + " " + group); + } + + @Override + public boolean setGroup(Player player, String group) { + final AnjoPermissionsHandler handler = groupManager.getWorldsHolder().getWorldPermissions(player); + for (String groupName : handler.getGroups(player.getName())) { + removeFromGroup(player, groupName); + } + + return Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "manuadd " + player.getName() + " " + group); + } + + @Override + public List getGroups(Player player) { + final AnjoPermissionsHandler handler = groupManager.getWorldsHolder().getWorldPermissions(player); + if (handler == null) + return new ArrayList<>(); + return Arrays.asList(handler.getGroups(player.getName())); + } + + @Override + public String getPrimaryGroup(Player player) { + final AnjoPermissionsHandler handler = groupManager.getWorldsHolder().getWorldPermissions(player); + if (handler == null) + return null; + return handler.getGroup(player.getName()); + } + + @Override + public PermissionsSystemType getPermissionSystem() { + return PermissionsSystemType.ESSENTIALS_GROUP_MANAGER; + } +} diff --git a/src/main/java/fr/xephi/authme/permission/handlers/PermissionHandler.java b/src/main/java/fr/xephi/authme/permission/handlers/PermissionHandler.java new file mode 100644 index 000000000..f222dac27 --- /dev/null +++ b/src/main/java/fr/xephi/authme/permission/handlers/PermissionHandler.java @@ -0,0 +1,97 @@ +package fr.xephi.authme.permission.handlers; + +import fr.xephi.authme.permission.PermissionNode; +import fr.xephi.authme.permission.PermissionsSystemType; +import org.bukkit.entity.Player; + +import java.util.List; +public interface PermissionHandler { + + /** + * Add the permission group of a player, if supported. + * + * @param player The player + * @param group The name of the group. + * + * @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); + + /** + * Check whether the current permissions system has group support. + * If no permissions system is hooked, false will be returned. + * + * @return True if the current permissions system supports groups, false otherwise. + */ + boolean hasGroupSupport(); + + /** + * Check if a player has permission. + * + * @param player The player. + * @param node The permission node. + * + * @return True if the player has permission. + */ + boolean hasPermission(Player player, PermissionNode node); + + /** + * Check whether the player is in the specified group. + * + * @param player The player. + * @param group The group name. + * + * @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. + */ + boolean isInGroup(Player player, String group); + + /** + * Remove the permission group of a player, if supported. + * + * @param player The player + * @param group The name of the group. + * + * @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); + + /** + * Set the permission group of a player, if supported. + * This clears the current groups of the player. + * + * @param player The player + * @param group The name of the group. + * + * @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); + + /** + * Get the permission groups of a player, if available. + * + * @param player The player. + * + * @return Permission groups, or an empty list if this feature is not supported. + */ + List getGroups(Player player); + + /** + * Get the primary group of a player, if available. + * + * @param player The player. + * + * @return The name of the primary permission group. Or null. + */ + String getPrimaryGroup(Player player); + + /** + * Get the permission system that is being used. + * + * @return The permission system. + */ + PermissionsSystemType getPermissionSystem(); +} diff --git a/src/main/java/fr/xephi/authme/permission/handlers/PermissionsBukkitHandler.java b/src/main/java/fr/xephi/authme/permission/handlers/PermissionsBukkitHandler.java new file mode 100644 index 000000000..fb1edcae0 --- /dev/null +++ b/src/main/java/fr/xephi/authme/permission/handlers/PermissionsBukkitHandler.java @@ -0,0 +1,68 @@ +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 java.util.ArrayList; +import java.util.List; + +public class PermissionsBukkitHandler implements PermissionHandler { + + @Override + public boolean addToGroup(Player player, String group) { + return Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "permissions player addgroup " + player.getName() + " " + group); + } + + @Override + public boolean hasGroupSupport() { + return true; + } + + @Override + public boolean hasPermission(Player player, PermissionNode node) { + return player.hasPermission(node.getNode()); + } + + @Override + public boolean isInGroup(Player player, String group) { + List groupNames = getGroups(player); + + return groupNames.contains(group); + } + + @Override + public boolean removeFromGroup(Player player, String group) { + return Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "permissions player removegroup " + player.getName() + " " + group); + } + + @Override + public boolean setGroup(Player player, String group) { + return Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "permissions player setgroup " + player.getName() + " " + group); + } + + @Override + public List getGroups(Player player) { + // FIXME Gnat008 20160601: Add support for this + return new ArrayList<>(); + } + + @Override + public String getPrimaryGroup(Player player) { + // Get the groups of the player + List groups = getGroups(player); + + // Make sure there is any group available, or return null + if (groups.size() == 0) + return null; + + // Return the first group + return groups.get(0); + } + + @Override + public PermissionsSystemType getPermissionSystem() { + return PermissionsSystemType.PERMISSIONS_BUKKIT; + } +} diff --git a/src/main/java/fr/xephi/authme/permission/handlers/PermissionsExHandler.java b/src/main/java/fr/xephi/authme/permission/handlers/PermissionsExHandler.java new file mode 100644 index 000000000..b49e4b371 --- /dev/null +++ b/src/main/java/fr/xephi/authme/permission/handlers/PermissionsExHandler.java @@ -0,0 +1,89 @@ +package fr.xephi.authme.permission.handlers; + +import fr.xephi.authme.ConsoleLogger; +import fr.xephi.authme.permission.PermissionNode; +import fr.xephi.authme.permission.PermissionsSystemType; +import org.bukkit.entity.Player; +import ru.tehkode.permissions.PermissionManager; +import ru.tehkode.permissions.PermissionUser; +import ru.tehkode.permissions.bukkit.PermissionsEx; + +import java.util.ArrayList; +import java.util.List; + +public class PermissionsExHandler implements PermissionHandler { + + private PermissionManager permissionManager; + + public PermissionsExHandler(PermissionManager permissionManager) { + this.permissionManager = permissionManager; + } + + @Override + public boolean addToGroup(Player player, String group) { + if (!PermissionsEx.getPermissionManager().getGroupNames().contains(group)) { + ConsoleLogger.showError("The plugin tried to set " + player + "'s group to '" + group + "', but it doesn't exist!"); + return false; + } + + PermissionUser user = PermissionsEx.getUser(player); + user.addGroup(group); + return true; + } + + @Override + public boolean hasGroupSupport() { + return true; + } + + @Override + public boolean hasPermission(Player player, PermissionNode node) { + PermissionUser user = permissionManager.getUser(player); + return user.has(node.getNode()); + } + + @Override + public boolean isInGroup(Player player, String group) { + PermissionUser user = permissionManager.getUser(player); + return user.inGroup(group); + } + + @Override + public boolean removeFromGroup(Player player, String group) { + PermissionUser user = permissionManager.getUser(player); + user.removeGroup(group); + return true; + } + + @Override + public boolean setGroup(Player player, String group) { + List groups = new ArrayList<>(); + groups.add(group); + + PermissionUser user = permissionManager.getUser(player); + user.setParentsIdentifier(groups); + return true; + } + + @Override + public List getGroups(Player player) { + PermissionUser user = permissionManager.getUser(player); + return user.getParentIdentifiers(null); + } + + @Override + public String getPrimaryGroup(Player player) { + PermissionUser user = permissionManager.getUser(player); + + List groups = user.getParentIdentifiers(null); + if (groups.size() == 0) + return null; + + return groups.get(0); + } + + @Override + public PermissionsSystemType getPermissionSystem() { + return PermissionsSystemType.PERMISSIONS_EX; + } +} diff --git a/src/main/java/fr/xephi/authme/permission/handlers/VaultHandler.java b/src/main/java/fr/xephi/authme/permission/handlers/VaultHandler.java new file mode 100644 index 000000000..7b99b5ed0 --- /dev/null +++ b/src/main/java/fr/xephi/authme/permission/handlers/VaultHandler.java @@ -0,0 +1,67 @@ +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.entity.Player; + +import java.util.Arrays; +import java.util.List; + +public class VaultHandler implements PermissionHandler { + + private Permission vaultProvider; + + public VaultHandler(Permission vaultProvider) { + this.vaultProvider = vaultProvider; + } + + @Override + public boolean addToGroup(Player player, String group) { + return vaultProvider.playerAddGroup(player, group); + } + + @Override + public boolean hasGroupSupport() { + return vaultProvider.hasGroupSupport(); + } + + @Override + public boolean hasPermission(Player player, PermissionNode node) { + return vaultProvider.has(player, node.getNode()); + } + + @Override + public boolean isInGroup(Player player, String group) { + return vaultProvider.playerInGroup(player, group); + } + + @Override + public boolean removeFromGroup(Player player, String group) { + return vaultProvider.playerRemoveGroup(player, group); + } + + @Override + public boolean setGroup(Player player, String group) { + for (String groupName : getGroups(player)) { + removeFromGroup(player, groupName); + } + + return vaultProvider.playerAddGroup(player, group); + } + + @Override + public List getGroups(Player player) { + return Arrays.asList(vaultProvider.getPlayerGroups(player)); + } + + @Override + public String getPrimaryGroup(Player player) { + return vaultProvider.getPrimaryGroup(player); + } + + @Override + public PermissionsSystemType getPermissionSystem() { + return PermissionsSystemType.VAULT; + } +} diff --git a/src/main/java/fr/xephi/authme/permission/handlers/ZPermissionsHandler.java b/src/main/java/fr/xephi/authme/permission/handlers/ZPermissionsHandler.java new file mode 100644 index 000000000..18e433ba4 --- /dev/null +++ b/src/main/java/fr/xephi/authme/permission/handlers/ZPermissionsHandler.java @@ -0,0 +1,72 @@ +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.tyrannyofheaven.bukkit.zPermissions.ZPermissionsService; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class ZPermissionsHandler implements PermissionHandler { + + private ZPermissionsService zPermissionsService; + + public ZPermissionsHandler(ZPermissionsService zPermissionsService) { + this.zPermissionsService = zPermissionsService; + } + + @Override + public boolean addToGroup(Player player, String group) { + return Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "permissions player " + player.getName() + " addgroup " + group); + } + + @Override + public boolean hasGroupSupport() { + return true; + } + + @Override + public boolean hasPermission(Player player, PermissionNode node) { + Map perms = zPermissionsService.getPlayerPermissions(player.getWorld().getName(), null, player.getName()); + if (perms.containsKey(node.getNode())) + return perms.get(node.getNode()); + else + return node.getDefaultPermission().evaluate(player); + } + + @Override + public boolean isInGroup(Player player, String group) { + return getGroups(player).contains(group); + } + + @Override + public boolean removeFromGroup(Player player, String group) { + return Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "permissions player " + player.getName() + " removegroup " + group); + } + + @Override + public boolean setGroup(Player player, String group) { + return Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "permissions player " + player.getName() + " setgroup " + group); + } + + @Override + @SuppressWarnings("unchecked") + public List getGroups(Player player) { + // TODO Gnat008 20160631: Use UUID not name? + return new ArrayList(zPermissionsService.getPlayerGroups(player.getName())); + } + + @Override + public String getPrimaryGroup(Player player) { + // TODO Gnat008 20160631: Use UUID not name? + return zPermissionsService.getPlayerPrimaryGroup(player.getName()); + } + + @Override + public PermissionsSystemType getPermissionSystem() { + return PermissionsSystemType.Z_PERMISSIONS; + } +}