WEPIF Improvement: code clean out, hardcoding removal and refactoring. Added Service discovery to PluginPermissionsResolver

This commit is contained in:
t3hk0d3 2011-09-27 07:40:18 +04:00 committed by zml2008
parent 102e5f142e
commit 4f80e78a7e
9 changed files with 188 additions and 128 deletions

View File

@ -142,4 +142,9 @@ public String[] getGroups(String player) {
return groups.toArray(new String[groups.size()]);
}
public String getDetectionMessage() {
return "No known permissions plugin detected. Using configuration file for permissions.";
}
}

View File

@ -25,6 +25,7 @@
import java.util.ArrayList;
import java.util.List;
import org.bukkit.util.config.Configuration;
public class DinnerPermsResolver implements PermissionsResolver {
@ -35,6 +36,14 @@ public DinnerPermsResolver(Server server) {
this.server = server;
}
public static PermissionsResolver factory(Server server, Configuration config) {
if(!config.getBoolean("dinnerperms", true)){
return null;
}
return new DinnerPermsResolver(server);
}
public void load() {
// Permissions are already loaded
}
@ -84,4 +93,10 @@ public String[] getGroups(String name) {
}
return groupNames.toArray(new String[groupNames.size()]);
}
public String getDetectionMessage() {
return "Using the Bukkit Permissions API.";
}
}

View File

