#761 Fix removal and restoration of primary permission group

- Improve how a player is being switched between permission groups (add new group before removing old one)
- Remove group handling logic from LimboCache: AuthGroupHandler is now solely responsible for changing the player's permission group
This commit is contained in:
ljacqu
2017-02-05 13:12:04 +01:00
parent 49f7e47645
commit 2b1a97e959
6 changed files with 78 additions and 84 deletions
@@ -13,6 +13,15 @@ import javax.inject.Inject;
/**
* Changes the permission group according to the auth status of the player and the configuration.
* <p>
* If this feature is enabled, the <i>primary permissions group</i> of a player is replaced until he has
* logged in. Some permission plugins have a notion of a primary group; for other permission plugins the
* first group is simply taken.
* <p>
* The groups that are used as replacement until the player logs in is configurable and depends on if
* the player is registered or not. Note that some (all?) permission systems require the group to actually
* exist for the replacement to take place. Furthermore, since some permission groups require that players
* be in at least one group, this will mean that the player is not removed from his primary group.
*/
public class AuthGroupHandler implements Reloadable {
@@ -36,11 +45,49 @@ public class AuthGroupHandler implements Reloadable {
*
* @param player the player
* @param groupType the group type
*
* @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 groupType) {
public void setGroup(Player player, AuthGroupType groupType) {
if (!useAuthGroups()) {
return;
}
String primaryGroup = "";
LimboPlayer limboPlayer = limboCache.getPlayerData(player.getName());
if (limboPlayer != null) {
primaryGroup = limboPlayer.getGroup();
}
switch (groupType) {
// Implementation note: some permission systems don't support players not being in any group,
// so add the new group before removing the old ones
case UNREGISTERED:
permissionsManager.addGroup(player, unregisteredGroup);
permissionsManager.removeGroups(player, registeredGroup, primaryGroup);
break;
case REGISTERED_UNAUTHENTICATED:
permissionsManager.addGroup(player, registeredGroup);
permissionsManager.removeGroups(player, unregisteredGroup, primaryGroup);
break;
case LOGGED_IN:
restoreGroup(player);
break;
default:
throw new IllegalStateException("Encountered unhandled auth group type '" + groupType + "'");
}
ConsoleLogger.debug(
() -> player.getName() + " changed to " + groupType + ": has groups " + permissionsManager.getGroups(player));
}
/**
* Returns whether the auth permissions group function should be used.
*
* @return true if should be used, false otherwise
*/
private boolean useAuthGroups() {
// Check whether the permissions check is enabled
if (!settings.getProperty(PluginSettings.ENABLE_PERMISSION_CHECK)) {
return false;
@@ -51,39 +98,21 @@ public class AuthGroupHandler implements Reloadable {
ConsoleLogger.warning("The current permissions system doesn't have group support, unable to set group!");
return false;
}
switch (groupType) {
case UNREGISTERED:
// Remove the other group, set the current group
permissionsManager.removeGroups(player, registeredGroup);
return permissionsManager.addGroup(player, unregisteredGroup);
case REGISTERED_UNAUTHENTICATED:
// Remove the other group, set the current group
permissionsManager.removeGroups(player, unregisteredGroup);
return permissionsManager.addGroup(player, registeredGroup);
case LOGGED_IN:
return restoreGroup(player);
default:
throw new IllegalStateException("Encountered unhandled auth group type '" + groupType + "'");
}
return true;
}
private boolean restoreGroup(Player player) {
// Get the player's LimboPlayer
/**
* Restores the player's original primary group (taken from LimboPlayer).
*
* @param player the player to process
*/
private void restoreGroup(Player player) {
LimboPlayer limbo = limboCache.getPlayerData(player.getName());
if (limbo == null) {
return false;
if (limbo != null) {
String primaryGroup = limbo.getGroup();
permissionsManager.addGroup(player, primaryGroup);
}
// Get the players group
String realGroup = limbo.getGroup();
// Remove the other group types groups, set the real group
permissionsManager.removeGroups(player, unregisteredGroup, registeredGroup);
return permissionsManager.addGroup(player, realGroup);
}
@Override