Store the entire player's list of groups instead of primary group
This commit is contained in:
+1
-1
@@ -72,7 +72,7 @@ class LimboPlayerViewer implements DebugSection {
|
||||
.sendEntry("Location", p -> formatLocation(p.getLocation()), l -> formatLocation(l.getLocation()))
|
||||
.sendEntry("Prim. group",
|
||||
p -> permissionsManager.hasGroupSupport() ? permissionsManager.getPrimaryGroup(p) : "N/A",
|
||||
LimboPlayer::getGroup);
|
||||
LimboPlayer::getGroups);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -9,6 +9,8 @@ import org.bukkit.entity.Player;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.inject.Inject;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* Changes the permission group according to the auth status of the player and the configuration.
|
||||
@@ -48,24 +50,28 @@ class AuthGroupHandler implements Reloadable {
|
||||
return;
|
||||
}
|
||||
|
||||
String primaryGroup = limbo == null ? "" : limbo.getGroup();
|
||||
Collection<String> previousGroups = limbo == null ? Collections.emptyList() : limbo.getGroups();
|
||||
|
||||
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);
|
||||
permissionsManager.removeGroup(player, registeredGroup);
|
||||
permissionsManager.removeGroups(player, previousGroups);
|
||||
break;
|
||||
|
||||
case REGISTERED_UNAUTHENTICATED:
|
||||
permissionsManager.addGroup(player, registeredGroup);
|
||||
permissionsManager.removeGroups(player, unregisteredGroup, primaryGroup);
|
||||
permissionsManager.removeGroup(player, unregisteredGroup);
|
||||
permissionsManager.removeGroups(player, previousGroups);
|
||||
|
||||
break;
|
||||
|
||||
case LOGGED_IN:
|
||||
permissionsManager.addGroup(player, primaryGroup);
|
||||
permissionsManager.removeGroups(player, unregisteredGroup, registeredGroup);
|
||||
permissionsManager.addGroups(player, previousGroups);
|
||||
permissionsManager.removeGroup(player, unregisteredGroup);
|
||||
permissionsManager.removeGroup(player, registeredGroup);
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
@@ -4,6 +4,9 @@ import fr.xephi.authme.task.MessageTask;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Represents a player which is not logged in and keeps track of certain states (like OP status, flying)
|
||||
* which may be revoked from the player until he has logged in or registered.
|
||||
@@ -15,17 +18,17 @@ public class LimboPlayer {
|
||||
|
||||
private final boolean canFly;
|
||||
private final boolean operator;
|
||||
private final String group;
|
||||
private Collection<String> groups;
|
||||
private final Location loc;
|
||||
private final float walkSpeed;
|
||||
private final float flySpeed;
|
||||
private BukkitTask timeoutTask = null;
|
||||
private MessageTask messageTask = null;
|
||||
|
||||
public LimboPlayer(Location loc, boolean operator, String group, boolean fly, float walkSpeed, float flySpeed) {
|
||||
public LimboPlayer(Location loc, boolean operator, Collection<String> groups, boolean fly, float walkSpeed, float flySpeed) {
|
||||
this.loc = loc;
|
||||
this.operator = operator;
|
||||
this.group = group;
|
||||
this.groups = groups;
|
||||
this.canFly = fly;
|
||||
this.walkSpeed = walkSpeed;
|
||||
this.flySpeed = flySpeed;
|
||||
@@ -50,12 +53,12 @@ public class LimboPlayer {
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the player's permissions group.
|
||||
* Return the player's permissions groups.
|
||||
*
|
||||
* @return The permissions group the player belongs to
|
||||
* @return The permissions groups the player belongs to
|
||||
*/
|
||||
public String getGroup() {
|
||||
return group;
|
||||
public Collection<String> getGroups() {
|
||||
return groups;
|
||||
}
|
||||
|
||||
public boolean isCanFly() {
|
||||
|
||||
@@ -9,6 +9,8 @@ import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* Helper class for the LimboService.
|
||||
@@ -38,11 +40,11 @@ class LimboServiceHelper {
|
||||
boolean flyEnabled = player.getAllowFlight();
|
||||
float walkSpeed = player.getWalkSpeed();
|
||||
float flySpeed = player.getFlySpeed();
|
||||
String playerGroup = permissionsManager.hasGroupSupport()
|
||||
? permissionsManager.getPrimaryGroup(player) : "";
|
||||
ConsoleLogger.debug("Player `{0}` has primary group `{1}`", player.getName(), playerGroup);
|
||||
Collection<String> playerGroups = permissionsManager.hasGroupSupport()
|
||||
? permissionsManager.getGroups(player) : Collections.emptyList();
|
||||
ConsoleLogger.debug("Player `{0}` has groups `{1}`", player.getName(), String.join(", ", playerGroups));
|
||||
|
||||
return new LimboPlayer(location, isOperator, playerGroup, flyEnabled, walkSpeed, flySpeed);
|
||||
return new LimboPlayer(location, isOperator, playerGroups, flyEnabled, walkSpeed, flySpeed);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -86,18 +88,10 @@ class LimboServiceHelper {
|
||||
boolean canFly = newLimbo.isCanFly() || oldLimbo.isCanFly();
|
||||
float flySpeed = Math.max(newLimbo.getFlySpeed(), oldLimbo.getFlySpeed());
|
||||
float walkSpeed = Math.max(newLimbo.getWalkSpeed(), oldLimbo.getWalkSpeed());
|
||||
String group = firstNotEmpty(newLimbo.getGroup(), oldLimbo.getGroup());
|
||||
Collection<String> groups = newLimbo.getGroups();
|
||||
Location location = firstNotNull(oldLimbo.getLocation(), newLimbo.getLocation());
|
||||
|
||||
return new LimboPlayer(location, isOperator, group, canFly, walkSpeed, flySpeed);
|
||||
}
|
||||
|
||||
private static String firstNotEmpty(String newGroup, String oldGroup) {
|
||||
ConsoleLogger.debug("Limbo merge: new and old perm groups are `{0}` and `{1}`", newGroup, oldGroup);
|
||||
if ("".equals(oldGroup)) {
|
||||
return newGroup;
|
||||
}
|
||||
return oldGroup;
|
||||
return new LimboPlayer(location, isOperator, groups, canFly, walkSpeed, flySpeed);
|
||||
}
|
||||
|
||||
private static Location firstNotNull(Location first, Location second) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package fr.xephi.authme.data.limbo.persistence;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
@@ -10,11 +11,15 @@ import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static fr.xephi.authme.data.limbo.persistence.LimboPlayerSerializer.CAN_FLY;
|
||||
import static fr.xephi.authme.data.limbo.persistence.LimboPlayerSerializer.FLY_SPEED;
|
||||
import static fr.xephi.authme.data.limbo.persistence.LimboPlayerSerializer.GROUP;
|
||||
import static fr.xephi.authme.data.limbo.persistence.LimboPlayerSerializer.GROUPS;
|
||||
import static fr.xephi.authme.data.limbo.persistence.LimboPlayerSerializer.IS_OP;
|
||||
import static fr.xephi.authme.data.limbo.persistence.LimboPlayerSerializer.LOCATION;
|
||||
import static fr.xephi.authme.data.limbo.persistence.LimboPlayerSerializer.LOC_PITCH;
|
||||
@@ -45,12 +50,13 @@ class LimboPlayerDeserializer implements JsonDeserializer<LimboPlayer> {
|
||||
|
||||
Location loc = deserializeLocation(jsonObject);
|
||||
boolean operator = getBoolean(jsonObject, IS_OP);
|
||||
String group = getString(jsonObject, GROUP);
|
||||
|
||||
Collection<String> groups = getStringList(jsonObject, GROUPS);
|
||||
boolean canFly = getBoolean(jsonObject, CAN_FLY);
|
||||
float walkSpeed = getFloat(jsonObject, WALK_SPEED, LimboPlayer.DEFAULT_WALK_SPEED);
|
||||
float flySpeed = getFloat(jsonObject, FLY_SPEED, LimboPlayer.DEFAULT_FLY_SPEED);
|
||||
|
||||
return new LimboPlayer(loc, operator, group, canFly, walkSpeed, flySpeed);
|
||||
return new LimboPlayer(loc, operator, groups, canFly, walkSpeed, flySpeed);
|
||||
}
|
||||
|
||||
private Location deserializeLocation(JsonObject jsonObject) {
|
||||
@@ -75,6 +81,19 @@ class LimboPlayerDeserializer implements JsonDeserializer<LimboPlayer> {
|
||||
return element != null ? element.getAsString() : "";
|
||||
}
|
||||
|
||||
private static List<String> getStringList(JsonObject jsonObject, String memberName) {
|
||||
JsonElement element = jsonObject.get(memberName);
|
||||
if (element == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
List<String> result = new ArrayList<>();
|
||||
JsonArray jsonArray = element.getAsJsonArray();
|
||||
for (JsonElement arrayElement : jsonArray) {
|
||||
result.add(arrayElement.getAsString());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static boolean getBoolean(JsonObject jsonObject, String memberName) {
|
||||
JsonElement element = jsonObject.get(memberName);
|
||||
return element != null && element.getAsBoolean();
|
||||
@@ -95,10 +114,11 @@ class LimboPlayerDeserializer implements JsonDeserializer<LimboPlayer> {
|
||||
/**
|
||||
* Gets a number from the given JsonElement safely.
|
||||
*
|
||||
* @param jsonElement the element to retrieve the number from
|
||||
* @param jsonElement the element to retrieve the number from
|
||||
* @param numberFunction the function to get the number from the element
|
||||
* @param defaultValue the value to return if the element is null or the number cannot be retrieved
|
||||
* @param <N> the number type
|
||||
* @param defaultValue the value to return if the element is null or the number cannot be retrieved
|
||||
* @param <N> the number type
|
||||
*
|
||||
* @return the number from the given JSON element, or the default value
|
||||
*/
|
||||
private static <N extends Number> N getNumberFromElement(JsonElement jsonElement,
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package fr.xephi.authme.data.limbo.persistence;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
@@ -8,6 +10,7 @@ import fr.xephi.authme.data.limbo.LimboPlayer;
|
||||
import org.bukkit.Location;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* Converts a LimboPlayer to a JsonElement.
|
||||
@@ -22,7 +25,7 @@ class LimboPlayerSerializer implements JsonSerializer<LimboPlayer> {
|
||||
static final String LOC_YAW = "yaw";
|
||||
static final String LOC_PITCH = "pitch";
|
||||
|
||||
static final String GROUP = "group";
|
||||
static final String GROUPS = "groups";
|
||||
static final String IS_OP = "operator";
|
||||
static final String CAN_FLY = "can-fly";
|
||||
static final String WALK_SPEED = "walk-speed";
|
||||
@@ -42,7 +45,8 @@ class LimboPlayerSerializer implements JsonSerializer<LimboPlayer> {
|
||||
|
||||
JsonObject obj = new JsonObject();
|
||||
obj.add(LOCATION, locationObject);
|
||||
obj.addProperty(GROUP, limboPlayer.getGroup());
|
||||
obj.add(GROUPS, new Gson().toJsonTree(limboPlayer.getGroups()).getAsJsonArray()); //TODO: maybe we should store the GSON instance somewhere. -sg
|
||||
|
||||
obj.addProperty(IS_OP, limboPlayer.isOperator());
|
||||
obj.addProperty(CAN_FLY, limboPlayer.isCanFly());
|
||||
obj.addProperty(WALK_SPEED, limboPlayer.getWalkSpeed());
|
||||
|
||||
@@ -19,8 +19,10 @@ import org.bukkit.plugin.PluginManager;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.inject.Inject;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* PermissionsManager.
|
||||
@@ -303,6 +305,33 @@ public class PermissionsManager implements Reloadable {
|
||||
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 at least one group was added, false otherwise.
|
||||
* False is also returned if this feature isn't supported for the current permissions system.
|
||||
*/
|
||||
public boolean addGroups(Player player, Collection<String> groupNames) {
|
||||
// If no permissions system is used, return false
|
||||
if (!isEnabled()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Add each group to the user
|
||||
boolean result = false;
|
||||
for (String groupName : groupNames) {
|
||||
if (!groupName.isEmpty()) {
|
||||
result |= handler.addToGroup(player, groupName);
|
||||
}
|
||||
}
|
||||
|
||||
// Return the result
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the permission group of a player, if supported.
|
||||
*
|
||||
@@ -312,7 +341,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 removeGroups(Player player, String groupName) {
|
||||
public boolean removeGroup(Player player, String groupName) {
|
||||
return isEnabled() && handler.removeFromGroup(player, groupName);
|
||||
}
|
||||
|
||||
@@ -320,12 +349,12 @@ public class PermissionsManager implements Reloadable {
|
||||
* Remove the permission groups of a player, if supported.
|
||||
*
|
||||
* @param player The player
|
||||
* @param groupNames The name of the groups to add.
|
||||
* @param groupNames The name of the groups to remove.
|
||||
*
|
||||
* @return True if at least one group was removed, false otherwise.
|
||||
* False is also returned if this feature isn't supported for the current permissions system.
|
||||
*/
|
||||
public boolean removeGroups(Player player, String... groupNames) {
|
||||
public boolean removeGroups(Player player, Collection<String> groupNames) {
|
||||
// If no permissions system is used, return false
|
||||
if (!isEnabled()) {
|
||||
return false;
|
||||
@@ -377,6 +406,6 @@ public class PermissionsManager implements Reloadable {
|
||||
Collection<String> groupNames = getGroups(player);
|
||||
|
||||
// Remove each group
|
||||
return removeGroups(player, groupNames.toArray(new String[groupNames.size()]));
|
||||
return removeGroups(player, groupNames);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user