Merge branch '604-default-permissions-on-nodes' of https://github.com/AuthMe-Team/AuthMeReloaded

This commit is contained in:
ljacqu 2016-05-31 09:15:36 +02:00
commit 3b6677ce99
13 changed files with 157 additions and 190 deletions

View File

@ -1,6 +1,5 @@
package fr.xephi.authme.command; package fr.xephi.authme.command;
import fr.xephi.authme.permission.DefaultPermission;
import fr.xephi.authme.permission.PermissionNode; import fr.xephi.authme.permission.PermissionNode;
import fr.xephi.authme.util.CollectionUtils; import fr.xephi.authme.util.CollectionUtils;
import fr.xephi.authme.util.StringUtils; import fr.xephi.authme.util.StringUtils;
@ -52,9 +51,9 @@ public class CommandDescription {
*/ */
private List<CommandArgumentDescription> arguments; private List<CommandArgumentDescription> arguments;
/** /**
* Command permissions required to execute this command. * Permission node required to execute this command.
*/ */
private CommandPermissions permissions; private PermissionNode permission;
/** /**
* Private constructor. Use {@link CommandDescription#builder()} to create instances of this class. * Private constructor. Use {@link CommandDescription#builder()} to create instances of this class.
@ -74,7 +73,7 @@ public class CommandDescription {
* @param executableCommand The executable command, or null. * @param executableCommand The executable command, or null.
* @param parent Parent command. * @param parent Parent command.
* @param arguments Command arguments. * @param arguments Command arguments.
* @param permissions The permissions required to execute this command. * @param permission The permission node required to execute this command.
* *
* @return The created instance * @return The created instance
* @see CommandDescription#builder() * @see CommandDescription#builder()
@ -82,7 +81,7 @@ public class CommandDescription {
private static CommandDescription createInstance(List<String> labels, String description, private static CommandDescription createInstance(List<String> labels, String description,
String detailedDescription, ExecutableCommand executableCommand, String detailedDescription, ExecutableCommand executableCommand,
CommandDescription parent, List<CommandArgumentDescription> arguments, CommandDescription parent, List<CommandArgumentDescription> arguments,
CommandPermissions permissions) { PermissionNode permission) {
CommandDescription instance = new CommandDescription(); CommandDescription instance = new CommandDescription();
instance.labels = labels; instance.labels = labels;
instance.description = description; instance.description = description;
@ -90,7 +89,7 @@ public class CommandDescription {
instance.executableCommand = executableCommand; instance.executableCommand = executableCommand;
instance.parent = parent; instance.parent = parent;
instance.arguments = arguments; instance.arguments = arguments;
instance.permissions = permissions; instance.permission = permission;
if (parent != null) { if (parent != null) {
parent.addChild(instance); parent.addChild(instance);
@ -196,12 +195,12 @@ public class CommandDescription {
} }
/** /**
* Return the permissions required to execute the command. * Return the permission node required to execute the command.
* *
* @return The command permissions, or null if none are required to execute the command. * @return The permission node, or null if none are required to execute the command.
*/ */
public CommandPermissions getCommandPermissions() { public PermissionNode getPermission() {
return permissions; return permission;
} }
/** /**
@ -223,7 +222,7 @@ public class CommandDescription {
private ExecutableCommand executableCommand; private ExecutableCommand executableCommand;
private CommandDescription parent; private CommandDescription parent;
private List<CommandArgumentDescription> arguments = new ArrayList<>(); private List<CommandArgumentDescription> arguments = new ArrayList<>();
private CommandPermissions permissions; private PermissionNode permission;
/** /**
* Build a CommandDescription from the builder or throw an exception if a mandatory * Build a CommandDescription from the builder or throw an exception if a mandatory
@ -239,7 +238,7 @@ public class CommandDescription {
// parents and permissions may be null; arguments may be empty // parents and permissions may be null; arguments may be empty
return createInstance(labels, description, detailedDescription, executableCommand, return createInstance(labels, description, detailedDescription, executableCommand,
parent, arguments, permissions); parent, arguments, permission);
} }
public CommandBuilder labels(List<String> labels) { public CommandBuilder labels(List<String> labels) {
@ -286,9 +285,14 @@ public class CommandDescription {
return this; return this;
} }
public CommandBuilder permissions(DefaultPermission defaultPermission, /**
PermissionNode... permissionNodes) { * Add a permission node that a user must have to execute the command.
this.permissions = new CommandPermissions(asList(permissionNodes), defaultPermission); *
* @param permission The PermissionNode to add
* @return The builder
*/
public CommandBuilder permission(PermissionNode permission) {
this.permission = permission;
return this; return this;
} }
} }

View File

