diff --git a/src/main/java/fr/xephi/authme/permission/AdminPermission.java b/src/main/java/fr/xephi/authme/permission/AdminPermission.java new file mode 100644 index 000000000..b3c4c0f9f --- /dev/null +++ b/src/main/java/fr/xephi/authme/permission/AdminPermission.java @@ -0,0 +1,55 @@ +package fr.xephi.authme.permission; + +/** + * AuthMe admin permissions. + */ +public enum AdminPermission implements PermissionsNode { + + REGISTER("authme.admin.register"), + + UNREGISTER("authme.admin.unregister"), + + FORCE_LOGIN("authme.admin.forcelogin"), + + CHANGE_PASSWORD("authme.admin.changepassword"), + + LAST_LOGIN("authme.admin.lastlogin"), + + ACCOUNTS("authme.admin.accounts"), + + GET_EMAIL("authme.admin.getemail"), + + CHANGE_EMAIL("authme.admin.chgemail"), + + GET_IP("authme.admin.getip"), + + SPAWN("authme.admin.spawn"), + + SET_SPAWN("authme.admin.setspawn"), + + FIRST_SPAWN("authme.admin.firstspawn"), + + SET_FIRST_SPAWN("authme.admin.setfirstspawn"), + + PURGE("authme.admin.purge"), + + PURGE_LAST_POSITION("authme.admin.purgelastpos"), + + PURGE_BANNED_PLAYERS("authme.admin.purgebannedplayers"), + + SWITCH_ANTIBOT("authme.admin.switchantibot"), + + RELOAD("authme.admin.reload"); + + private String node; + + @Override + public String getNode() { + return node; + } + + AdminPermission(String node) { + this.node = node; + } + +} diff --git a/src/main/java/fr/xephi/authme/permission/PermissionsManager.java b/src/main/java/fr/xephi/authme/permission/PermissionsManager.java index fc6e3014c..049425df0 100644 --- a/src/main/java/fr/xephi/authme/permission/PermissionsManager.java +++ b/src/main/java/fr/xephi/authme/permission/PermissionsManager.java @@ -325,6 +325,19 @@ public class PermissionsManager { return hasPermission(player, permsNode, player.isOp()); } + /** + * 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. + * + * @param player The player. + * @param permissionsNode The permissions node to verify. + * + * @return True if the player has the permission, false otherwise. + */ + public boolean hasPermission(Player player, PermissionsNode permissionsNode) { + return hasPermission(player, permissionsNode.getNode(), player.isOp()); + } + /** * Check if a player has permission. * diff --git a/src/main/java/fr/xephi/authme/permission/PermissionsNode.java b/src/main/java/fr/xephi/authme/permission/PermissionsNode.java new file mode 100644 index 000000000..1ccb16e6e --- /dev/null +++ b/src/main/java/fr/xephi/authme/permission/PermissionsNode.java @@ -0,0 +1,11 @@ +package fr.xephi.authme.permission; + +/** + * Common interface for AuthMe permission nodes. + */ +public interface PermissionsNode { + + /** Return the node of the permission, e.g. "authme.unregister". */ + String getNode(); + +} diff --git a/src/main/java/fr/xephi/authme/permission/UserPermission.java b/src/main/java/fr/xephi/authme/permission/UserPermission.java new file mode 100644 index 000000000..e77e63c83 --- /dev/null +++ b/src/main/java/fr/xephi/authme/permission/UserPermission.java @@ -0,0 +1,52 @@ +package fr.xephi.authme.permission; + +/** + * AuthMe user permission nodes. + */ +public enum UserPermission implements PermissionsNode { + + BYPASS_ANTIBOT("authme.bypassantibot"), + + IS_VIP("authme.vip"), + + LOGIN("authme.login"), + + LOGOUT("authme.logout"), + + REGISTER("authme.register"), + + UNREGISTER("authme.unregister"), + + CHANGE_PASSWORD("authme.changepassword"), + + ADD_EMAIL("authme.email.add"), + + CHANGE_EMAIL("authme.email.change"), + + RECOVER_EMAIL("authme.email.recover"), + + CAPTCHA("authme.captcha"), + + CONVERTER("authme.converter"), + + CAN_LOGIN_BE_FORCED("authme.canbeforced"), + + BYPASS_FORCE_SURVIVAL("authme.bypassforcesurvival"), + + ALLOW_MULTIPLE_ACCOUNTS("authme.allow2accounts"), + + SEE_OTHER_ACCOUNTS("authme.seeOtherAccounts"); + + private String node; + + @Override + public String getNode() { + return node; + } + + UserPermission(String node) { + this.node = node; + } + + +} diff --git a/src/test/java/fr/xephi/authme/permission/AdminPermissionTest.java b/src/test/java/fr/xephi/authme/permission/AdminPermissionTest.java new file mode 100644 index 000000000..14bf58b54 --- /dev/null +++ b/src/test/java/fr/xephi/authme/permission/AdminPermissionTest.java @@ -0,0 +1,42 @@ +package fr.xephi.authme.permission; + +import org.junit.Test; + +import java.util.HashSet; +import java.util.Set; + +import static org.junit.Assert.fail; + +/** + * Test for {@link AdminPermission}. + */ +public class AdminPermissionTest { + + @Test + public void shouldStartWithAuthMeAdminPrefix() { + // given + String requiredPrefix = "authme.admin."; + + // when/then + for (AdminPermission perm : AdminPermission.values()) { + if (!perm.getNode().startsWith(requiredPrefix)) { + fail("The permission '" + perm + "' does not start with the required prefix '" + requiredPrefix + "'"); + } + } + } + + @Test + public void shouldHaveUniqueNodes() { + // given + Set nodes = new HashSet<>(); + + // when/then + for (AdminPermission perm : AdminPermission.values()) { + if (nodes.contains(perm.getNode())) { + fail("More than one enum value defines the node '" + perm.getNode() + "'"); + } + nodes.add(perm.getNode()); + } + } + +} diff --git a/src/test/java/fr/xephi/authme/permission/UserPermissionTest.java b/src/test/java/fr/xephi/authme/permission/UserPermissionTest.java new file mode 100644 index 000000000..48d4bbed8 --- /dev/null +++ b/src/test/java/fr/xephi/authme/permission/UserPermissionTest.java @@ -0,0 +1,45 @@ +package fr.xephi.authme.permission; + +import org.junit.Test; + +import java.util.HashSet; +import java.util.Set; + +import static org.junit.Assert.fail; + +/** + * Test for {@link UserPermission}. + */ +public class UserPermissionTest { + + @Test + public void shouldStartWithRegularAuthMePrefix() { + // given + String requiredPrefix = "authme."; + String adminPrefix = "authme.admin"; + + // when/then + for (UserPermission perm : UserPermission.values()) { + if (!perm.getNode().startsWith(requiredPrefix)) { + fail("The permission '" + perm + "' does not start with the required prefix '" + requiredPrefix + "'"); + } else if (perm.getNode().startsWith(adminPrefix)) { + fail("The permission '" + perm + "' should not use a node with the admin-specific prefix '" + + adminPrefix + "'"); + } + } + } + + @Test + public void shouldHaveUniqueNodes() { + // given + Set nodes = new HashSet<>(); + + // when/then + for (UserPermission perm : UserPermission.values()) { + if (nodes.contains(perm.getNode())) { + fail("More than one enum value defines the node '" + perm.getNode() + "'"); + } + nodes.add(perm.getNode()); + } + } +}