Moved WEPIF to the com.sk89q.wepif package and made PermissionsResolverManager a singleton

Added OfflinePlayer permissions fetching methods to WEPIF
This commit is contained in:
zml2008 2011-12-04 09:56:35 -08:00
parent 04a08e7e0c
commit daf1dde701
18 changed files with 726 additions and 275 deletions

View File

@ -0,0 +1,103 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010, 2011 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 com.sk89q.util.yaml.YAMLProcessor;
import org.bukkit.OfflinePlayer;
import org.bukkit.Server;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.RegisteredServiceProvider;
import java.util.List;
@Deprecated
public class LegacyPluginPermissionsResolver implements com.sk89q.wepif.PermissionsResolver {
protected PermissionsProvider resolver;
protected Plugin plugin;
public static com.sk89q.wepif.PermissionsResolver factory(Server server, YAMLProcessor config) {
// Looking for service
RegisteredServiceProvider<PermissionsProvider> serviceProvider = server.getServicesManager().getRegistration(PermissionsProvider.class);
if (serviceProvider != null) {
return new LegacyPluginPermissionsResolver(serviceProvider.getProvider(), serviceProvider.getPlugin());
}
// Looking for plugin
for (Plugin plugin : server.getPluginManager().getPlugins()) {
if (plugin instanceof PermissionsProvider) {
return new LegacyPluginPermissionsResolver((PermissionsProvider) plugin, plugin);
}
}
return null;
}
public LegacyPluginPermissionsResolver(PermissionsProvider resolver, Plugin permissionsPlugin) {
this.resolver = resolver;
this.plugin = permissionsPlugin;
}
public void load() {
}
public boolean hasPermission(String name, String permission) {
return resolver.hasPermission(name, permission);
}
public boolean hasPermission(String worldName, String name, String permission) {
return resolver.hasPermission(worldName, name, permission);
}
public boolean inGroup(String player, String group) {
return resolver.inGroup(player, group);
}
public String[] getGroups(String player) {
return resolver.getGroups(player);
}
public boolean hasPermission(OfflinePlayer player, String permission) {
return hasPermission(player.getName(), permission);
}
public boolean hasPermission(String worldName, OfflinePlayer player, String permission) {
return hasPermission(worldName, player.getName(), permission);
}
public boolean inGroup(OfflinePlayer player, String group) {
return inGroup(player.getName(), group);
}
public String[] getGroups(OfflinePlayer player) {
return getGroups(player.getName());
}
public String getDetectionMessage() {
String author = "the author";
List<String> authors = plugin.getDescription().getAuthors();
if (authors != null && authors.size() > 0) {
author = authors.get(0);
}
return "Using legacy plugin '" + this.plugin.getDescription().getName() + "' for permissions. Bug " + author + " to update it! ";
}
}

View File