@ -82,7 +82,7 @@ public class CommandInitializer {
.detailedDescription("Register the specified player with the specified password.") .detailedDescription("Register the specified player with the specified password.")
.withArgument("player", "Player name", false) .withArgument("player", "Player name", false)
.withArgument("password", "Password", false) .withArgument("password", "Password", false)
.permissions(OP_ONLY, AdminPermission.REGISTER) .permission(AdminPermission.REGISTER)
.executableCommand(initializer.newInstance(RegisterAdminCommand.class)) .executableCommand(initializer.newInstance(RegisterAdminCommand.class))
.build(); .build();
@ -93,7 +93,7 @@ public class CommandInitializer {
.description("Unregister a player") .description("Unregister a player")
.detailedDescription("Unregister the specified player.") .detailedDescription("Unregister the specified player.")
.withArgument("player", "Player name", false) .withArgument("player", "Player name", false)
.permissions(OP_ONLY, AdminPermission.UNREGISTER) .permission(AdminPermission.UNREGISTER)
.executableCommand(initializer.newInstance(UnregisterAdminCommand.class)) .executableCommand(initializer.newInstance(UnregisterAdminCommand.class))
.build(); .build();
@ -104,7 +104,7 @@ public class CommandInitializer {
.description("Enforce login player") .description("Enforce login player")
.detailedDescription("Enforce the specified player to login.") .detailedDescription("Enforce the specified player to login.")
.withArgument("player", "Online player name", true) .withArgument("player", "Online player name", true)
.permissions(OP_ONLY, AdminPermission.FORCE_LOGIN) .permission(AdminPermission.FORCE_LOGIN)
.executableCommand(initializer.newInstance(ForceLoginCommand.class)) .executableCommand(initializer.newInstance(ForceLoginCommand.class))
.build(); .build();
@ -116,7 +116,7 @@ public class CommandInitializer {
.detailedDescription("Change the password of a player.") .detailedDescription("Change the password of a player.")
.withArgument("player", "Player name", false) .withArgument("player", "Player name", false)
.withArgument("pwd", "New password", false) .withArgument("pwd", "New password", false)
.permissions(OP_ONLY, AdminPermission.CHANGE_PASSWORD) .permission(AdminPermission.CHANGE_PASSWORD)
.executableCommand(initializer.newInstance(ChangePasswordAdminCommand.class)) .executableCommand(initializer.newInstance(ChangePasswordAdminCommand.class))
.build(); .build();
@ -127,7 +127,7 @@ public class CommandInitializer {
.description("Player's last login") .description("Player's last login")
.detailedDescription("View the date of the specified players last login.") .detailedDescription("View the date of the specified players last login.")
.withArgument("player", "Player name", true) .withArgument("player", "Player name", true)
.permissions(OP_ONLY, AdminPermission.LAST_LOGIN) .permission(AdminPermission.LAST_LOGIN)
.executableCommand(initializer.newInstance(LastLoginCommand.class)) .executableCommand(initializer.newInstance(LastLoginCommand.class))
.build(); .build();
@ -138,7 +138,7 @@ public class CommandInitializer {
.description("Display player accounts") .description("Display player accounts")
.detailedDescription("Display all accounts of a player by his player name or IP.") .detailedDescription("Display all accounts of a player by his player name or IP.")
.withArgument("player", "Player name or IP", true) .withArgument("player", "Player name or IP", true)
.permissions(OP_ONLY, AdminPermission.ACCOUNTS) .permission(AdminPermission.ACCOUNTS)
.executableCommand(initializer.newInstance(AccountsCommand.class)) .executableCommand(initializer.newInstance(AccountsCommand.class))
.build(); .build();
@ -149,7 +149,7 @@ public class CommandInitializer {
.description("Display player's email") .description("Display player's email")
.detailedDescription("Display the email address of the specified player if set.") .detailedDescription("Display the email address of the specified player if set.")
.withArgument("player", "Player name", true) .withArgument("player", "Player name", true)
.permissions(OP_ONLY, AdminPermission.GET_EMAIL) .permission(AdminPermission.GET_EMAIL)
.executableCommand(initializer.newInstance(GetEmailCommand.class)) .executableCommand(initializer.newInstance(GetEmailCommand.class))
.build(); .build();
@ -161,7 +161,7 @@ public class CommandInitializer {
.detailedDescription("Change the email address of the specified player.") .detailedDescription("Change the email address of the specified player.")
.withArgument("player", "Player name", false) .withArgument("player", "Player name", false)
.withArgument("email", "Player email", false) .withArgument("email", "Player email", false)
.permissions(OP_ONLY, AdminPermission.CHANGE_EMAIL) .permission(AdminPermission.CHANGE_EMAIL)
.executableCommand(initializer.newInstance(SetEmailCommand.class)) .executableCommand(initializer.newInstance(SetEmailCommand.class))
.build(); .build();
@ -172,7 +172,7 @@ public class CommandInitializer {
.description("Get player's IP") .description("Get player's IP")
.detailedDescription("Get the IP address of the specified online player.") .detailedDescription("Get the IP address of the specified online player.")
.withArgument("player", "Player name", false) .withArgument("player", "Player name", false)
.permissions(OP_ONLY, AdminPermission.GET_IP) .permission(AdminPermission.GET_IP)
.executableCommand(initializer.newInstance(GetIpCommand.class)) .executableCommand(initializer.newInstance(GetIpCommand.class))
.build(); .build();
@ -182,7 +182,7 @@ public class CommandInitializer {
.labels("spawn", "home") .labels("spawn", "home")
.description("Teleport to spawn") .description("Teleport to spawn")
.detailedDescription("Teleport to the spawn.") .detailedDescription("Teleport to the spawn.")
.permissions(OP_ONLY, AdminPermission.SPAWN) .permission(AdminPermission.SPAWN)
.executableCommand(initializer.newInstance(SpawnCommand.class)) .executableCommand(initializer.newInstance(SpawnCommand.class))
.build(); .build();
@ -192,7 +192,7 @@ public class CommandInitializer {
.labels("setspawn", "chgspawn") .labels("setspawn", "chgspawn")
.description("Change the spawn") .description("Change the spawn")
.detailedDescription("Change the player's spawn to your current position.") .detailedDescription("Change the player's spawn to your current position.")
.permissions(OP_ONLY, AdminPermission.SET_SPAWN) .permission(AdminPermission.SET_SPAWN)
.executableCommand(initializer.newInstance(SetSpawnCommand.class)) .executableCommand(initializer.newInstance(SetSpawnCommand.class))
.build(); .build();
@ -202,7 +202,7 @@ public class CommandInitializer {
.labels("firstspawn", "firsthome") .labels("firstspawn", "firsthome")
.description("Teleport to first spawn") .description("Teleport to first spawn")
.detailedDescription("Teleport to the first spawn.") .detailedDescription("Teleport to the first spawn.")
.permissions(OP_ONLY, AdminPermission.FIRST_SPAWN) .permission(AdminPermission.FIRST_SPAWN)
.executableCommand(initializer.newInstance(FirstSpawnCommand.class)) .executableCommand(initializer.newInstance(FirstSpawnCommand.class))
.build(); .build();
@ -212,7 +212,7 @@ public class CommandInitializer {
.labels("setfirstspawn", "chgfirstspawn") .labels("setfirstspawn", "chgfirstspawn")
.description("Change the first spawn") .description("Change the first spawn")
.detailedDescription("Change the first player's spawn to your current position.") .detailedDescription("Change the first player's spawn to your current position.")
.permissions(OP_ONLY, AdminPermission.SET_FIRST_SPAWN) .permission(AdminPermission.SET_FIRST_SPAWN)
.executableCommand(initializer.newInstance(SetFirstSpawnCommand.class)) .executableCommand(initializer.newInstance(SetFirstSpawnCommand.class))
.build(); .build();
@ -223,7 +223,7 @@ public class CommandInitializer {
.description("Purge old data") .description("Purge old data")
.detailedDescription("Purge old AuthMeReloaded data longer than the specified amount of days ago.") .detailedDescription("Purge old AuthMeReloaded data longer than the specified amount of days ago.")
.withArgument("days", "Number of days", false) .withArgument("days", "Number of days", false)
.permissions(OP_ONLY, AdminPermission.PURGE) .permission(AdminPermission.PURGE)
.executableCommand(initializer.newInstance(PurgeCommand.class)) .executableCommand(initializer.newInstance(PurgeCommand.class))
.build(); .build();
@ -235,7 +235,7 @@ public class CommandInitializer {
.description("Purge player's last position") .description("Purge player's last position")
.detailedDescription("Purge the last know position of the specified player or all of them.") .detailedDescription("Purge the last know position of the specified player or all of them.")
.withArgument("player/*", "Player name or * for all players", false) .withArgument("player/*", "Player name or * for all players", false)
.permissions(OP_ONLY, AdminPermission.PURGE_LAST_POSITION) .permission(AdminPermission.PURGE_LAST_POSITION)
.executableCommand(initializer.newInstance(PurgeLastPositionCommand.class)) .executableCommand(initializer.newInstance(PurgeLastPositionCommand.class))
.build(); .build();
@ -245,7 +245,7 @@ public class CommandInitializer {
.labels("purgebannedplayers", "purgebannedplayer", "deletebannedplayers", "deletebannedplayer") .labels("purgebannedplayers", "purgebannedplayer", "deletebannedplayers", "deletebannedplayer")
.description("Purge banned players data") .description("Purge banned players data")
.detailedDescription("Purge all AuthMeReloaded data for banned players.") .detailedDescription("Purge all AuthMeReloaded data for banned players.")
.permissions(OP_ONLY, AdminPermission.PURGE_BANNED_PLAYERS) .permission(AdminPermission.PURGE_BANNED_PLAYERS)
.executableCommand(initializer.newInstance(PurgeBannedPlayersCommand.class)) .executableCommand(initializer.newInstance(PurgeBannedPlayersCommand.class))
.build(); .build();
@ -256,7 +256,7 @@ public class CommandInitializer {
.description("Switch AntiBot mode") .description("Switch AntiBot mode")
.detailedDescription("Switch or toggle the AntiBot mode to the specified state.") .detailedDescription("Switch or toggle the AntiBot mode to the specified state.")
.withArgument("mode", "ON / OFF", true) .withArgument("mode", "ON / OFF", true)
.permissions(OP_ONLY, AdminPermission.SWITCH_ANTIBOT) .permission(AdminPermission.SWITCH_ANTIBOT)
.executableCommand(initializer.newInstance(SwitchAntiBotCommand.class)) .executableCommand(initializer.newInstance(SwitchAntiBotCommand.class))
.build(); .build();
@ -266,7 +266,7 @@ public class CommandInitializer {
.labels("reload", "rld") .labels("reload", "rld")
.description("Reload plugin") .description("Reload plugin")
.detailedDescription("Reload the AuthMeReloaded plugin.") .detailedDescription("Reload the AuthMeReloaded plugin.")
.permissions(OP_ONLY, AdminPermission.RELOAD) .permission(AdminPermission.RELOAD)
.executableCommand(initializer.newInstance(ReloadCommand.class)) .executableCommand(initializer.newInstance(ReloadCommand.class))
.build(); .build();
@ -287,7 +287,7 @@ public class CommandInitializer {
.detailedDescription("Converter command for AuthMeReloaded.") .detailedDescription("Converter command for AuthMeReloaded.")
.withArgument("job", "Conversion job: xauth / crazylogin / rakamak / " + .withArgument("job", "Conversion job: xauth / crazylogin / rakamak / " +
"royalauth / vauth / sqlitetosql", false) "royalauth / vauth / sqlitetosql", false)
.permissions(OP_ONLY, AdminPermission.CONVERTER) .permission(AdminPermission.CONVERTER)
.executableCommand(initializer.newInstance(ConverterCommand.class)) .executableCommand(initializer.newInstance(ConverterCommand.class))
.build(); .build();
@ -298,7 +298,7 @@ public class CommandInitializer {
.description("Login command") .description("Login command")
.detailedDescription("Command to log in using AuthMeReloaded.") .detailedDescription("Command to log in using AuthMeReloaded.")
.withArgument("password", "Login password", false) .withArgument("password", "Login password", false)
.permissions(ALLOWED, PlayerPermission.LOGIN) .permission(PlayerPermission.LOGIN)
.executableCommand(initializer.newInstance(LoginCommand.class)) .executableCommand(initializer.newInstance(LoginCommand.class))
.build(); .build();
@ -308,7 +308,7 @@ public class CommandInitializer {
.labels("logout") .labels("logout")
.description("Logout command") .description("Logout command")
.detailedDescription("Command to logout using AuthMeReloaded.") .detailedDescription("Command to logout using AuthMeReloaded.")
.permissions(ALLOWED, PlayerPermission.LOGOUT) .permission(PlayerPermission.LOGOUT)
.executableCommand(initializer.newInstance(LogoutCommand.class)) .executableCommand(initializer.newInstance(LogoutCommand.class))
.build(); .build();
@ -320,7 +320,7 @@ public class CommandInitializer {
.detailedDescription("Command to register using AuthMeReloaded.") .detailedDescription("Command to register using AuthMeReloaded.")
.withArgument("password", "Password", true) .withArgument("password", "Password", true)
.withArgument("verifyPassword", "Verify password", true) .withArgument("verifyPassword", "Verify password", true)
.permissions(ALLOWED, PlayerPermission.REGISTER) .permission(PlayerPermission.REGISTER)
.executableCommand(initializer.newInstance(RegisterCommand.class)) .executableCommand(initializer.newInstance(RegisterCommand.class))
.build(); .build();
@ -331,7 +331,7 @@ public class CommandInitializer {
.description("Unregistration Command") .description("Unregistration Command")
.detailedDescription("Command to unregister using AuthMeReloaded.") .detailedDescription("Command to unregister using AuthMeReloaded.")
.withArgument("password", "Password", false) .withArgument("password", "Password", false)
.permissions(ALLOWED, PlayerPermission.UNREGISTER) .permission(PlayerPermission.UNREGISTER)
.executableCommand(initializer.newInstance(UnregisterCommand.class)) .executableCommand(initializer.newInstance(UnregisterCommand.class))
.build(); .build();
@ -343,7 +343,7 @@ public class CommandInitializer {
.detailedDescription("Command to change your password using AuthMeReloaded.") .detailedDescription("Command to change your password using AuthMeReloaded.")
.withArgument("oldPassword", "Old Password", false) .withArgument("oldPassword", "Old Password", false)
.withArgument("newPassword", "New Password.", false) .withArgument("newPassword", "New Password.", false)
.permissions(ALLOWED, PlayerPermission.CHANGE_PASSWORD) .permission(PlayerPermission.CHANGE_PASSWORD)
.executableCommand(initializer.newInstance(ChangePasswordCommand.class)) .executableCommand(initializer.newInstance(ChangePasswordCommand.class))
.build(); .build();
@ -364,7 +364,7 @@ public class CommandInitializer {
.detailedDescription("Add a new email address to your account.") .detailedDescription("Add a new email address to your account.")
.withArgument("email", "Email address", false) .withArgument("email", "Email address", false)
.withArgument("verifyEmail", "Email address verification", false) .withArgument("verifyEmail", "Email address verification", false)
.permissions(ALLOWED, PlayerPermission.ADD_EMAIL) .permission(PlayerPermission.ADD_EMAIL)
.executableCommand(initializer.newInstance(AddEmailCommand.class)) .executableCommand(initializer.newInstance(AddEmailCommand.class))
.build(); .build();
@ -376,7 +376,7 @@ public class CommandInitializer {
.detailedDescription("Change an email address of your account.") .detailedDescription("Change an email address of your account.")
.withArgument("oldEmail", "Old email address", false) .withArgument("oldEmail", "Old email address", false)
.withArgument("newEmail", "New email address", false) .withArgument("newEmail", "New email address", false)
.permissions(ALLOWED, PlayerPermission.CHANGE_EMAIL) .permission(PlayerPermission.CHANGE_EMAIL)
.executableCommand(initializer.newInstance(ChangeEmailCommand.class)) .executableCommand(initializer.newInstance(ChangeEmailCommand.class))
.build(); .build();
@ -388,7 +388,7 @@ public class CommandInitializer {
.detailedDescription("Recover your account using an Email address by sending a mail containing " + .detailedDescription("Recover your account using an Email address by sending a mail containing " +
"a new password.") "a new password.")
.withArgument("email", "Email address", false) .withArgument("email", "Email address", false)
.permissions(ALLOWED, PlayerPermission.RECOVER_EMAIL) .permission(PlayerPermission.RECOVER_EMAIL)
.executableCommand(initializer.newInstance(RecoverEmailCommand.class)) .executableCommand(initializer.newInstance(RecoverEmailCommand.class))
.build(); .build();
@ -399,7 +399,7 @@ public class CommandInitializer {
.description("Captcha Command") .description("Captcha Command")
.detailedDescription("Captcha command for AuthMeReloaded.") .detailedDescription("Captcha command for AuthMeReloaded.")
.withArgument("captcha", "The Captcha", false) .withArgument("captcha", "The Captcha", false)
.permissions(ALLOWED, PlayerPermission.CAPTCHA) .permission(PlayerPermission.CAPTCHA)
.executableCommand(initializer.newInstance(CaptchaCommand.class)) .executableCommand(initializer.newInstance(CaptchaCommand.class))
.build(); .build();

View File

@ -1,52 +0,0 @@
package fr.xephi.authme.command;
import fr.xephi.authme.permission.DefaultPermission;
import fr.xephi.authme.permission.PermissionNode;
import java.util.List;
/**
*/
public class CommandPermissions {
/**
* Defines the permission nodes required to have permission to execute this command.
*/
private List<PermissionNode> permissionNodes;
/**
* Defines the default permission if the permission nodes couldn't be used.
*/
private DefaultPermission defaultPermission;
/**
* Constructor.
*
* @param permissionNodes The permission nodes required to execute a command.
* @param defaultPermission The default permission if the permission nodes couldn't be used.
*/
public CommandPermissions(List<PermissionNode> permissionNodes, DefaultPermission defaultPermission) {
this.permissionNodes = permissionNodes;
this.defaultPermission = defaultPermission;
}
/**
* Get the permission nodes required to execute this command.
*
* @return The permission nodes required to execute this command.
*/
public List<PermissionNode> getPermissionNodes() {
return this.permissionNodes;
}
/**
* Get the default permission if the permission nodes couldn't be used.
*
* @return The default permission.
*/
public DefaultPermission getDefaultPermission() {
return this.defaultPermission;
}
}

View File

@ -4,7 +4,6 @@ import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import fr.xephi.authme.command.CommandArgumentDescription; import fr.xephi.authme.command.CommandArgumentDescription;
import fr.xephi.authme.command.CommandDescription; import fr.xephi.authme.command.CommandDescription;
import fr.xephi.authme.command.CommandPermissions;
import fr.xephi.authme.command.CommandUtils; import fr.xephi.authme.command.CommandUtils;
import fr.xephi.authme.command.FoundCommandResult; import fr.xephi.authme.command.FoundCommandResult;
import fr.xephi.authme.initialization.SettingsDependent; import fr.xephi.authme.initialization.SettingsDependent;
@ -13,7 +12,6 @@ import fr.xephi.authme.permission.PermissionNode;
import fr.xephi.authme.permission.PermissionsManager; import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.settings.NewSetting; import fr.xephi.authme.settings.NewSetting;
import fr.xephi.authme.settings.properties.PluginSettings; import fr.xephi.authme.settings.properties.PluginSettings;
import fr.xephi.authme.util.CollectionUtils;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -139,22 +137,19 @@ public class HelpProvider implements SettingsDependent {
private static void printPermissions(CommandDescription command, CommandSender sender, private static void printPermissions(CommandDescription command, CommandSender sender,
PermissionsManager permissionsManager, List<String> lines) { PermissionsManager permissionsManager, List<String> lines) {
CommandPermissions permissions = command.getCommandPermissions(); PermissionNode permission = command.getPermission();
// TODO ljacqu 20151224: Isn't it possible to have a default permission but no permission nodes? if (permission == null) {
if (permissions == null || CollectionUtils.isEmpty(permissions.getPermissionNodes())) {
return; return;
} }
lines.add(ChatColor.GOLD + "Permissions:"); lines.add(ChatColor.GOLD + "Permissions:");
for (PermissionNode node : permissions.getPermissionNodes()) { boolean hasPermission = permissionsManager.hasPermission(sender, permission);
boolean hasPermission = permissionsManager.hasPermission(sender, node);
final String nodePermsString = "" + ChatColor.GRAY + ChatColor.ITALIC final String nodePermsString = "" + ChatColor.GRAY + ChatColor.ITALIC
+ (hasPermission ? " (You have permission)" : " (No permission)"); + (hasPermission ? " (You have permission)" : " (No permission)");
lines.add(" " + ChatColor.YELLOW + ChatColor.ITALIC + node.getNode() + nodePermsString); lines.add(" " + ChatColor.YELLOW + ChatColor.ITALIC + permission.getNode() + nodePermsString);
}
// Addendum to the line to specify whether the sender has permission or not when default is OP_ONLY // Addendum to the line to specify whether the sender has permission or not when default is OP_ONLY
final DefaultPermission defaultPermission = permissions.getDefaultPermission(); final DefaultPermission defaultPermission = permission.getDefaultPermission();
String addendum = ""; String addendum = "";
if (DefaultPermission.OP_ONLY.equals(defaultPermission)) { if (DefaultPermission.OP_ONLY.equals(defaultPermission)) {
addendum = PermissionsManager.evaluateDefaultPermission(defaultPermission, sender) addendum = PermissionsManager.evaluateDefaultPermission(defaultPermission, sender)

View File

@ -8,115 +8,121 @@ public enum AdminPermission implements PermissionNode {
/** /**
* Administrator command to register a new user. * Administrator command to register a new user.
*/ */
REGISTER("authme.admin.register"), REGISTER("authme.admin.register", DefaultPermission.OP_ONLY),
/** /**
* Administrator command to unregister an existing user. * Administrator command to unregister an existing user.
*/ */
UNREGISTER("authme.admin.unregister"), UNREGISTER("authme.admin.unregister", DefaultPermission.OP_ONLY),
/** /**
* Administrator command to force-login an existing user. * Administrator command to force-login an existing user.
*/ */
FORCE_LOGIN("authme.admin.forcelogin"), FORCE_LOGIN("authme.admin.forcelogin", DefaultPermission.OP_ONLY),
/** /**
* Administrator command to change the password of a user. * Administrator command to change the password of a user.
*/ */
CHANGE_PASSWORD("authme.admin.changepassword"), CHANGE_PASSWORD("authme.admin.changepassword", DefaultPermission.OP_ONLY),
/** /**
* Administrator command to see the last login date and time of a user. * Administrator command to see the last login date and time of a user.
*/ */
LAST_LOGIN("authme.admin.lastlogin"), LAST_LOGIN("authme.admin.lastlogin", DefaultPermission.OP_ONLY),
/** /**
* Administrator command to see all accounts associated with a user. * Administrator command to see all accounts associated with a user.
*/ */
ACCOUNTS("authme.admin.accounts"), ACCOUNTS("authme.admin.accounts", DefaultPermission.OP_ONLY),
/** /**
* Administrator command to get the email address of a user, if set. * Administrator command to get the email address of a user, if set.
*/ */
GET_EMAIL("authme.admin.getemail"), GET_EMAIL("authme.admin.getemail", DefaultPermission.OP_ONLY),
/** /**
* Administrator command to set or change the email address of a user. * Administrator command to set or change the email address of a user.
*/ */
CHANGE_EMAIL("authme.admin.changemail"), CHANGE_EMAIL("authme.admin.changemail", DefaultPermission.OP_ONLY),
/** /**
* Administrator command to get the last known IP of a user. * Administrator command to get the last known IP of a user.
*/ */
GET_IP("authme.admin.getip"), GET_IP("authme.admin.getip", DefaultPermission.OP_ONLY),
/** /**
* Administrator command to teleport to the AuthMe spawn. * Administrator command to teleport to the AuthMe spawn.
*/ */
SPAWN("authme.admin.spawn"), SPAWN("authme.admin.spawn", DefaultPermission.OP_ONLY),
/** /**
* Administrator command to set the AuthMe spawn. * Administrator command to set the AuthMe spawn.
*/ */
SET_SPAWN("authme.admin.setspawn"), SET_SPAWN("authme.admin.setspawn", DefaultPermission.OP_ONLY),
/** /**
* Administrator command to teleport to the first AuthMe spawn. * Administrator command to teleport to the first AuthMe spawn.
*/ */
FIRST_SPAWN("authme.admin.firstspawn"), FIRST_SPAWN("authme.admin.firstspawn", DefaultPermission.OP_ONLY),
/** /**
* Administrator command to set the first AuthMe spawn. * Administrator command to set the first AuthMe spawn.
*/ */
SET_FIRST_SPAWN("authme.admin.setfirstspawn"), SET_FIRST_SPAWN("authme.admin.setfirstspawn", DefaultPermission.OP_ONLY),
/** /**
* Administrator command to purge old user data. * Administrator command to purge old user data.
*/ */
PURGE("authme.admin.purge"), PURGE("authme.admin.purge", DefaultPermission.OP_ONLY),
/** /**
* Administrator command to purge the last position of a user. * Administrator command to purge the last position of a user.
*/ */
PURGE_LAST_POSITION("authme.admin.purgelastpos"), PURGE_LAST_POSITION("authme.admin.purgelastpos", DefaultPermission.OP_ONLY),
/** /**
* Administrator command to purge all data associated with banned players. * Administrator command to purge all data associated with banned players.
*/ */
PURGE_BANNED_PLAYERS("authme.admin.purgebannedplayers"), PURGE_BANNED_PLAYERS("authme.admin.purgebannedplayers", DefaultPermission.OP_ONLY),
/** /**
* Administrator command to toggle the AntiBot protection status. * Administrator command to toggle the AntiBot protection status.
*/ */
SWITCH_ANTIBOT("authme.admin.switchantibot"), SWITCH_ANTIBOT("authme.admin.switchantibot", DefaultPermission.OP_ONLY),
/** /**
* Administrator command to convert old or other data to AuthMe data. * Administrator command to convert old or other data to AuthMe data.
*/ */
CONVERTER("authme.admin.converter"), CONVERTER("authme.admin.converter", DefaultPermission.OP_ONLY),
/** /**
* Administrator command to reload the plugin configuration. * Administrator command to reload the plugin configuration.
*/ */
RELOAD("authme.admin.reload"), RELOAD("authme.admin.reload", DefaultPermission.OP_ONLY),
/** /**
* Permission to see the other accounts of the players that log in. * Permission to see the other accounts of the players that log in.
*/ */
SEE_OTHER_ACCOUNTS("authme.admin.seeotheraccounts"); SEE_OTHER_ACCOUNTS("authme.admin.seeotheraccounts", DefaultPermission.OP_ONLY);
/** /**
* The permission node. * The permission node.
*/ */
private String node; private String node;
/**
* The default permission level
*/
private DefaultPermission defaultPermission;
/** /**
* Constructor. * Constructor.
* *
* @param node Permission node. * @param node Permission node.
*/ */
AdminPermission(String node) { AdminPermission(String node, DefaultPermission defaultPermission) {
this.node = node; this.node = node;
this.defaultPermission = defaultPermission;
} }
@Override @Override
@ -124,4 +130,8 @@ public enum AdminPermission implements PermissionNode {
return node; return node;
} }
@Override
public DefaultPermission getDefaultPermission() {
return defaultPermission;
}
} }

View File

@ -12,4 +12,10 @@ public interface PermissionNode {
*/ */
String getNode(); String getNode();
/**
* Return the default permission for this node, e.g. "OP_ONLY"
*
* @return The default level of permission
*/
DefaultPermission getDefaultPermission();
} }

View File

@ -285,15 +285,14 @@ public class PermissionsManager implements PermissionsService {
} }
public boolean hasPermission(CommandSender sender, CommandDescription command) { public boolean hasPermission(CommandSender sender, CommandDescription command) {
if (command.getCommandPermissions() == null if (command.getPermission() == null) {
|| CollectionUtils.isEmpty(command.getCommandPermissions().getPermissionNodes())) {
return true; return true;
} }
DefaultPermission defaultPermission = command.getCommandPermissions().getDefaultPermission(); DefaultPermission defaultPermission = command.getPermission().getDefaultPermission();
boolean def = evaluateDefaultPermission(defaultPermission, sender); boolean def = evaluateDefaultPermission(defaultPermission, sender);
return (sender instanceof Player) return (sender instanceof Player)
? hasPermission((Player) sender, command.getCommandPermissions().getPermissionNodes(), def) ? hasPermission((Player) sender, command.getPermission().getNode(), def)
: def; : def;
} }

View File

@ -8,70 +8,76 @@ public enum PlayerPermission implements PermissionNode {
/** /**
* Command permission to login. * Command permission to login.
*/ */
LOGIN("authme.player.login"), LOGIN("authme.player.login", DefaultPermission.ALLOWED),
/** /**
* Command permission to logout. * Command permission to logout.
*/ */
LOGOUT("authme.player.logout"), LOGOUT("authme.player.logout", DefaultPermission.ALLOWED),
/** /**
* Command permission to register. * Command permission to register.
*/ */
REGISTER("authme.player.register"), REGISTER("authme.player.register", DefaultPermission.ALLOWED),
/** /**
* Command permission to unregister. * Command permission to unregister.
*/ */
UNREGISTER("authme.player.unregister"), UNREGISTER("authme.player.unregister", DefaultPermission.ALLOWED),
/** /**
* Command permission to change the password. * Command permission to change the password.
*/ */
CHANGE_PASSWORD("authme.player.changepassword"), CHANGE_PASSWORD("authme.player.changepassword", DefaultPermission.ALLOWED),
/** /**
* Command permission to add an email address. * Command permission to add an email address.
*/ */
ADD_EMAIL("authme.player.email.add"), ADD_EMAIL("authme.player.email.add", DefaultPermission.ALLOWED),
/** /**
* Command permission to change the email address. * Command permission to change the email address.
*/ */
CHANGE_EMAIL("authme.player.email.change"), CHANGE_EMAIL("authme.player.email.change", DefaultPermission.ALLOWED),
/** /**
* Command permission to recover an account using it's email address. * Command permission to recover an account using it's email address.
*/ */
RECOVER_EMAIL("authme.player.email.recover"), RECOVER_EMAIL("authme.player.email.recover", DefaultPermission.ALLOWED),
/** /**
* Command permission to use captcha. * Command permission to use captcha.
*/ */
CAPTCHA("authme.player.captcha"), CAPTCHA("authme.player.captcha", DefaultPermission.ALLOWED),
/** /**
* Permission for users a login can be forced to. * Permission for users a login can be forced to.
*/ */
CAN_LOGIN_BE_FORCED("authme.player.canbeforced"), CAN_LOGIN_BE_FORCED("authme.player.canbeforced", DefaultPermission.ALLOWED),
/** /**
* Permission to use to see own other accounts. * Permission to use to see own other accounts.
*/ */
SEE_OWN_ACCOUNTS("authme.player.seeownaccounts"); SEE_OWN_ACCOUNTS("authme.player.seeownaccounts", DefaultPermission.ALLOWED);
/** /**
* The permission node. * The permission node.
*/ */
private String node; private String node;
/**
* The default permission level
*/
private DefaultPermission defaultPermission;
/** /**
* Constructor. * Constructor.
* *
* @param node Permission node. * @param node Permission node.
*/ */
PlayerPermission(String node) { PlayerPermission(String node, DefaultPermission defaultPermission) {
this.node = node; this.node = node;
this.defaultPermission = defaultPermission;
} }
@Override @Override
@ -79,4 +85,9 @@ public enum PlayerPermission implements PermissionNode {
return node; return node;
} }
@Override
public DefaultPermission getDefaultPermission() {
return defaultPermission;
}
} }

View File

@ -9,39 +9,50 @@ public enum PlayerStatePermission implements PermissionNode {
/** /**
* Permission node to bypass AntiBot protection. * Permission node to bypass AntiBot protection.
*/ */
BYPASS_ANTIBOT("authme.bypassantibot"), BYPASS_ANTIBOT("authme.bypassantibot", DefaultPermission.OP_ONLY),
/** /**
* Permission for users to bypass force-survival mode. * Permission for users to bypass force-survival mode.
*/ */
BYPASS_FORCE_SURVIVAL("authme.bypassforcesurvival"), BYPASS_FORCE_SURVIVAL("authme.bypassforcesurvival", DefaultPermission.OP_ONLY),
/** /**
* Permission node to identify VIP users. * Permission node to identify VIP users.
*/ */
IS_VIP("authme.vip"), IS_VIP("authme.vip", DefaultPermission.OP_ONLY),
/** /**
* Permission to be able to register multiple accounts. * Permission to be able to register multiple accounts.
*/ */
ALLOW_MULTIPLE_ACCOUNTS("authme.allowmultipleaccounts"); ALLOW_MULTIPLE_ACCOUNTS("authme.allowmultipleaccounts", DefaultPermission.OP_ONLY);
/** /**
* The permission node. * The permission node.
*/ */
private String node; private String node;
/**
* The default permission level
*/
private DefaultPermission defaultPermission;
/** /**
* Constructor. * Constructor.
* *
* @param node Permission node. * @param node Permission node.
*/ */
PlayerStatePermission(String node) { PlayerStatePermission(String node, DefaultPermission defaultPermission) {
this.node = node; this.node = node;
this.defaultPermission = defaultPermission;
} }
@Override @Override
public String getNode() { public String getNode() {
return node; return node;
} }
@Override
public DefaultPermission getDefaultPermission() {
return defaultPermission;
}
} }

View File

@ -256,21 +256,16 @@ public class CommandInitializerTest {
BiConsumer adminPermissionChecker = new BiConsumer() { BiConsumer adminPermissionChecker = new BiConsumer() {
@Override @Override
public void accept(CommandDescription command, int depth) { public void accept(CommandDescription command, int depth) {
CommandPermissions permissions = command.getCommandPermissions(); PermissionNode permission = command.getPermission();
if (permissions != null && OP_ONLY.equals(permissions.getDefaultPermission()) if (permission != null && OP_ONLY.equals(permission.getDefaultPermission())
&& !hasAdminNode(permissions)) { && !hasAdminNode(permission)) {
fail("The command with labels " + command.getLabels() + " has OP_ONLY default " fail("The command with labels " + command.getLabels() + " has OP_ONLY default "
+ "permission but no permission node on admin level"); + "permission but no permission node on admin level");
} }
} }
private boolean hasAdminNode(CommandPermissions permissions) { private boolean hasAdminNode(PermissionNode permission) {
for (PermissionNode node : permissions.getPermissionNodes()) { return permission instanceof AdminPermission;
if (node instanceof AdminPermission) {
return true;
}
}
return false;
} }
}; };

View File

@ -1,7 +1,7 @@
package fr.xephi.authme.command; package fr.xephi.authme.command;
import fr.xephi.authme.command.executable.HelpCommand; import fr.xephi.authme.command.executable.HelpCommand;
import fr.xephi.authme.permission.DefaultPermission; import fr.xephi.authme.permission.AdminPermission;
import fr.xephi.authme.permission.PermissionNode; import fr.xephi.authme.permission.PermissionNode;
import fr.xephi.authme.permission.PlayerPermission; import fr.xephi.authme.permission.PlayerPermission;
@ -43,8 +43,8 @@ public final class TestCommandsUtil {
.description("test").detailedDescription("Test.").withArgument("Query", "", false).build(); .description("test").detailedDescription("Test.").withArgument("Query", "", false).build();
// Register /unregister <player>, alias: /unreg // Register /unregister <player>, alias: /unreg
CommandDescription unregisterBase = createCommand(null, null, asList("unregister", "unreg"), CommandDescription unregisterBase = createCommand(AdminPermission.UNREGISTER, null,
newArgument("player", false)); asList("unregister", "unreg"), newArgument("player", false));
return newHashSet(authMeBase, emailBase, unregisterBase); return newHashSet(authMeBase, emailBase, unregisterBase);
} }
@ -84,18 +84,10 @@ public final class TestCommandsUtil {
/** Shortcut command to initialize a new test command. */ /** Shortcut command to initialize a new test command. */
private static CommandDescription createCommand(PermissionNode permission, CommandDescription parent, private static CommandDescription createCommand(PermissionNode permission, CommandDescription parent,
List<String> labels, CommandArgumentDescription... arguments) { List<String> labels, CommandArgumentDescription... arguments) {
PermissionNode[] notNullPermission;
if (permission == null) {
notNullPermission = new PermissionNode[0];
} else {
notNullPermission = new PermissionNode[1];
notNullPermission[0] = permission;
}
CommandDescription.CommandBuilder command = CommandDescription.builder() CommandDescription.CommandBuilder command = CommandDescription.builder()
.labels(labels) .labels(labels)
.parent(parent) .parent(parent)
.permissions(DefaultPermission.OP_ONLY, notNullPermission) .permission(permission)
.description(labels.get(0) + " cmd") .description(labels.get(0) + " cmd")
.detailedDescription("'" + labels.get(0) + "' test command") .detailedDescription("'" + labels.get(0) + "' test command")
.executableCommand(mock(ExecutableCommand.class)); .executableCommand(mock(ExecutableCommand.class));

View File

@ -4,8 +4,8 @@ import fr.xephi.authme.command.CommandDescription;
import fr.xephi.authme.command.FoundCommandResult; import fr.xephi.authme.command.FoundCommandResult;
import fr.xephi.authme.command.FoundResultStatus; import fr.xephi.authme.command.FoundResultStatus;
import fr.xephi.authme.command.TestCommandsUtil; import fr.xephi.authme.command.TestCommandsUtil;
import fr.xephi.authme.permission.AdminPermission;
import fr.xephi.authme.permission.PermissionsManager; import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.permission.PlayerPermission;
import fr.xephi.authme.settings.NewSetting; import fr.xephi.authme.settings.NewSetting;
import fr.xephi.authme.settings.properties.PluginSettings; import fr.xephi.authme.settings.properties.PluginSettings;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
@ -126,10 +126,10 @@ public class HelpProviderTest {
@Test @Test
public void shouldShowAndEvaluatePermissions() { public void shouldShowAndEvaluatePermissions() {
// given // given
CommandDescription command = getCommandWithLabel(commands, "authme", "login"); CommandDescription command = getCommandWithLabel(commands, "unregister");
FoundCommandResult result = newFoundResult(command, Collections.singletonList("authme")); FoundCommandResult result = newFoundResult(command, Collections.singletonList("unreg"));
given(sender.isOp()).willReturn(true); given(sender.isOp()).willReturn(true);
given(permissionsManager.hasPermission(sender, PlayerPermission.LOGIN)).willReturn(true); given(permissionsManager.hasPermission(sender, AdminPermission.UNREGISTER)).willReturn(true);
given(permissionsManager.hasPermission(sender, command)).willReturn(true); given(permissionsManager.hasPermission(sender, command)).willReturn(true);
// when // when
@ -139,7 +139,7 @@ public class HelpProviderTest {
assertThat(lines, hasSize(5)); assertThat(lines, hasSize(5));
assertThat(removeColors(lines.get(1)), containsString("Permissions:")); assertThat(removeColors(lines.get(1)), containsString("Permissions:"));
assertThat(removeColors(lines.get(2)), assertThat(removeColors(lines.get(2)),
containsString(PlayerPermission.LOGIN.getNode() + " (You have permission)")); containsString(AdminPermission.UNREGISTER.getNode() + " (You have permission)"));
assertThat(removeColors(lines.get(3)), containsString("Default: OP's only (You have permission)")); assertThat(removeColors(lines.get(3)), containsString("Default: OP's only (You have permission)"));
assertThat(removeColors(lines.get(4)), containsString("Result: You have permission")); assertThat(removeColors(lines.get(4)), containsString("Result: You have permission"));
} }
@ -147,10 +147,10 @@ public class HelpProviderTest {
@Test @Test
public void shouldShowAndEvaluateForbiddenPermissions() { public void shouldShowAndEvaluateForbiddenPermissions() {
// given // given
CommandDescription command = getCommandWithLabel(commands, "authme", "login"); CommandDescription command = getCommandWithLabel(commands, "unregister");
FoundCommandResult result = newFoundResult(command, Collections.singletonList("authme")); FoundCommandResult result = newFoundResult(command, Collections.singletonList("unregister"));
given(sender.isOp()).willReturn(false); given(sender.isOp()).willReturn(false);
given(permissionsManager.hasPermission(sender, PlayerPermission.LOGIN)).willReturn(false); given(permissionsManager.hasPermission(sender, AdminPermission.UNREGISTER)).willReturn(false);
given(permissionsManager.hasPermission(sender, command)).willReturn(false); given(permissionsManager.hasPermission(sender, command)).willReturn(false);
// when // when
@ -160,7 +160,7 @@ public class HelpProviderTest {
assertThat(lines, hasSize(5)); assertThat(lines, hasSize(5));
assertThat(removeColors(lines.get(1)), containsString("Permissions:")); assertThat(removeColors(lines.get(1)), containsString("Permissions:"));
assertThat(removeColors(lines.get(2)), assertThat(removeColors(lines.get(2)),
containsString(PlayerPermission.LOGIN.getNode() + " (No permission)")); containsString(AdminPermission.UNREGISTER.getNode() + " (No permission)"));
assertThat(removeColors(lines.get(3)), containsString("Default: OP's only (No permission)")); assertThat(removeColors(lines.get(3)), containsString("Default: OP's only (No permission)"));
assertThat(removeColors(lines.get(4)), containsString("Result: No permission")); assertThat(removeColors(lines.get(4)), containsString("Result: No permission"));
} }
@ -182,7 +182,7 @@ public class HelpProviderTest {
public void shouldNotShowAnythingForNullPermissionsOnCommand() { public void shouldNotShowAnythingForNullPermissionsOnCommand() {
// given // given
CommandDescription command = mock(CommandDescription.class); CommandDescription command = mock(CommandDescription.class);
given(command.getCommandPermissions()).willReturn(null); given(command.getPermission()).willReturn(null);
given(command.getLabels()).willReturn(Collections.singletonList("test")); given(command.getLabels()).willReturn(Collections.singletonList("test"));
FoundCommandResult result = newFoundResult(command, Collections.singletonList("test")); FoundCommandResult result = newFoundResult(command, Collections.singletonList("test"));

View File

@ -3,7 +3,6 @@ package tools.commands;
import fr.xephi.authme.command.CommandArgumentDescription; import fr.xephi.authme.command.CommandArgumentDescription;
import fr.xephi.authme.command.CommandDescription; import fr.xephi.authme.command.CommandDescription;
import fr.xephi.authme.command.CommandInitializer; import fr.xephi.authme.command.CommandInitializer;
import fr.xephi.authme.command.CommandPermissions;
import fr.xephi.authme.command.CommandUtils; import fr.xephi.authme.command.CommandUtils;
import fr.xephi.authme.command.ExecutableCommand; import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.initialization.AuthMeServiceInitializer; import fr.xephi.authme.initialization.AuthMeServiceInitializer;
@ -58,7 +57,7 @@ public class CommandPageCreater implements AutoToolTask {
.put("command", CommandUtils.constructCommandPath(command)) .put("command", CommandUtils.constructCommandPath(command))
.put("description", command.getDetailedDescription()) .put("description", command.getDetailedDescription())
.put("arguments", formatArguments(command.getArguments())) .put("arguments", formatArguments(command.getArguments()))
.put("permissions", formatPermissions(command.getCommandPermissions())); .put("permissions", formatPermissions(command.getPermission()));
commandTags.add(tags); commandTags.add(tags);
if (!command.getChildren().isEmpty()) { if (!command.getChildren().isEmpty()) {
@ -67,15 +66,12 @@ public class CommandPageCreater implements AutoToolTask {
} }
} }
private static String formatPermissions(CommandPermissions permissions) { private static String formatPermissions(PermissionNode permission) {
if (permissions == null) { if (permission == null) {
return ""; return "";
} else {
return permission.getNode();
} }
String result = "";
for (PermissionNode node : permissions.getPermissionNodes()) {
result += node.getNode() + " ";
}
return result.trim();
} }
private static String formatArguments(Iterable<CommandArgumentDescription> arguments) { private static String formatArguments(Iterable<CommandArgumentDescription> arguments) {