Added configurable detection order of permissions resolvers, restored old default order, and updated configurations to the new Configuration

This commit is contained in:
zml2008 2011-10-22 01:13:52 -07:00
parent d93d85cd37
commit 0e6597fb86
7 changed files with 104 additions and 54 deletions

View File

@ -19,12 +19,10 @@
package com.sk89q.bukkit.migration;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.HashMap;
import org.bukkit.util.config.Configuration;
import java.util.*;
import org.bukkit.configuration.Configuration;
import org.bukkit.configuration.ConfigurationSection;
public class ConfigurationPermissionsResolver implements PermissionsResolver {
private Configuration config;
@ -36,15 +34,15 @@ public ConfigurationPermissionsResolver(Configuration config) {
this.config = config;
}
public static void generateDefaultPerms(Configuration config) {
config.setProperty("permissions.groups.default.permissions", new String[] {
public static ConfigurationSection generateDefaultPerms(ConfigurationSection section) {
section.set("permissions.groups.default.permissions", Arrays.asList(
"worldedit.reload",
"worldedit.selection.*",
"worlds.creative.worldedit.region.*"});
config.setProperty("permissions.groups.admins.permissions", new String[]{"*"});
config.setProperty("permissions.users.sk89q.permissions", new String[]{"worldedit.*"});
config.setProperty("permissions.users.sk89q.groups", new String[]{"admins"});
"worldedit.selection",
"worlds.creative.worldedit.region"));
section.set("permissions.groups.admins.permissions", Arrays.asList("*"));
section.set("permissions.users.sk89q.permissions", Arrays.asList("worldedit"));
section.set("permissions.users.sk89q.groups", Arrays.asList("admins"));
return section;
}
public void load() {
@ -54,12 +52,12 @@ public void load() {
Map<String, Set<String>> userGroupPermissions = new HashMap<String, Set<String>>();
List<String> groupKeys = config.getKeys("permissions.groups");
List<String> groupKeys = config.getList("permissions.groups");
if (groupKeys != null) {
for (String key : groupKeys) {
List<String> permissions =
config.getStringList("permissions.groups." + key + ".permissions", null);
config.getList("permissions.groups." + key + ".permissions", null);
if (permissions.size() > 0) {
Set<String> groupPerms = new HashSet<String>(permissions);
@ -72,21 +70,21 @@ public void load() {
}
}
List<String> userKeys = config.getKeys("permissions.users");
List<String> userKeys = config.getList("permissions.users");
if (userKeys != null) {
for (String key : userKeys) {
Set<String> permsCache = new HashSet<String>();
List<String> permissions =
config.getStringList("permissions.users." + key + ".permissions", null);
config.getList("permissions.users." + key + ".permissions", null);
if (permissions.size() > 0) {
permsCache.addAll(permissions);
}
List<String> groups =
config.getStringList("permissions.users." + key + ".groups", null);
config.getList("permissions.users." + key + ".groups", null);
groups.add("default");
if (groups.size() > 0) {

View File

@ -26,7 +26,7 @@
import java.util.ArrayList;
import java.util.List;
import org.bukkit.util.config.Configuration;
import org.bukkit.configuration.Configuration;
public class DinnerPermsResolver implements PermissionsResolver {
@ -38,9 +38,6 @@ public DinnerPermsResolver(Server server) {
}
public static PermissionsResolver factory(Server server, Configuration config) {
if (!config.getBoolean("dinnerperms", true)) {
return null;
}
return new DinnerPermsResolver(server);
}

View File

@ -29,7 +29,7 @@
import java.util.Set;
import java.util.HashMap;
import org.bukkit.Server;
import org.bukkit.util.config.Configuration;
import org.bukkit.configuration.Configuration;
public class FlatFilePermissionsResolver implements PermissionsResolver {
private Map<String, Set<String>> userPermissionsCache;

View File

@ -26,7 +26,7 @@
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import com.nijikokun.bukkit.Permissions.Permissions;
import org.bukkit.util.config.Configuration;
import org.bukkit.configuration.Configuration;
public class NijiPermissionsResolver implements PermissionsResolver {
private Server server;

View File

@ -25,7 +25,7 @@
import ru.tehkode.permissions.PermissionManager;
import ru.tehkode.permissions.PermissionUser;
import org.bukkit.util.config.Configuration;
import org.bukkit.configuration.Configuration;
public class PermissionsExResolver implements PermissionsResolver {
private final PermissionManager manager;

View File

@ -22,11 +22,15 @@
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.Server;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.configuration.file.YamlConfigurationOptions;
import org.bukkit.plugin.Plugin;
import org.bukkit.util.config.Configuration;
import org.bukkit.configuration.Configuration;
public class PermissionsResolverManager implements PermissionsResolver {
private static final String CONFIG_HEADER = "#\r\n" +
@ -55,21 +59,22 @@ public class PermissionsResolverManager implements PermissionsResolver {
private Server server;
private PermissionsResolver permissionResolver;
private PermissionsResolverServerListener listener;
private Configuration config;
private FileConfiguration 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[] {
DinnerPermsResolver.class,
NijiPermissionsResolver.class,
PermissionsExResolver.class,
PluginPermissionsResolver.class,
PermissionsExResolver.class,
NijiPermissionsResolver.class,
DinnerPermsResolver.class,
FlatFilePermissionsResolver.class
};
@Deprecated
public PermissionsResolverManager(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;
@ -88,23 +93,25 @@ public PermissionsResolverManager(Plugin plugin, String name, Logger logger) {
}
public void findResolver() {
for (Class<? extends PermissionsResolver> resolverClass : availableResolvers) {
for (Class<? extends PermissionsResolver> resolverClass : enabledResolvers) {
try {
Method factoryMethod = resolverClass.getMethod("factory", Server.class, Configuration.class);
this.permissionResolver = (PermissionsResolver) factoryMethod.invoke(null, this.server, this.config);
if (this.permissionResolver != null) {
logger.info(name + ": " + this.permissionResolver.getDetectionMessage());
return;
break;
}
} catch (Throwable e) {
logger.warning("Error in factory method for " + resolverClass.getSimpleName() + ": " + e);
e.printStackTrace();
continue;
}
}
permissionResolver = new ConfigurationPermissionsResolver(config);
logger.info(name + ": No known permissions plugin detected. Using configuration file for permissions.");
if (permissionResolver == null) {
permissionResolver = new ConfigurationPermissionsResolver(config);
}
logger.info(name + ": " + permissionResolver.getDetectionMessage());
}
public void setPluginPermissionsResolver(Plugin plugin) {
@ -113,7 +120,7 @@ public void setPluginPermissionsResolver(Plugin plugin) {
}
permissionResolver = new PluginPermissionsResolver((PermissionsProvider) plugin, plugin);
logger.info(name + ": Using plugin '" + plugin.getDescription().getName() + "' for permissions.");
logger.info(name + ": " + permissionResolver.getDetectionMessage());
}
public void load() {
@ -136,6 +143,7 @@ public String[] getGroups(String player) {
return permissionResolver.getGroups(player);
}
@SuppressWarnings("unchecked")
private boolean loadConfig(File file) {
boolean isUpdated = false;
if (!file.exists()) {
@ -145,29 +153,76 @@ private boolean loadConfig(File file) {
e.printStackTrace();
}
}
config = new Configuration(file);
config.load();
List<String> keys = config.getKeys();
config.setHeader(CONFIG_HEADER);
if (!keys.contains("dinnerperms")) {
config.setProperty("dinnerperms", config.getBoolean("dinner-perms", true));
isUpdated = true;
}
config = YamlConfiguration.loadConfiguration(file);
((YamlConfigurationOptions)config.options()).indent(4);
Set<String> keys = config.getKeys(true);
config.options().header(CONFIG_HEADER);
if (!keys.contains("ignore-nijiperms-bridges")) {
config.setProperty("ignore-nijiperms-bridges", true);
config.set("ignore-nijiperms-bridges", true);
isUpdated = true;
}
if (keys.contains("dinner-perms")) {
config.removeProperty("dinner-perms");
if (!keys.contains("resolvers.enabled")) {
List<String> resolvers = new ArrayList<String>();
for (Class<?> clazz : availableResolvers) {
resolvers.add(clazz.getSimpleName());
}
enabledResolvers.addAll(Arrays.asList(availableResolvers));
config.set("resolvers.enabled", resolvers);
isUpdated = true;
} else {
List<String> disabledResolvers = config.getList("resolvers.disabled", new ArrayList<String>());
List<String> stagedEnabled = config.getList("resolvers.enabled");
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.getName());
logger.info("New permissions resolver: "
+ clazz.getSimpleName() + " detected. " +
"Added to disabled resolvers list.");
isUpdated = true;
}
}
config.set("resolvers.disabled", disabledResolvers);
config.set("resolvers.enabled", stagedEnabled);
}
if (keys.contains("dinner-perms") || keys.contains("dinnerperms")) {
config.set("dinner-perms", null);
config.set("dinnerperms", null);
isUpdated = true;
}
if (!keys.contains("permissions")) {
ConfigurationPermissionsResolver.generateDefaultPerms(config);
ConfigurationPermissionsResolver.generateDefaultPerms(
config.createSection("permissions"));
isUpdated = true;
}
if (isUpdated) {
logger.info("WEPIF: Updated config file");
config.save();
try {
config.save(file);
} catch (IOException e) {
logger.log(Level.SEVERE, "WEPIF: Unable to save config file", e);
}
}
return isUpdated;
}

View File

@ -22,7 +22,7 @@
import org.bukkit.Server;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.util.config.Configuration;
import org.bukkit.configuration.Configuration;
public class PluginPermissionsResolver implements PermissionsResolver {