@ -19,6 +19,7 @@
package com.sk89q.bukkit.migration; package com.sk89q.bukkit.migration;
@Deprecated
public interface PermissionsProvider { public interface PermissionsProvider {
public boolean hasPermission(String name, String permission); public boolean hasPermission(String name, String permission);
@ -27,4 +28,5 @@ public interface PermissionsProvider {
public boolean inGroup(String player, String group); public boolean inGroup(String player, String group);
public String[] getGroups(String player); public String[] getGroups(String player);
} }

View File

@ -19,8 +19,7 @@
package com.sk89q.bukkit.migration; package com.sk89q.bukkit.migration;
public interface PermissionsResolver extends PermissionsProvider {
public void load();
public String getDetectionMessage(); @Deprecated
public interface PermissionsResolver extends com.sk89q.wepif.PermissionsResolver {
} }

View File

@ -19,228 +19,71 @@
package com.sk89q.bukkit.migration; package com.sk89q.bukkit.migration;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.logging.Logger; import java.util.logging.Logger;
import com.sk89q.util.yaml.YAMLFormat;
import com.sk89q.util.yaml.YAMLProcessor;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
public class PermissionsResolverManager implements PermissionsResolver { @Deprecated
private static final String CONFIG_HEADER = "#\r\n" + public class PermissionsResolverManager implements PermissionsProvider {
"# WEPIF Configuration File\r\n" + private static boolean setUp;
"#\r\n" +
"# This file handles permissions configuration for every plugin using WEPIF\r\n" +
"#\r\n" +
"# About editing this file:\r\n" +
"# - DO NOT USE TABS. You MUST use spaces or Bukkit will complain. If\r\n" +
"# you use an editor like Notepad++ (recommended for Windows users), you\r\n" +
"# must configure it to \"replace tabs with spaces.\" In Notepad++, this can\r\n" +
"# be changed in Settings > Preferences > Language Menu.\r\n" +
"# - Don't get rid of the indents. They are indented so some entries are\r\n" +
"# in categories (like \"enforce-single-session\" is in the \"protection\"\r\n" +
"# category.\r\n" +
"# - If you want to check the format of this file before putting it\r\n" +
"# into WEPIF, paste it into http://yaml-online-parser.appspot.com/\r\n" +
"# and see if it gives \"ERROR:\".\r\n" +
"# - Lines starting with # are comments and so they are ignored.\r\n" +
"#\r\n" +
"# About Configuration Permissions\r\n" +
"# - See http://wiki.sk89q.com/wiki/WorldEdit/Permissions/Bukkit\r\n" +
"# - Now with multiworld support (see example)\r\n" +
"\r\n";
private Server server;
private PermissionsResolver permissionResolver;
private PermissionsResolverServerListener listener;
private YAMLProcessor config;
private String name;
private Logger logger;
private List<Class<? extends PermissionsResolver>> enabledResolvers = new ArrayList<Class<? extends PermissionsResolver>>();
@SuppressWarnings("unchecked")
protected Class<? extends PermissionsResolver>[] availableResolvers = new Class[] {
PluginPermissionsResolver.class,
PermissionsExResolver.class,
NijiPermissionsResolver.class,
DinnerPermsResolver.class,
FlatFilePermissionsResolver.class
};
@Deprecated @Deprecated
public PermissionsResolverManager(org.bukkit.util.config.Configuration config, Server server, String name, Logger logger) { public PermissionsResolverManager(org.bukkit.util.config.Configuration config, Server server, String name, Logger logger) {}
this.server = server;
this.name = name;
this.logger = logger;
loadConfig(new File("wepif.yml")); // TODO: config migration, maybe
findResolver();
}
@Deprecated
public PermissionsResolverManager(Plugin plugin, String name, Logger logger) { public PermissionsResolverManager(Plugin plugin, String name, Logger logger) {
this.server = plugin.getServer(); if (!setUp) {
this.name = name; setUp(plugin);
this.logger = logger; }
this.listener = new PermissionsResolverServerListener(this, plugin);
loadConfig(new File("wepif.yml"));
findResolver();
} }
@Deprecated
public void findResolver() { public void findResolver() {
for (Class<? extends PermissionsResolver> resolverClass : enabledResolvers) { getRealResolver().findResolver();
try {
Method factoryMethod = resolverClass.getMethod("factory", Server.class, YAMLProcessor.class);
this.permissionResolver = (PermissionsResolver) factoryMethod.invoke(null, this.server, this.config);
if (this.permissionResolver != null) {
break;
}
} catch (Throwable e) {
logger.warning("Error in factory method for " + resolverClass.getSimpleName() + ": " + e);
e.printStackTrace();
continue;
}
}
if (permissionResolver == null) {
permissionResolver = new ConfigurationPermissionsResolver(config);
}
permissionResolver.load();
logger.info(name + ": " + permissionResolver.getDetectionMessage());
} }
@Deprecated
public void setPluginPermissionsResolver(Plugin plugin) { public void setPluginPermissionsResolver(Plugin plugin) {
if (!(plugin instanceof PermissionsProvider)) { getRealResolver().setPluginPermissionsResolver(plugin);
return;
}
permissionResolver = new PluginPermissionsResolver((PermissionsProvider) plugin, plugin);
logger.info(name + ": " + permissionResolver.getDetectionMessage());
} }
@Deprecated
public void load() { public void load() {
permissionResolver.load(); getRealResolver().load();
} }
public boolean hasPermission(String name, String permission) { public boolean hasPermission(String name, String permission) {
return permissionResolver.hasPermission(name, permission); return getRealResolver().hasPermission(name, permission);
} }
public boolean hasPermission(String worldName, String name, String permission) { public boolean hasPermission(String worldName, String name, String permission) {
return permissionResolver.hasPermission(worldName, name, permission); return getRealResolver().hasPermission(worldName, name, permission);
} }
public boolean inGroup(String player, String group) { public boolean inGroup(String player, String group) {
return permissionResolver.inGroup(player, group); return getRealResolver().inGroup(player, group);
} }
public String[] getGroups(String player) { public String[] getGroups(String player) {
return permissionResolver.getGroups(player); return getRealResolver().getGroups(player);
}
private boolean loadConfig(File file) {
boolean isUpdated = false;
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
config = new YAMLProcessor(file, false, YAMLFormat.EXTENDED);
try {
config.load();
} catch (IOException e) {
logger.severe("Error loading WEPIF Config: " + e);
e.printStackTrace();
}
List<String> keys = config.getKeys(null);
config.setHeader(CONFIG_HEADER);
if (!keys.contains("ignore-nijiperms-bridges")) {
config.setProperty("ignore-nijiperms-bridges", true);
isUpdated = true;
}
if (!keys.contains("resolvers")) {
//List<String> resolverKeys = config.getKeys("resolvers");
List<String> resolvers = new ArrayList<String>();
for (Class<?> clazz : availableResolvers) {
resolvers.add(clazz.getSimpleName());
}
enabledResolvers.addAll(Arrays.asList(availableResolvers));
config.setProperty("resolvers.enabled", resolvers);
isUpdated = true;
} else {
List<String> disabledResolvers = config.getStringList("resolvers.disabled", new ArrayList<String>());
List<String> stagedEnabled = config.getStringList("resolvers.enabled", null);
for (Iterator<String> i = stagedEnabled.iterator(); i.hasNext();) {
String nextName = i.next();
Class<?> next = null;
try {
next = Class.forName(getClass().getPackage().getName() + "." + nextName);
} catch (ClassNotFoundException e) {}
if (next == null || !PermissionsResolver.class.isAssignableFrom(next)) {
logger.warning("WEPIF: Invalid or unknown class found in enabled resolvers: "
+ nextName + ". Moving to disabled resolvers list.");
i.remove();
disabledResolvers.add(nextName);
isUpdated = true;
continue;
}
enabledResolvers.add(next.asSubclass(PermissionsResolver.class));
}
for (Class<?> clazz : availableResolvers) {
if (!stagedEnabled.contains(clazz.getSimpleName()) &&
!disabledResolvers.contains(clazz.getSimpleName())) {
disabledResolvers.add(clazz.getSimpleName());
logger.info("New permissions resolver: "
+ clazz.getSimpleName() + " detected. " +
"Added to disabled resolvers list.");
isUpdated = true;
}
}
config.setProperty("resolvers.disabled", disabledResolvers);
config.setProperty("resolvers.enabled", stagedEnabled);
}
if (keys.contains("dinner-perms") || keys.contains("dinnerperms")) {
config.setProperty("dinner-perms", null);
config.setProperty("dinnerperms", null);
isUpdated = true;
}
if (!keys.contains("permissions")) {
ConfigurationPermissionsResolver.generateDefaultPerms(
config.addNode("permissions"));
isUpdated = true;
}
if (isUpdated) {
logger.info("WEPIF: Updated config file");
config.save();
}
return isUpdated;
}
boolean hasServerListener() {
return listener != null;
}
void setServerListener(PermissionsResolverServerListener listener) {
this.listener = listener;
}
public static class MissingPluginException extends Exception {
private static final long serialVersionUID = 7044832912491608706L;
} }
public String getDetectionMessage() { public String getDetectionMessage() {
return "Using WEPIF for permissions"; return getRealResolver().getDetectionMessage();
}
boolean isSetUp() {
return setUp;
}
void setUp(Plugin plugin) {
com.sk89q.wepif.PermissionsResolverManager.initialize(plugin);
setUp = true;
}
public com.sk89q.wepif.PermissionsResolverManager getRealResolver() {
return com.sk89q.wepif.PermissionsResolverManager.getInstance();
} }
} }

View File

@ -19,13 +19,10 @@
package com.sk89q.bukkit.migration; package com.sk89q.bukkit.migration;
import org.bukkit.event.Event;
import org.bukkit.event.Event.Priority;
import org.bukkit.event.server.PluginDisableEvent;
import org.bukkit.event.server.PluginEnableEvent;
import org.bukkit.event.server.ServerListener; import org.bukkit.event.server.ServerListener;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
@Deprecated
public class PermissionsResolverServerListener extends ServerListener { public class PermissionsResolverServerListener extends ServerListener {
private PermissionsResolverManager manager; private PermissionsResolverManager manager;
@ -34,52 +31,17 @@ public class PermissionsResolverServerListener extends ServerListener {
this.manager = manager; this.manager = manager;
} }
@Deprecated
public PermissionsResolverServerListener(PermissionsResolverManager manager, Plugin plugin) { public PermissionsResolverServerListener(PermissionsResolverManager manager, Plugin plugin) {
this.manager = manager; this.manager = manager;
if (!manager.hasServerListener()) { register(plugin);
register(plugin);
}
}
/**
* Called when a plugin is enabled
*
* @param event Relevant event details
*/
@Override
public void onPluginEnable(PluginEnableEvent event) {
Plugin plugin = event.getPlugin();
String name = plugin.getDescription().getName();
if (plugin instanceof PermissionsProvider) {
manager.setPluginPermissionsResolver(plugin);
} else if ("Permissions".equals(name) || "PermissionsEx".equals(name)) {
manager.findResolver();
manager.load();
}
}
/**
* Called when a plugin is disabled
*
* @param event Relevant event details
*/
@Override
public void onPluginDisable(PluginDisableEvent event) {
Plugin plugin = event.getPlugin();
String name = plugin.getDescription().getName();
if (plugin instanceof PermissionsProvider || "Permissions".equals(name) || "PermissionsEx".equals(name)) {
manager.findResolver();
manager.load();
}
} }
@Deprecated
public void register(Plugin plugin) { public void register(Plugin plugin) {
plugin.getServer().getPluginManager().registerEvent(Event.Type.PLUGIN_ENABLE, if (!manager.isSetUp()) {
this, Priority.Normal, plugin); manager.setUp(plugin);
plugin.getServer().getPluginManager().registerEvent(Event.Type.PLUGIN_DISABLE, }
this, Priority.Normal, plugin);
manager.setServerListener(this);
} }
} }

