From 94b2b4713eb7102044f1e92be345a601aee157fa Mon Sep 17 00:00:00 2001 From: sk89q Date: Sun, 16 Jan 2011 12:51:35 -0800 Subject: [PATCH] Added com.sk89q.bukkit.migration.ConfigurationPermissionsResolver. --- .../ConfigurationPermissionsResolver.java | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 src/com/sk89q/bukkit/migration/ConfigurationPermissionsResolver.java diff --git a/src/com/sk89q/bukkit/migration/ConfigurationPermissionsResolver.java b/src/com/sk89q/bukkit/migration/ConfigurationPermissionsResolver.java new file mode 100644 index 000000000..328448084 --- /dev/null +++ b/src/com/sk89q/bukkit/migration/ConfigurationPermissionsResolver.java @@ -0,0 +1,107 @@ +// $Id$ +/* + * WorldGuard + * 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.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.HashMap; +import org.bukkit.util.config.Configuration; + +public class ConfigurationPermissionsResolver { + private Configuration config; + private Map> userPermissionsCache; + private Map> userGroups; + + public ConfigurationPermissionsResolver(Configuration config) { + this.config = config; + } + + public void load() { + userGroups = new HashMap>(); + userPermissionsCache = new HashMap>(); + + Map> userGroupPermissions = new HashMap>(); + + List groupKeys = config.getKeys("permissions.groups"); + + if (groupKeys != null) { + for (String key : groupKeys) { + List permissions = + config.getStringList("permissions.groups." + key + ".permissions", null); + + if (permissions.size() > 0) { + Set groupPerms = new HashSet(permissions); + userGroupPermissions.put(key, groupPerms); + } + } + } + + List userKeys = config.getKeys("permissions.users"); + + if (userKeys != null) { + for (String key : userKeys) { + Set permsCache = new HashSet(); + + List permissions = + config.getStringList("permissions.users." + key + ".permissions", null); + + if (permissions.size() > 0) { + permsCache.addAll(permissions); + } + + List groups = + config.getStringList("permissions.users." + key + ".groups", null); + groups.add("default"); + + if (groups.size() > 0) { + 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(groups)); + } + } + } + + public boolean hasPermission(String player, String permission) { + Set perms = userPermissionsCache.get(player.toLowerCase()); + if (perms == null) { + return false; + } + + return 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); + } +}