2011-01-16 20:51:35 +00:00
|
|
|
// $Id$
|
|
|
|
/*
|
2011-01-18 00:47:38 +00:00
|
|
|
* WorldEdit
|
2011-01-16 20:51:35 +00:00
|
|
|
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
|
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
2011-09-27 07:32:46 +00:00
|
|
|
*/
|
2011-01-16 20:51:35 +00:00
|
|
|
|
|
|
|
package com.sk89q.bukkit.migration;
|
|
|
|
|
2011-10-22 08:13:52 +00:00
|
|
|
import java.util.*;
|
|
|
|
|
2011-10-23 05:22:50 +00:00
|
|
|
import com.sk89q.util.yaml.YAMLNode;
|
|
|
|
import com.sk89q.util.yaml.YAMLProcessor;
|
2011-01-16 20:51:35 +00:00
|
|
|
|
2011-01-18 00:47:38 +00:00
|
|
|
public class ConfigurationPermissionsResolver implements PermissionsResolver {
|
2011-10-23 05:22:50 +00:00
|
|
|
private YAMLProcessor config;
|
2011-09-27 07:32:46 +00:00
|
|
|
private Map<String, Set<String>> userPermissionsCache;
|
2011-01-22 01:53:27 +00:00
|
|
|
private Set<String> defaultPermissionsCache;
|
2011-09-27 07:32:46 +00:00
|
|
|
private Map<String, Set<String>> userGroups;
|
|
|
|
|
2011-10-23 05:22:50 +00:00
|
|
|
public ConfigurationPermissionsResolver(YAMLProcessor config) {
|
2011-01-16 20:51:35 +00:00
|
|
|
this.config = config;
|
|
|
|
}
|
2011-08-06 07:35:40 +00:00
|
|
|
|
2011-10-23 05:22:50 +00:00
|
|
|
public static YAMLNode generateDefaultPerms(YAMLNode section) {
|
|
|
|
section.setProperty("permissions.groups.default.permissions", new String[] {
|
2011-08-06 07:35:40 +00:00
|
|
|
"worldedit.reload",
|
2011-10-22 08:13:52 +00:00
|
|
|
"worldedit.selection",
|
2011-10-23 05:22:50 +00:00
|
|
|
"worlds.creative.worldedit.region"});
|
2011-10-26 20:50:46 +00:00
|
|
|
section.setProperty("permissions.groups.admins.permissions", new String[] {"*"});
|
|
|
|
section.setProperty("permissions.users.sk89q.permissions", new String[] {"worldedit"});
|
|
|
|
section.setProperty("permissions.users.sk89q.groups", new String[] {"admins"});
|
2011-10-22 08:13:52 +00:00
|
|
|
return section;
|
2011-08-06 07:35:40 +00:00
|
|
|
}
|
2011-09-27 07:32:46 +00:00
|
|
|
|
2011-01-16 20:51:35 +00:00
|
|
|
public void load() {
|
2011-09-27 07:32:46 +00:00
|
|
|
userGroups = new HashMap<String, Set<String>>();
|
|
|
|
userPermissionsCache = new HashMap<String, Set<String>>();
|
2011-01-22 01:53:27 +00:00
|
|
|
defaultPermissionsCache = new HashSet<String>();
|
2011-01-16 20:51:35 +00:00
|
|
|
|
2011-09-27 07:32:46 +00:00
|
|
|
Map<String, Set<String>> userGroupPermissions = new HashMap<String, Set<String>>();
|
|
|
|
|
2011-10-23 05:22:50 +00:00
|
|
|
List<String> groupKeys = config.getStringList("permissions.groups", null);
|
2011-09-27 07:32:46 +00:00
|
|
|
|
2011-01-16 20:51:35 +00:00
|
|
|
if (groupKeys != null) {
|
|
|
|
for (String key : groupKeys) {
|
|
|
|
List<String> permissions =
|
2011-10-23 05:22:50 +00:00
|
|
|
config.getStringList("permissions.groups." + key + ".permissions", null);
|
2011-09-27 07:32:46 +00:00
|
|
|
|
2011-01-16 20:51:35 +00:00
|
|
|
if (permissions.size() > 0) {
|
|
|
|
Set<String> groupPerms = new HashSet<String>(permissions);
|
|
|
|
userGroupPermissions.put(key, groupPerms);
|
2011-09-27 07:32:46 +00:00
|
|
|
|
2011-01-22 01:53:27 +00:00
|
|
|
if (key.equals("default")) {
|
|
|
|
defaultPermissionsCache.addAll(permissions);
|
|
|
|
}
|
2011-01-16 20:51:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-09-27 07:32:46 +00:00
|
|
|
|
2011-10-23 05:22:50 +00:00
|
|
|
List<String> userKeys = config.getStringList("permissions.users", null);
|
2011-01-16 20:51:35 +00:00
|
|
|
|
|
|
|
if (userKeys != null) {
|
|
|
|
for (String key : userKeys) {
|
|
|
|
Set<String> permsCache = new HashSet<String>();
|
2011-09-27 07:32:46 +00:00
|
|
|
|
2011-01-16 20:51:35 +00:00
|
|
|
List<String> permissions =
|
2011-10-23 05:22:50 +00:00
|
|
|
config.getStringList("permissions.users." + key + ".permissions", null);
|
2011-09-27 07:32:46 +00:00
|
|
|
|
2011-01-16 20:51:35 +00:00
|
|
|
if (permissions.size() > 0) {
|
|
|
|
permsCache.addAll(permissions);
|
|
|
|
}
|
2011-09-27 07:32:46 +00:00
|
|
|
|
2011-01-16 20:51:35 +00:00
|
|
|
List<String> groups =
|
2011-10-23 05:22:50 +00:00
|
|
|
config.getStringList("permissions.users." + key + ".groups", null);
|
2011-01-16 20:51:35 +00:00
|
|
|
groups.add("default");
|
2011-09-27 07:32:46 +00:00
|
|
|
|
2011-01-16 20:51:35 +00:00
|
|
|
if (groups.size() > 0) {
|
|
|
|
for (String group : groups) {
|
|
|
|
Set<String> groupPerms = userGroupPermissions.get(group);
|
|
|
|
if (groupPerms != null) {
|
|
|
|
permsCache.addAll(groupPerms);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
userPermissionsCache.put(key.toLowerCase(), permsCache);
|
|
|
|
userGroups.put(key.toLowerCase(), new HashSet<String>(groups));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-09-27 07:32:46 +00:00
|
|
|
|
2011-01-16 20:51:35 +00:00
|
|
|
public boolean hasPermission(String player, String permission) {
|
2011-01-29 17:01:21 +00:00
|
|
|
int dotPos = permission.lastIndexOf(".");
|
|
|
|
if (dotPos > -1) {
|
|
|
|
if (hasPermission(player, permission.substring(0, dotPos))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2011-09-27 07:32:46 +00:00
|
|
|
|
2011-01-16 20:51:35 +00:00
|
|
|
Set<String> perms = userPermissionsCache.get(player.toLowerCase());
|
|
|
|
if (perms == null) {
|
2011-01-22 01:53:27 +00:00
|
|
|
return defaultPermissionsCache.contains(permission)
|
|
|
|
|| defaultPermissionsCache.contains("*");
|
2011-01-16 20:51:35 +00:00
|
|
|
}
|
2011-09-27 07:32:46 +00:00
|
|
|
|
|
|
|
return perms.contains("*") || perms.contains(permission);
|
2011-01-16 20:51:35 +00:00
|
|
|
}
|
2011-06-09 04:16:39 +00:00
|
|
|
|
|
|
|
public boolean hasPermission(String worldName, String player, String permission) {
|
2011-09-27 07:32:46 +00:00
|
|
|
return hasPermission(player, "worlds." + worldName + "." + permission)
|
|
|
|
|| hasPermission(player, permission);
|
2011-06-09 04:16:39 +00:00
|
|
|
}
|
|
|
|
|
2011-01-16 20:51:35 +00:00
|
|
|
public boolean inGroup(String player, String group) {
|
|
|
|
Set<String> groups = userGroups.get(player.toLowerCase());
|
|
|
|
if (groups == null) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-09-27 07:32:46 +00:00
|
|
|
|
|
|
|
return groups.contains(group);
|
2011-01-16 20:51:35 +00:00
|
|
|
}
|
2011-09-27 07:32:46 +00:00
|
|
|
|
2011-01-16 21:19:29 +00:00
|
|
|
public String[] getGroups(String player) {
|
|
|
|
Set<String> groups = userGroups.get(player.toLowerCase());
|
|
|
|
if (groups == null) {
|
|
|
|
return new String[0];
|
|
|
|
}
|
2011-09-27 07:32:46 +00:00
|
|
|
|
|
|
|
return groups.toArray(new String[groups.size()]);
|
2011-01-16 21:19:29 +00:00
|
|
|
}
|
2011-09-27 03:40:18 +00:00
|
|
|
|
|
|
|
public String getDetectionMessage() {
|
|
|
|
return "No known permissions plugin detected. Using configuration file for permissions.";
|
|
|
|
}
|
2011-09-27 07:32:46 +00:00
|
|
|
|
2011-01-16 20:51:35 +00:00
|
|
|
}
|