From 488f841d69302f6347dd0a09227c19adedf7cabc Mon Sep 17 00:00:00 2001 From: sk89q Date: Wed, 2 Mar 2011 00:49:59 -0800 Subject: [PATCH] Added PermissionsProvider interface that can also handle permissions. --- .../bukkit/migration/PermissionsProvider.java | 26 ++++++++++ .../migration/PermissionsResolverManager.java | 27 ++++++++++ .../PermissionsResolverServerListener.java | 14 ++++-- .../migration/PluginPermissionsResolver.java | 49 +++++++++++++++++++ 4 files changed, 112 insertions(+), 4 deletions(-) create mode 100644 src/com/sk89q/bukkit/migration/PermissionsProvider.java create mode 100644 src/com/sk89q/bukkit/migration/PluginPermissionsResolver.java diff --git a/src/com/sk89q/bukkit/migration/PermissionsProvider.java b/src/com/sk89q/bukkit/migration/PermissionsProvider.java new file mode 100644 index 000000000..b332d7f6a --- /dev/null +++ b/src/com/sk89q/bukkit/migration/PermissionsProvider.java @@ -0,0 +1,26 @@ +// $Id$ +/* + * WorldEdit + * Copyright (C) 2010, 2011 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 PermissionsProvider { + 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 index 6bc403668..bed82f7cb 100644 --- a/src/com/sk89q/bukkit/migration/PermissionsResolverManager.java +++ b/src/com/sk89q/bukkit/migration/PermissionsResolverManager.java @@ -21,6 +21,7 @@ package com.sk89q.bukkit.migration; import java.util.logging.Logger; import org.bukkit.Server; +import org.bukkit.plugin.Plugin; import org.bukkit.util.config.Configuration; public class PermissionsResolverManager implements PermissionsResolver { @@ -40,6 +41,7 @@ public class PermissionsResolverManager implements PermissionsResolver { } public void findResolver() { + if (tryPluginPermissionsResolver()) return; if (tryGroupsUsers()) return; if (tryNijiPermissions()) return; if (tryFlatFilePermissions()) return; @@ -77,6 +79,31 @@ public class PermissionsResolverManager implements PermissionsResolver { return false; } + + private boolean tryPluginPermissionsResolver() { + for (Plugin plugin : server.getPluginManager().getPlugins()) { + if (plugin instanceof PermissionsProvider) { + perms = new PluginPermissionsResolver( + (PermissionsProvider) plugin); + logger.info(name + ": Using plugin '" + + plugin.getDescription().getName() + " for permissions."); + return true; + } + } + + return false; + } + + public void setPluginPermissionsResolver(Plugin plugin) { + if (!(plugin instanceof PermissionsProvider)) { + return; + } + + perms = new PluginPermissionsResolver( + (PermissionsProvider) plugin); + logger.info(name + ": Using plugin '" + + plugin.getDescription().getName() + " for permissions."); + } public void load() { perms.load(); diff --git a/src/com/sk89q/bukkit/migration/PermissionsResolverServerListener.java b/src/com/sk89q/bukkit/migration/PermissionsResolverServerListener.java index a5672d935..007a3b6e0 100644 --- a/src/com/sk89q/bukkit/migration/PermissionsResolverServerListener.java +++ b/src/com/sk89q/bukkit/migration/PermissionsResolverServerListener.java @@ -39,9 +39,12 @@ public class PermissionsResolverServerListener extends ServerListener { */ @Override public void onPluginEnabled(PluginEvent event) { - String name = event.getPlugin().getDescription().getName(); + Plugin plugin = event.getPlugin(); + String name = plugin.getDescription().getName(); - if (name.equalsIgnoreCase("GroupUsers") || name.equalsIgnoreCase("Permissions")) { + if (plugin instanceof PermissionsProvider) { + manager.setPluginPermissionsResolver(plugin); + } else if (name.equalsIgnoreCase("GroupUsers") || name.equalsIgnoreCase("Permissions")) { manager.findResolver(); } } @@ -53,9 +56,12 @@ public class PermissionsResolverServerListener extends ServerListener { */ @Override public void onPluginDisabled(PluginEvent event) { - String name = event.getPlugin().getDescription().getName(); + Plugin plugin = event.getPlugin(); + String name = plugin.getDescription().getName(); - if (name.equalsIgnoreCase("GroupUsers") || name.equalsIgnoreCase("Permissions")) { + if (plugin instanceof PermissionsProvider + || name.equalsIgnoreCase("GroupUsers") + || name.equalsIgnoreCase("Permissions")) { manager.findResolver(); } } diff --git a/src/com/sk89q/bukkit/migration/PluginPermissionsResolver.java b/src/com/sk89q/bukkit/migration/PluginPermissionsResolver.java new file mode 100644 index 000000000..44990385f --- /dev/null +++ b/src/com/sk89q/bukkit/migration/PluginPermissionsResolver.java @@ -0,0 +1,49 @@ +// $Id$ +/* + * WorldEdit + * Copyright (C) 2010, 2011 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 class PluginPermissionsResolver implements PermissionsResolver { + + protected PermissionsProvider resolver; + + public PluginPermissionsResolver(PermissionsProvider resolver) { + this.resolver = resolver; + } + + @Override + public void load() { + } + + @Override + public boolean hasPermission(String name, String permission) { + return resolver.hasPermission(name, permission); + } + + @Override + public boolean inGroup(String player, String group) { + return resolver.inGroup(player, group); + } + + @Override + public String[] getGroups(String player) { + return resolver.getGroups(player); + } + +}