#314 Evaluate default permission on enum, remove default from handler interface

- Evaluate permission for DefaultPermission on the enum itself
- Remove boolean default from PermissionHandler for hasPermission()
- Remove some unused / intermediary hasPermission() flavors in PermissionsManager
This commit is contained in:
ljacqu 2016-06-01 21:56:57 +02:00
parent fdb9227ec1
commit 95343e366b
10 changed files with 50 additions and 49 deletions

View File

@ -48,7 +48,7 @@ public class HelpProvider implements SettingsDependent {
private String helpHeader;
@Inject
public HelpProvider(PermissionsManager permissionsManager, NewSetting settings) {
HelpProvider(PermissionsManager permissionsManager, NewSetting settings) {
this.permissionsManager = permissionsManager;
loadSettings(settings);
}
@ -152,7 +152,7 @@ public class HelpProvider implements SettingsDependent {
final DefaultPermission defaultPermission = permission.getDefaultPermission();
String addendum = "";
if (DefaultPermission.OP_ONLY.equals(defaultPermission)) {
addendum = PermissionsManager.evaluateDefaultPermission(defaultPermission, sender)
addendum = defaultPermission.evaluate(sender)
? " (You have permission)"
: " (No permission)";
}

View File

@ -1,18 +1,35 @@
package fr.xephi.authme.permission;
import org.bukkit.command.CommandSender;
/**
* The default permission for a command if there is no support for permission nodes.
* The default permission to fall back to if there is no support for permission nodes.
*/
public enum DefaultPermission {
/** No one can execute the command. */
NOT_ALLOWED("No permission"),
/** No one has permission. */
NOT_ALLOWED("No permission") {
@Override
public boolean evaluate(CommandSender sender) {
return false;
}
},
/** Only players with the OP status may execute the command. */
OP_ONLY("OP's only"),
/** Only players with OP status have permission. */
OP_ONLY("OP's only") {
@Override
public boolean evaluate(CommandSender sender) {
return sender.isOp();
}
},
/** The command can be executed by anyone. */
ALLOWED("Everyone allowed");
/** Everyone is granted permission. */
ALLOWED("Everyone allowed") {
@Override
public boolean evaluate(CommandSender sender) {
return true;
}
};
/** Textual representation of the default permission. */
private final String title;
@ -26,9 +43,17 @@ public enum DefaultPermission {
}
/**
* Return the textual representation.
*
* @return The textual representation
* Evaluates whether permission is granted to the sender or not.
*
* @param sender the sender to process
* @return true if the sender has permission, false otherwise
*/
public abstract boolean evaluate(CommandSender sender);
/**
* Return the textual representation.
*
* @return the textual representation
*/
public String getTitle() {
return title;

View File

@ -224,7 +224,7 @@ public class PermissionsManager {
* @param event Event instance.
*/
public void onPluginEnable(PluginEnableEvent event) {
// Get the plugin and it's name
// Get the plugin and its name
Plugin plugin = event.getPlugin();
String pluginName = plugin.getName();
@ -280,41 +280,18 @@ public class PermissionsManager {
return hasPermission(player, permissionNode, def);
}
public boolean hasPermission(Player player, Iterable<PermissionNode> nodes, boolean def) {
for (PermissionNode node : nodes) {
if (!hasPermission(player, node, def)) {
return false;
}
}
return true;
}
public boolean hasPermission(CommandSender sender, CommandDescription command) {
if (command.getPermission() == null) {
return true;
}
DefaultPermission defaultPermission = command.getPermission().getDefaultPermission();
boolean def = evaluateDefaultPermission(defaultPermission, sender);
boolean def = defaultPermission.evaluate(sender);
return (sender instanceof Player)
? hasPermission((Player) sender, command.getPermission(), def)
: def;
}
public static boolean evaluateDefaultPermission(DefaultPermission defaultPermission, CommandSender sender) {
switch (defaultPermission) {
case ALLOWED:
return true;
case OP_ONLY:
return sender.isOp();
case NOT_ALLOWED:
default:
return false;
}
}
/**
* Check if a player has permission.
*
@ -329,7 +306,7 @@ public class PermissionsManager {
if (!isEnabled())
return def;
return handler.hasPermission(player, node, def);
return handler.hasPermission(player, node);
}
/**
@ -530,7 +507,7 @@ public class PermissionsManager {
/**
* Remove all groups of the specified player, if supported.
* Systems like Essentials GroupManager don't allow all groups to be removed from a player, thus the user will stay
* in it's primary group. All the subgroups are removed just fine.
* in its primary group. All the subgroups are removed just fine.
*
* @param player The player to remove all groups from.
*

View File

@ -23,7 +23,7 @@ public class BPermissionsHandler implements PermissionHandler {
}
@Override
public boolean hasPermission(Player player, PermissionNode node, boolean def) {
public boolean hasPermission(Player player, PermissionNode node) {
return ApiLayer.hasPermission(player.getWorld().getName(), CalculableType.USER, player.getName(), node.getNode());
}

View File

@ -30,7 +30,7 @@ public class GroupManagerHandler implements PermissionHandler {
}
@Override
public boolean hasPermission(Player player, PermissionNode node, boolean def) {
public boolean hasPermission(Player player, PermissionNode node) {
final AnjoPermissionsHandler handler = groupManager.getWorldsHolder().getWorldPermissions(player);
return handler != null && handler.has(player, node.getNode());
}

View File

@ -31,11 +31,10 @@ public interface PermissionHandler {
*
* @param player The player.
* @param node The permission node.
* @param def Default returned if no permissions system is used.
*
* @return True if the player has permission.
*/
boolean hasPermission(Player player, PermissionNode node, boolean def);
boolean hasPermission(Player player, PermissionNode node);
/**
* Check whether the player is in the specified group.

View File

@ -21,7 +21,7 @@ public class PermissionsBukkitHandler implements PermissionHandler {
}
@Override
public boolean hasPermission(Player player, PermissionNode node, boolean def) {
public boolean hasPermission(Player player, PermissionNode node) {
return player.hasPermission(node.getNode());
}

View File

@ -21,7 +21,7 @@ public class PermissionsExHandler implements PermissionHandler {
@Override
public boolean addToGroup(Player player, String group) {
if(!PermissionsEx.getPermissionManager().getGroupNames().contains(group)) {
if (!PermissionsEx.getPermissionManager().getGroupNames().contains(group)) {
ConsoleLogger.showError("The plugin tried to set " + player + "'s group to '" + group + "', but it doesn't exist!");
return false;
}
@ -37,7 +37,7 @@ public class PermissionsExHandler implements PermissionHandler {
}
@Override
public boolean hasPermission(Player player, PermissionNode node, boolean def) {
public boolean hasPermission(Player player, PermissionNode node) {
PermissionUser user = permissionManager.getUser(player);
return user.has(node.getNode());
}

View File

@ -27,7 +27,7 @@ public class VaultHandler implements PermissionHandler {
}
@Override
public boolean hasPermission(Player player, PermissionNode node, boolean def) {
public boolean hasPermission(Player player, PermissionNode node) {
return vaultProvider.has(player, node.getNode());
}

View File

@ -29,12 +29,12 @@ public class ZPermissionsHandler implements PermissionHandler {
}
@Override
public boolean hasPermission(Player player, PermissionNode node, boolean def) {
public boolean hasPermission(Player player, PermissionNode node) {
Map<String, Boolean> perms = zPermissionsService.getPlayerPermissions(player.getWorld().getName(), null, player.getName());
if (perms.containsKey(node.getNode()))
return perms.get(node.getNode());
else
return def;
return node.getDefaultPermission().evaluate(player);
}
@Override