View File

@ -17,12 +17,13 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package com.sk89q.bukkit.migration; package com.sk89q.wepif;
import java.util.*; import java.util.*;
import com.sk89q.util.yaml.YAMLNode; import com.sk89q.util.yaml.YAMLNode;
import com.sk89q.util.yaml.YAMLProcessor; import com.sk89q.util.yaml.YAMLProcessor;
import org.bukkit.OfflinePlayer;
public class ConfigurationPermissionsResolver implements PermissionsResolver { public class ConfigurationPermissionsResolver implements PermissionsResolver {
private YAMLProcessor config; private YAMLProcessor config;
@ -35,13 +36,13 @@ public class ConfigurationPermissionsResolver implements PermissionsResolver {
} }
public static YAMLNode generateDefaultPerms(YAMLNode section) { public static YAMLNode generateDefaultPerms(YAMLNode section) {
section.setProperty("permissions.groups.default.permissions", new String[] { section.setProperty("groups.default.permissions", new String[] {
"worldedit.reload", "worldedit.reload",
"worldedit.selection", "worldedit.selection",
"worlds.creative.worldedit.region" }); "worlds.creative.worldedit.region"});
section.setProperty("permissions.groups.admins.permissions", new String[] { "*" }); section.setProperty("groups.admins.permissions", new String[] { "*" });
section.setProperty("permissions.users.sk89q.permissions", new String[] { "worldedit" }); section.setProperty("users.sk89q.permissions", new String[] { "worldedit" });
section.setProperty("permissions.users.sk89q.groups", new String[] { "admins" }); section.setProperty("users.sk89q.groups", new String[] { "admins" });
return section; return section;
} }
@ -142,6 +143,22 @@ public class ConfigurationPermissionsResolver implements PermissionsResolver {
return groups.toArray(new String[groups.size()]); return groups.toArray(new String[groups.size()]);
} }
public boolean hasPermission(OfflinePlayer player, String permission) {
return hasPermission(player.getName(), permission);
}
public boolean hasPermission(String worldName, OfflinePlayer player, String permission) {
return hasPermission(worldName, player.getName(), permission);
}
public boolean inGroup(OfflinePlayer player, String group) {
return inGroup(player.getName(), group);
}
public String[] getGroups(OfflinePlayer player) {
return getGroups(player.getName());
}
public String getDetectionMessage() { public String getDetectionMessage() {
return "No known permissions plugin detected. Using configuration file for permissions."; return "No known permissions plugin detected. Using configuration file for permissions.";
} }

