From bcb46d6b54262ca12e9268ac65ad84a82a0f3d16 Mon Sep 17 00:00:00 2001 From: sk89q Date: Fri, 21 Jan 2011 17:01:08 -0800 Subject: [PATCH] Added new flat file permissions resolver, added * support for ConfigurationPermissionsResolver. --- .../ConfigurationPermissionsResolver.java | 2 +- .../FlatFilePermissionsResolver.java | 175 ++++++++++++++++++ .../migration/PermissionsResolverManager.java | 31 +++- 3 files changed, 199 insertions(+), 9 deletions(-) create mode 100644 src/com/sk89q/bukkit/migration/FlatFilePermissionsResolver.java diff --git a/src/com/sk89q/bukkit/migration/ConfigurationPermissionsResolver.java b/src/com/sk89q/bukkit/migration/ConfigurationPermissionsResolver.java index a4c0f662e..ac8a0ba16 100644 --- a/src/com/sk89q/bukkit/migration/ConfigurationPermissionsResolver.java +++ b/src/com/sk89q/bukkit/migration/ConfigurationPermissionsResolver.java @@ -93,7 +93,7 @@ public class ConfigurationPermissionsResolver implements PermissionsResolver { return false; } - return perms.contains(permission); + return perms.contains("*") || perms.contains(permission); } public boolean inGroup(String player, String group) { diff --git a/src/com/sk89q/bukkit/migration/FlatFilePermissionsResolver.java b/src/com/sk89q/bukkit/migration/FlatFilePermissionsResolver.java new file mode 100644 index 000000000..b57440ff7 --- /dev/null +++ b/src/com/sk89q/bukkit/migration/FlatFilePermissionsResolver.java @@ -0,0 +1,175 @@ +// $Id$ +/* + * WorldEdit + * Copyright (C) 2010 sk89q + * + * 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 . +*/ + +package com.sk89q.bukkit.migration; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.HashMap; + +public class FlatFilePermissionsResolver implements PermissionsResolver { + private Map> userPermissionsCache; + private Map> userGroups; + + public FlatFilePermissionsResolver() { + } + + public static boolean filesExists() { + return (new File("perms_groups.txt")).exists() + && (new File("perms_users.txt")).exists(); + } + + public Map> loadGroupPermissions() { + Map> userGroupPermissions = new HashMap>(); + + File file = new File("perms_groups.txt"); + FileReader input = null; + + try { + input = new FileReader(file); + BufferedReader buff = new BufferedReader(input); + + String line; + while ((line = buff.readLine()) != null) { + line = line.trim(); + + // Blank line + if (line.length() == 0) { + continue; + } else if (line.charAt(0) == ';' || line.charAt(0) == '#') { + continue; + } + + String[] parts = line.split(":"); + + String key = parts[0]; + + if (parts.length > 1) { + String[] perms = parts[1].split(","); + + Set groupPerms = new HashSet(Arrays.asList(perms)); + userGroupPermissions.put(key, groupPerms); + } + } + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + if (input != null) { + input.close(); + } + } catch (IOException e2) { + } + } + + return userGroupPermissions; + } + + public void load() { + userGroups = new HashMap>(); + userPermissionsCache = new HashMap>(); + + Map> userGroupPermissions = loadGroupPermissions(); + + File file = new File("perms_users.txt"); + FileReader input = null; + + try { + input = new FileReader(file); + BufferedReader buff = new BufferedReader(input); + + String line; + while ((line = buff.readLine()) != null) { + Set permsCache = new HashSet(); + + line = line.trim(); + + // Blank line + if (line.length() == 0) { + continue; + } else if (line.charAt(0) == ';' || line.charAt(0) == '#') { + continue; + } + + String[] parts = line.split(":"); + + String key = parts[0]; + + if (parts.length > 1) { + String[] groups = (parts[1] + ",default").split(","); + String[] perms = parts.length > 2 ? parts[2].split(",") : new String[0]; + + permsCache.addAll(Arrays.asList(perms)); + + for (String group : groups) { + Set groupPerms = userGroupPermissions.get(group); + if (groupPerms != null) { + permsCache.addAll(groupPerms); + } + } + + userPermissionsCache.put(key.toLowerCase(), permsCache); + userGroups.put(key.toLowerCase(), new HashSet(Arrays.asList(groups))); + } + } + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + if (input != null) { + input.close(); + } + } catch (IOException e2) { + } + } + } + + public boolean hasPermission(String player, String permission) { + Set perms = userPermissionsCache.get(player.toLowerCase()); + if (perms == null) { + return false; + } + + return perms.contains("*") || perms.contains(permission); + } + + public boolean inGroup(String player, String group) { + Set groups = userGroups.get(player.toLowerCase()); + if (groups == null) { + return false; + } + + return groups.contains(group); + } + + public String[] getGroups(String player) { + Set groups = userGroups.get(player.toLowerCase()); + if (groups == null) { + return new String[0]; + } + + return groups.toArray(new String[groups.size()]); + } +} diff --git a/src/com/sk89q/bukkit/migration/PermissionsResolverManager.java b/src/com/sk89q/bukkit/migration/PermissionsResolverManager.java index d2b28e021..7a6ec0ca7 100644 --- a/src/com/sk89q/bukkit/migration/PermissionsResolverManager.java +++ b/src/com/sk89q/bukkit/migration/PermissionsResolverManager.java @@ -42,23 +42,38 @@ public class PermissionsResolverManager implements PermissionsResolver { } public void findResolver() { + if (tryGroupsUsers()) return; + if (tryFlatFilePermissions()) return; + + perms = new ConfigurationPermissionsResolver(config); + logger.info(name + ": No known permissions plugin detected. Using configuration file for permissions."); + } + + private boolean tryGroupsUsers() { try { perms = new GroupUsersPemissionsResolver(server); logger.info(name + ": GroupUsers detected! Using GroupUsers for permissions."); + return true; } catch (PluginAccessException e) { - perms = new ConfigurationPermissionsResolver(config); - logger.warning(name + ": Failed to access GroupUsers. Falling back to configuration file for permissions."); + return false; } catch (NoClassDefFoundError e) { - perms = new ConfigurationPermissionsResolver(config); - logger.info(name + ": No known permissions plugin detected. Using configuration file for permissions. (code: 10)"); + return false; } catch (MissingPluginException e) { - perms = new ConfigurationPermissionsResolver(config); - logger.info(name + ": No known permissions plugin detected. Using configuration file for permissions. (code: 11)"); + return false; } catch (Throwable e) { - perms = new ConfigurationPermissionsResolver(config); - logger.info(name + ": No known permissions plugin detected. Using configuration file for permissions. (code: 12)"); + 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; + } public void load() { perms.load();