package fr.xephi.authme.permission; import com.google.common.annotations.VisibleForTesting; import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.data.limbo.UserGroup; import fr.xephi.authme.initialization.Reloadable; import fr.xephi.authme.output.ConsoleLoggerFactory; import fr.xephi.authme.permission.handlers.LuckPermsHandler; import fr.xephi.authme.permission.handlers.PermissionHandler; 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.VaultHandler; import fr.xephi.authme.permission.handlers.ZPermissionsHandler; import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.properties.PluginSettings; import fr.xephi.authme.util.StringUtils; import org.bukkit.OfflinePlayer; import org.bukkit.Server; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginManager; import javax.annotation.PostConstruct; import javax.inject.Inject; import java.util.Collection; import java.util.Collections; import java.util.UUID; /** * PermissionsManager. *
* A permissions manager, to manage and use various permissions systems. * This manager supports dynamic plugin hooking and various other features. *
* Written by Tim Visée.
*
* @author Tim Visée, http://timvisee.com
* @version 0.3
*/
public class PermissionsManager implements Reloadable {
private final ConsoleLogger logger = ConsoleLoggerFactory.get(PermissionsManager.class);
private final Server server;
private final PluginManager pluginManager;
private final Settings settings;
/**
* The permission handler that is currently in use.
* Null if no permission system is hooked.
*/
private PermissionHandler handler = null;
@Inject
PermissionsManager(Server server, PluginManager pluginManager, Settings settings) {
this.server = server;
this.pluginManager = pluginManager;
this.settings = settings;
}
/**
* Check if the permissions manager is currently hooked into any of the supported permissions systems.
*
* @return False if there isn't any permissions system used.
*/
public boolean isEnabled() {
return handler != null;
}
/**
* Setup and hook into the permissions systems.
*/
@PostConstruct
@VisibleForTesting
void setup() {
if (settings.getProperty(PluginSettings.FORCE_VAULT_HOOK)) {
try {
PermissionHandler handler = createPermissionHandler(PermissionsSystemType.VAULT);
if (handler != null) {
// Show a success message and return
this.handler = handler;
logger.info("Hooked into " + PermissionsSystemType.VAULT.getDisplayName() + "!");
return;
}
} catch (PermissionHandlerException e) {
logger.logException("Failed to create Vault hook (forced):", e);
}
} else {
// Loop through all the available permissions system types
for (PermissionsSystemType type : PermissionsSystemType.values()) {
try {
PermissionHandler handler = createPermissionHandler(type);
if (handler != null) {
// Show a success message and return
this.handler = handler;
logger.info("Hooked into " + type.getDisplayName() + "!");
return;
}
} catch (Exception ex) {
// An error occurred, show a warning message
logger.logException("Error while hooking into " + type.getDisplayName(), ex);
}
}
}
// No recognized permissions system found, show a message and return
logger.info("No supported permissions system found! Permissions are disabled!");
}
/**
* 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
*
* @return the permission handler, or {@code null} if not possible
*
* @throws PermissionHandlerException during initialization of the permission handler
*/
private PermissionHandler createPermissionHandler(PermissionsSystemType type) throws PermissionHandlerException {
// Try to find the plugin for the current permissions system
Plugin plugin = pluginManager.getPlugin(type.getPluginName());
if (plugin == null) {
return null;
}
// Make sure the plugin is enabled before hooking
if (!plugin.isEnabled()) {
logger.info("Not hooking into " + type.getDisplayName() + " because it's disabled!");
return null;
}
switch (type) {
case LUCK_PERMS:
return new LuckPermsHandler();
case PERMISSIONS_EX:
return new PermissionsExHandler();
case Z_PERMISSIONS:
return new ZPermissionsHandler();
case VAULT:
return new VaultHandler(server);
default:
throw new IllegalStateException("Unhandled permission type '" + type + "'");
}
}
/**
* Break the hook with all permission systems.
*/
private void unhook() {
// Reset the current used permissions system
this.handler = null;
// Print a status message to the console
logger.info("Unhooked from Permissions!");
}
/**
* Reload the permissions manager, and re-hook all permission plugins.
*/
@Override
public void reload() {
// Unhook all permission plugins
unhook();
// Set up the permissions manager again
setup();
}
/**
* Method called when a plugin is being enabled.
*
* @param pluginName The name of the plugin being enabled.
*/
public void onPluginEnable(String pluginName) {
// Check if any known permissions system is enabling
if (PermissionsSystemType.isPermissionSystem(pluginName)) {
logger.info(pluginName + " plugin enabled, dynamically updating permissions hooks!");
setup();
}
}
/**
* Method called when a plugin is being disabled.
*
* @param pluginName The name of the plugin being disabled.
*/
public void onPluginDisable(String pluginName) {
// Check if any known permission system is being disabled
if (PermissionsSystemType.isPermissionSystem(pluginName)) {
logger.info(pluginName + " plugin disabled, updating hooks!");
setup();
}
}
/**
* Return the permissions system that is hooked into.
*
* @return The permissions system, or null.
*/
public PermissionsSystemType getPermissionSystem() {
return isEnabled() ? handler.getPermissionSystem() : null;
}
/**
* Check if the command sender has permission for the given permissions node. If no permissions system is used or
* if the sender is not a player (e.g. console user), the player has to be OP in order to have the permission.
*
* @param sender The command sender.
* @param permissionNode The permissions node to verify.
*
* @return True if the sender has the permission, false otherwise.
*/
public boolean hasPermission(CommandSender sender, PermissionNode permissionNode) {
// Check if the permission node is null
if (permissionNode == null) {
return true;
}
// Return default if sender is not a player or no permission system is in use
if (!(sender instanceof Player) || !isEnabled()) {
return permissionNode.getDefaultPermission().evaluate(sender);
}
Player player = (Player) sender;
return player.hasPermission(permissionNode.getNode());
}
/**
* Check if a player has permission for the given permission node. This is for offline player checks.
* If no permissions system is used, then the player will not have permission.
*
* @param player The offline player
* @param permissionNode The permission node to verify
*
* @return true if the player has permission, false otherwise
*/
public boolean hasPermissionOffline(OfflinePlayer player, PermissionNode permissionNode) {
// Check if the permission node is null
if (permissionNode == null) {
return true;
}
if (!isEnabled()) {
return permissionNode.getDefaultPermission().evaluate(player);
}
return handler.hasPermissionOffline(player.getName(), permissionNode);
}
/**
* Check whether the offline player with the given name has permission for the given permission node.
* This method is used as a last resort when nothing besides the name is known.
*
* @param name The name of the player
* @param permissionNode The permission node to verify
*
* @return true if the player has permission, false otherwise
*/
public boolean hasPermissionOffline(String name, PermissionNode permissionNode) {
if (permissionNode == null) {
return true;
}
if (!isEnabled()) {
return permissionNode.getDefaultPermission().evaluate(null);
}
return handler.hasPermissionOffline(name, permissionNode);
}
/**
* 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.
*/
public boolean hasGroupSupport() {
return isEnabled() && handler.hasGroupSupport();
}
/**
* Get the permission groups of a player, if available.
*
* @param player The player.
*
* @return Permission groups, or an empty collection if this feature is not supported.
*/
public Collection