View File

@ -17,10 +17,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package com.sk89q.bukkit.migration; package com.sk89q.wepif;
import com.sk89q.util.yaml.YAMLProcessor; import com.sk89q.util.yaml.YAMLProcessor;
import org.bukkit.OfflinePlayer;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permissible; import org.bukkit.permissions.Permissible;
import org.bukkit.permissions.Permission; import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionAttachmentInfo; import org.bukkit.permissions.PermissionAttachmentInfo;
@ -45,43 +47,59 @@ public class DinnerPermsResolver implements PermissionsResolver {
} }
public boolean hasPermission(String name, String permission) { public boolean hasPermission(String name, String permission) {
Permissible perms = server.getPlayerExact(name); return hasPermission(server.getOfflinePlayer(name), permission);
}
public boolean hasPermission(String worldName, String name, String permission) {
return hasPermission(worldName, server.getOfflinePlayer(name), permission);
}
public boolean inGroup(String name, String group) {
return inGroup(server.getOfflinePlayer(name), group);
}
public String[] getGroups(String name) {
return getGroups(server.getOfflinePlayer(name));
}
public boolean hasPermission(OfflinePlayer player, String permission) {
Permissible perms = getPermissible(player);
if (perms == null) { if (perms == null) {
return false; // Permissions are only registered for online players return false; // Permissions are only registered for objects with a Permissible
} }
switch (internalHasPermission(perms, permission)) { switch (internalHasPermission(perms, permission)) {
case -1:
return false;
case 1:
return true;
}
int dotPos = permission.lastIndexOf(".");
while (dotPos > -1) {
switch (internalHasPermission(perms, permission.substring(0, dotPos + 1) + "*")) {
case -1: case -1:
return false; return false;
case 1: case 1:
return true; return true;
}
int dotPos = permission.lastIndexOf(".");
while (dotPos > -1) {
switch (internalHasPermission(perms, permission.substring(0, dotPos + 1) + "*")) {
case -1:
return false;
case 1:
return true;
} }
dotPos = permission.lastIndexOf(".", dotPos - 1); dotPos = permission.lastIndexOf(".", dotPos - 1);
} }
return internalHasPermission(perms, "*") == 1; return internalHasPermission(perms, "*") == 1;
} }
public boolean hasPermission(String worldName, String name, String permission) { public boolean hasPermission(String worldName, OfflinePlayer player, String permission) {
return hasPermission(name, permission); // no per-world ability to check permissions in dinnerperms return hasPermission(player, permission); // no per-world ability to check permissions in dinnerperms
} }
public boolean inGroup(String name, String group) { public boolean inGroup(OfflinePlayer player, String group) {
Permissible perms = server.getPlayerExact(name); Permissible perms = getPermissible(player);
if (perms == null) { if (perms == null) {
return false; return false;
} }
return perms.hasPermission(GROUP_PREFIX + group); return perms.hasPermission(GROUP_PREFIX + group);
} }
public String[] getGroups(String name) { public String[] getGroups(OfflinePlayer player) {
Permissible perms = server.getPlayerExact(name); Permissible perms = getPermissible(player);
if (perms == null) { if (perms == null) {
return new String[0]; return new String[0];
} }
@ -95,6 +113,18 @@ public class DinnerPermsResolver implements PermissionsResolver {
} }
return groupNames.toArray(new String[groupNames.size()]); return groupNames.toArray(new String[groupNames.size()]);
} }
public Permissible getPermissible(OfflinePlayer offline) {
if (offline == null) return null;
Permissible perm = null;
if (offline instanceof Permissible) {
perm = (Permissible) offline;
} else {
Player player = offline.getPlayer();
if (player != null) perm = player;
}
return perm;
}
/** /**
* Checks the permission from dinnerperms * Checks the permission from dinnerperms

View File

@ -17,7 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package com.sk89q.bukkit.migration; package com.sk89q.wepif;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
@ -30,6 +30,7 @@ import java.util.Set;
import java.util.HashMap; import java.util.HashMap;
import com.sk89q.util.yaml.YAMLProcessor; import com.sk89q.util.yaml.YAMLProcessor;
import org.bukkit.OfflinePlayer;
import org.bukkit.Server; import org.bukkit.Server;
public class FlatFilePermissionsResolver implements PermissionsResolver { public class FlatFilePermissionsResolver implements PermissionsResolver {
@ -213,6 +214,22 @@ public class FlatFilePermissionsResolver implements PermissionsResolver {
return groups.toArray(new String[groups.size()]); return groups.toArray(new String[groups.size()]);
} }
public boolean hasPermission(OfflinePlayer player, String permission) {
return hasPermission(player.getName(), permission);
}
public boolean hasPermission(String worldName, OfflinePlayer player, String permission) {
return hasPermission(worldName, player.getName(), permission);
}
public boolean inGroup(OfflinePlayer player, String group) {
return inGroup(player.getName(), group);
}
public String[] getGroups(OfflinePlayer player) {
return getGroups(player.getName());
}
public String getDetectionMessage() { public String getDetectionMessage() {
return "perms_groups.txt and perms_users.txt detected! Using flat file permissions."; return "perms_groups.txt and perms_users.txt detected! Using flat file permissions.";
} }

View File

@ -17,10 +17,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package com.sk89q.bukkit.migration; package com.sk89q.wepif;
import com.sk89q.util.yaml.YAMLProcessor; import com.sk89q.util.yaml.YAMLProcessor;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.command.PluginCommand; import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -132,6 +133,22 @@ public class NijiPermissionsResolver implements PermissionsResolver {
} }
} }
public boolean hasPermission(OfflinePlayer player, String permission) {
return hasPermission(player.getName(), permission);
}
public boolean hasPermission(String worldName, OfflinePlayer player, String permission) {
return hasPermission(worldName, player.getName(), permission);
}
public boolean inGroup(OfflinePlayer player, String group) {
return inGroup(player.getName(), group);
}
public String[] getGroups(OfflinePlayer player) {
return getGroups(player.getName());
}
public static boolean isFakeNijiPerms(Plugin plugin) { public static boolean isFakeNijiPerms(Plugin plugin) {
PluginCommand permsCommand = Bukkit.getServer().getPluginCommand("permissions"); PluginCommand permsCommand = Bukkit.getServer().getPluginCommand("permissions");

View File

@ -17,9 +17,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package com.sk89q.bukkit.migration; package com.sk89q.wepif;
import com.sk89q.util.yaml.YAMLProcessor; import com.sk89q.util.yaml.YAMLProcessor;
import org.bukkit.OfflinePlayer;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -54,7 +55,7 @@ public class PermissionsExResolver implements PermissionsResolver {
} }
public boolean hasPermission(String name, String permission) { public boolean hasPermission(String name, String permission) {
Player player = server.getPlayer(name); Player player = server.getPlayerExact(name);
return manager.has(name, permission, player == null ? null : player.getWorld().getName()); return manager.has(name, permission, player == null ? null : player.getWorld().getName());
} }
@ -78,6 +79,23 @@ public class PermissionsExResolver implements PermissionsResolver {
return user.getGroupsNames(); return user.getGroupsNames();
} }
public boolean hasPermission(OfflinePlayer player, String permission) {
Player onlinePlayer = player.getPlayer();
return manager.has(player.getName(), permission, onlinePlayer == null ? null : onlinePlayer.getWorld().getName());
}
public boolean hasPermission(String worldName, OfflinePlayer player, String permission) {
return hasPermission(worldName, player.getName(), permission);
}
public boolean inGroup(OfflinePlayer player, String group) {
return inGroup(player.getName(), group);
}
public String[] getGroups(OfflinePlayer player) {
return getGroups(player.getName());
}
public String getDetectionMessage() { public String getDetectionMessage() {
return "PermissionsEx detected! Using PermissionsEx for permissions."; return "PermissionsEx detected! Using PermissionsEx for permissions.";
} }

View File

@ -0,0 +1,40 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010, 2011 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.wepif;
import org.bukkit.OfflinePlayer;
public interface PermissionsProvider {
public boolean hasPermission(String name, String permission);
public boolean hasPermission(String worldName, String name, String permission);
public boolean inGroup(String player, String group);
public String[] getGroups(String player);
public boolean hasPermission(OfflinePlayer player, String permission);
public boolean hasPermission(String worldName, OfflinePlayer player, String permission);
public boolean inGroup(OfflinePlayer player, String group);
public String[] getGroups(OfflinePlayer player);
}

View File

@ -0,0 +1,26 @@
// $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.wepif;
public interface PermissionsResolver extends PermissionsProvider {
public void load();
public String getDetectionMessage();
}

View File

@ -0,0 +1,272 @@
// $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.wepif;
import com.sk89q.bukkit.migration.LegacyPluginPermissionsResolver;
import com.sk89q.util.yaml.YAMLFormat;
import com.sk89q.util.yaml.YAMLProcessor;
import org.bukkit.OfflinePlayer;
import org.bukkit.Server;
import org.bukkit.plugin.Plugin;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Logger;
public class PermissionsResolverManager implements PermissionsResolver {
private static final String CONFIG_HEADER = "#\r\n" +
"# WEPIF Configuration File\r\n" +
"#\r\n" +
"# This file handles permissions configuration for every plugin using WEPIF\r\n" +
"#\r\n" +
"# About editing this file:\r\n" +
"# - DO NOT USE TABS. You MUST use spaces or Bukkit will complain. If\r\n" +
"# you use an editor like Notepad++ (recommended for Windows users), you\r\n" +
"# must configure it to \"replace tabs with spaces.\" In Notepad++, this can\r\n" +
"# be changed in Settings > Preferences > Language Menu.\r\n" +
"# - Don't get rid of the indents. They are indented so some entries are\r\n" +
"# in categories (like \"enforce-single-session\" is in the \"protection\"\r\n" +
"# category.\r\n" +
"# - If you want to check the format of this file before putting it\r\n" +
"# into WEPIF, paste it into http://yaml-online-parser.appspot.com/\r\n" +
"# and see if it gives \"ERROR:\".\r\n" +
"# - Lines starting with # are comments and so they are ignored.\r\n" +
"#\r\n" +
"# About Configuration Permissions\r\n" +
"# - See http://wiki.sk89q.com/wiki/WorldEdit/Permissions/Bukkit\r\n" +
"# - Now with multiworld support (see example)\r\n" +
"\r\n";
private static PermissionsResolverManager instance;
public static void initialize(Plugin plugin) {
if (instance == null) {
instance = new PermissionsResolverManager(plugin);
}
}
public static PermissionsResolverManager getInstance() {
if (instance == null) {
throw new WEPIFRutimeException("WEPIF has not yet been initialized!");
}
return instance;
}
private Server server;
private PermissionsResolver permissionResolver;
private PermissionsResolverServerListener listener;
private YAMLProcessor config;
private Logger logger = Logger.getLogger(getClass().getCanonicalName());
private List<Class<? extends PermissionsResolver>> enabledResolvers = new ArrayList<Class<? extends PermissionsResolver>>();
@SuppressWarnings("unchecked")
protected Class<? extends PermissionsResolver>[] availableResolvers = new Class[] {
PluginPermissionsResolver.class,
LegacyPluginPermissionsResolver.class,
PermissionsExResolver.class,
NijiPermissionsResolver.class,
DinnerPermsResolver.class,
FlatFilePermissionsResolver.class
};
protected PermissionsResolverManager(Plugin plugin) {
this.server = plugin.getServer();
this.listener = new PermissionsResolverServerListener(this, plugin);
loadConfig(new File("wepif.yml"));
findResolver();
}
public void findResolver() {
for (Class<? extends PermissionsResolver> resolverClass : enabledResolvers) {
try {
Method factoryMethod = resolverClass.getMethod("factory", Server.class, YAMLProcessor.class);
this.permissionResolver = (PermissionsResolver) factoryMethod.invoke(null, this.server, this.config);
if (this.permissionResolver != null) {
break;
}
} catch (Throwable e) {
logger.warning("Error in factory method for " + resolverClass.getSimpleName() + ": " + e);
e.printStackTrace();
continue;
}
}
if (permissionResolver == null) {
permissionResolver = new ConfigurationPermissionsResolver(config);
}
permissionResolver.load();
logger.info("WEPIF: " + permissionResolver.getDetectionMessage());
}
public void setPluginPermissionsResolver(Plugin plugin) {
if (!(plugin instanceof PermissionsProvider)) {
return;
}
permissionResolver = new PluginPermissionsResolver((PermissionsProvider) plugin, plugin);
logger.info("WEPIF: " + permissionResolver.getDetectionMessage());
}
public void load() {
findResolver();
permissionResolver.load();
}
public boolean hasPermission(String name, String permission) {
return permissionResolver.hasPermission(name, permission);
}
public boolean hasPermission(String worldName, String name, String permission) {
return permissionResolver.hasPermission(worldName, name, permission);
}
public boolean inGroup(String player, String group) {
return permissionResolver.inGroup(player, group);
}
public String[] getGroups(String player) {
return permissionResolver.getGroups(player);
}
public boolean hasPermission(OfflinePlayer player, String permission) {
return permissionResolver.hasPermission(player, permission);
}
public boolean hasPermission(String worldName, OfflinePlayer player, String permission) {
return permissionResolver.hasPermission(worldName, player, permission);
}
public boolean inGroup(OfflinePlayer player, String group) {
return permissionResolver.inGroup(player, group);
}
public String[] getGroups(OfflinePlayer player) {
return permissionResolver.getGroups(player);
}
private boolean loadConfig(File file) {
boolean isUpdated = false;
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
config = new YAMLProcessor(file, false, YAMLFormat.EXTENDED);
try {
config.load();
} catch (IOException e) {
logger.severe("Error loading WEPIF Config: " + e);
e.printStackTrace();
}
List<String> keys = config.getKeys(null);
config.setHeader(CONFIG_HEADER);
if (!keys.contains("ignore-nijiperms-bridges")) {
config.setProperty("ignore-nijiperms-bridges", true);
isUpdated = true;
}
if (!keys.contains("resolvers")) {
//List<String> resolverKeys = config.getKeys("resolvers");
List<String> resolvers = new ArrayList<String>();
for (Class<?> clazz : availableResolvers) {
resolvers.add(clazz.getSimpleName());
}
enabledResolvers.addAll(Arrays.asList(availableResolvers));
config.setProperty("resolvers.enabled", resolvers);
isUpdated = true;
} else {
List<String> disabledResolvers = config.getStringList("resolvers.disabled", new ArrayList<String>());
List<String> stagedEnabled = config.getStringList("resolvers.enabled", null);
for (Iterator<String> i = stagedEnabled.iterator(); i.hasNext();) {
String nextName = i.next();
Class<?> next = null;
try {
next = Class.forName(getClass().getPackage().getName() + "." + nextName);
} catch (ClassNotFoundException e) {}
if (next == null || !PermissionsResolver.class.isAssignableFrom(next)) {
logger.warning("WEPIF: Invalid or unknown class found in enabled resolvers: "
+ nextName + ". Moving to disabled resolvers list.");
i.remove();
disabledResolvers.add(nextName);
isUpdated = true;
continue;
}
enabledResolvers.add(next.asSubclass(PermissionsResolver.class));
}
for (Class<?> clazz : availableResolvers) {
if (!stagedEnabled.contains(clazz.getSimpleName()) &&
!disabledResolvers.contains(clazz.getSimpleName())) {
disabledResolvers.add(clazz.getSimpleName());
logger.info("New permissions resolver: "
+ clazz.getSimpleName() + " detected. " +
"Added to disabled resolvers list.");
isUpdated = true;
}
}
config.setProperty("resolvers.disabled", disabledResolvers);
config.setProperty("resolvers.enabled", stagedEnabled);
}
if (keys.contains("dinner-perms") || keys.contains("dinnerperms")) {
config.setProperty("dinner-perms", null);
config.setProperty("dinnerperms", null);
isUpdated = true;
}
if (!keys.contains("permissions")) {
ConfigurationPermissionsResolver.generateDefaultPerms(
config.addNode("permissions"));
isUpdated = true;
}
if (isUpdated) {
logger.info("WEPIF: Updated config file");
config.save();
}
return isUpdated;
}
boolean hasServerListener() {
return listener != null;
}
void setServerListener(PermissionsResolverServerListener listener) {
this.listener = listener;
}
public static class MissingPluginException extends Exception {
private static final long serialVersionUID = 7044832912491608706L;
}
public String getDetectionMessage() {
return "Using WEPIF for permissions";
}
}

View File

@ -0,0 +1,80 @@
// $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.wepif;
import org.bukkit.event.Event;
import org.bukkit.event.Event.Priority;
import org.bukkit.event.server.PluginDisableEvent;
import org.bukkit.event.server.PluginEnableEvent;
import org.bukkit.event.server.ServerListener;
import org.bukkit.plugin.Plugin;
public class PermissionsResolverServerListener extends ServerListener {
private PermissionsResolverManager manager;
public PermissionsResolverServerListener(PermissionsResolverManager manager, Plugin plugin) {
this.manager = manager;
if (!manager.hasServerListener()) {
register(plugin);
}
}
/**
* Called when a plugin is enabled
*
* @param event Relevant event details
*/
@Override
public void onPluginEnable(PluginEnableEvent event) {
Plugin plugin = event.getPlugin();
String name = plugin.getDescription().getName();
if (plugin instanceof PermissionsProvider) {
manager.setPluginPermissionsResolver(plugin);
} else if ("Permissions".equals(name) || "PermissionsEx".equals(name)) {
manager.findResolver();
manager.load();
}
}
/**
* Called when a plugin is disabled
*
* @param event Relevant event details
*/
@Override
public void onPluginDisable(PluginDisableEvent event) {
Plugin plugin = event.getPlugin();
String name = plugin.getDescription().getName();
if (plugin instanceof PermissionsProvider || "Permissions".equals(name) || "PermissionsEx".equals(name)) {
manager.findResolver();
manager.load();
}
}
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);
manager.setServerListener(this);
}
}

