#314 Encapsulate permissions package better
- Move permissions responsibilities from command to permissions (remove some logic from CommandPermissions, add DefaultPermission enum to permissions package) - Start possible interface for the future per-permission system implementations of permissions managers
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package fr.xephi.authme.command;
|
||||
|
||||
import fr.xephi.authme.permission.DefaultPermission;
|
||||
import fr.xephi.authme.permission.PermissionNode;
|
||||
import fr.xephi.authme.util.CollectionUtils;
|
||||
import fr.xephi.authme.util.StringUtils;
|
||||
@@ -555,16 +556,6 @@ public class CommandDescription {
|
||||
return this.permissions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the command permissions.
|
||||
*
|
||||
* @param permissionNode The permission node required.
|
||||
* @param defaultPermission The default permission.
|
||||
*/
|
||||
public void setCommandPermissions(PermissionNode permissionNode, CommandPermissions.DefaultPermission defaultPermission) {
|
||||
this.permissions = new CommandPermissions(permissionNode, defaultPermission);
|
||||
}
|
||||
|
||||
public static CommandBuilder builder() {
|
||||
return new CommandBuilder();
|
||||
}
|
||||
@@ -643,7 +634,7 @@ public class CommandDescription {
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommandBuilder permissions(CommandPermissions.DefaultPermission defaultPermission,
|
||||
public CommandBuilder permissions(DefaultPermission defaultPermission,
|
||||
PermissionNode... permissionNodes) {
|
||||
this.permissions = new CommandPermissions(asMutableList(permissionNodes), defaultPermission);
|
||||
return this;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package fr.xephi.authme.command;
|
||||
|
||||
import fr.xephi.authme.command.CommandPermissions.DefaultPermission;
|
||||
import fr.xephi.authme.command.executable.HelpCommand;
|
||||
import fr.xephi.authme.command.executable.authme.AccountsCommand;
|
||||
import fr.xephi.authme.command.executable.authme.AuthMeCommand;
|
||||
@@ -38,8 +37,8 @@ import fr.xephi.authme.util.Wrapper;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static fr.xephi.authme.command.CommandPermissions.DefaultPermission.ALLOWED;
|
||||
import static fr.xephi.authme.command.CommandPermissions.DefaultPermission.OP_ONLY;
|
||||
import static fr.xephi.authme.permission.DefaultPermission.ALLOWED;
|
||||
import static fr.xephi.authme.permission.DefaultPermission.OP_ONLY;
|
||||
|
||||
/**
|
||||
* Initializes all available AuthMe commands.
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package fr.xephi.authme.command;
|
||||
|
||||
import fr.xephi.authme.AuthMe;
|
||||
import fr.xephi.authme.permission.DefaultPermission;
|
||||
import fr.xephi.authme.permission.PermissionsManager;
|
||||
import fr.xephi.authme.permission.PermissionNode;
|
||||
import org.bukkit.command.CommandSender;
|
||||
@@ -22,18 +23,6 @@ public class CommandPermissions {
|
||||
*/
|
||||
private DefaultPermission defaultPermission;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param permissionNode The permission node required to execute a command.
|
||||
* @param defaultPermission The default permission if the permission nodes couldn't be used.
|
||||
*/
|
||||
public CommandPermissions(PermissionNode permissionNode, DefaultPermission defaultPermission) {
|
||||
this.permissionNodes = new ArrayList<>();
|
||||
this.permissionNodes.add(permissionNode);
|
||||
this.defaultPermission = defaultPermission;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
@@ -54,16 +43,6 @@ public class CommandPermissions {
|
||||
return this.permissionNodes;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the number of permission nodes set.
|
||||
*
|
||||
* @return Permission node count.
|
||||
*/
|
||||
public int getPermissionNodeCount() {
|
||||
return this.permissionNodes.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether this command requires any permission to be executed. This is based on the getPermission() method.
|
||||
*
|
||||
@@ -73,31 +52,28 @@ public class CommandPermissions {
|
||||
*/
|
||||
public boolean hasPermission(CommandSender sender) {
|
||||
// Make sure any permission node is set
|
||||
if (getPermissionNodeCount() == 0)
|
||||
if (permissionNodes.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Get the default permission
|
||||
PermissionsManager permissionsManager = AuthMe.getInstance().getPermissionsManager();
|
||||
final boolean defaultPermission = getDefaultPermissionCommandSender(sender);
|
||||
|
||||
// Make sure the command sender is a player, if not use the default
|
||||
if (!(sender instanceof Player))
|
||||
if (!(sender instanceof Player)) {
|
||||
return defaultPermission;
|
||||
}
|
||||
|
||||
// Get the player instance
|
||||
Player player = (Player) sender;
|
||||
|
||||
// Get the permissions manager, and make sure it's instance is valid
|
||||
PermissionsManager permissionsManager = AuthMe.getInstance().getPermissionsManager();
|
||||
|
||||
if (permissionsManager == null)
|
||||
return false;
|
||||
|
||||
// Check whether the player has permission, return the result
|
||||
for (PermissionNode node : this.permissionNodes) {
|
||||
if (!permissionsManager.hasPermission(player, node, defaultPermission)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return permissionsManager.hasPermission(player, this.permissionNodes, defaultPermission);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -109,14 +85,6 @@ public class CommandPermissions {
|
||||
return this.defaultPermission;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the default permission used if the permission nodes couldn't be used.
|
||||
*
|
||||
* @param defaultPermission The default permission.
|
||||
*/
|
||||
public void setDefaultPermission(DefaultPermission defaultPermission) {
|
||||
this.defaultPermission = defaultPermission;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default permission for a specified command sender.
|
||||
@@ -138,12 +106,4 @@ public class CommandPermissions {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public enum DefaultPermission {
|
||||
NOT_ALLOWED,
|
||||
OP_ONLY,
|
||||
ALLOWED
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,12 +6,14 @@ import fr.xephi.authme.command.CommandDescription;
|
||||
import fr.xephi.authme.command.CommandParts;
|
||||
import fr.xephi.authme.command.CommandPermissions;
|
||||
import fr.xephi.authme.permission.PermissionNode;
|
||||
import fr.xephi.authme.util.CollectionUtils;
|
||||
import fr.xephi.authme.util.StringUtils;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
@@ -85,14 +87,11 @@ public class HelpPrinter {
|
||||
* @param command The command to print the permissions help for.
|
||||
*/
|
||||
public static void printPermissions(CommandSender sender, CommandDescription command) {
|
||||
// Get the permissions and make sure it isn't null
|
||||
// Get the permissions and make sure they aren't missing
|
||||
CommandPermissions permissions = command.getCommandPermissions();
|
||||
if (permissions == null)
|
||||
return;
|
||||
|
||||
// Make sure any permission node is set
|
||||
if (permissions.getPermissionNodeCount() <= 0)
|
||||
if (permissions == null || CollectionUtils.isEmpty(permissions.getPermissionNodes())) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Print the header
|
||||
sender.sendMessage(ChatColor.GOLD + "Permissions:");
|
||||
@@ -107,13 +106,16 @@ public class HelpPrinter {
|
||||
}
|
||||
|
||||
// Print the default permission
|
||||
// TODO ljacqu 20151205: This is duplicating the logic in PermissionsManager#evaluateDefaultPermission
|
||||
// Either use the command manager here, or if that's too heavy, look into moving certain permissions logic
|
||||
// into a Utils class
|
||||
switch (permissions.getDefaultPermission()) {
|
||||
case ALLOWED:
|
||||
sender.sendMessage(ChatColor.GOLD + " Default: " + ChatColor.GRAY + ChatColor.ITALIC + "Permission!");
|
||||
break;
|
||||
|
||||
case OP_ONLY:
|
||||
final String defaultPermsString = ChatColor.GRAY + (permissions.getDefaultPermissionCommandSender(sender) ? ChatColor.ITALIC + " (Permission!)" : ChatColor.ITALIC + " (No Permission!)");
|
||||
final String defaultPermsString = ChatColor.GRAY + (sender.isOp() ? ChatColor.ITALIC + " (Permission!)" : ChatColor.ITALIC + " (No Permission!)");
|
||||
sender.sendMessage(ChatColor.GOLD + " Default: " + ChatColor.YELLOW + ChatColor.ITALIC + "OP's Only!" + defaultPermsString);
|
||||
break;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user