// $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.util.*; import org.bukkit.configuration.Configuration; import org.bukkit.configuration.ConfigurationSection; public class ConfigurationPermissionsResolver implements PermissionsResolver { private Configuration config; private Map> userPermissionsCache; private Set defaultPermissionsCache; private Map> userGroups; public ConfigurationPermissionsResolver(Configuration config) { this.config = config; } public static ConfigurationSection generateDefaultPerms(ConfigurationSection section) { section.set("permissions.groups.default.permissions", Arrays.asList( "worldedit.reload", "worldedit.selection", "worlds.creative.worldedit.region")); section.set("permissions.groups.admins.permissions", Arrays.asList("*")); section.set("permissions.users.sk89q.permissions", Arrays.asList("worldedit")); section.set("permissions.users.sk89q.groups", Arrays.asList("admins")); return section; } public void load() { userGroups = new HashMap>(); userPermissionsCache = new HashMap>(); defaultPermissionsCache = new HashSet(); Map> userGroupPermissions = new HashMap>(); List groupKeys = config.getList("permissions.groups"); if (groupKeys != null) { for (String key : groupKeys) { List permissions = config.getList("permissions.groups." + key + ".permissions", null); if (permissions.size() > 0) { Set groupPerms = new HashSet(permissions); userGroupPermissions.put(key, groupPerms); if (key.equals("default")) { defaultPermissionsCache.addAll(permissions); } } } } List userKeys = config.getList("permissions.users"); if (userKeys != null) { for (String key : userKeys) { Set permsCache = new HashSet(); List permissions = config.getList("permissions.users." + key + ".permissions", null); if (permissions.size() > 0) { permsCache.addAll(permissions); } List groups = config.getList("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) { int dotPos = permission.lastIndexOf("."); if (dotPos > -1) { if (hasPermission(player, permission.substring(0, dotPos))) { return true; } } Set perms = userPermissionsCache.get(player.toLowerCase()); if (perms == null) { return defaultPermissionsCache.contains(permission) || defaultPermissionsCache.contains("*"); } return perms.contains("*") || perms.contains(permission); } public boolean hasPermission(String worldName, String player, String permission) { return hasPermission(player, "worlds." + worldName + "." + permission) || hasPermission(player, 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()]); } public String getDetectionMessage() { return "No known permissions plugin detected. Using configuration file for permissions."; } }