#305 Modularize logic / separate from data classes (wip – doesn't compile)

- Remove permission logic on command side; make PermissionsManager handle checks for all CommandSender objects (not only Player), cf. #314
- Remove unnecessary redundancies in passed arguments ("command references" that can be inferred from the FoundResult)
- Extend FoundCommandResult to represent all possible error cases
This commit is contained in:
ljacqu
2015-12-12 00:43:55 +01:00
parent ec9009d776
commit 2f153fb85c
8 changed files with 178 additions and 245 deletions
@@ -301,19 +301,24 @@ public class PermissionsManager implements PermissionsService {
/**
* Check if the player has permission for the given permissions node. If no permissions system is used,
* the player has to be OP in order to have the permission.
* Check if the command sender has permission for the given permissions node. If no permissions system is used or
* if the sender is not a player (e.g. console user), the player has to be OP in order to have the permission.
*
* @param player The player.
* @param sender The command sender.
* @param permissionNode The permissions node to verify.
*
* @return True if the player has the permission, false otherwise.
* @return True if the sender has the permission, false otherwise.
*/
public boolean hasPermission(Player player, PermissionNode permissionNode) {
return hasPermission(player, permissionNode, player.isOp());
public boolean hasPermission(CommandSender sender, PermissionNode permissionNode) {
return hasPermission(sender, permissionNode, sender.isOp());
}
public boolean hasPermission(Player player, PermissionNode permissionNode, boolean def) {
public boolean hasPermission(CommandSender sender, PermissionNode permissionNode, boolean def) {
if (!(sender instanceof Player)) {
return def;
}
Player player = (Player) sender;
return hasPermission(player, permissionNode.getNode(), def)
|| hasPermission(player, permissionNode.getWildcardNode().getNode(), def);
}
@@ -327,15 +332,17 @@ public class PermissionsManager implements PermissionsService {
return true;
}
public boolean hasPermission(Player player, CommandDescription command) {
public boolean hasPermission(CommandSender sender, CommandDescription command) {
if (command.getCommandPermissions() == null
|| CollectionUtils.isEmpty(command.getCommandPermissions().getPermissionNodes())) {
return true;
}
DefaultPermission defaultPermission = command.getCommandPermissions().getDefaultPermission();
boolean def = evaluateDefaultPermission(defaultPermission, player);
return hasPermission(player, command.getCommandPermissions().getPermissionNodes(), def);
boolean def = evaluateDefaultPermission(defaultPermission, sender);
return (sender instanceof Player)
? hasPermission((Player) sender, command.getCommandPermissions().getPermissionNodes(), def)
: def;
}
public static boolean evaluateDefaultPermission(DefaultPermission defaultPermission, CommandSender sender) {
@@ -1,6 +1,7 @@
package fr.xephi.authme.permission;
import fr.xephi.authme.command.CommandDescription;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
/**
@@ -11,23 +12,23 @@ public interface PermissionsService {
/**
* Check if the player has the given permission.
*
* @param player The player
* @param sender The command sender
* @param permission The permission node to check
* @param def Default returned if no permissions system is used
*
* @return True if the player has permission
*/
boolean hasPermission(Player player, PermissionNode permission, boolean def);
boolean hasPermission(CommandSender sender, PermissionNode permission, boolean def);
/**
* Check if the player has the permissions for the given command.
*
* @param player The player
* @param sender The command sender
* @param command The command whose permissions should be checked
*
* @return True if the player may execute the command
*/
boolean hasPermission(Player player, CommandDescription command);
boolean hasPermission(CommandSender sender, CommandDescription command);
/**
* Return the permission system the service is working with.