From 01fa16aa543219ae2cde699602ffebbcdb3e05e4 Mon Sep 17 00:00:00 2001 From: sk89q Date: Fri, 21 Jan 2011 17:35:10 -0800 Subject: [PATCH] Added support for Niji's permission system. --- build.xml | 1 + .../migration/NijiPermissionsResolver.java | 101 ++++++++++++++++++ .../migration/PermissionsResolverManager.java | 17 +-- .../PermissionsResolverServerListener.java | 8 +- 4 files changed, 118 insertions(+), 9 deletions(-) create mode 100644 src/com/sk89q/bukkit/migration/NijiPermissionsResolver.java diff --git a/build.xml b/build.xml index 7b814c675..ce2c1495f 100644 --- a/build.xml +++ b/build.xml @@ -22,6 +22,7 @@ + diff --git a/src/com/sk89q/bukkit/migration/NijiPermissionsResolver.java b/src/com/sk89q/bukkit/migration/NijiPermissionsResolver.java new file mode 100644 index 000000000..3c313ba67 --- /dev/null +++ b/src/com/sk89q/bukkit/migration/NijiPermissionsResolver.java @@ -0,0 +1,101 @@ +// $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.Server; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.PluginManager; +import com.nijikokun.bukkit.Permissions.Permissions; + +public class NijiPermissionsResolver implements PermissionsResolver { + private Server server; + private Permissions api; + + public void load() { + + } + + public NijiPermissionsResolver(Server server) + throws PluginAccessException, MissingPluginException { + this.server = server; + PluginManager manager = server.getPluginManager(); + + Plugin plugin = manager.getPlugin("Permissions"); + if (plugin == null) { + throw new MissingPluginException(); + } + + try { + api = (Permissions)plugin; + } catch (ClassCastException e) { + throw new PluginAccessException(); + } + } + + @SuppressWarnings("static-access") + public boolean hasPermission(String name, String permission) { + try { + Player player = server.getPlayer(name); + if (player == null) return false; + return api.Security.permission(player, permission); + } catch (Throwable t) { + t.printStackTrace(); + return false; + } + } + + @SuppressWarnings("static-access") + public boolean inGroup(String name, String group) { + try { + Player player = server.getPlayer(name); + if (player == null) return false; + return group.equals(api.Security.getGroup(player.getName())); + } catch (Throwable t) { + t.printStackTrace(); + return false; + } + } + + @SuppressWarnings("static-access") + public String[] getGroups(String name) { + try { + Player player = server.getPlayer(name); + if (player == null) return new String[0]; + String group = api.Security.getGroup(player.getName()); + if (group == null) { + return new String[0]; + } else { + return new String[]{ group }; + } + } catch (Throwable t) { + t.printStackTrace(); + 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/PermissionsResolverManager.java b/src/com/sk89q/bukkit/migration/PermissionsResolverManager.java index 7a6ec0ca7..6bc403668 100644 --- a/src/com/sk89q/bukkit/migration/PermissionsResolverManager.java +++ b/src/com/sk89q/bukkit/migration/PermissionsResolverManager.java @@ -22,8 +22,6 @@ 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; @@ -43,6 +41,7 @@ public PermissionsResolverManager(Configuration config, Server server, String na public void findResolver() { if (tryGroupsUsers()) return; + if (tryNijiPermissions()) return; if (tryFlatFilePermissions()) return; perms = new ConfigurationPermissionsResolver(config); @@ -54,12 +53,16 @@ private boolean tryGroupsUsers() { perms = new GroupUsersPemissionsResolver(server); logger.info(name + ": GroupUsers detected! Using GroupUsers for permissions."); return true; - } catch (PluginAccessException e) { - return false; - } catch (NoClassDefFoundError e) { - return false; - } catch (MissingPluginException e) { + } catch (Throwable e) { return false; + } + } + + private boolean tryNijiPermissions() { + try { + perms = new NijiPermissionsResolver(server); + logger.info(name + ": Permissions plugin detected! Using Permissions plugin for permissions."); + return true; } catch (Throwable e) { return false; } diff --git a/src/com/sk89q/bukkit/migration/PermissionsResolverServerListener.java b/src/com/sk89q/bukkit/migration/PermissionsResolverServerListener.java index f81626768..b870c6500 100644 --- a/src/com/sk89q/bukkit/migration/PermissionsResolverServerListener.java +++ b/src/com/sk89q/bukkit/migration/PermissionsResolverServerListener.java @@ -38,7 +38,9 @@ public PermissionsResolverServerListener(PermissionsResolverManager manager) { * @param event Relevant event details */ public void onPluginEnabled(PluginEvent event) { - if (event.getPlugin().getDescription().getName().equalsIgnoreCase("GroupUsers")) { + String name = event.getPlugin().getDescription().getName(); + + if (name.equalsIgnoreCase("GroupUsers") || name.equalsIgnoreCase("Permissions")) { manager.findResolver(); } } @@ -49,7 +51,9 @@ public void onPluginEnabled(PluginEvent event) { * @param event Relevant event details */ public void onPluginDisabled(PluginEvent event) { - if (event.getPlugin().getDescription().getName().equalsIgnoreCase("GroupUsers")) { + String name = event.getPlugin().getDescription().getName(); + + if (name.equalsIgnoreCase("GroupUsers") || name.equalsIgnoreCase("Permissions")) { manager.findResolver(); } }