View File

@ -17,9 +17,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package com.sk89q.bukkit.migration; package com.sk89q.wepif;
import com.sk89q.util.yaml.YAMLProcessor; import com.sk89q.util.yaml.YAMLProcessor;
import org.bukkit.OfflinePlayer;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.RegisteredServiceProvider;
@ -71,6 +72,22 @@ public class PluginPermissionsResolver implements PermissionsResolver {
return resolver.getGroups(player); return resolver.getGroups(player);
} }
public boolean hasPermission(OfflinePlayer player, String permission) {
return resolver.hasPermission(player, permission);
}
public boolean hasPermission(String worldName, OfflinePlayer player, String permission) {
return resolver.hasPermission(worldName, player, permission);
}
public boolean inGroup(OfflinePlayer player, String group) {
return resolver.inGroup(player, group);
}
public String[] getGroups(OfflinePlayer player) {
return resolver.getGroups(player);
}
public String getDetectionMessage() { public String getDetectionMessage() {
return "Using plugin '" + this.plugin.getDescription().getName() + "' for permissions."; return "Using plugin '" + this.plugin.getDescription().getName() + "' for permissions.";
} }

View File

@ -0,0 +1,12 @@
package com.sk89q.wepif;
/**
* @author zml2008
*/
public class WEPIFRutimeException extends RuntimeException {
public WEPIFRutimeException(String message) {
super(message);
}
}

