Make permissions-plugin specific implementations extend Vault, fix build permission checks

This commit is contained in:
vemacs 2015-06-02 13:08:53 -06:00
parent 904a9700b2
commit b19dbb1b8f
10 changed files with 65 additions and 544 deletions

View File

@ -1,24 +1,18 @@
package com.earth2me.essentials.perm;
import com.earth2me.essentials.Essentials;
import net.milkbowl.vault.chat.Chat;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider;
import java.util.Arrays;
import java.util.List;
public class VaultHandler extends SuperpermsHandler {
private Essentials plugin;
public abstract class AbstractVaultHandler extends SuperpermsHandler {
private static Permission perms = null;
private static Chat chat = null;
public VaultHandler(Essentials plugin) {
this.plugin = plugin;
}
public boolean setupProviders() {
try {
Class.forName("net.milkbowl.vault.permission.Permission");
@ -27,9 +21,9 @@ public class VaultHandler extends SuperpermsHandler {
return false;
}
RegisteredServiceProvider<Permission> permsProvider = plugin.getServer().getServicesManager().getRegistration(Permission.class);
RegisteredServiceProvider<Permission> permsProvider = Bukkit.getServer().getServicesManager().getRegistration(Permission.class);
perms = permsProvider.getProvider();
RegisteredServiceProvider<Chat> chatProvider = plugin.getServer().getServicesManager().getRegistration(Chat.class);
RegisteredServiceProvider<Chat> chatProvider = Bukkit.getServer().getServicesManager().getRegistration(Chat.class);
chat = chatProvider.getProvider();
return perms != null && chat != null;
}

View File

@ -1,51 +1,11 @@
package com.earth2me.essentials.perm;
import de.bananaco.bpermissions.api.ApiLayer;
import de.bananaco.bpermissions.api.util.CalculableType;
import org.bukkit.entity.Player;
import java.util.Arrays;
import java.util.List;
public class BPermissions2Handler extends SuperpermsHandler {
public BPermissions2Handler() {
}
@Override
public String getGroup(final Player base) {
final List<String> groups = getGroups(base);
if (groups == null || groups.isEmpty()) {
return null;
}
return groups.get(0);
}
@Override
public List<String> getGroups(final Player base) {
final String[] groups = ApiLayer.getGroups(base.getWorld().getName(), CalculableType.USER, base.getName());
return Arrays.asList(groups);
}
@Override
public boolean inGroup(final Player base, final String group) {
return ApiLayer.hasGroupRecursive(base.getWorld().getName(), CalculableType.USER, base.getName(), group);
}
public class BPermissions2Handler extends AbstractVaultHandler {
@Override
public boolean canBuild(final Player base, final String group) {
return hasPermission(base, "bPermissions.build");
}
@Override
public String getPrefix(final Player base) {
return ApiLayer.getValue(base.getWorld().getName(), CalculableType.USER, base.getName(), "prefix");
}
@Override
public String getSuffix(final Player base) {
return ApiLayer.getValue(base.getWorld().getName(), CalculableType.USER, base.getName(), "suffix");
}
}

View File

@ -0,0 +1,4 @@
package com.earth2me.essentials.perm;
public class GenericVaultHandler extends AbstractVaultHandler {
}

View File

@ -6,78 +6,18 @@ import org.anjocaido.groupmanager.permissions.AnjoPermissionsHandler;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import java.util.Arrays;
import java.util.List;
public class GroupManagerHandler implements IPermissionsHandler {
public class GroupManagerHandler extends AbstractVaultHandler {
private final transient GroupManager groupManager;
public GroupManagerHandler(final Plugin permissionsPlugin) {
groupManager = ((GroupManager) permissionsPlugin);
}
@Override
public String getGroup(final Player base) {
final AnjoPermissionsHandler handler = getHandler(base);
if (handler == null) {
return null;
}
return handler.getGroup(base.getName());
}
@Override
public List<String> getGroups(final Player base) {
final AnjoPermissionsHandler handler = getHandler(base);
if (handler == null) {
return null;
}
return Arrays.asList(handler.getGroups(base.getName()));
}
@Override
public boolean canBuild(final Player base, final String group) {
final AnjoPermissionsHandler handler = getHandler(base);
if (handler == null) {
return false;
}
return handler.canUserBuild(base.getName());
}
@Override
public boolean inGroup(final Player base, final String group) {
AnjoPermissionsHandler handler = getHandler(base);
if (handler == null) {
return false;
}
return handler.inGroup(base.getName(), group);
}
@Override
public boolean hasPermission(final Player base, final String node) {
AnjoPermissionsHandler handler = getHandler(base);
if (handler == null) {
return false;
}
return handler.has(base, node);
}
@Override
public String getPrefix(final Player base) {
AnjoPermissionsHandler handler = getHandler(base);
if (handler == null) {
return null;
}
return handler.getUserPrefix(base.getName());
}
@Override
public String getSuffix(final Player base) {
AnjoPermissionsHandler handler = getHandler(base);
if (handler == null) {
return null;
}
return handler.getUserSuffix(base.getName());
return handler != null && handler.canUserBuild(base.getName());
}
private AnjoPermissionsHandler getHandler(final Player base) {

View File

@ -1,66 +0,0 @@
package com.earth2me.essentials.perm;
import com.platymuus.bukkit.permissions.Group;
import com.platymuus.bukkit.permissions.PermissionInfo;
import com.platymuus.bukkit.permissions.PermissionsPlugin;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class PermissionsBukkitHandler extends SuperpermsHandler {
private final transient PermissionsPlugin plugin;
public PermissionsBukkitHandler(final Plugin plugin) {
this.plugin = (PermissionsPlugin) plugin;
}
@Override
public String getGroup(final Player base) {
final List<Group> groups = getPBGroups(base);
if (groups == null || groups.isEmpty()) {
return null;
}
return groups.get(0).getName();
}
@Override
public List<String> getGroups(final Player base) {
final List<Group> groups = getPBGroups(base);
if (groups.size() == 1) {
return Collections.singletonList(groups.get(0).getName());
}
final List<String> groupNames = new ArrayList<String>(groups.size());
for (Group group : groups) {
groupNames.add(group.getName());
}
return groupNames;
}
private List<Group> getPBGroups(final Player base) {
final PermissionInfo info = plugin.getPlayerInfo(base.getName());
if (info == null) {
return Collections.emptyList();
}
final List<Group> groups = info.getGroups();
if (groups == null || groups.isEmpty()) {
return Collections.emptyList();
}
return groups;
}
@Override
public boolean inGroup(final Player base, final String group) {
final List<Group> groups = getPBGroups(base);
for (Group group1 : groups) {
if (group1.getName().equalsIgnoreCase(group)) {
return true;
}
}
return false;
}
}

View File

@ -5,88 +5,16 @@ import ru.tehkode.permissions.PermissionManager;
import ru.tehkode.permissions.PermissionUser;
import ru.tehkode.permissions.bukkit.PermissionsEx;
import java.util.Arrays;
import java.util.List;
public class PermissionsExHandler extends SuperpermsHandler {
public class PermissionsExHandler extends AbstractVaultHandler {
private final transient PermissionManager manager;
public PermissionsExHandler() {
manager = PermissionsEx.getPermissionManager();
}
@Override
public String getGroup(final Player base) {
final PermissionUser user = manager.getUser(base.getName());
if (user == null) {
return null;
}
String[] groupsNames = user.getGroupsNames();
if (groupsNames == null || groupsNames.length == 0) {
return null;
}
return groupsNames[0];
}
@Override
public List<String> getGroups(final Player base) {
final PermissionUser user = manager.getUser(base.getName());
if (user == null) {
return null;
}
String[] groupsNames = user.getGroupsNames();
if (groupsNames == null) {
return null;
}
return Arrays.asList(groupsNames);
}
@Override
public boolean canBuild(final Player base, final String group) {
final PermissionUser user = manager.getUser(base.getName());
if (user == null) {
return false;
}
return user.getOptionBoolean("build", base.getWorld().getName(), false);
}
@Override
public boolean inGroup(final Player base, final String group) {
final PermissionUser user = manager.getUser(base.getName());
if (user == null) {
return false;
}
return user.inGroup(group);
}
@Override
public boolean hasPermission(final Player base, final String node) {
return super.hasPermission(base, node);
}
@Override
public String getPrefix(final Player base) {
final PermissionUser user = manager.getUser(base.getName());
if (user == null) {
return null;
}
return user.getPrefix(base.getWorld().getName());
}
@Override
public String getSuffix(final Player base) {
final PermissionUser user = manager.getUser(base.getName());
if (user == null) {
return null;
}
return user.getSuffix(base.getWorld().getName());
return user != null && user.getOptionBoolean("build", base.getWorld().getName(), false);
}
}

View File

@ -15,7 +15,6 @@ public class PermissionsHandler implements IPermissionsHandler {
private transient IPermissionsHandler handler = new NullPermissionsHandler();
private transient String defaultGroup = "default";
private final transient Essentials ess;
private static final Logger LOGGER = Logger.getLogger("Essentials");
private transient boolean useSuperperms = false;
public PermissionsHandler(final Essentials plugin) {
@ -96,23 +95,12 @@ public class PermissionsHandler implements IPermissionsHandler {
public void checkPermissions() {
final PluginManager pluginManager = ess.getServer().getPluginManager();
final Plugin vaultAPI = pluginManager.getPlugin("Vault");
if (vaultAPI != null && vaultAPI.isEnabled()) {
if (!(handler instanceof VaultHandler)) {
VaultHandler vault = new VaultHandler(ess);
if (vault.setupProviders()) {
LOGGER.log(Level.INFO, "Essentials: Using Vault based permissions.");
handler = vault;
}
}
return;
}
final Plugin permExPlugin = pluginManager.getPlugin("PermissionsEx");
if (permExPlugin != null && permExPlugin.isEnabled()) {
if (!(handler instanceof PermissionsExHandler)) {
LOGGER.log(Level.INFO, "Essentials: Using PermissionsEx based permissions.");
ess.getLogger().info("Using PermissionsEX based permissions with Vault.");
handler = new PermissionsExHandler();
}
return;
@ -121,26 +109,17 @@ public class PermissionsHandler implements IPermissionsHandler {
final Plugin GMplugin = pluginManager.getPlugin("GroupManager");
if (GMplugin != null && GMplugin.isEnabled()) {
if (!(handler instanceof GroupManagerHandler)) {
LOGGER.log(Level.INFO, "Essentials: Using GroupManager based permissions.");
ess.getLogger().info("Using GroupManager based permissions with Vault.");
handler = new GroupManagerHandler(GMplugin);
}
return;
}
final Plugin permBukkitPlugin = pluginManager.getPlugin("PermissionsBukkit");
if (permBukkitPlugin != null && permBukkitPlugin.isEnabled()) {
if (!(handler instanceof PermissionsBukkitHandler)) {
LOGGER.log(Level.INFO, "Essentials: Using PermissionsBukkit based permissions.");
handler = new PermissionsBukkitHandler(permBukkitPlugin);
}
return;
}
final Plugin simplyPermsPlugin = pluginManager.getPlugin("SimplyPerms");
if (simplyPermsPlugin != null && simplyPermsPlugin.isEnabled()) {
if (!(handler instanceof SimplyPermsHandler)) {
LOGGER.log(Level.INFO, "Essentials: Using SimplyPerms based permissions.");
handler = new SimplyPermsHandler(simplyPermsPlugin);
ess.getLogger().info("Using SimplyPerms based permissions with Vault.");
handler = new SimplyPermsHandler();
}
return;
}
@ -148,8 +127,8 @@ public class PermissionsHandler implements IPermissionsHandler {
final Plugin privPlugin = pluginManager.getPlugin("Privileges");
if (privPlugin != null && privPlugin.isEnabled()) {
if (!(handler instanceof PrivilegesHandler)) {
LOGGER.log(Level.INFO, "Essentials: Using Privileges based permissions.");
handler = new PrivilegesHandler(privPlugin);
ess.getLogger().info("Using Privileges based permissions with Vault.");
handler = new PrivilegesHandler();
}
return;
}
@ -157,30 +136,31 @@ public class PermissionsHandler implements IPermissionsHandler {
final Plugin bPermPlugin = pluginManager.getPlugin("bPermissions");
if (bPermPlugin != null && bPermPlugin.isEnabled()) {
if (!(handler instanceof BPermissions2Handler)) {
LOGGER.log(Level.INFO, "Essentials: Using bPermissions2 based permissions.");
ess.getLogger().info("Using bPermissions based permissions with Vault.");
handler = new BPermissions2Handler();
}
return;
}
final Plugin zPermsPlugin = pluginManager.getPlugin("zPermissions");
if (zPermsPlugin != null && zPermsPlugin.isEnabled()) {
if (!(handler instanceof ZPermissionsHandler)) {
LOGGER.log(Level.INFO, "Essentials: Using zPermissions based permissions.");
handler = new ZPermissionsHandler(ess);
if (!(handler instanceof GenericVaultHandler)) {
AbstractVaultHandler vault = new GenericVaultHandler();
if (vault.setupProviders()) {
ess.getLogger().info("Using generic Vault based permissions.");
handler = vault;
}
}
return;
}
if (useSuperperms) {
if (!(handler instanceof SuperpermsHandler)) {
LOGGER.log(Level.INFO, "Essentials: Using superperms based permissions.");
ess.getLogger().info("Using superperms based permissions.");
handler = new SuperpermsHandler();
}
} else {
if (!(handler instanceof ConfigPermissionsHandler)) {
LOGGER.log(Level.INFO, "Essentials: Using config file enhanced permissions.");
LOGGER.log(Level.INFO, "Permissions listed in as player-commands will be given to all users.");
ess.getLogger().info("Essentials: Using config file enhanced permissions.");
ess.getLogger().info("Permissions listed in as player-commands will be given to all users.");
handler = new ConfigPermissionsHandler(ess);
}
}

View File

@ -1,53 +1,10 @@
package com.earth2me.essentials.perm;
import net.krinsoft.privileges.Privileges;
import net.krinsoft.privileges.groups.Group;
import net.krinsoft.privileges.groups.GroupManager;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import java.util.ArrayList;
import java.util.List;
public class PrivilegesHandler extends SuperpermsHandler {
private final transient Privileges plugin;
private final GroupManager manager;
public PrivilegesHandler(final Plugin plugin) {
this.plugin = (Privileges) plugin;
this.manager = this.plugin.getGroupManager();
}
@Override
public String getGroup(final Player base) {
Group group = manager.getGroup(base);
if (group == null) {
return null;
}
return group.getName();
}
@Override
public List<String> getGroups(final Player base) {
Group group = manager.getGroup(base);
if (group == null) {
return new ArrayList<String>();
}
return group.getGroupTree();
}
@Override
public boolean inGroup(final Player base, final String group) {
Group pGroup = manager.getGroup(base);
if (pGroup == null) {
return false;
}
return pGroup.isMemberOf(group);
}
@Override
public boolean canBuild(Player base, String group) {
return hasPermission(base, "privileges.build");
}
}

View File

@ -1,48 +1,10 @@
package com.earth2me.essentials.perm;
import net.crystalyx.bukkit.simplyperms.SimplyAPI;
import net.crystalyx.bukkit.simplyperms.SimplyPlugin;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import java.util.List;
public class SimplyPermsHandler extends SuperpermsHandler {
private final transient SimplyAPI api;
public SimplyPermsHandler(final Plugin plugin) {
this.api = ((SimplyPlugin) plugin).getAPI();
}
@Override
public String getGroup(final Player base) {
final List<String> groups = api.getPlayerGroups(base.getName());
if (groups == null || groups.isEmpty()) {
return null;
}
return groups.get(0);
}
@Override
public List<String> getGroups(final Player base) {
return api.getPlayerGroups(base.getName());
}
@Override
public boolean inGroup(final Player base, final String group) {
final List<String> groups = api.getPlayerGroups(base.getName());
for (String group1 : groups) {
if (group1.equalsIgnoreCase(group)) {
return true;
}
}
return false;
}
public class SimplyPermsHandler extends AbstractVaultHandler {
@Override
public boolean canBuild(Player base, String group) {
return hasPermission(base, "permissions.allow.build");
}
}

View File

@ -1,138 +0,0 @@
package com.earth2me.essentials.perm;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.server.ServiceRegisterEvent;
import org.bukkit.plugin.Plugin;
import org.tyrannyofheaven.bukkit.zPermissions.ZPermissionsService;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
public class ZPermissionsHandler extends SuperpermsHandler implements Listener {
private ZPermissionsService service = null;
private boolean hasGetPlayerPrimaryGroup = false; // This is a post-1.0 addition
public ZPermissionsHandler(Plugin plugin) {
acquireZPermissionsService();
if (!isReady()) {
// Shouldn't get to this point, since caller checks if zPerms
// is enabled. But for the sake of correctness...
Bukkit.getPluginManager().registerEvents(this, plugin);
}
}
@EventHandler
public void onServiceRegister(ServiceRegisterEvent event) {
if (ZPermissionsService.class.equals(event.getProvider().getService())) {
acquireZPermissionsService();
}
}
@Override
public String getGroup(Player base) {
if (!isReady()) {
return super.getGroup(base);
} else {
return getPrimaryGroup(base.getUniqueId());
}
}
@Override
public List<String> getGroups(Player base) {
if (!isReady()) {
return super.getGroups(base);
} else {
return new ArrayList<String>(service.getPlayerGroups(base.getUniqueId()));
}
}
@Override
public boolean inGroup(Player base, String group) {
if (!isReady()) {
return super.inGroup(base, group);
} else {
Set<String> groups = service.getPlayerGroups(base.getUniqueId());
for (String test : groups) {
if (test.equalsIgnoreCase(group)) {
return true;
}
}
return false;
}
}
@Override
public String getPrefix(Player base) {
if (!isReady()) {
return super.getPrefix(base);
} else {
return getPrefixSuffix(base, "prefix");
}
}
@Override
public String getSuffix(Player base) {
if (!isReady()) {
return super.getSuffix(base);
} else {
return getPrefixSuffix(base, "suffix");
}
}
private String getPrefixSuffix(Player base, String metadataName) {
String playerPrefixSuffix;
try {
playerPrefixSuffix = service.getPlayerMetadata(base.getUniqueId(), metadataName, String.class);
} catch (IllegalStateException e) {
// User error. They set prefix to a non-string.
playerPrefixSuffix = null;
}
if (playerPrefixSuffix == null) {
// Try prefix/suffix of their "primary group"
try {
return service.getGroupMetadata(getPrimaryGroup(base.getUniqueId()), metadataName, String.class);
} catch (IllegalStateException e) {
// User error, again
return null;
}
} else {
return playerPrefixSuffix;
}
}
private void acquireZPermissionsService() {
service = Bukkit.getServicesManager().load(ZPermissionsService.class);
if (isReady()) {
// getPlayerPrimaryGroup(String) was added in an unreleased version
// Check if it exists.
try {
service.getClass().getMethod("getPlayerPrimaryGroup", String.class);
hasGetPlayerPrimaryGroup = true;
} catch (NoSuchMethodException e) {
hasGetPlayerPrimaryGroup = false;
} catch (SecurityException e) {
hasGetPlayerPrimaryGroup = false;
}
}
}
private boolean isReady() {
return service != null;
}
private String getPrimaryGroup(UUID playerId) {
if (hasGetPlayerPrimaryGroup) {
return service.getPlayerPrimaryGroup(playerId);
} else {
// Fall back to using highest-weight assigned group
List<String> groups = service.getPlayerAssignedGroups(playerId);
return groups.get(0);
}
}
}