Create enum classes for permission nodes

This commit is contained in:
ljacqu 2015-11-28 21:26:07 +01:00
parent a1a14aa760
commit e519906dc5
6 changed files with 218 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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.
*

View File

@ -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();
}

View File

@ -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;
}
}

View File

@ -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<String> 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());
}
}
}

View File

@ -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<String> 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());
}
}
}