Added GroupUsers support.

This commit is contained in:
sk89q 2011-01-17 16:47:38 -08:00
parent b37d9a6ddc
commit 7c24c7eadc
7 changed files with 279 additions and 5 deletions

View File

@ -21,6 +21,7 @@
<include name="minecraft_server_cb.jar" /> <include name="minecraft_server_cb.jar" />
<include name="CraftBukkit.jar" /> <include name="CraftBukkit.jar" />
<include name="Bukkit.jar" /> <include name="Bukkit.jar" />
<include name="GroupUsers.jar" />
</fileset> </fileset>
</classpath> </classpath>
</javac> </javac>

View File

@ -1,6 +1,6 @@
// $Id$ // $Id$
/* /*
* WorldGuard * WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com> * Copyright (C) 2010 sk89q <http://www.sk89q.com>
* *
* This program is free software: you can redistribute it and/or modify * 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 java.util.HashMap;
import org.bukkit.util.config.Configuration; import org.bukkit.util.config.Configuration;
public class ConfigurationPermissionsResolver { public class ConfigurationPermissionsResolver implements PermissionsResolver {
private Configuration config; private Configuration config;
private Map<String,Set<String>> userPermissionsCache; private Map<String,Set<String>> userPermissionsCache;
private Map<String,Set<String>> userGroups; private Map<String,Set<String>> userGroups;

View File

@ -0,0 +1,98 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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;
}
}

View File

@ -0,0 +1,27 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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);
}

View File

@ -0,0 +1,76 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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);
}
}

View File

@ -0,0 +1,63 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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);
}
}

View File

@ -31,7 +31,8 @@ import org.bukkit.event.Event;
import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginLoader; import org.bukkit.plugin.PluginLoader;
import org.bukkit.plugin.java.JavaPlugin; 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.*; import com.sk89q.worldedit.*;
/** /**
@ -47,11 +48,13 @@ public class WorldEditPlugin extends JavaPlugin {
public final WorldEditAPI api; public final WorldEditAPI api;
private final LocalConfiguration config; private final LocalConfiguration config;
private final PermissionsResolverManager perms;
private final WorldEditPlayerListener playerListener = private final WorldEditPlayerListener playerListener =
new WorldEditPlayerListener(this); new WorldEditPlayerListener(this);
private final WorldEditBlockListener blockListener = private final WorldEditBlockListener blockListener =
new WorldEditBlockListener(this); new WorldEditBlockListener(this);
private final ConfigurationPermissionsResolver perms; private final PermissionsResolverServerListener permsListener;
public WorldEditPlugin(PluginLoader pluginLoader, Server instance, public WorldEditPlugin(PluginLoader pluginLoader, Server instance,
PluginDescriptionFile desc, File folder, File plugin, ClassLoader cLoader) { PluginDescriptionFile desc, File folder, File plugin, ClassLoader cLoader) {
@ -64,7 +67,9 @@ public class WorldEditPlugin extends JavaPlugin {
createDefaultConfiguration("config.yml"); createDefaultConfiguration("config.yml");
config = new BukkitConfiguration(getConfiguration(), logger); config = new BukkitConfiguration(getConfiguration(), logger);
perms = new ConfigurationPermissionsResolver(getConfiguration()); perms = new PermissionsResolverManager(getConfiguration(), getServer(),
"WorldEdit", logger);
permsListener = new PermissionsResolverServerListener(perms);
loadConfiguration(); loadConfiguration();
server = new BukkitServerInterface(getServer()); server = new BukkitServerInterface(getServer());
@ -90,6 +95,10 @@ public class WorldEditPlugin extends JavaPlugin {
blockListener, Priority.Normal, this); blockListener, Priority.Normal, this);
getServer().getPluginManager().registerEvent(Event.Type.BLOCK_RIGHTCLICKED, getServer().getPluginManager().registerEvent(Event.Type.BLOCK_RIGHTCLICKED,
blockListener, Priority.Normal, this); blockListener, Priority.Normal, this);
getServer().getPluginManager().registerEvent(Event.Type.BLOCK_RIGHTCLICKED,
blockListener, Priority.Normal, this);
permsListener.register(this);
} }
private void createDefaultConfiguration(String name) { private void createDefaultConfiguration(String name) {