From 7c24c7eadc8a05eff4c95659c0c5a2c9b0498f79 Mon Sep 17 00:00:00 2001 From: sk89q Date: Mon, 17 Jan 2011 16:47:38 -0800 Subject: [PATCH] Added GroupUsers support. --- build.xml | 1 + .../ConfigurationPermissionsResolver.java | 4 +- .../GroupUsersPemissionsResolver.java | 98 +++++++++++++++++++ .../bukkit/migration/PermissionsResolver.java | 27 +++++ .../migration/PermissionsResolverManager.java | 76 ++++++++++++++ .../PermissionsResolverServerListener.java | 63 ++++++++++++ .../worldedit/bukkit/WorldEditPlugin.java | 15 ++- 7 files changed, 279 insertions(+), 5 deletions(-) create mode 100644 src/com/sk89q/bukkit/migration/GroupUsersPemissionsResolver.java create mode 100644 src/com/sk89q/bukkit/migration/PermissionsResolver.java create mode 100644 src/com/sk89q/bukkit/migration/PermissionsResolverManager.java create mode 100644 src/com/sk89q/bukkit/migration/PermissionsResolverServerListener.java diff --git a/build.xml b/build.xml index 94364af11..6e6e680e6 100644 --- a/build.xml +++ b/build.xml @@ -21,6 +21,7 @@ + diff --git a/src/com/sk89q/bukkit/migration/ConfigurationPermissionsResolver.java b/src/com/sk89q/bukkit/migration/ConfigurationPermissionsResolver.java index 7029af233..8771217a4 100644 --- a/src/com/sk89q/bukkit/migration/ConfigurationPermissionsResolver.java +++ b/src/com/sk89q/bukkit/migration/ConfigurationPermissionsResolver.java @@ -1,6 +1,6 @@ // $Id$ /* - * WorldGuard + * WorldEdit * Copyright (C) 2010 sk89q * * This program is free software: you can redistribute it and/or modify @@ -26,7 +26,7 @@ import java.util.Set; import java.util.HashMap; import org.bukkit.util.config.Configuration; -public class ConfigurationPermissionsResolver { +public class ConfigurationPermissionsResolver implements PermissionsResolver { private Configuration config; private Map> userPermissionsCache; private Map> userGroups; diff --git a/src/com/sk89q/bukkit/migration/GroupUsersPemissionsResolver.java b/src/com/sk89q/bukkit/migration/GroupUsersPemissionsResolver.java new file mode 100644 index 000000000..f42d652c1 --- /dev/null +++ b/src/com/sk89q/bukkit/migration/GroupUsersPemissionsResolver.java @@ -0,0 +1,98 @@ +// $Id$ +/* + * WorldEdit + * Copyright (C) 2010 sk89q + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +*/ + +package com.sk89q.bukkit.migration; + +import java.lang.reflect.*; +import org.bukkit.Server; +import org.bukkit.entity.Player; +import org.bukkit.event.Event; +import org.bukkit.event.player.PlayerChatEvent; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.PluginManager; +import com.bukkit.authorblues.GroupUsers.GroupUsers; +import com.bukkit.authorblues.GroupUsers.GroupUsersPlayerListener; + +public class GroupUsersPemissionsResolver implements PermissionsResolver { + private Server server; + private GroupUsersPlayerListener listener; + + public void load() { + + } + + public GroupUsersPemissionsResolver(Server server) + throws PluginAccessException, MissingPluginException { + this.server = server; + PluginManager manager = server.getPluginManager(); + + Plugin plugin = manager.getPlugin("GroupUsers"); + if (plugin == null) { + throw new MissingPluginException(); + } + + GroupUsers groupUsers = (GroupUsers)plugin; + try { + Field field = groupUsers.getClass().getDeclaredField("playerListener"); + field.setAccessible(true); + listener = (GroupUsersPlayerListener)field.get(groupUsers); + } catch (SecurityException e) { + throw new PluginAccessException(); + } catch (NoSuchFieldException e) { + throw new PluginAccessException(); + } catch (IllegalArgumentException e) { + throw new PluginAccessException(); + } catch (IllegalAccessException e) { + throw new PluginAccessException(); + } catch (ClassCastException e) { + throw new PluginAccessException(); + } + } + + public boolean hasPermission(String name, String permission) { + try { + Player player = server.getPlayer(name); + if (player == null) { + return false; + } + PlayerChatEvent event = new PlayerChatEvent(Event.Type.PLAYER_CHAT, player, permission); + listener.onPlayerCommand(event); + return !event.isCancelled(); + } catch (Throwable t) { + t.printStackTrace(); + return false; + } + } + + public boolean inGroup(String player, String group) { + return false; + } + + public String[] getGroups(String player) { + return new String[0]; + } + + public static class PluginAccessException extends Exception { + private static final long serialVersionUID = 7044832912491608706L; + } + + public static class MissingPluginException extends Exception { + private static final long serialVersionUID = 7044832912491608706L; + } +} diff --git a/src/com/sk89q/bukkit/migration/PermissionsResolver.java b/src/com/sk89q/bukkit/migration/PermissionsResolver.java new file mode 100644 index 000000000..3c3cf41ec --- /dev/null +++ b/src/com/sk89q/bukkit/migration/PermissionsResolver.java @@ -0,0 +1,27 @@ +// $Id$ +/* + * WorldEdit + * Copyright (C) 2010 sk89q + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +*/ + +package com.sk89q.bukkit.migration; + +public interface PermissionsResolver { + public void load(); + public boolean hasPermission(String name, String permission); + public boolean inGroup(String player, String group); + public String[] getGroups(String player); +} diff --git a/src/com/sk89q/bukkit/migration/PermissionsResolverManager.java b/src/com/sk89q/bukkit/migration/PermissionsResolverManager.java new file mode 100644 index 000000000..08c5be985 --- /dev/null +++ b/src/com/sk89q/bukkit/migration/PermissionsResolverManager.java @@ -0,0 +1,76 @@ +// $Id$ +/* + * WorldEdit + * Copyright (C) 2010 sk89q + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +*/ + +package com.sk89q.bukkit.migration; + +import java.util.logging.Logger; +import org.bukkit.Server; +import org.bukkit.util.config.Configuration; +import com.sk89q.bukkit.migration.GroupUsersPemissionsResolver.MissingPluginException; +import com.sk89q.bukkit.migration.GroupUsersPemissionsResolver.PluginAccessException; + +public class PermissionsResolverManager implements PermissionsResolver { + private Configuration config; + private Server server; + private PermissionsResolver perms; + private String name; + private Logger logger; + + public PermissionsResolverManager(Configuration config, Server server, String name, Logger logger) { + this.config = config; + this.server = server; + this.name = name; + this.logger = logger; + + findResolver(); + } + + public void findResolver() { + try { + perms = new GroupUsersPemissionsResolver(server); + logger.info(name + ": GroupUsers detected! Using GroupUsers for permissions."); + } catch (PluginAccessException e) { + perms = new ConfigurationPermissionsResolver(config); + logger.warning(name + ": Failed to access GroupUsers. Falling back to configuration file for permissions."); + } catch (NoClassDefFoundError e) { + perms = new ConfigurationPermissionsResolver(config); + logger.info(name + ": No known permissions plugin detected. Using configuration file for permissions. (code: 10)"); + } catch (MissingPluginException e) { + perms = new ConfigurationPermissionsResolver(config); + logger.info(name + ": No known permissions plugin detected. Using configuration file for permissions. (code: 11)"); + } + } + + public void load() { + perms.load(); + } + + public boolean hasPermission(String name, String permission) { + return perms.hasPermission(name, permission); + } + + public boolean inGroup(String player, String group) { + return perms.inGroup(player, group); + } + + public String[] getGroups(String player) { + return perms.getGroups(player); + } + +} diff --git a/src/com/sk89q/bukkit/migration/PermissionsResolverServerListener.java b/src/com/sk89q/bukkit/migration/PermissionsResolverServerListener.java new file mode 100644 index 000000000..f81626768 --- /dev/null +++ b/src/com/sk89q/bukkit/migration/PermissionsResolverServerListener.java @@ -0,0 +1,63 @@ +// $Id$ +/* + * WorldEdit + * Copyright (C) 2010 sk89q + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +*/ + +package com.sk89q.bukkit.migration; + +import org.bukkit.event.Event; +import org.bukkit.event.Event.Priority; +import org.bukkit.event.server.PluginEvent; +import org.bukkit.event.server.ServerListener; +import org.bukkit.plugin.Plugin; + +public class PermissionsResolverServerListener extends ServerListener { + private PermissionsResolverManager manager; + + public PermissionsResolverServerListener(PermissionsResolverManager manager) { + this.manager = manager; + } + + /** + * Called when a plugin is enabled + * + * @param event Relevant event details + */ + public void onPluginEnabled(PluginEvent event) { + if (event.getPlugin().getDescription().getName().equalsIgnoreCase("GroupUsers")) { + manager.findResolver(); + } + } + + /** + * Called when a plugin is disabled + * + * @param event Relevant event details + */ + public void onPluginDisabled(PluginEvent event) { + if (event.getPlugin().getDescription().getName().equalsIgnoreCase("GroupUsers")) { + manager.findResolver(); + } + } + + public void register(Plugin plugin) { + plugin.getServer().getPluginManager().registerEvent(Event.Type.PLUGIN_ENABLE, + this, Priority.Normal, plugin); + plugin.getServer().getPluginManager().registerEvent(Event.Type.PLUGIN_DISABLE, + this, Priority.Normal, plugin); + } +} diff --git a/src/com/sk89q/worldedit/bukkit/WorldEditPlugin.java b/src/com/sk89q/worldedit/bukkit/WorldEditPlugin.java index e312da36e..e8c8361a5 100644 --- a/src/com/sk89q/worldedit/bukkit/WorldEditPlugin.java +++ b/src/com/sk89q/worldedit/bukkit/WorldEditPlugin.java @@ -31,7 +31,8 @@ import org.bukkit.event.Event; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginLoader; import org.bukkit.plugin.java.JavaPlugin; -import com.sk89q.bukkit.migration.ConfigurationPermissionsResolver; +import com.sk89q.bukkit.migration.PermissionsResolverManager; +import com.sk89q.bukkit.migration.PermissionsResolverServerListener; import com.sk89q.worldedit.*; /** @@ -47,11 +48,13 @@ public class WorldEditPlugin extends JavaPlugin { public final WorldEditAPI api; private final LocalConfiguration config; + private final PermissionsResolverManager perms; + private final WorldEditPlayerListener playerListener = new WorldEditPlayerListener(this); private final WorldEditBlockListener blockListener = new WorldEditBlockListener(this); - private final ConfigurationPermissionsResolver perms; + private final PermissionsResolverServerListener permsListener; public WorldEditPlugin(PluginLoader pluginLoader, Server instance, PluginDescriptionFile desc, File folder, File plugin, ClassLoader cLoader) { @@ -64,7 +67,9 @@ public class WorldEditPlugin extends JavaPlugin { createDefaultConfiguration("config.yml"); config = new BukkitConfiguration(getConfiguration(), logger); - perms = new ConfigurationPermissionsResolver(getConfiguration()); + perms = new PermissionsResolverManager(getConfiguration(), getServer(), + "WorldEdit", logger); + permsListener = new PermissionsResolverServerListener(perms); loadConfiguration(); server = new BukkitServerInterface(getServer()); @@ -90,6 +95,10 @@ public class WorldEditPlugin extends JavaPlugin { blockListener, Priority.Normal, this); getServer().getPluginManager().registerEvent(Event.Type.BLOCK_RIGHTCLICKED, blockListener, Priority.Normal, this); + getServer().getPluginManager().registerEvent(Event.Type.BLOCK_RIGHTCLICKED, + blockListener, Priority.Normal, this); + + permsListener.register(this); } private void createDefaultConfiguration(String name) {