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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -22,7 +22,7 @@ package com.sk89q.bukkit.migration;
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;
import org.bukkit.util.config.Configuration; import org.bukkit.configuration.Configuration;
public class PluginPermissionsResolver implements PermissionsResolver { public class PluginPermissionsResolver implements PermissionsResolver {