mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2025-06-12 04:23:54 +00:00
Moved WEPIF to the com.sk89q.wepif package and made PermissionsResolverManager a singleton
Added OfflinePlayer permissions fetching methods to WEPIF
This commit is contained in:
@ -0,0 +1,166 @@
|
||||
// $Id$
|
||||
/*
|
||||
* WorldEdit
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.wepif;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import com.sk89q.util.yaml.YAMLNode;
|
||||
import com.sk89q.util.yaml.YAMLProcessor;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
|
||||
public class ConfigurationPermissionsResolver implements PermissionsResolver {
|
||||
private YAMLProcessor config;
|
||||
private Map<String, Set<String>> userPermissionsCache;
|
||||
private Set<String> defaultPermissionsCache;
|
||||
private Map<String, Set<String>> userGroups;
|
||||
|
||||
public ConfigurationPermissionsResolver(YAMLProcessor config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public static YAMLNode generateDefaultPerms(YAMLNode section) {
|
||||
section.setProperty("groups.default.permissions", new String[] {
|
||||
"worldedit.reload",
|
||||
"worldedit.selection",
|
||||
"worlds.creative.worldedit.region"});
|
||||
section.setProperty("groups.admins.permissions", new String[] { "*" });
|
||||
section.setProperty("users.sk89q.permissions", new String[] { "worldedit" });
|
||||
section.setProperty("users.sk89q.groups", new String[] { "admins" });
|
||||
return section;
|
||||
}
|
||||
|
||||
public void load() {
|
||||
userGroups = new HashMap<String, Set<String>>();
|
||||
userPermissionsCache = new HashMap<String, Set<String>>();
|
||||
defaultPermissionsCache = new HashSet<String>();
|
||||
|
||||
Map<String, Set<String>> userGroupPermissions = new HashMap<String, Set<String>>();
|
||||
|
||||
List<String> groupKeys = config.getStringList("permissions.groups", null);
|
||||
|
||||
if (groupKeys != null) {
|
||||
for (String key : groupKeys) {
|
||||
List<String> permissions =
|
||||
config.getStringList("permissions.groups." + key + ".permissions", null);
|
||||
|
||||
if (permissions.size() > 0) {
|
||||
Set<String> groupPerms = new HashSet<String>(permissions);
|
||||
userGroupPermissions.put(key, groupPerms);
|
||||
|
||||
if (key.equals("default")) {
|
||||
defaultPermissionsCache.addAll(permissions);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
List<String> userKeys = config.getStringList("permissions.users", null);
|
||||
|
||||
if (userKeys != null) {
|
||||
for (String key : userKeys) {
|
||||
Set<String> permsCache = new HashSet<String>();
|
||||
|
||||
List<String> permissions =
|
||||
config.getStringList("permissions.users." + key + ".permissions", null);
|
||||
|
||||
if (permissions.size() > 0) {
|
||||
permsCache.addAll(permissions);
|
||||
}
|
||||
|
||||
List<String> groups =
|
||||
config.getStringList("permissions.users." + key + ".groups", null);
|
||||
groups.add("default");
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasPermission(String player, String permission) {
|
||||
int dotPos = permission.lastIndexOf(".");
|
||||
if (dotPos > -1) {
|
||||
if (hasPermission(player, permission.substring(0, dotPos))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Set<String> 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<String> groups = userGroups.get(player.toLowerCase());
|
||||
if (groups == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return groups.contains(group);
|
||||
}
|
||||
|
||||
public String[] getGroups(String player) {
|
||||
Set<String> groups = userGroups.get(player.toLowerCase());
|
||||
if (groups == null) {
|
||||
return new String[0];
|
||||
}
|
||||
|
||||
return groups.toArray(new String[groups.size()]);
|
||||
}
|
||||
|
||||
public boolean hasPermission(OfflinePlayer player, String permission) {
|
||||
return hasPermission(player.getName(), permission);
|
||||
}
|
||||
|
||||
public boolean hasPermission(String worldName, OfflinePlayer player, String permission) {
|
||||
return hasPermission(worldName, player.getName(), permission);
|
||||
}
|
||||
|
||||
public boolean inGroup(OfflinePlayer player, String group) {
|
||||
return inGroup(player.getName(), group);
|
||||
}
|
||||
|
||||
public String[] getGroups(OfflinePlayer player) {
|
||||
return getGroups(player.getName());
|
||||
}
|
||||
|
||||
public String getDetectionMessage() {
|
||||
return "No known permissions plugin detected. Using configuration file for permissions.";
|
||||
}
|
||||
|
||||
}
|
152
src/main/java/com/sk89q/wepif/DinnerPermsResolver.java
Normal file
152
src/main/java/com/sk89q/wepif/DinnerPermsResolver.java
Normal file
@ -0,0 +1,152 @@
|
||||
// $Id$
|
||||
/*
|
||||
* WorldEdit
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.wepif;
|
||||
|
||||
import com.sk89q.util.yaml.YAMLProcessor;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.permissions.Permissible;
|
||||
import org.bukkit.permissions.Permission;
|
||||
import org.bukkit.permissions.PermissionAttachmentInfo;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class DinnerPermsResolver implements PermissionsResolver {
|
||||
|
||||
private static final String GROUP_PREFIX = "group.";
|
||||
private final Server server;
|
||||
|
||||
public DinnerPermsResolver(Server server) {
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
public static PermissionsResolver factory(Server server, YAMLProcessor config) {
|
||||
return new DinnerPermsResolver(server);
|
||||
}
|
||||
|
||||
public void load() {
|
||||
}
|
||||
|
||||
public boolean hasPermission(String name, String permission) {
|
||||
return hasPermission(server.getOfflinePlayer(name), permission);
|
||||
}
|
||||
|
||||
public boolean hasPermission(String worldName, String name, String permission) {
|
||||
return hasPermission(worldName, server.getOfflinePlayer(name), permission);
|
||||
}
|
||||
|
||||
public boolean inGroup(String name, String group) {
|
||||
return inGroup(server.getOfflinePlayer(name), group);
|
||||
}
|
||||
|
||||
public String[] getGroups(String name) {
|
||||
return getGroups(server.getOfflinePlayer(name));
|
||||
}
|
||||
|
||||
public boolean hasPermission(OfflinePlayer player, String permission) {
|
||||
Permissible perms = getPermissible(player);
|
||||
if (perms == null) {
|
||||
return false; // Permissions are only registered for objects with a Permissible
|
||||
}
|
||||
switch (internalHasPermission(perms, permission)) {
|
||||
case -1:
|
||||
return false;
|
||||
case 1:
|
||||
return true;
|
||||
}
|
||||
int dotPos = permission.lastIndexOf(".");
|
||||
while (dotPos > -1) {
|
||||
switch (internalHasPermission(perms, permission.substring(0, dotPos + 1) + "*")) {
|
||||
case -1:
|
||||
return false;
|
||||
case 1:
|
||||
return true;
|
||||
}
|
||||
dotPos = permission.lastIndexOf(".", dotPos - 1);
|
||||
}
|
||||
return internalHasPermission(perms, "*") == 1;
|
||||
}
|
||||
|
||||
public boolean hasPermission(String worldName, OfflinePlayer player, String permission) {
|
||||
return hasPermission(player, permission); // no per-world ability to check permissions in dinnerperms
|
||||
}
|
||||
|
||||
public boolean inGroup(OfflinePlayer player, String group) {
|
||||
Permissible perms = getPermissible(player);
|
||||
if (perms == null) {
|
||||
return false;
|
||||
}
|
||||
return perms.hasPermission(GROUP_PREFIX + group);
|
||||
}
|
||||
|
||||
public String[] getGroups(OfflinePlayer player) {
|
||||
Permissible perms = getPermissible(player);
|
||||
if (perms == null) {
|
||||
return new String[0];
|
||||
}
|
||||
List<String> groupNames = new ArrayList<String>();
|
||||
for (PermissionAttachmentInfo permAttach : perms.getEffectivePermissions()) {
|
||||
String perm = permAttach.getPermission();
|
||||
if (!(perm.startsWith(GROUP_PREFIX) && permAttach.getValue())) {
|
||||
continue;
|
||||
}
|
||||
groupNames.add(perm.substring(GROUP_PREFIX.length(), perm.length()));
|
||||
}
|
||||
return groupNames.toArray(new String[groupNames.size()]);
|
||||
}
|
||||
|
||||
public Permissible getPermissible(OfflinePlayer offline) {
|
||||
if (offline == null) return null;
|
||||
Permissible perm = null;
|
||||
if (offline instanceof Permissible) {
|
||||
perm = (Permissible) offline;
|
||||
} else {
|
||||
Player player = offline.getPlayer();
|
||||
if (player != null) perm = player;
|
||||
}
|
||||
return perm;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the permission from dinnerperms
|
||||
* @param perms Permissible to check for
|
||||
* @param permission The permission to check
|
||||
* @return -1 if the permission is explicitly denied, 1 if the permission is allowed,
|
||||
* 0 if the permission is denied by a default.
|
||||
*/
|
||||
public int internalHasPermission(Permissible perms, String permission) {
|
||||
if (perms.isPermissionSet(permission)) {
|
||||
return perms.hasPermission(permission) ? 1 : -1;
|
||||
} else {
|
||||
Permission perm = server.getPluginManager().getPermission(permission);
|
||||
if (perm != null) {
|
||||
return perm.getDefault().getValue(perms.isOp()) ? 1 : 0;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String getDetectionMessage() {
|
||||
return "Using the Bukkit Permissions API.";
|
||||
}
|
||||
}
|
237
src/main/java/com/sk89q/wepif/FlatFilePermissionsResolver.java
Normal file
237
src/main/java/com/sk89q/wepif/FlatFilePermissionsResolver.java
Normal file
@ -0,0 +1,237 @@
|
||||
// $Id$
|
||||
/*
|
||||
* WorldEdit
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.wepif;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.HashMap;
|
||||
|
||||
import com.sk89q.util.yaml.YAMLProcessor;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.Server;
|
||||
|
||||
public class FlatFilePermissionsResolver implements PermissionsResolver {
|
||||
private Map<String, Set<String>> userPermissionsCache;
|
||||
private Set<String> defaultPermissionsCache;
|
||||
private Map<String, Set<String>> userGroups;
|
||||
|
||||
protected File groupFile;
|
||||
protected File userFile;
|
||||
|
||||
public static PermissionsResolver factory(Server server, YAMLProcessor config) {
|
||||
File groups = new File("perms_groups.txt");
|
||||
File users = new File("perms_users.txt");
|
||||
|
||||
if (!groups.exists() || !users.exists()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new FlatFilePermissionsResolver(groups, users);
|
||||
}
|
||||
|
||||
public FlatFilePermissionsResolver() {
|
||||
this(new File("perms_groups.txt"), new File("perms_users.txt"));
|
||||
}
|
||||
|
||||
public FlatFilePermissionsResolver(File groupFile, File userFile) {
|
||||
this.groupFile = groupFile;
|
||||
this.userFile = userFile;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static boolean filesExists() {
|
||||
return (new File("perms_groups.txt")).exists() && (new File("perms_users.txt")).exists();
|
||||
}
|
||||
|
||||
public Map<String, Set<String>> loadGroupPermissions() {
|
||||
Map<String, Set<String>> userGroupPermissions = new HashMap<String, Set<String>>();
|
||||
|
||||
FileReader input = null;
|
||||
|
||||
try {
|
||||
input = new FileReader(this.groupFile);
|
||||
BufferedReader buff = new BufferedReader(input);
|
||||
|
||||
String line;
|
||||
while ((line = buff.readLine()) != null) {
|
||||
line = line.trim();
|
||||
|
||||
// Blank line
|
||||
if (line.length() == 0) {
|
||||
continue;
|
||||
} else if (line.charAt(0) == ';' || line.charAt(0) == '#') {
|
||||
continue;
|
||||
}
|
||||
|
||||
String[] parts = line.split(":");
|
||||
|
||||
String key = parts[0];
|
||||
|
||||
if (parts.length > 1) {
|
||||
String[] perms = parts[1].split(",");
|
||||
|
||||
Set<String> groupPerms = new HashSet<String>(Arrays.asList(perms));
|
||||
userGroupPermissions.put(key, groupPerms);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (input != null) {
|
||||
input.close();
|
||||
}
|
||||
} catch (IOException e2) {
|
||||
}
|
||||
}
|
||||
|
||||
return userGroupPermissions;
|
||||
}
|
||||
|
||||
public void load() {
|
||||
userGroups = new HashMap<String, Set<String>>();
|
||||
userPermissionsCache = new HashMap<String, Set<String>>();
|
||||
defaultPermissionsCache = new HashSet<String>();
|
||||
|
||||
Map<String, Set<String>> userGroupPermissions = loadGroupPermissions();
|
||||
|
||||
if (userGroupPermissions.containsKey("default")) {
|
||||
defaultPermissionsCache = userGroupPermissions.get("default");
|
||||
}
|
||||
|
||||
FileReader input = null;
|
||||
|
||||
try {
|
||||
input = new FileReader(this.userFile);
|
||||
BufferedReader buff = new BufferedReader(input);
|
||||
|
||||
String line;
|
||||
while ((line = buff.readLine()) != null) {
|
||||
Set<String> permsCache = new HashSet<String>();
|
||||
|
||||
line = line.trim();
|
||||
|
||||
// Blank line
|
||||
if (line.length() == 0) {
|
||||
continue;
|
||||
} else if (line.charAt(0) == ';' || line.charAt(0) == '#') {
|
||||
continue;
|
||||
}
|
||||
|
||||
String[] parts = line.split(":");
|
||||
|
||||
String key = parts[0];
|
||||
|
||||
if (parts.length > 1) {
|
||||
String[] groups = (parts[1] + ",default").split(",");
|
||||
String[] perms = parts.length > 2 ? parts[2].split(",") : new String[0];
|
||||
|
||||
permsCache.addAll(Arrays.asList(perms));
|
||||
|
||||
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>(Arrays.asList(groups)));
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (input != null) {
|
||||
input.close();
|
||||
}
|
||||
} catch (IOException e2) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasPermission(String player, String permission) {
|
||||
int dotPos = permission.lastIndexOf(".");
|
||||
if (dotPos > -1) {
|
||||
if (hasPermission(player, permission.substring(0, dotPos))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Set<String> 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<String> groups = userGroups.get(player.toLowerCase());
|
||||
if (groups == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return groups.contains(group);
|
||||
}
|
||||
|
||||
public String[] getGroups(String player) {
|
||||
Set<String> groups = userGroups.get(player.toLowerCase());
|
||||
if (groups == null) {
|
||||
return new String[0];
|
||||
}
|
||||
|
||||
return groups.toArray(new String[groups.size()]);
|
||||
}
|
||||
|
||||
public boolean hasPermission(OfflinePlayer player, String permission) {
|
||||
return hasPermission(player.getName(), permission);
|
||||
}
|
||||
|
||||
public boolean hasPermission(String worldName, OfflinePlayer player, String permission) {
|
||||
return hasPermission(worldName, player.getName(), permission);
|
||||
}
|
||||
|
||||
public boolean inGroup(OfflinePlayer player, String group) {
|
||||
return inGroup(player.getName(), group);
|
||||
}
|
||||
|
||||
public String[] getGroups(OfflinePlayer player) {
|
||||
return getGroups(player.getName());
|
||||
}
|
||||
|
||||
public String getDetectionMessage() {
|
||||
return "perms_groups.txt and perms_users.txt detected! Using flat file permissions.";
|
||||
}
|
||||
|
||||
}
|
161
src/main/java/com/sk89q/wepif/NijiPermissionsResolver.java
Normal file
161
src/main/java/com/sk89q/wepif/NijiPermissionsResolver.java
Normal file
@ -0,0 +1,161 @@
|
||||
// $Id$
|
||||
/*
|
||||
* WorldEdit
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.wepif;
|
||||
|
||||
import com.sk89q.util.yaml.YAMLProcessor;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.command.PluginCommand;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import com.nijikokun.bukkit.Permissions.Permissions;
|
||||
|
||||
public class NijiPermissionsResolver implements PermissionsResolver {
|
||||
private Server server;
|
||||
private Permissions api;
|
||||
|
||||
public static PermissionsResolver factory(Server server, YAMLProcessor config) {
|
||||
PluginManager pluginManager = server.getPluginManager();
|
||||
try {
|
||||
Class.forName("com.nijikokun.bukkit.Permissions.Permissions");
|
||||
} catch (ClassNotFoundException e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Plugin plugin = pluginManager.getPlugin("Permissions");
|
||||
|
||||
// Check if plugin is loaded and has Permissions interface
|
||||
if (plugin == null || !(plugin instanceof Permissions)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Check for fake permissions
|
||||
if (config.getBoolean("ignore-nijiperms-bridges", true) && isFakeNijiPerms(plugin)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new NijiPermissionsResolver(server, (Permissions) plugin);
|
||||
}
|
||||
|
||||
public void load() {
|
||||
|
||||
}
|
||||
|
||||
public NijiPermissionsResolver(Server server, Permissions plugin) {
|
||||
this.server = server;
|
||||
this.api = plugin;
|
||||
}
|
||||
|
||||
@SuppressWarnings("static-access")
|
||||
public boolean hasPermission(String name, String permission) {
|
||||
try {
|
||||
Player player = server.getPlayerExact(name);
|
||||
if (player == null) return false;
|
||||
try {
|
||||
return api.getHandler().has(player, permission);
|
||||
} catch (Throwable t) {
|
||||
return api.Security.permission(player, permission);
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasPermission(String worldName, String name, String permission) {
|
||||
try {
|
||||
try {
|
||||
return api.getHandler().has(worldName, name, permission);
|
||||
} catch (Throwable t) {
|
||||
return api.getHandler().has(server.getPlayerExact(name), permission);
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("static-access")
|
||||
public boolean inGroup(String name, String group) {
|
||||
try {
|
||||
Player player = server.getPlayerExact(name);
|
||||
if (player == null) return false;
|
||||
try {
|
||||
return api.getHandler().inGroup(player.getWorld().getName(), name, group);
|
||||
} catch (Throwable t) {
|
||||
return api.Security.inGroup(name, group);
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("static-access")
|
||||
public String[] getGroups(String name) {
|
||||
try {
|
||||
Player player = server.getPlayerExact(name);
|
||||
if (player == null) return new String[0];
|
||||
String[] groups = null;
|
||||
try {
|
||||
groups = api.getHandler().getGroups(player.getWorld().getName(), player.getName());
|
||||
} catch (Throwable t) {
|
||||
String group = api.Security.getGroup(player.getWorld().getName(), player.getName());
|
||||
if (group != null) groups = new String[] { group };
|
||||
}
|
||||
if (groups == null) {
|
||||
return new String[0];
|
||||
} else {
|
||||
return groups;
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
return new String[0];
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasPermission(OfflinePlayer player, String permission) {
|
||||
return hasPermission(player.getName(), permission);
|
||||
}
|
||||
|
||||
public boolean hasPermission(String worldName, OfflinePlayer player, String permission) {
|
||||
return hasPermission(worldName, player.getName(), permission);
|
||||
}
|
||||
|
||||
public boolean inGroup(OfflinePlayer player, String group) {
|
||||
return inGroup(player.getName(), group);
|
||||
}
|
||||
|
||||
public String[] getGroups(OfflinePlayer player) {
|
||||
return getGroups(player.getName());
|
||||
}
|
||||
|
||||
public static boolean isFakeNijiPerms(Plugin plugin) {
|
||||
PluginCommand permsCommand = Bukkit.getServer().getPluginCommand("permissions");
|
||||
|
||||
return permsCommand == null || !(permsCommand.getPlugin().equals(plugin));
|
||||
}
|
||||
|
||||
public String getDetectionMessage() {
|
||||
return "Permissions plugin detected! Using Permissions plugin for permissions.";
|
||||
}
|
||||
}
|
102
src/main/java/com/sk89q/wepif/PermissionsExResolver.java
Normal file
102
src/main/java/com/sk89q/wepif/PermissionsExResolver.java
Normal file
@ -0,0 +1,102 @@
|
||||
// $Id$
|
||||
/*
|
||||
* WorldEdit
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.wepif;
|
||||
|
||||
import com.sk89q.util.yaml.YAMLProcessor;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import ru.tehkode.permissions.PermissionManager;
|
||||
import ru.tehkode.permissions.PermissionUser;
|
||||
|
||||
public class PermissionsExResolver implements PermissionsResolver {
|
||||
private final PermissionManager manager;
|
||||
private final Server server;
|
||||
|
||||
public static PermissionsResolver factory(Server server, YAMLProcessor config) {
|
||||
try {
|
||||
PermissionManager manager = server.getServicesManager().load(PermissionManager.class);
|
||||
|
||||
if (manager == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new PermissionsExResolver(server, manager);
|
||||
} catch (Throwable t) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public PermissionsExResolver(Server server, PermissionManager manager) {
|
||||
this.server = server;
|
||||
this.manager = manager;
|
||||
}
|
||||
|
||||
public void load() {
|
||||
|
||||
}
|
||||
|
||||
public boolean hasPermission(String name, String permission) {
|
||||
Player player = server.getPlayerExact(name);
|
||||
return manager.has(name, permission, player == null ? null : player.getWorld().getName());
|
||||
}
|
||||
|
||||
public boolean hasPermission(String worldName, String name, String permission) {
|
||||
return manager.has(name, permission, worldName);
|
||||
}
|
||||
|
||||
public boolean inGroup(String player, String group) {
|
||||
PermissionUser user = manager.getUser(player);
|
||||
if (user == null) {
|
||||
return false;
|
||||
}
|
||||
return user.inGroup(group);
|
||||
}
|
||||
|
||||
public String[] getGroups(String player) {
|
||||
PermissionUser user = manager.getUser(player);
|
||||
if (user == null) {
|
||||
return new String[0];
|
||||
}
|
||||
return user.getGroupsNames();
|
||||
}
|
||||
|
||||
public boolean hasPermission(OfflinePlayer player, String permission) {
|
||||
Player onlinePlayer = player.getPlayer();
|
||||
return manager.has(player.getName(), permission, onlinePlayer == null ? null : onlinePlayer.getWorld().getName());
|
||||
}
|
||||
|
||||
public boolean hasPermission(String worldName, OfflinePlayer player, String permission) {
|
||||
return hasPermission(worldName, player.getName(), permission);
|
||||
}
|
||||
|
||||
public boolean inGroup(OfflinePlayer player, String group) {
|
||||
return inGroup(player.getName(), group);
|
||||
}
|
||||
|
||||
public String[] getGroups(OfflinePlayer player) {
|
||||
return getGroups(player.getName());
|
||||
}
|
||||
|
||||
public String getDetectionMessage() {
|
||||
return "PermissionsEx detected! Using PermissionsEx for permissions.";
|
||||
}
|
||||
}
|
40
src/main/java/com/sk89q/wepif/PermissionsProvider.java
Normal file
40
src/main/java/com/sk89q/wepif/PermissionsProvider.java
Normal file
@ -0,0 +1,40 @@
|
||||
// $Id$
|
||||
/*
|
||||
* WorldEdit
|
||||
* Copyright (C) 2010, 2011 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/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.wepif;
|
||||
|
||||
import org.bukkit.OfflinePlayer;
|
||||
|
||||
public interface PermissionsProvider {
|
||||
public boolean hasPermission(String name, String permission);
|
||||
|
||||
public boolean hasPermission(String worldName, String name, String permission);
|
||||
|
||||
public boolean inGroup(String player, String group);
|
||||
|
||||
public String[] getGroups(String player);
|
||||
|
||||
public boolean hasPermission(OfflinePlayer player, String permission);
|
||||
|
||||
public boolean hasPermission(String worldName, OfflinePlayer player, String permission);
|
||||
|
||||
public boolean inGroup(OfflinePlayer player, String group);
|
||||
|
||||
public String[] getGroups(OfflinePlayer player);
|
||||
}
|
26
src/main/java/com/sk89q/wepif/PermissionsResolver.java
Normal file
26
src/main/java/com/sk89q/wepif/PermissionsResolver.java
Normal file
@ -0,0 +1,26 @@
|
||||
// $Id$
|
||||
/*
|
||||
* WorldEdit
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.wepif;
|
||||
|
||||
public interface PermissionsResolver extends PermissionsProvider {
|
||||
public void load();
|
||||
|
||||
public String getDetectionMessage();
|
||||
}
|
272
src/main/java/com/sk89q/wepif/PermissionsResolverManager.java
Normal file
272
src/main/java/com/sk89q/wepif/PermissionsResolverManager.java
Normal file
@ -0,0 +1,272 @@
|
||||
// $Id$
|
||||
/*
|
||||
* WorldEdit
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.wepif;
|
||||
|
||||
import com.sk89q.bukkit.migration.LegacyPluginPermissionsResolver;
|
||||
import com.sk89q.util.yaml.YAMLFormat;
|
||||
import com.sk89q.util.yaml.YAMLProcessor;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class PermissionsResolverManager implements PermissionsResolver {
|
||||
private static final String CONFIG_HEADER = "#\r\n" +
|
||||
"# WEPIF Configuration File\r\n" +
|
||||
"#\r\n" +
|
||||
"# This file handles permissions configuration for every plugin using WEPIF\r\n" +
|
||||
"#\r\n" +
|
||||
"# About editing this file:\r\n" +
|
||||
"# - DO NOT USE TABS. You MUST use spaces or Bukkit will complain. If\r\n" +
|
||||
"# you use an editor like Notepad++ (recommended for Windows users), you\r\n" +
|
||||
"# must configure it to \"replace tabs with spaces.\" In Notepad++, this can\r\n" +
|
||||
"# be changed in Settings > Preferences > Language Menu.\r\n" +
|
||||
"# - Don't get rid of the indents. They are indented so some entries are\r\n" +
|
||||
"# in categories (like \"enforce-single-session\" is in the \"protection\"\r\n" +
|
||||
"# category.\r\n" +
|
||||
"# - If you want to check the format of this file before putting it\r\n" +
|
||||
"# into WEPIF, paste it into http://yaml-online-parser.appspot.com/\r\n" +
|
||||
"# and see if it gives \"ERROR:\".\r\n" +
|
||||
"# - Lines starting with # are comments and so they are ignored.\r\n" +
|
||||
"#\r\n" +
|
||||
"# About Configuration Permissions\r\n" +
|
||||
"# - See http://wiki.sk89q.com/wiki/WorldEdit/Permissions/Bukkit\r\n" +
|
||||
"# - Now with multiworld support (see example)\r\n" +
|
||||
"\r\n";
|
||||
|
||||
private static PermissionsResolverManager instance;
|
||||
|
||||
public static void initialize(Plugin plugin) {
|
||||
if (instance == null) {
|
||||
instance = new PermissionsResolverManager(plugin);
|
||||
}
|
||||
}
|
||||
|
||||
public static PermissionsResolverManager getInstance() {
|
||||
if (instance == null) {
|
||||
throw new WEPIFRutimeException("WEPIF has not yet been initialized!");
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
private Server server;
|
||||
private PermissionsResolver permissionResolver;
|
||||
private PermissionsResolverServerListener listener;
|
||||
private YAMLProcessor config;
|
||||
private Logger logger = Logger.getLogger(getClass().getCanonicalName());
|
||||
private List<Class<? extends PermissionsResolver>> enabledResolvers = new ArrayList<Class<? extends PermissionsResolver>>();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Class<? extends PermissionsResolver>[] availableResolvers = new Class[] {
|
||||
PluginPermissionsResolver.class,
|
||||
LegacyPluginPermissionsResolver.class,
|
||||
PermissionsExResolver.class,
|
||||
NijiPermissionsResolver.class,
|
||||
DinnerPermsResolver.class,
|
||||
FlatFilePermissionsResolver.class
|
||||
};
|
||||
|
||||
protected PermissionsResolverManager(Plugin plugin) {
|
||||
this.server = plugin.getServer();
|
||||
this.listener = new PermissionsResolverServerListener(this, plugin);
|
||||
|
||||
loadConfig(new File("wepif.yml"));
|
||||
findResolver();
|
||||
}
|
||||
|
||||
public void findResolver() {
|
||||
for (Class<? extends PermissionsResolver> resolverClass : enabledResolvers) {
|
||||
try {
|
||||
Method factoryMethod = resolverClass.getMethod("factory", Server.class, YAMLProcessor.class);
|
||||
|
||||
this.permissionResolver = (PermissionsResolver) factoryMethod.invoke(null, this.server, this.config);
|
||||
|
||||
if (this.permissionResolver != null) {
|
||||
break;
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
logger.warning("Error in factory method for " + resolverClass.getSimpleName() + ": " + e);
|
||||
e.printStackTrace();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (permissionResolver == null) {
|
||||
permissionResolver = new ConfigurationPermissionsResolver(config);
|
||||
}
|
||||
permissionResolver.load();
|
||||
logger.info("WEPIF: " + permissionResolver.getDetectionMessage());
|
||||
}
|
||||
|
||||
public void setPluginPermissionsResolver(Plugin plugin) {
|
||||
if (!(plugin instanceof PermissionsProvider)) {
|
||||
return;
|
||||
}
|
||||
|
||||
permissionResolver = new PluginPermissionsResolver((PermissionsProvider) plugin, plugin);
|
||||
logger.info("WEPIF: " + permissionResolver.getDetectionMessage());
|
||||
}
|
||||
|
||||
public void load() {
|
||||
findResolver();
|
||||
permissionResolver.load();
|
||||
}
|
||||
|
||||
public boolean hasPermission(String name, String permission) {
|
||||
return permissionResolver.hasPermission(name, permission);
|
||||
}
|
||||
|
||||
public boolean hasPermission(String worldName, String name, String permission) {
|
||||
return permissionResolver.hasPermission(worldName, name, permission);
|
||||
}
|
||||
|
||||
public boolean inGroup(String player, String group) {
|
||||
return permissionResolver.inGroup(player, group);
|
||||
}
|
||||
|
||||
public String[] getGroups(String player) {
|
||||
return permissionResolver.getGroups(player);
|
||||
}
|
||||
|
||||
public boolean hasPermission(OfflinePlayer player, String permission) {
|
||||
return permissionResolver.hasPermission(player, permission);
|
||||
}
|
||||
|
||||
public boolean hasPermission(String worldName, OfflinePlayer player, String permission) {
|
||||
return permissionResolver.hasPermission(worldName, player, permission);
|
||||
}
|
||||
|
||||
public boolean inGroup(OfflinePlayer player, String group) {
|
||||
return permissionResolver.inGroup(player, group);
|
||||
}
|
||||
|
||||
public String[] getGroups(OfflinePlayer player) {
|
||||
return permissionResolver.getGroups(player);
|
||||
}
|
||||
|
||||
private boolean loadConfig(File file) {
|
||||
boolean isUpdated = false;
|
||||
if (!file.exists()) {
|
||||
try {
|
||||
file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
config = new YAMLProcessor(file, false, YAMLFormat.EXTENDED);
|
||||
try {
|
||||
config.load();
|
||||
} catch (IOException e) {
|
||||
logger.severe("Error loading WEPIF Config: " + e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
List<String> keys = config.getKeys(null);
|
||||
config.setHeader(CONFIG_HEADER);
|
||||
|
||||
if (!keys.contains("ignore-nijiperms-bridges")) {
|
||||
config.setProperty("ignore-nijiperms-bridges", true);
|
||||
isUpdated = true;
|
||||
}
|
||||
|
||||
if (!keys.contains("resolvers")) {
|
||||
//List<String> resolverKeys = config.getKeys("resolvers");
|
||||
List<String> resolvers = new ArrayList<String>();
|
||||
for (Class<?> clazz : availableResolvers) {
|
||||
resolvers.add(clazz.getSimpleName());
|
||||
}
|
||||
enabledResolvers.addAll(Arrays.asList(availableResolvers));
|
||||
config.setProperty("resolvers.enabled", resolvers);
|
||||
isUpdated = true;
|
||||
} else {
|
||||
List<String> disabledResolvers = config.getStringList("resolvers.disabled", new ArrayList<String>());
|
||||
List<String> stagedEnabled = config.getStringList("resolvers.enabled", null);
|
||||
for (Iterator<String> i = stagedEnabled.iterator(); i.hasNext();) {
|
||||
String nextName = i.next();
|
||||
Class<?> next = null;
|
||||
try {
|
||||
next = Class.forName(getClass().getPackage().getName() + "." + nextName);
|
||||
} catch (ClassNotFoundException e) {}
|
||||
|
||||
if (next == null || !PermissionsResolver.class.isAssignableFrom(next)) {
|
||||
logger.warning("WEPIF: Invalid or unknown class found in enabled resolvers: "
|
||||
+ nextName + ". Moving to disabled resolvers list.");
|
||||
i.remove();
|
||||
disabledResolvers.add(nextName);
|
||||
isUpdated = true;
|
||||
continue;
|
||||
}
|
||||
enabledResolvers.add(next.asSubclass(PermissionsResolver.class));
|
||||
}
|
||||
|
||||
for (Class<?> clazz : availableResolvers) {
|
||||
if (!stagedEnabled.contains(clazz.getSimpleName()) &&
|
||||
!disabledResolvers.contains(clazz.getSimpleName())) {
|
||||
disabledResolvers.add(clazz.getSimpleName());
|
||||
logger.info("New permissions resolver: "
|
||||
+ clazz.getSimpleName() + " detected. " +
|
||||
"Added to disabled resolvers list.");
|
||||
isUpdated = true;
|
||||
}
|
||||
}
|
||||
config.setProperty("resolvers.disabled", disabledResolvers);
|
||||
config.setProperty("resolvers.enabled", stagedEnabled);
|
||||
}
|
||||
|
||||
if (keys.contains("dinner-perms") || keys.contains("dinnerperms")) {
|
||||
config.setProperty("dinner-perms", null);
|
||||
config.setProperty("dinnerperms", null);
|
||||
isUpdated = true;
|
||||
}
|
||||
if (!keys.contains("permissions")) {
|
||||
ConfigurationPermissionsResolver.generateDefaultPerms(
|
||||
config.addNode("permissions"));
|
||||
isUpdated = true;
|
||||
}
|
||||
if (isUpdated) {
|
||||
logger.info("WEPIF: Updated config file");
|
||||
config.save();
|
||||
}
|
||||
return isUpdated;
|
||||
}
|
||||
|
||||
boolean hasServerListener() {
|
||||
return listener != null;
|
||||
}
|
||||
|
||||
void setServerListener(PermissionsResolverServerListener listener) {
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
public static class MissingPluginException extends Exception {
|
||||
private static final long serialVersionUID = 7044832912491608706L;
|
||||
}
|
||||
|
||||
public String getDetectionMessage() {
|
||||
return "Using WEPIF for permissions";
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,80 @@
|
||||
// $Id$
|
||||
/*
|
||||
* WorldEdit
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.wepif;
|
||||
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.Event.Priority;
|
||||
import org.bukkit.event.server.PluginDisableEvent;
|
||||
import org.bukkit.event.server.PluginEnableEvent;
|
||||
import org.bukkit.event.server.ServerListener;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
public class PermissionsResolverServerListener extends ServerListener {
|
||||
private PermissionsResolverManager manager;
|
||||
|
||||
public PermissionsResolverServerListener(PermissionsResolverManager manager, Plugin plugin) {
|
||||
this.manager = manager;
|
||||
if (!manager.hasServerListener()) {
|
||||
register(plugin);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when a plugin is enabled
|
||||
*
|
||||
* @param event Relevant event details
|
||||
*/
|
||||
@Override
|
||||
public void onPluginEnable(PluginEnableEvent event) {
|
||||
Plugin plugin = event.getPlugin();
|
||||
String name = plugin.getDescription().getName();
|
||||
if (plugin instanceof PermissionsProvider) {
|
||||
manager.setPluginPermissionsResolver(plugin);
|
||||
} else if ("Permissions".equals(name) || "PermissionsEx".equals(name)) {
|
||||
manager.findResolver();
|
||||
manager.load();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when a plugin is disabled
|
||||
*
|
||||
* @param event Relevant event details
|
||||
*/
|
||||
@Override
|
||||
public void onPluginDisable(PluginDisableEvent event) {
|
||||
Plugin plugin = event.getPlugin();
|
||||
String name = plugin.getDescription().getName();
|
||||
|
||||
if (plugin instanceof PermissionsProvider || "Permissions".equals(name) || "PermissionsEx".equals(name)) {
|
||||
manager.findResolver();
|
||||
manager.load();
|
||||
}
|
||||
}
|
||||
|
||||
public void register(Plugin plugin) {
|
||||
plugin.getServer().getPluginManager().registerEvent(Event.Type.PLUGIN_ENABLE,
|
||||
this, Priority.Normal, plugin);
|
||||
plugin.getServer().getPluginManager().registerEvent(Event.Type.PLUGIN_DISABLE,
|
||||
this, Priority.Normal, plugin);
|
||||
manager.setServerListener(this);
|
||||
}
|
||||
|
||||
}
|
95
src/main/java/com/sk89q/wepif/PluginPermissionsResolver.java
Normal file
95
src/main/java/com/sk89q/wepif/PluginPermissionsResolver.java
Normal file
@ -0,0 +1,95 @@
|
||||
// $Id$
|
||||
/*
|
||||
* WorldEdit
|
||||
* Copyright (C) 2010, 2011 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/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.wepif;
|
||||
|
||||
import com.sk89q.util.yaml.YAMLProcessor;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||
|
||||
public class PluginPermissionsResolver implements PermissionsResolver {
|
||||
|
||||
protected PermissionsProvider resolver;
|
||||
protected Plugin plugin;
|
||||
|
||||
public static PermissionsResolver factory(Server server, YAMLProcessor config) {
|
||||
// Looking for service
|
||||
RegisteredServiceProvider<PermissionsProvider> serviceProvider = server.getServicesManager().getRegistration(PermissionsProvider.class);
|
||||
|
||||
if (serviceProvider != null) {
|
||||
return new PluginPermissionsResolver(serviceProvider.getProvider(), serviceProvider.getPlugin());
|
||||
}
|
||||
|
||||
// Looking for plugin
|
||||
for (Plugin plugin : server.getPluginManager().getPlugins()) {
|
||||
if (plugin instanceof PermissionsProvider) {
|
||||
return new PluginPermissionsResolver((PermissionsProvider) plugin, plugin);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public PluginPermissionsResolver(PermissionsProvider resolver, Plugin permissionsPlugin) {
|
||||
this.resolver = resolver;
|
||||
this.plugin = permissionsPlugin;
|
||||
}
|
||||
|
||||
public void load() {
|
||||
}
|
||||
|
||||
public boolean hasPermission(String name, String permission) {
|
||||
return resolver.hasPermission(name, permission);
|
||||
}
|
||||
|
||||
public boolean hasPermission(String worldName, String name, String permission) {
|
||||
return resolver.hasPermission(worldName, name, permission);
|
||||
}
|
||||
|
||||
public boolean inGroup(String player, String group) {
|
||||
return resolver.inGroup(player, group);
|
||||
}
|
||||
|
||||
public String[] getGroups(String player) {
|
||||
return resolver.getGroups(player);
|
||||
}
|
||||
|
||||
public boolean hasPermission(OfflinePlayer player, String permission) {
|
||||
return resolver.hasPermission(player, permission);
|
||||
}
|
||||
|
||||
public boolean hasPermission(String worldName, OfflinePlayer player, String permission) {
|
||||
return resolver.hasPermission(worldName, player, permission);
|
||||
}
|
||||
|
||||
public boolean inGroup(OfflinePlayer player, String group) {
|
||||
return resolver.inGroup(player, group);
|
||||
}
|
||||
|
||||
public String[] getGroups(OfflinePlayer player) {
|
||||
return resolver.getGroups(player);
|
||||
}
|
||||
|
||||
public String getDetectionMessage() {
|
||||
return "Using plugin '" + this.plugin.getDescription().getName() + "' for permissions.";
|
||||
}
|
||||
|
||||
}
|
12
src/main/java/com/sk89q/wepif/WEPIFRutimeException.java
Normal file
12
src/main/java/com/sk89q/wepif/WEPIFRutimeException.java
Normal file
@ -0,0 +1,12 @@
|
||||
package com.sk89q.wepif;
|
||||
|
||||
/**
|
||||
* @author zml2008
|
||||
*/
|
||||
public class WEPIFRutimeException extends RuntimeException {
|
||||
|
||||
public WEPIFRutimeException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user