Redo some permissions code, support anticheat * permission nodes

This commit is contained in:
h31ix committed 2013-03-11 22:00:26 -04:00
1 parent 22dafba2b0
commit e481af55cc
3 files changed
+63 -56

No files matched your search

@@ -53,7 +53,7 @@ public class CommandHandler implements CommandExecutor {
private static final String MENU_END = "-----------------------------------------------------"; private static final String MENU_END = "-----------------------------------------------------";
public void handleLog(CommandSender cs, String[] args) { public void handleLog(CommandSender cs, String[] args) {
if (Permission.SYSTEM_LOG.get(cs)) { if (hasPermission(cs, Permission.SYSTEM_LOG)) {
if (args[1].equalsIgnoreCase("enable")) { if (args[1].equalsIgnoreCase("enable")) {
if (!CONFIG.logConsole()) { if (!CONFIG.logConsole()) {
CONFIG.setLog(true); CONFIG.setLog(true);
@@ -71,33 +71,29 @@ public class CommandHandler implements CommandExecutor {
} else { } else {
cs.sendMessage(RED + "Usage: /anticheat log [enable/disable]"); cs.sendMessage(RED + "Usage: /anticheat log [enable/disable]");
} }
} else {
cs.sendMessage(PERMISSIONS_ERROR);
} }
} }
public void handleDebug(CommandSender cs) { public void handleDebug(CommandSender cs) {
if (Permission.SYSTEM_REPORT.get(cs)) { if (hasPermission(cs, Permission.SYSTEM_DEBUG)) {
cs.sendMessage(GRAY + "Please wait while I collect some data...");
PastebinReport report = new PastebinReport(cs); PastebinReport report = new PastebinReport(cs);
cs.sendMessage(GREEN + "Debug information posted to: " + WHITE + report.getURL()); cs.sendMessage(GREEN + "Debug information posted to: " + WHITE + report.getURL());
cs.sendMessage(GREEN + "Please include this link when making bug reports."); cs.sendMessage(GREEN + "Please include this link when making bug reports.");
} else {
cs.sendMessage(PERMISSIONS_ERROR);
} }
} }
public void handleDebug(CommandSender cs, Player tp) { public void handleDebug(CommandSender cs, Player tp) {
if (Permission.SYSTEM_REPORT.get(cs)) { if (hasPermission(cs, Permission.SYSTEM_REPORT)) {
cs.sendMessage(GRAY + "Please wait while I collect some data...");
PastebinReport report = new PastebinReport(cs, tp); PastebinReport report = new PastebinReport(cs, tp);
cs.sendMessage(GREEN + "Debug information posted to: " + WHITE + report.getURL()); cs.sendMessage(GREEN + "Debug information posted to: " + WHITE + report.getURL());
cs.sendMessage(GREEN + "Please include this link when making bug reports."); cs.sendMessage(GREEN + "Please include this link when making bug reports.");
} else {
cs.sendMessage(PERMISSIONS_ERROR);
} }
} }
public void handleXRay(CommandSender cs, String[] args) { public void handleXRay(CommandSender cs, String[] args) {
if (Permission.SYSTEM_XRAY.get(cs)) { if (hasPermission(cs, Permission.SYSTEM_XRAY)) {
if (CONFIG.logXRay()) { if (CONFIG.logXRay()) {
List<Player> list = SERVER.matchPlayer(args[1]); List<Player> list = SERVER.matchPlayer(args[1]);
if (list.size() == 1) { if (list.size() == 1) {
@@ -119,13 +115,11 @@ public class CommandHandler implements CommandExecutor {
} else { } else {
cs.sendMessage(RED + "XRay logging is off in the config."); cs.sendMessage(RED + "XRay logging is off in the config.");
} }
} else {
cs.sendMessage(PERMISSIONS_ERROR);
} }
} }
public void handleReset(CommandSender cs, String[] args) { public void handleReset(CommandSender cs, String[] args) {
if (Permission.SYSTEM_RESET.get(cs)) { if (hasPermission(cs, Permission.SYSTEM_RESET)) {
List<Player> list = SERVER.matchPlayer(args[1]); List<Player> list = SERVER.matchPlayer(args[1]);
if (list.size() == 1) { if (list.size() == 1) {
Player player = list.get(0); Player player = list.get(0);
@@ -142,14 +136,12 @@ public class CommandHandler implements CommandExecutor {
} else { } else {
cs.sendMessage(RED + "Player: " + WHITE + args[1] + RED + " not found."); cs.sendMessage(RED + "Player: " + WHITE + args[1] + RED + " not found.");
} }
} else {
cs.sendMessage(PERMISSIONS_ERROR);
} }
} }
public void handleSpy(CommandSender cs, String[] args) { public void handleSpy(CommandSender cs, String[] args) {
if (cs instanceof Player) { if (cs instanceof Player) {
if (Permission.SYSTEM_SPY.get(cs)) { if (hasPermission(cs, Permission.SYSTEM_SPY)) {
Player sender = (Player) cs; Player sender = (Player) cs;
if (!args[1].equalsIgnoreCase("off")) { if (!args[1].equalsIgnoreCase("off")) {
List<Player> list = SERVER.matchPlayer(args[1]); List<Player> list = SERVER.matchPlayer(args[1]);
@@ -190,8 +182,6 @@ public class CommandHandler implements CommandExecutor {
sender.sendMessage(RED + "You were not spying."); sender.sendMessage(RED + "You were not spying.");
} }
} }
} else {
cs.sendMessage(PERMISSIONS_ERROR);
} }
} else { } else {
cs.sendMessage(RED + "Sorry, but you can't spy on a player from the console."); cs.sendMessage(RED + "Sorry, but you can't spy on a player from the console.");
@@ -199,27 +189,32 @@ public class CommandHandler implements CommandExecutor {
} }
public void handleHelp(CommandSender cs) { public void handleHelp(CommandSender cs) {
if (Permission.SYSTEM_HELP.get(cs)) { if (hasPermission(cs, Permission.SYSTEM_HELP)) {
String base = "/AntiCheat "; String base = "/AntiCheat ";
String [] lines = {
"log [Enable/Disable]" + WHITE + " - toggle logging",
"report [low/medium/high]" + WHITE + " - show users in groups",
"report [user]" + WHITE + " - get a player's cheat report",
"reload" + WHITE + " - reload AntiCheat configuration",
"reset [user]" + WHITE + " - reset user's hack level",
"xray [user]" + WHITE + " - check user's xray levels",
"spy [user]" + WHITE + " - spy on a user in secret",
"help" + WHITE + " - access this page",
"debug" + WHITE + " - post debug information",
"update" + WHITE + " - check update status",
};
cs.sendMessage("----------------------[" + GREEN + "AntiCheat" + WHITE + "]----------------------"); cs.sendMessage("----------------------[" + GREEN + "AntiCheat" + WHITE + "]----------------------");
cs.sendMessage(base + GREEN + "log [Enable/Disable]" + WHITE + " - toggle logging"); for(String s : lines) {
cs.sendMessage(base + GREEN + "report [low/medium/high]" + WHITE + " - show users in groups"); cs.sendMessage(base + GREEN + s);
cs.sendMessage(base + GREEN + "report [user]" + WHITE + " - get a player's cheat report"); }
cs.sendMessage(base + GREEN + "reload" + WHITE + " - reload AntiCheat configuration");
cs.sendMessage(base + GREEN + "reset [user]" + WHITE + " - reset user's hack level");
cs.sendMessage(base + GREEN + "xray [user]" + WHITE + " - check user's xray levels");
cs.sendMessage(base + GREEN + "spy [user]" + WHITE + " - spy on a user in secret");
cs.sendMessage(base + GREEN + "help" + WHITE + " - access this page");
cs.sendMessage(base + GREEN + "debug" + WHITE + " - post debug information");
cs.sendMessage(base + GREEN + "update" + WHITE + " - check update status");
cs.sendMessage(MENU_END); cs.sendMessage(MENU_END);
} else {
cs.sendMessage(PERMISSIONS_ERROR);
} }
} }
public void handleUpdate(CommandSender cs) { public void handleUpdate(CommandSender cs) {
if (Permission.SYSTEM_UPDATE.get(cs)) { if (hasPermission(cs, Permission.SYSTEM_UPDATE)) {
cs.sendMessage("Running " + GREEN + "AntiCheat " + WHITE + "v" + GREEN + Anticheat.getVersion()); cs.sendMessage("Running " + GREEN + "AntiCheat " + WHITE + "v" + GREEN + Anticheat.getVersion());
cs.sendMessage(MENU_END); cs.sendMessage(MENU_END);
if (!Anticheat.isUpdated()) { if (!Anticheat.isUpdated()) {
@@ -233,27 +228,21 @@ public class CommandHandler implements CommandExecutor {
} else { } else {
cs.sendMessage(GRAY + "AntiCheat is " + GREEN + "UP TO DATE!"); cs.sendMessage(GRAY + "AntiCheat is " + GREEN + "UP TO DATE!");
} }
} else {
cs.sendMessage(PERMISSIONS_ERROR);
} }
} }
public void handleCalibrate(CommandSender cs) { public void handleCalibrate(CommandSender cs) {
if (cs instanceof Player) { if (cs instanceof Player) {
if (Permission.SYSTEM_CALIBRATE.get(cs)) { if (hasPermission(cs, Permission.SYSTEM_CALIBRATE)) {
Calibrator c = new Calibrator((Player) cs); Calibrator c = new Calibrator((Player) cs);
SERVER.getPluginManager().registerEvents(c, Anticheat.getPlugin()); SERVER.getPluginManager().registerEvents(c, Anticheat.getPlugin());
c.calibrate(); c.calibrate();
} else {
cs.sendMessage(PERMISSIONS_ERROR);
} }
} else {
cs.sendMessage(RED + "Sorry, but you cannot calibrate from console.");
} }
} }
public void handleReport(CommandSender cs, String[] args) { public void handleReport(CommandSender cs, String[] args) {
if (Permission.SYSTEM_REPORT.get(cs)) { if (hasPermission(cs, Permission.SYSTEM_REPORT)) {
getPlayers(); getPlayers();
if (args.length > 1) { if (args.length > 1) {
String group = args[1]; String group = args[1];
@@ -281,8 +270,6 @@ public class CommandHandler implements CommandExecutor {
} }
} }
} }
} else {
cs.sendMessage(PERMISSIONS_ERROR);
} }
} }
@@ -322,7 +309,7 @@ public class CommandHandler implements CommandExecutor {
} }
public void handlePlayerReport(CommandSender cs, String[] args) { public void handlePlayerReport(CommandSender cs, String[] args) {
if (Permission.SYSTEM_REPORT.get(cs)) { if (hasPermission(cs, Permission.SYSTEM_REPORT)) {
User user = Anticheat.getManager().getUserManager().getUser(args[1]); User user = Anticheat.getManager().getUserManager().getUser(args[1]);
boolean cont = false; boolean cont = false;
if (user == null) { if (user == null) {
@@ -350,8 +337,6 @@ public class CommandHandler implements CommandExecutor {
cs.sendMessage(RED + "Not a valid page number: " + WHITE + args[2]); cs.sendMessage(RED + "Not a valid page number: " + WHITE + args[2]);
} }
} }
} else {
cs.sendMessage(PERMISSIONS_ERROR);
} }
} }
@@ -396,11 +381,9 @@ public class CommandHandler implements CommandExecutor {
} }
public void handleReload(CommandSender cs) { public void handleReload(CommandSender cs) {
if (Permission.SYSTEM_RELOAD.get(cs)) { if (hasPermission(cs, Permission.SYSTEM_RELOAD)) {
CONFIG.load(); CONFIG.load();
cs.sendMessage(GREEN + "AntiCheat configuration reloaded."); cs.sendMessage(GREEN + "AntiCheat configuration reloaded.");
} else {
cs.sendMessage(PERMISSIONS_ERROR);
} }
} }
@@ -460,7 +443,7 @@ public class CommandHandler implements CommandExecutor {
cs.sendMessage(RED + "Unrecognized command."); cs.sendMessage(RED + "Unrecognized command.");
} }
} else { } else {
cs.sendMessage(RED + "Unrecognized command. Try " + ChatColor.WHITE + "/anticheat help"); handleHelp(cs); // Handle no args as alias of help
} }
return true; return true;
} }
@@ -480,4 +463,13 @@ public class CommandHandler implements CommandExecutor {
} }
} }
} }
public boolean hasPermission(CommandSender cs, Permission perm) {
if(perm.get(cs)) {
return true;
} else {
cs.sendMessage(PERMISSIONS_ERROR + " (" + WHITE + perm.toString() + RED + ")");
return false;
}
}
} }
@@ -75,7 +75,11 @@ public class PastebinReport {
// alrighty then noobs. // alrighty then noobs.
for (Permission node : Permission.values()) { for (Permission node : Permission.values()) {
report.append(player.getName() + ": " + node.toString() + " " + node.get(player) + '\n'); report.append(player.getName() + ": " + node.toString() + " " + node.get(player));
if(node.get(player) && !node.whichPermission(player).equals(node.toString())) {
report.append(" (Applied by " + node.whichPermission(player) +")");
}
report.append('\n');
} }
} }
/* Not working at the moment. /* Not working at the moment.
@@ -112,6 +116,7 @@ public class PastebinReport {
report.append("Max Memory: " + runtime.maxMemory() / 1024 / 1024 + "MB" + '\n'); report.append("Max Memory: " + runtime.maxMemory() / 1024 / 1024 + "MB" + '\n');
report.append("Total Memory: " + runtime.totalMemory() / 1024 / 1024 + "MB" + '\n'); report.append("Total Memory: " + runtime.totalMemory() / 1024 / 1024 + "MB" + '\n');
report.append("Server ID + Name: " + Bukkit.getServerId() + " - " + Bukkit.getName() + '\n'); report.append("Server ID + Name: " + Bukkit.getServerId() + " - " + Bukkit.getName() + '\n');
report.append("Online Mode: " + String.valueOf(Bukkit.getOnlineMode()).toUpperCase() + '\n');
report.append("Players: " + Bukkit.getOnlinePlayers().length + "/" + Bukkit.getMaxPlayers() + '\n'); report.append("Players: " + Bukkit.getOnlinePlayers().length + "/" + Bukkit.getMaxPlayers() + '\n');
} }
@@ -60,16 +60,26 @@ public enum Permission {
SYSTEM_REPORT, SYSTEM_REPORT,
SYSTEM_ALERT, SYSTEM_ALERT,
SYSTEM_CALIBRATE, SYSTEM_CALIBRATE,
SYSTEM_DEBUG,
SYSTEM_RELOAD; SYSTEM_RELOAD;
private static final String PERMISSION_ALL = "anticheat.*";
public boolean get(CommandSender cs) { public boolean get(CommandSender cs) {
return cs.hasPermission(toString()); return cs.hasPermission(toString()) || cs.hasPermission(getBase()) || cs.hasPermission(PERMISSION_ALL);
} }
// Now h31ix, I know this is redundant, but this may actually solve the issue. public String getBase() {
// So please, just give it a try :P return "anticheat." + this.name().toLowerCase().split("_")[0] + ".*";
public boolean get(Player p) { }
return p.hasPermission(toString());
public String whichPermission(CommandSender cs) {
for(String s : new String[]{toString(), getBase(), PERMISSION_ALL}) {
if(cs.hasPermission(s)) {
return s;
}
}
return null;
} }
@Override @Override