Fix bPermissions

This commit is contained in:
Mike Primm 2012-03-30 22:07:33 -05:00
parent a3528e9937
commit 1d6cedeeb0
2 changed files with 65 additions and 9 deletions

View File

@ -2,9 +2,11 @@ package org.dynmap.bukkit;
import java.io.File;
import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
@ -48,6 +50,8 @@ import org.bukkit.event.world.SpawnChangeEvent;
import org.bukkit.event.world.StructureGrowEvent;
import org.bukkit.event.world.WorldLoadEvent;
import org.bukkit.event.world.WorldUnloadEvent;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginManager;
@ -442,9 +446,16 @@ public class DynmapPlugin extends JavaPlugin implements DynmapAPI {
/* Set up player login/quit event handler */
registerPlayerLoginListener();
/* Build default permissions from our plugin */
Map<String, Boolean> perdefs = new HashMap<String, Boolean>();
List<Permission> pd = plugin.getDescription().getPermissions();
for(Permission p : pd) {
perdefs.put(p.getName(), p.getDefault() == PermissionDefault.TRUE);
}
permissions = PEXPermissions.create(getServer(), "dynmap");
if (permissions == null)
permissions = bPermPermissions.create(getServer(), "dynmap");
permissions = bPermPermissions.create(getServer(), "dynmap", perdefs);
if (permissions == null)
permissions = PermBukkitPermissions.create(getServer(), "dynmap");
if (permissions == null)

View File

@ -1,51 +1,77 @@
package org.dynmap.bukkit.permissions;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.permissions.PermissionDefault;
import org.bukkit.plugin.Plugin;
import org.dynmap.Log;
import ru.tehkode.permissions.bukkit.PermissionsEx;
import de.bananaco.bpermissions.api.ApiLayer;
import de.bananaco.bpermissions.api.User;
import de.bananaco.bpermissions.api.WorldManager;
import de.bananaco.bpermissions.api.util.CalculableType;
import de.bananaco.bpermissions.api.util.Permission;
public class bPermPermissions implements PermissionProvider {
String name;
String defworld;
WorldManager wm;
Map<String,Boolean> pd;
public static bPermPermissions create(Server server, String name) {
public static bPermPermissions create(Server server, String name, Map<String,Boolean> pd) {
Plugin permissionsPlugin = server.getPluginManager().getPlugin("bPermissions");
if (permissionsPlugin == null)
return null;
server.getPluginManager().enablePlugin(permissionsPlugin);
Log.info("Using bPermissions " + permissionsPlugin.getDescription().getVersion() + " for access control");
return new bPermPermissions(name);
return new bPermPermissions(name, pd);
}
public bPermPermissions(String name) {
public bPermPermissions(String name, Map<String,Boolean> pd) {
this.name = name;
defworld = Bukkit.getServer().getWorlds().get(0).getName();
wm = WorldManager.getInstance();
this.pd = pd;
}
@Override
public boolean has(CommandSender sender, String permission) {
Player player = sender instanceof Player ? (Player) sender : null;
return (player != null) ? ApiLayer.hasPermission(defworld, CalculableType.USER, player.getName(), name + "." + permission) : true;
boolean rslt = true;
if(player != null) {
rslt = hasOfflinePermission(player.getName(), permission);
}
return rslt;
}
@Override
public Set<String> hasOfflinePermissions(String player, Set<String> perms) {
HashSet<String> hasperms = new HashSet<String>();
for (String pp : perms) {
if(ApiLayer.hasPermission(defworld, CalculableType.USER, player, name + "." + pp)) {
hasperms.add(pp);
User usr = wm.getDefaultWorld().getUser(player);
if(usr != null) {
try { usr.calculateEffectivePermissions(); } catch (Exception x) {}
Map<String,Boolean> p = usr.getMappedPermissions();
for (String pp : perms) {
String permval = name + "." + pp;
Boolean v = p.get(permval);
if (v != null) {
if(v.booleanValue())
hasperms.add(permval);
}
else {
v = pd.get(permval);
if((v != null) && v.booleanValue())
hasperms.add(permval);
}
}
}
return hasperms;
@ -53,7 +79,26 @@ public class bPermPermissions implements PermissionProvider {
@Override
public boolean hasOfflinePermission(String player, String perm) {
return ApiLayer.hasPermission(defworld, CalculableType.USER, player, name + "." + perm);
boolean rslt;
String permval = name + "." + perm;
User usr = wm.getDefaultWorld().getUser(player);
if(usr != null) {
try { usr.calculateEffectivePermissions(); } catch (Exception x) {}
if(usr.getMappedPermissions().containsKey(permval)) {
rslt = usr.hasPermission(permval);
}
else {
Boolean v = pd.get(permval);
if(v != null)
rslt = v;
else
rslt = false;
}
}
else {
rslt = false;
}
return rslt;
}
}