Moved WEPIF config to a global config file. Permissions configured per-plugin are not migrated to the global config.

This commit is contained in:
zml2008 2011-08-06 00:35:40 -07:00
parent 501c9fa482
commit 266f272d88
2 changed files with 74 additions and 7 deletions

View File

@ -25,6 +25,7 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.HashMap; import java.util.HashMap;
import org.bukkit.util.config.Configuration; import org.bukkit.util.config.Configuration;
import org.bukkit.util.config.ConfigurationNode;
public class ConfigurationPermissionsResolver implements PermissionsResolver { public class ConfigurationPermissionsResolver implements PermissionsResolver {
private Configuration config; private Configuration config;
@ -36,6 +37,16 @@ public class ConfigurationPermissionsResolver implements PermissionsResolver {
this.config = config; this.config = config;
} }
public static void generateDefaultPerms(Configuration config) {
config.setProperty("permissions.groups.default.permissions", new String[] {
"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"});
}
public void load() { public void load() {
userGroups = new HashMap<String,Set<String>>(); userGroups = new HashMap<String,Set<String>>();
userPermissionsCache = new HashMap<String,Set<String>>(); userPermissionsCache = new HashMap<String,Set<String>>();

View File

@ -19,34 +19,58 @@
package com.sk89q.bukkit.migration; package com.sk89q.bukkit.migration;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.util.config.Configuration; import org.bukkit.util.config.Configuration;
public class PermissionsResolverManager implements PermissionsResolver { public class PermissionsResolverManager implements PermissionsResolver {
private Configuration config; 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 Server server; private Server server;
private PermissionsResolver perms; private PermissionsResolver perms;
private Configuration permsConfig;
private String name; private String name;
private Logger logger; private Logger logger;
public PermissionsResolverManager(Configuration config, Server server, String name, Logger logger) { public PermissionsResolverManager(Configuration config, Server server, String name, Logger logger) {
this.config = config;
this.server = server; this.server = server;
this.name = name; this.name = name;
this.logger = logger; this.logger = logger;
loadConfig(new File("wepif.yml")); // TODO: config migration, maybe
findResolver(); findResolver();
} }
public void findResolver() { public void findResolver() {
if (tryDinnerPerms()) return; if (tryDinnerPerms()) return;
if (tryPluginPermissionsResolver()) return; if (tryPluginPermissionsResolver()) return;
if (tryNijiPermissions()) return; if (tryNijiPermissions()) return;
if (tryFlatFilePermissions()) return; if (tryFlatFilePermissions()) return;
perms = new ConfigurationPermissionsResolver(config); perms = new ConfigurationPermissionsResolver(permsConfig);
logger.info(name + ": No known permissions plugin detected. Using configuration file for permissions."); logger.info(name + ": No known permissions plugin detected. Using configuration file for permissions.");
} }
@ -85,7 +109,7 @@ public class PermissionsResolverManager implements PermissionsResolver {
} }
private boolean tryDinnerPerms() { private boolean tryDinnerPerms() {
if (!config.getBoolean("permissions.dinner-perms", true)) if (!permsConfig.getBoolean("dinnerperms", true))
return false; return false;
perms = new DinnerPermsResolver(server); perms = new DinnerPermsResolver(server);
logger.info(name + ": Using the Bukkit Permissions API."); logger.info(name + ": Using the Bukkit Permissions API.");
@ -123,4 +147,36 @@ public class PermissionsResolverManager implements PermissionsResolver {
return perms.getGroups(player); return perms.getGroups(player);
} }
private boolean loadConfig(File file) {
boolean isUpdated = false;
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
permsConfig = new Configuration(file);
permsConfig.load();
List<String> keys = permsConfig.getKeys();
permsConfig.setHeader(CONFIG_HEADER);
if (!keys.contains("dinnerperms")) {
permsConfig.setProperty("dinnerperms", permsConfig.getBoolean("dinner-perms", true));
isUpdated = true;
}
if (keys.contains("dinner-perms")) {
permsConfig.removeProperty("dinner-perms");
isUpdated = true;
}
if (!keys.contains("permissions")) {
ConfigurationPermissionsResolver.generateDefaultPerms(permsConfig);
isUpdated = true;
}
if (isUpdated) {
logger.info("WEPIF: Updated config file");
permsConfig.save();
}
return isUpdated;
}
} }