#761 Improve permissions group support
- Drop one auth group type in favor of three: logged in, registered but not logged in, and unregistered - Move properties to same parent path
This commit is contained in:
@@ -5,14 +5,11 @@ import fr.xephi.authme.data.limbo.LimboCache;
|
||||
import fr.xephi.authme.data.limbo.LimboPlayer;
|
||||
import fr.xephi.authme.initialization.Reloadable;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.properties.HooksSettings;
|
||||
import fr.xephi.authme.settings.properties.PluginSettings;
|
||||
import fr.xephi.authme.settings.properties.SecuritySettings;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.inject.Inject;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Changes the permission group according to the auth status of the player and the configuration.
|
||||
@@ -28,7 +25,6 @@ public class AuthGroupHandler implements Reloadable {
|
||||
@Inject
|
||||
private LimboCache limboCache;
|
||||
|
||||
private String unloggedInGroup;
|
||||
private String unregisteredGroup;
|
||||
private String registeredGroup;
|
||||
|
||||
@@ -36,15 +32,15 @@ public class AuthGroupHandler implements Reloadable {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the group of a player, by its AuthMe group type.
|
||||
* Sets the group of a player by its authentication status.
|
||||
*
|
||||
* @param player The player.
|
||||
* @param group The group type.
|
||||
* @param player the player
|
||||
* @param groupType the group type
|
||||
*
|
||||
* @return True if succeeded, false otherwise. False is also returned if groups aren't supported
|
||||
* @return True upon success, false otherwise. False is also returned if groups aren't supported
|
||||
* with the current permissions system.
|
||||
*/
|
||||
public boolean setGroup(Player player, AuthGroupType group) {
|
||||
public boolean setGroup(Player player, AuthGroupType groupType) {
|
||||
// Check whether the permissions check is enabled
|
||||
if (!settings.getProperty(PluginSettings.ENABLE_PERMISSION_CHECK)) {
|
||||
return false;
|
||||
@@ -56,71 +52,45 @@ public class AuthGroupHandler implements Reloadable {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (group) {
|
||||
switch (groupType) {
|
||||
case UNREGISTERED:
|
||||
// Remove the other group type groups, set the current group
|
||||
permissionsManager.removeGroups(player, Arrays.asList(registeredGroup, unloggedInGroup));
|
||||
// Remove the other group, set the current group
|
||||
permissionsManager.removeGroups(player, registeredGroup);
|
||||
return permissionsManager.addGroup(player, unregisteredGroup);
|
||||
|
||||
case REGISTERED:
|
||||
// Remove the other group type groups, set the current group
|
||||
permissionsManager.removeGroups(player, Arrays.asList(unregisteredGroup, unloggedInGroup));
|
||||
case REGISTERED_UNAUTHENTICATED:
|
||||
// Remove the other group, set the current group
|
||||
permissionsManager.removeGroups(player, unregisteredGroup);
|
||||
return permissionsManager.addGroup(player, registeredGroup);
|
||||
|
||||
case NOT_LOGGED_IN:
|
||||
// Remove the other group type groups, set the current group
|
||||
permissionsManager.removeGroups(player, Arrays.asList(unregisteredGroup, registeredGroup));
|
||||
return permissionsManager.addGroup(player, unloggedInGroup);
|
||||
|
||||
case LOGGED_IN:
|
||||
// Get the player data
|
||||
LimboPlayer data = limboCache.getPlayerData(player.getName().toLowerCase());
|
||||
if (data == null) {
|
||||
return false;
|
||||
}
|
||||
return restoreGroup(player);
|
||||
|
||||
// Get the players group
|
||||
String realGroup = data.getGroup();
|
||||
|
||||
// Remove the other group types groups, set the real group
|
||||
permissionsManager.removeGroups(player,
|
||||
Arrays.asList(unregisteredGroup, registeredGroup, unloggedInGroup)
|
||||
);
|
||||
return permissionsManager.addGroup(player, realGroup);
|
||||
default:
|
||||
return false;
|
||||
throw new IllegalStateException("Encountered unhandled auth group type '" + groupType + "'");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO: This method requires better explanation.
|
||||
* <p>
|
||||
* Set the normal group of a player.
|
||||
*
|
||||
* @param player The player.
|
||||
* @param group The normal group.
|
||||
*
|
||||
* @return True on success, false on failure.
|
||||
*/
|
||||
public boolean addNormal(Player player, String group) {
|
||||
// Check whether the permissions check is enabled
|
||||
if (!settings.getProperty(PluginSettings.ENABLE_PERMISSION_CHECK)) {
|
||||
private boolean restoreGroup(Player player) {
|
||||
// Get the player's LimboPlayer
|
||||
LimboPlayer limbo = limboCache.getPlayerData(player.getName());
|
||||
if (limbo == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Remove old groups
|
||||
permissionsManager.removeGroups(player, Arrays.asList(unregisteredGroup, registeredGroup, unloggedInGroup));
|
||||
// Get the players group
|
||||
String realGroup = limbo.getGroup();
|
||||
|
||||
// Add the normal group, return the result
|
||||
return permissionsManager.addGroup(player, group);
|
||||
// Remove the other group types groups, set the real group
|
||||
permissionsManager.removeGroups(player, unregisteredGroup, registeredGroup);
|
||||
return permissionsManager.addGroup(player, realGroup);
|
||||
}
|
||||
|
||||
@Override
|
||||
@PostConstruct
|
||||
public void reload() {
|
||||
unloggedInGroup = settings.getProperty(SecuritySettings.UNLOGGEDIN_GROUP);
|
||||
unregisteredGroup = settings.getProperty(HooksSettings.UNREGISTERED_GROUP);
|
||||
registeredGroup = settings.getProperty(HooksSettings.REGISTERED_GROUP);
|
||||
unregisteredGroup = settings.getProperty(PluginSettings.UNREGISTERED_GROUP);
|
||||
registeredGroup = settings.getProperty(PluginSettings.REGISTERED_GROUP);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -8,11 +8,8 @@ public enum AuthGroupType {
|
||||
/** Player does not have an account. */
|
||||
UNREGISTERED,
|
||||
|
||||
/** Registered? */
|
||||
REGISTERED, // TODO #761: Remove this or the NOT_LOGGED_IN one
|
||||
|
||||
/** Player is registered and not logged in. */
|
||||
NOT_LOGGED_IN,
|
||||
/** Player is registered but not logged in. */
|
||||
REGISTERED_UNAUTHENTICATED,
|
||||
|
||||
/** Player is logged in. */
|
||||
LOGGED_IN
|
||||
|
||||
@@ -289,7 +289,7 @@ public class PermissionsManager implements Reloadable {
|
||||
* @return True if the player is in the specified group, false otherwise.
|
||||
* False is also returned if groups aren't supported by the used permissions system.
|
||||
*/
|
||||
public boolean inGroup(Player player, String groupName) {
|
||||
public boolean isInGroup(Player player, String groupName) {
|
||||
// If no permissions system is used, return false
|
||||
if (!isEnabled())
|
||||
return false;
|
||||
@@ -307,42 +307,12 @@ public class PermissionsManager implements Reloadable {
|
||||
* False is also returned if this feature isn't supported for the current permissions system.
|
||||
*/
|
||||
public boolean addGroup(Player player, String groupName) {
|
||||
if (StringUtils.isEmpty(groupName)) {
|
||||
if (!isEnabled() || StringUtils.isEmpty(groupName)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// If no permissions system is used, return false
|
||||
if (!isEnabled()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return handler.addToGroup(player, groupName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the permission groups of a player, if supported.
|
||||
*
|
||||
* @param player The player
|
||||
* @param groupNames The name of the groups to add.
|
||||
*
|
||||
* @return True if succeed, false otherwise.
|
||||
* False is also returned if this feature isn't supported for the current permissions system.
|
||||
*/
|
||||
public boolean addGroups(Player player, List<String> groupNames) {
|
||||
// If no permissions system is used, return false
|
||||
if (!isEnabled())
|
||||
return false;
|
||||
|
||||
// Add each group to the user
|
||||
boolean result = true;
|
||||
for (String groupName : groupNames)
|
||||
if (!addGroup(player, groupName))
|
||||
result = false;
|
||||
|
||||
// Return the result
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the permission group of a player, if supported.
|
||||
*
|
||||
@@ -352,8 +322,7 @@ public class PermissionsManager implements Reloadable {
|
||||
* @return True if succeed, false otherwise.
|
||||
* False is also returned if this feature isn't supported for the current permissions system.
|
||||
*/
|
||||
public boolean removeGroup(Player player, String groupName) {
|
||||
// If no permissions system is used, return false
|
||||
public boolean removeGroups(Player player, String groupName) {
|
||||
if (!isEnabled())
|
||||
return false;
|
||||
|
||||
@@ -369,16 +338,18 @@ public class PermissionsManager implements Reloadable {
|
||||
* @return True if succeed, false otherwise.
|
||||
* False is also returned if this feature isn't supported for the current permissions system.
|
||||
*/
|
||||
public boolean removeGroups(Player player, List<String> groupNames) {
|
||||
public boolean removeGroups(Player player, String... groupNames) {
|
||||
// If no permissions system is used, return false
|
||||
if (!isEnabled())
|
||||
return false;
|
||||
|
||||
// Add each group to the user
|
||||
boolean result = true;
|
||||
for (String groupName : groupNames)
|
||||
if (!removeGroup(player, groupName))
|
||||
for (String groupName : groupNames) {
|
||||
if (!handler.removeFromGroup(player, groupName)) {
|
||||
result = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Return the result
|
||||
return result;
|
||||
@@ -402,41 +373,6 @@ public class PermissionsManager implements Reloadable {
|
||||
return handler.setGroup(player, groupName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the permission groups of a player, if supported.
|
||||
* This clears the current groups of the player.
|
||||
*
|
||||
* @param player The player
|
||||
* @param groupNames The name of the groups to set.
|
||||
*
|
||||
* @return True if succeed, false otherwise.
|
||||
* False is also returned if this feature isn't supported for the current permissions system.
|
||||
*/
|
||||
public boolean setGroups(Player player, List<String> groupNames) {
|
||||
// If no permissions system is used or if there's no group supplied, return false
|
||||
if (!isEnabled() || groupNames.isEmpty())
|
||||
return false;
|
||||
|
||||
// Set the main group
|
||||
if (!setGroup(player, groupNames.get(0)))
|
||||
return false;
|
||||
|
||||
// Add the rest of the groups
|
||||
boolean result = true;
|
||||
for (int i = 1; i < groupNames.size(); i++) {
|
||||
// Get the group name
|
||||
String groupName = groupNames.get(i);
|
||||
|
||||
// Add this group
|
||||
if (!addGroup(player, groupName)) {
|
||||
result = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Return the result
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
@@ -456,6 +392,6 @@ public class PermissionsManager implements Reloadable {
|
||||
List<String> groupNames = getGroups(player);
|
||||
|
||||
// Remove each group
|
||||
return removeGroups(player, groupNames);
|
||||
return removeGroups(player, groupNames.toArray(new String[groupNames.size()]));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user