Started work on implementing a permission checking system for Vault. (Issue #6)
This commit is contained in:
parent
5b1ac6e8f7
commit
880071140a
@ -2,6 +2,7 @@ package de.craftinc.gates.commands;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
@ -77,50 +78,53 @@ public abstract class BaseCommand
|
|||||||
{
|
{
|
||||||
boolean allParamtertersThere = parameters.size() >= requiredParameters.size();
|
boolean allParamtertersThere = parameters.size() >= requiredParameters.size();
|
||||||
boolean senderIsPlayer = this.sender instanceof Player;
|
boolean senderIsPlayer = this.sender instanceof Player;
|
||||||
boolean parameterIsGate = this.parameters.size() > 0 ? this.getGateForParamater(this.parameters.get(0)) : false;
|
boolean hasGateParameter = false;
|
||||||
boolean senderHasPermission;
|
|
||||||
|
|
||||||
try {
|
if (this.hasGateParam == true && this.parameters.size() > 0 && this.setGateUsingParameter(this.parameters.get(0))) {
|
||||||
senderHasPermission = this.hasPermission();
|
hasGateParameter = true;
|
||||||
}
|
|
||||||
catch (Exception e) { // the gate paramter is missing or incorrect!
|
|
||||||
senderHasPermission = this.hasGateParam ? false : true; // only display the lack of permission message if there is a gate
|
|
||||||
// this should prevent giving permission to the user if there is
|
|
||||||
// a bug inside the permission validation code.
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boolean senderHasPermission = this.hasPermission();
|
||||||
|
|
||||||
|
|
||||||
if(!senderHasPermission)
|
boolean valid = false;
|
||||||
{
|
|
||||||
sendMessage(ChatColor.RED + "You lack the permissions to " + this.helpDescription.toLowerCase() + ".");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (this.senderMustBePlayer && !senderIsPlayer)
|
if (this.senderMustBePlayer && !senderIsPlayer)
|
||||||
{
|
{
|
||||||
sendMessage(ChatColor.RED + "This command can only be used by ingame players.");
|
sendMessage(ChatColor.RED + "This command can only be used by ingame players.");
|
||||||
return false;
|
valid = false;
|
||||||
}
|
}
|
||||||
|
else if (!allParamtertersThere)
|
||||||
if (!allParamtertersThere)
|
|
||||||
{
|
{
|
||||||
sendMessage("Usage: " + this.getUseageTemplate(true));
|
sendMessage(ChatColor.RED + "Some parameters are missing! " + ChatColor.AQUA + "Usage: " + this.getUseageTemplate(true));
|
||||||
return false;
|
valid = false;
|
||||||
|
}
|
||||||
|
else if (!senderHasPermission && this.hasGateParam)
|
||||||
|
{
|
||||||
|
sendMessage(ChatColor.RED + "You either provided a invalid gate or do not have permission to " + this.helpDescription.toLowerCase());
|
||||||
|
valid = false;
|
||||||
|
}
|
||||||
|
else if (!senderHasPermission)
|
||||||
|
{
|
||||||
|
sendMessage(ChatColor.RED + "You lack the permissions to " + this.helpDescription.toLowerCase());
|
||||||
|
valid = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
else if (this.hasGateParam && !hasGateParameter)
|
||||||
if (this.hasGateParam && !parameterIsGate)
|
|
||||||
{
|
{
|
||||||
sendMessage(ChatColor.RED + "There exists no gate with id " + this.parameters.get(0));
|
sendMessage(ChatColor.RED + "There exists no gate with id " + this.parameters.get(0));
|
||||||
return false;
|
valid = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
valid = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return valid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected boolean getGateForParamater(String param)
|
protected boolean setGateUsingParameter(String param)
|
||||||
{
|
{
|
||||||
if (!Gate.exists(param))
|
if (!Gate.exists(param))
|
||||||
{
|
{
|
||||||
@ -134,8 +138,10 @@ public abstract class BaseCommand
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
protected boolean hasPermission() throws Exception
|
* This will return false if a gate is required for this command but this.gate == null.
|
||||||
|
*/
|
||||||
|
protected boolean hasPermission()
|
||||||
{
|
{
|
||||||
if (Plugin.permission == null) // fallback Ð use the standard bukkit permission system
|
if (Plugin.permission == null) // fallback Ð use the standard bukkit permission system
|
||||||
{
|
{
|
||||||
@ -143,51 +149,72 @@ public abstract class BaseCommand
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Player p = null;
|
||||||
|
|
||||||
|
if (this.sender instanceof Player)
|
||||||
|
{
|
||||||
|
p = (Player) this.sender;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Plugin.log(Level.FINE, "Command sender is no player! Switching to bukki for checking permissions!");
|
||||||
|
return this.sender.hasPermission(this.requiredPermission);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
boolean hasPermission = false;
|
||||||
|
|
||||||
if (this.requiredPermission.equals(Plugin.permissionInfo))
|
if (this.requiredPermission.equals(Plugin.permissionInfo))
|
||||||
{
|
{
|
||||||
return Plugin.permission.has(this.player.getWorld(), this.player.getName(), this.requiredPermission);
|
hasPermission = Plugin.permission.has(p.getWorld(), p.getName(), this.requiredPermission);
|
||||||
}
|
}
|
||||||
|
else if (this.requiredPermission.equals(Plugin.permissionUse) )
|
||||||
|
|
||||||
if (this.requiredPermission.equals(Plugin.permissionUse) )
|
|
||||||
{
|
{
|
||||||
return this.hasPermissionAtGateLocationAndExit();
|
hasPermission = this.hasPermissionAtGateLocationAndExit(p);
|
||||||
}
|
}
|
||||||
|
else if (this.requiredPermission.equals(Plugin.permissionManage))
|
||||||
|
|
||||||
if (this.requiredPermission.equals(Plugin.permissionManage))
|
|
||||||
{
|
{
|
||||||
|
|
||||||
if (this.needsPermissionAtCurrentLocation && this.hasGateParam)
|
if (this.needsPermissionAtCurrentLocation && this.hasGateParam)
|
||||||
{
|
{
|
||||||
boolean hasPersmissionAtCurrentLocation = Plugin.permission.has(this.player.getWorld(), this.player.getName(), this.requiredPermission);
|
boolean hasPersmissionAtCurrentLocation = Plugin.permission.has(p.getWorld(), p.getName(), this.requiredPermission);
|
||||||
return hasPersmissionAtCurrentLocation && this.hasPermissionAtGateLocationAndExit();
|
hasPermission = hasPersmissionAtCurrentLocation && this.hasPermissionAtGateLocationAndExit(p);
|
||||||
}
|
}
|
||||||
else if (this.needsPermissionAtCurrentLocation)
|
else if (this.needsPermissionAtCurrentLocation)
|
||||||
{
|
{
|
||||||
return Plugin.permission.has(this.player.getWorld(), this.player.getName(), this.requiredPermission);
|
hasPermission = Plugin.permission.has(p.getWorld(), p.getName(), this.requiredPermission);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return this.hasPermissionAtGateLocationAndExit();
|
hasPermission = this.hasPermissionAtGateLocationAndExit(p);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return false;
|
return hasPermission;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected boolean hasPermissionAtGateLocationAndExit() throws Exception
|
protected boolean hasPermissionAtGateLocationAndExit(Player p)
|
||||||
{
|
{
|
||||||
if (this.gate == null) // make sure we don't run into a nullpointer exception
|
if (this.gate == null || p == null) // make sure we don't run into a nullpointer exception
|
||||||
{
|
{
|
||||||
throw new Exception("Cannot check permissons with no gate provided!");
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boolean permAtLocation = Plugin.permission.has(this.gate.getLocation().getWorld(), p.getName(), this.requiredPermission);
|
||||||
|
|
||||||
boolean permAtLocation = Plugin.permission.has(this.gate.getLocation().getWorld(), player.getName(), this.requiredPermission);
|
boolean permAtExit;
|
||||||
boolean permAtExit = Plugin.permission.has(this.gate.getExit().getWorld(), player.getName(), this.requiredPermission);
|
|
||||||
|
|
||||||
return permAtLocation && permAtExit;
|
if (this.gate.getExit() == null)
|
||||||
|
{
|
||||||
|
permAtExit = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
permAtExit = Plugin.permission.has(this.gate.getExit().getWorld(), p.getName(), this.requiredPermission);
|
||||||
|
}
|
||||||
|
|
||||||
|
return permAtLocation & permAtExit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user