@ -28,28 +28,49 @@
import java.util.Map;
import java.util.Set;
import java.util.HashMap;
import org.bukkit.Server;
import org.bukkit.util.config.Configuration;
public class FlatFilePermissionsResolver implements PermissionsResolver {
private Map<String,Set<String>> userPermissionsCache;
private Set<String> defaultPermissionsCache;
private Map<String,Set<String>> userGroups;
public FlatFilePermissionsResolver() {
protected File groupFile;
protected File userFile;
public static PermissionsResolver factory(Server server, Configuration config){
File groups = new File("perms_groups.txt");
File users = new File("perms_users.txt");
if(!groups.exists() || !users.exists()){
return null;
}
return new FlatFilePermissionsResolver(groups, users);
}
public FlatFilePermissionsResolver(){
this(new File("perms_groups.txt"), new File("perms_users.txt"));
}
public FlatFilePermissionsResolver(File groupFile, File userFile) {
this.groupFile = groupFile;
this.userFile = userFile;
}
@Deprecated
public static boolean filesExists() {
return (new File("perms_groups.txt")).exists()
&& (new File("perms_users.txt")).exists();
return (new File("perms_groups.txt")).exists() && (new File("perms_users.txt")).exists();
}
public Map<String,Set<String>> loadGroupPermissions() {
Map<String,Set<String>> userGroupPermissions = new HashMap<String,Set<String>>();
File file = new File("perms_groups.txt");
FileReader input = null;
try {
input = new FileReader(file);
input = new FileReader(this.groupFile);
BufferedReader buff = new BufferedReader(input);
String line;
@ -99,11 +120,10 @@ public void load() {
defaultPermissionsCache = userGroupPermissions.get("default");
}
File file = new File("perms_users.txt");
FileReader input = null;
try {
input = new FileReader(file);
input = new FileReader(this.userFile);
BufferedReader buff = new BufferedReader(input);
String line;
@ -191,4 +211,9 @@ public String[] getGroups(String player) {
return groups.toArray(new String[groups.size()]);
}
public String getDetectionMessage() {
return "perms_groups.txt and perms_users.txt detected! Using flat file permissions.";
}
}

View File

@ -26,34 +26,37 @@
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import com.nijikokun.bukkit.Permissions.Permissions;
import com.sk89q.bukkit.migration.PermissionsResolverManager.MissingPluginException;
import org.bukkit.util.config.Configuration;
public class NijiPermissionsResolver implements PermissionsResolver {
private Server server;
private Permissions api;
public static PermissionsResolver factory(Server server, Configuration config) {
PluginManager pluginManager = server.getPluginManager();
Plugin plugin = pluginManager.getPlugin("Permissions");
// Check if plugin is loaded and have Permissions interface
if(plugin == null || !(plugin instanceof Permissions)){
return null;
}
// Check for fake permissions
if(config.getBoolean("ignore-nijiperms-bridges", true) && isFakeNijiPerms(plugin)){
return null;
}
return new NijiPermissionsResolver(server, (Permissions)plugin);
}
public void load() {
}
public NijiPermissionsResolver(Server server, boolean ignoreBridges)
throws PluginAccessException, MissingPluginException {
public NijiPermissionsResolver(Server server, Permissions plugin) {
this.server = server;
PluginManager manager = server.getPluginManager();
Plugin plugin = manager.getPlugin("Permissions");
if (plugin == null) {
throw new MissingPluginException();
}
if (!checkRealNijiPerms(ignoreBridges)) {
throw new MissingPluginException();
}
try {
api = (Permissions)plugin;
} catch (ClassCastException e) {
throw new PluginAccessException();
}
this.api = plugin;
}
@SuppressWarnings("static-access")
@ -129,6 +132,7 @@ public static class PluginAccessException extends Exception {
private static final long serialVersionUID = 7044832912491608706L;
}
@Deprecated
public static boolean checkRealNijiPerms(boolean ignoreBridges) {
if (!ignoreBridges) {
return true;
@ -139,4 +143,14 @@ public static boolean checkRealNijiPerms(boolean ignoreBridges) {
}
return permsCommand.getPlugin().getDescription().getName().equals("Permissions");
}
public static boolean isFakeNijiPerms(Plugin plugin){
PluginCommand permsCommand = Bukkit.getServer().getPluginCommand("permissions");
return !(permsCommand.getPlugin().equals(plugin));
}
public String getDetectionMessage() {
return "Permissions plugin detected! Using Permissions plugin for permissions.";
}
}

View File

@ -25,18 +25,25 @@
import ru.tehkode.permissions.PermissionManager;
import ru.tehkode.permissions.PermissionUser;
import com.sk89q.bukkit.migration.PermissionsResolverManager.MissingPluginException;
import org.bukkit.util.config.Configuration;
public class PermissionsExResolver implements PermissionsResolver {
private final PermissionManager manager;
private final Server server;
public PermissionsExResolver(Server server) throws MissingPluginException {
this.server = server;
manager = server.getServicesManager().load(PermissionManager.class);
public static PermissionsResolver factory(Server server, Configuration config){
PermissionManager manager = server.getServicesManager().load(PermissionManager.class);
if(manager == null){
throw new MissingPluginException();
return null;
}
return new PermissionsExResolver(server, manager);
}
public PermissionsExResolver(Server server, PermissionManager manager) {
this.server = server;
this.manager = manager;
}
public void load() {
@ -68,4 +75,7 @@ public String[] getGroups(String player) {
return user.getGroupsNames();
}
public String getDetectionMessage() {
return "PermissionsEx detected! Using PermissionsEx for permissions.";
}
}

View File

@ -19,10 +19,8 @@
package com.sk89q.bukkit.migration;
public interface PermissionsResolver {
public interface PermissionsResolver extends PermissionsProvider {
public void load();
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 String getDetectionMessage();
}

View File

@ -21,6 +21,7 @@
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.logging.Logger;
import org.bukkit.Server;
@ -52,12 +53,19 @@ public class PermissionsResolverManager implements PermissionsResolver {
"\r\n";
private Server server;
private PermissionsResolver perms;
private PermissionsResolver permissionResolver;
private PermissionsResolverServerListener listener;
private Configuration permsConfig;
private Configuration config;
private String name;
private Logger logger;
protected boolean ignoreNijiPermsBridges;
protected Class<? extends PermissionsResolver>[] availableResolvers = new Class[]{
PluginPermissionsResolver.class,
PermissionsExResolver.class,
NijiPermissionsResolver.class,
DinnerPermsResolver.class,
FlatFilePermissionsResolver.class
};
@Deprecated
public PermissionsResolverManager(Configuration config, Server server, String name, Logger logger) {
@ -72,104 +80,59 @@ public PermissionsResolverManager(Plugin plugin, String name, Logger logger) {
this.server = plugin.getServer();
this.name = name;
this.logger = logger;
new PermissionsResolverServerListener(this, plugin);
this.listener = new PermissionsResolverServerListener(this, plugin);
loadConfig(new File("wepif.yml"));
findResolver();
}
public void findResolver() {
if (tryPluginPermissionsResolver()) return;
if (tryNijiPermissions()) return;
if (tryPermissionsEx()) return;
if (tryDinnerPerms()) return;
if (tryFlatFilePermissions()) return;
for (Class resolverClass : availableResolvers){
try {
Method factoryMethod = resolverClass.getMethod("factory", Server.class, Configuration.class);
perms = new ConfigurationPermissionsResolver(permsConfig);
this.permissionResolver = (PermissionsResolver)factoryMethod.invoke(null, this.server, this.config);
if(this.permissionResolver != null){
logger.info(name + ": " + this.permissionResolver.getDetectionMessage());
return;
}
} catch (Throwable e) {
continue;
}
}
permissionResolver = new ConfigurationPermissionsResolver(config);
logger.info(name + ": No known permissions plugin detected. Using configuration file for permissions.");
}
private boolean tryNijiPermissions() {
try {
perms = new NijiPermissionsResolver(server, ignoreNijiPermsBridges);
logger.info(name + ": Permissions plugin detected! Using Permissions plugin for permissions.");
return true;
} catch (Throwable e) {
return false;
}
}
private boolean tryPermissionsEx() {
try {
perms = new PermissionsExResolver(server);
logger.info(name + ": PermissionsEx detected! Using PermissionsEx for permissions.");
return true;
} catch (Throwable e) {
return false;
}
}
private boolean tryFlatFilePermissions() {
if (FlatFilePermissionsResolver.filesExists()) {
perms = new FlatFilePermissionsResolver();
logger.info(name + ": perms_groups.txt and perms_users.txt detected! Using flat file permissions.");
return true;
}
return false;
}
private boolean tryPluginPermissionsResolver() {
for (Plugin plugin : server.getPluginManager().getPlugins()) {
if (plugin instanceof PermissionsProvider) {
perms = new PluginPermissionsResolver(
(PermissionsProvider) plugin);
logger.info(name + ": Using plugin '"
+ plugin.getDescription().getName() + "' for permissions.");
return true;
}
}
return false;
}
private boolean tryDinnerPerms() {
if (!permsConfig.getBoolean("dinnerperms", true)) {
return false;
}
perms = new DinnerPermsResolver(server);
logger.info(name + ": Using the Bukkit Permissions API.");
return true;
}
public void setPluginPermissionsResolver(Plugin plugin) {
if (!(plugin instanceof PermissionsProvider)) {
return;
}
perms = new PluginPermissionsResolver(
(PermissionsProvider) plugin);
logger.info(name + ": Using plugin '"
+ plugin.getDescription().getName() + "' for permissions.");
permissionResolver = new PluginPermissionsResolver((PermissionsProvider) plugin, plugin);
logger.info(name + ": Using plugin '" + plugin.getDescription().getName() + "' for permissions.");
}
public void load() {
perms.load();
permissionResolver.load();
}
public boolean hasPermission(String name, String permission) {
return perms.hasPermission(name, permission);
return permissionResolver.hasPermission(name, permission);
}
public boolean hasPermission(String worldName, String name, String permission) {
return perms.hasPermission(worldName, name, permission);
return permissionResolver.hasPermission(worldName, name, permission);
}
public boolean inGroup(String player, String group) {
return perms.inGroup(player, group);
return permissionResolver.inGroup(player, group);
}
public String[] getGroups(String player) {
return perms.getGroups(player);
return permissionResolver.getGroups(player);
}
private boolean loadConfig(File file) {
@ -181,30 +144,30 @@ private boolean loadConfig(File file) {
e.printStackTrace();
}
}
permsConfig = new Configuration(file);
permsConfig.load();
List<String> keys = permsConfig.getKeys();
permsConfig.setHeader(CONFIG_HEADER);
config = new Configuration(file);
config.load();
List<String> keys = config.getKeys();
config.setHeader(CONFIG_HEADER);
if (!keys.contains("dinnerperms")) {
permsConfig.setProperty("dinnerperms", permsConfig.getBoolean("dinner-perms", true));
config.setProperty("dinnerperms", config.getBoolean("dinner-perms", true));
isUpdated = true;
}
if (!keys.contains("ignore-nijiperms-bridges")) {
permsConfig.setProperty("ignore-nijiperms-bridges", true);
config.setProperty("ignore-nijiperms-bridges", true);
isUpdated = true;
}
ignoreNijiPermsBridges = permsConfig.getBoolean("ignore-nijiperms-bridges", true);
if (keys.contains("dinner-perms")) {
permsConfig.removeProperty("dinner-perms");
config.removeProperty("dinner-perms");
isUpdated = true;
}
if (!keys.contains("permissions")) {
ConfigurationPermissionsResolver.generateDefaultPerms(permsConfig);
ConfigurationPermissionsResolver.generateDefaultPerms(config);
isUpdated = true;
}
if (isUpdated) {
logger.info("WEPIF: Updated config file");
permsConfig.save();
config.save();
}
return isUpdated;
}
@ -221,4 +184,8 @@ public static class MissingPluginException extends Exception {
private static final long serialVersionUID = 7044832912491608706L;
}
public String getDetectionMessage() {
return "Using WEPIF for permissions";
}
}

View File

@ -52,9 +52,7 @@ public void onPluginEnable(PluginEnableEvent event) {
String name = plugin.getDescription().getName();
if (plugin instanceof PermissionsProvider) {
manager.setPluginPermissionsResolver(plugin);
} else if ((name.equalsIgnoreCase("Permissions") &&
NijiPermissionsResolver.checkRealNijiPerms(manager.ignoreNijiPermsBridges)) ||
name.equalsIgnoreCase("PermissionsEx")) {
} else if ("Permissions".equals(name) || "PermissionsEx".equals(name)) {
manager.findResolver();
manager.load();
}
@ -70,10 +68,7 @@ public void onPluginDisable(PluginDisableEvent event) {
Plugin plugin = event.getPlugin();
String name = plugin.getDescription().getName();
if (plugin instanceof PermissionsProvider
|| (name.equalsIgnoreCase("Permissions") &&
NijiPermissionsResolver.checkRealNijiPerms(manager.ignoreNijiPermsBridges)) ||
name.equalsIgnoreCase("PermissionsEx")) {
if (plugin instanceof PermissionsProvider || "Permissions".equals(name) || "PermissionsEx".equals(name)) {
manager.findResolver();
manager.load();
}
@ -86,4 +81,6 @@ public void register(Plugin plugin) {
this, Priority.Normal, plugin);
manager.setServerListener(this);
}
}

View File

@ -19,12 +19,37 @@
package com.sk89q.bukkit.migration;
import org.bukkit.Server;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.util.config.Configuration;
public class PluginPermissionsResolver implements PermissionsResolver {
protected PermissionsProvider resolver;
protected Plugin plugin;
public PluginPermissionsResolver(PermissionsProvider resolver) {
public static PermissionsResolver factory(Server server, Configuration config){
// Looking for service
RegisteredServiceProvider<PermissionsProvider> serviceProvider = server.getServicesManager().getRegistration(PermissionsProvider.class);
if(serviceProvider != null){
return new PluginPermissionsResolver(serviceProvider.getProvider(), serviceProvider.getPlugin());
}
// Looking for plugin
for (Plugin plugin : server.getPluginManager().getPlugins()) {
if(plugin instanceof PermissionsProvider){
return new PluginPermissionsResolver((PermissionsProvider)plugin, plugin);
}
}
return null;
}
public PluginPermissionsResolver(PermissionsProvider resolver, Plugin permissionsPlugin) {
this.resolver = resolver;
this.plugin = permissionsPlugin;
}
public void load() {
@ -46,4 +71,8 @@ public String[] getGroups(String player) {
return resolver.getGroups(player);
}
public String getDetectionMessage() {
return "Using plugin '" + this.plugin.getDescription().getName() + "' for permissions.";
}
}