View File

@ -98,7 +98,7 @@ public class BukkitPlayer extends LocalPlayer {
@Override @Override
public String[] getGroups() { public String[] getGroups() {
return plugin.getPermissionsResolver().getGroups(player.getName()); return plugin.getPermissionsResolver().getGroups(player);
} }
@Override @Override
@ -110,7 +110,7 @@ public class BukkitPlayer extends LocalPlayer {
public boolean hasPermission(String perm) { public boolean hasPermission(String perm) {
return (!plugin.getLocalConfiguration().noOpPermissions && player.isOp()) return (!plugin.getLocalConfiguration().noOpPermissions && player.isOp())
|| plugin.getPermissionsResolver().hasPermission( || plugin.getPermissionsResolver().hasPermission(
player.getWorld().getName(), player.getName(), perm); player.getWorld().getName(), player, perm);
} }
@Override @Override

View File

@ -29,6 +29,7 @@ import java.util.logging.Logger;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import com.sk89q.util.yaml.YAMLProcessor; import com.sk89q.util.yaml.YAMLProcessor;
import com.sk89q.wepif.PermissionsResolverManager;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -37,7 +38,6 @@ import org.bukkit.event.Event.Priority;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import com.sk89q.bukkit.migration.PermissionsResolverManager;
import com.sk89q.worldedit.*; import com.sk89q.worldedit.*;
import com.sk89q.worldedit.bags.BlockBag; import com.sk89q.worldedit.bags.BlockBag;
import com.sk89q.worldedit.bukkit.selections.*; import com.sk89q.worldedit.bukkit.selections.*;
@ -71,10 +71,6 @@ public class WorldEditPlugin extends JavaPlugin {
* Holds the configuration for WorldEdit. * Holds the configuration for WorldEdit.
*/ */
private BukkitConfiguration config; private BukkitConfiguration config;
/**
* The permissions resolver in use.
*/
private PermissionsResolverManager perms;
/** /**
* Called on plugin enable. * Called on plugin enable.
@ -97,7 +93,7 @@ public class WorldEditPlugin extends JavaPlugin {
// Set up configuration and such, including the permissions // Set up configuration and such, including the permissions
// resolver // resolver
config = new BukkitConfiguration(new YAMLProcessor(new File(getDataFolder(), "config.yml"), true), logger); config = new BukkitConfiguration(new YAMLProcessor(new File(getDataFolder(), "config.yml"), true), logger);
perms = new PermissionsResolverManager(this, "WorldEdit", logger); PermissionsResolverManager.initialize(this);
// Load the configuration // Load the configuration
loadConfiguration(); loadConfiguration();
@ -135,7 +131,7 @@ public class WorldEditPlugin extends JavaPlugin {
protected void loadConfiguration() { protected void loadConfiguration() {
config.unload(); config.unload();
config.load(); config.load();
perms.load(); getPermissionsResolver().load();
} }
/** /**
@ -336,7 +332,7 @@ public class WorldEditPlugin extends JavaPlugin {
* @return * @return
*/ */
public PermissionsResolverManager getPermissionsResolver() { public PermissionsResolverManager getPermissionsResolver() {
return perms; return PermissionsResolverManager.getInstance();
} }
/** /**