LuckPerm: keep contexts active after reload of groups (#2186)
* Use context aware groups * Revert "Use context aware groups" This reverts commit 5adc6ef7 * Keep luckperm contexts after authentication * Code cleanup * Code cleanup * Fix nullpointer for context maps * Code cleanup * Use uuids for sqlite and postgresql * Revert "Use uuids for sqlite and postgresql" This reverts commit 05296e5f23bd4379a89647656f08432c718e6f9c. * Cleanup imports * Fix test Co-authored-by: David Maes <david.maes@kbc.be>
This commit is contained in:
@@ -35,8 +35,8 @@ class AuthGroupHandler implements Reloadable {
|
||||
@Inject
|
||||
private Settings settings;
|
||||
|
||||
private String unregisteredGroup;
|
||||
private String registeredGroup;
|
||||
private UserGroup unregisteredGroup;
|
||||
private UserGroup registeredGroup;
|
||||
|
||||
AuthGroupHandler() {
|
||||
}
|
||||
@@ -53,7 +53,7 @@ class AuthGroupHandler implements Reloadable {
|
||||
return;
|
||||
}
|
||||
|
||||
Collection<String> previousGroups = limbo == null ? Collections.emptyList() : limbo.getGroups();
|
||||
Collection<UserGroup> previousGroups = limbo == null ? Collections.emptyList() : limbo.getGroups();
|
||||
|
||||
switch (groupType) {
|
||||
// Implementation note: some permission systems don't support players not being in any group,
|
||||
@@ -107,8 +107,8 @@ class AuthGroupHandler implements Reloadable {
|
||||
@Override
|
||||
@PostConstruct
|
||||
public void reload() {
|
||||
unregisteredGroup = settings.getProperty(PluginSettings.UNREGISTERED_GROUP);
|
||||
registeredGroup = settings.getProperty(PluginSettings.REGISTERED_GROUP);
|
||||
unregisteredGroup = new UserGroup(settings.getProperty(PluginSettings.UNREGISTERED_GROUP));
|
||||
registeredGroup = new UserGroup(settings.getProperty(PluginSettings.REGISTERED_GROUP));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ public class LimboPlayer {
|
||||
|
||||
private final boolean canFly;
|
||||
private final boolean operator;
|
||||
private final Collection<String> groups;
|
||||
private final Collection<UserGroup> groups;
|
||||
private final Location loc;
|
||||
private final float walkSpeed;
|
||||
private final float flySpeed;
|
||||
@@ -25,7 +25,7 @@ public class LimboPlayer {
|
||||
private MessageTask messageTask = null;
|
||||
private LimboPlayerState state = LimboPlayerState.PASSWORD_REQUIRED;
|
||||
|
||||
public LimboPlayer(Location loc, boolean operator, Collection<String> groups, boolean fly, float walkSpeed,
|
||||
public LimboPlayer(Location loc, boolean operator, Collection<UserGroup> groups, boolean fly, float walkSpeed,
|
||||
float flySpeed) {
|
||||
this.loc = loc;
|
||||
this.operator = operator;
|
||||
@@ -58,7 +58,7 @@ public class LimboPlayer {
|
||||
*
|
||||
* @return The permissions groups the player belongs to
|
||||
*/
|
||||
public Collection<String> getGroups() {
|
||||
public Collection<UserGroup> getGroups() {
|
||||
return groups;
|
||||
}
|
||||
|
||||
|
||||
@@ -12,8 +12,10 @@ import org.bukkit.entity.Player;
|
||||
import javax.inject.Inject;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static fr.xephi.authme.util.Utils.isCollectionEmpty;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
/**
|
||||
* Helper class for the LimboService.
|
||||
@@ -31,9 +33,9 @@ class LimboServiceHelper {
|
||||
/**
|
||||
* Creates a LimboPlayer with the given player's details.
|
||||
*
|
||||
* @param player the player to process
|
||||
* @param player the player to process
|
||||
* @param isRegistered whether the player is registered
|
||||
* @param location the player location
|
||||
* @param location the player location
|
||||
* @return limbo player with the player's data
|
||||
*/
|
||||
LimboPlayer createLimboPlayer(Player player, boolean isRegistered, Location location) {
|
||||
@@ -42,10 +44,14 @@ class LimboServiceHelper {
|
||||
boolean flyEnabled = player.getAllowFlight();
|
||||
float walkSpeed = player.getWalkSpeed();
|
||||
float flySpeed = player.getFlySpeed();
|
||||
Collection<String> playerGroups = permissionsManager.hasGroupSupport()
|
||||
Collection<UserGroup> playerGroups = permissionsManager.hasGroupSupport()
|
||||
? permissionsManager.getGroups(player) : Collections.emptyList();
|
||||
logger.debug("Player `{0}` has groups `{1}`", player.getName(), String.join(", ", playerGroups));
|
||||
|
||||
List<String> groupNames = playerGroups.stream()
|
||||
.map(UserGroup::getGroupName)
|
||||
.collect(toList());
|
||||
|
||||
logger.debug("Player `{0}` has groups `{1}`", player.getName(), String.join(", ", groupNames));
|
||||
return new LimboPlayer(location, isOperator, playerGroups, flyEnabled, walkSpeed, flySpeed);
|
||||
}
|
||||
|
||||
@@ -91,7 +97,7 @@ class LimboServiceHelper {
|
||||
boolean canFly = newLimbo.isCanFly() || oldLimbo.isCanFly();
|
||||
float flySpeed = Math.max(newLimbo.getFlySpeed(), oldLimbo.getFlySpeed());
|
||||
float walkSpeed = Math.max(newLimbo.getWalkSpeed(), oldLimbo.getWalkSpeed());
|
||||
Collection<String> groups = getLimboGroups(oldLimbo.getGroups(), newLimbo.getGroups());
|
||||
Collection<UserGroup> groups = getLimboGroups(oldLimbo.getGroups(), newLimbo.getGroups());
|
||||
Location location = firstNotNull(oldLimbo.getLocation(), newLimbo.getLocation());
|
||||
|
||||
return new LimboPlayer(location, isOperator, groups, canFly, walkSpeed, flySpeed);
|
||||
@@ -101,7 +107,8 @@ class LimboServiceHelper {
|
||||
return first == null ? second : first;
|
||||
}
|
||||
|
||||
private Collection<String> getLimboGroups(Collection<String> oldLimboGroups, Collection<String> newLimboGroups) {
|
||||
private Collection<UserGroup> getLimboGroups(Collection<UserGroup> oldLimboGroups,
|
||||
Collection<UserGroup> newLimboGroups) {
|
||||
logger.debug("Limbo merge: new and old groups are `{0}` and `{1}`", newLimboGroups, oldLimboGroups);
|
||||
return isCollectionEmpty(oldLimboGroups) ? newLimboGroups : oldLimboGroups;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
package fr.xephi.authme.data.limbo;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
public class UserGroup {
|
||||
|
||||
private String groupName;
|
||||
private Map<String, String> contextMap;
|
||||
|
||||
public UserGroup(String groupName) {
|
||||
this.groupName = groupName;
|
||||
}
|
||||
|
||||
public UserGroup(String groupName, Map<String, String> contextMap) {
|
||||
this.groupName = groupName;
|
||||
this.contextMap = contextMap;
|
||||
}
|
||||
|
||||
public String getGroupName() {
|
||||
return groupName;
|
||||
}
|
||||
|
||||
public Map<String, String> getContextMap() {
|
||||
return contextMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
UserGroup userGroup = (UserGroup) o;
|
||||
return Objects.equals(groupName, userGroup.groupName)
|
||||
&& Objects.equals(contextMap, userGroup.contextMap);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(groupName, contextMap);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,14 @@
|
||||
package fr.xephi.authme.data.limbo.persistence;
|
||||
|
||||
import com.google.common.reflect.TypeToken;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import fr.xephi.authme.data.limbo.LimboPlayer;
|
||||
import fr.xephi.authme.data.limbo.UserGroup;
|
||||
import fr.xephi.authme.service.BukkitService;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
@@ -15,6 +18,7 @@ import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static fr.xephi.authme.data.limbo.persistence.LimboPlayerSerializer.CAN_FLY;
|
||||
@@ -37,6 +41,8 @@ import static java.util.Optional.ofNullable;
|
||||
class LimboPlayerDeserializer implements JsonDeserializer<LimboPlayer> {
|
||||
|
||||
private static final String GROUP_LEGACY = "group";
|
||||
private static final String CONTEXT_MAP = "contextMap";
|
||||
private static final String GROUP_NAME = "groupName";
|
||||
|
||||
private BukkitService bukkitService;
|
||||
|
||||
@@ -54,7 +60,7 @@ class LimboPlayerDeserializer implements JsonDeserializer<LimboPlayer> {
|
||||
Location loc = deserializeLocation(jsonObject);
|
||||
boolean operator = getBoolean(jsonObject, IS_OP);
|
||||
|
||||
Collection<String> groups = getLimboGroups(jsonObject);
|
||||
Collection<UserGroup> groups = getLimboGroups(jsonObject);
|
||||
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);
|
||||
@@ -84,16 +90,35 @@ class LimboPlayerDeserializer implements JsonDeserializer<LimboPlayer> {
|
||||
return element != null ? element.getAsString() : "";
|
||||
}
|
||||
|
||||
private static List<String> getLimboGroups(JsonObject jsonObject) {
|
||||
/**
|
||||
* @param jsonObject LimboPlayer represented as JSON
|
||||
* @return The list of UserGroups create from JSON
|
||||
*/
|
||||
private static List<UserGroup> getLimboGroups(JsonObject jsonObject) {
|
||||
JsonElement element = jsonObject.get(GROUPS);
|
||||
if (element == null) {
|
||||
String legacyGroup = ofNullable(jsonObject.get(GROUP_LEGACY)).map(JsonElement::getAsString).orElse(null);
|
||||
return legacyGroup == null ? Collections.emptyList() : Collections.singletonList(legacyGroup);
|
||||
return legacyGroup == null ? Collections.emptyList() :
|
||||
Collections.singletonList(new UserGroup(legacyGroup, null));
|
||||
}
|
||||
List<String> result = new ArrayList<>();
|
||||
List<UserGroup> result = new ArrayList<>();
|
||||
JsonArray jsonArray = element.getAsJsonArray();
|
||||
for (JsonElement arrayElement : jsonArray) {
|
||||
result.add(arrayElement.getAsString());
|
||||
if (!arrayElement.isJsonObject()) {
|
||||
result.add(new UserGroup(arrayElement.getAsString(), null));
|
||||
} else {
|
||||
JsonObject jsonGroup = arrayElement.getAsJsonObject();
|
||||
Map<String, String> contextMap = null;
|
||||
if (jsonGroup.has(CONTEXT_MAP)) {
|
||||
JsonElement contextMapJson = jsonGroup.get("contextMap");
|
||||
Type type = new TypeToken<Map<String, String>>() {
|
||||
}.getType();
|
||||
contextMap = new Gson().fromJson(contextMapJson.getAsString(), type);
|
||||
}
|
||||
|
||||
String groupName = jsonGroup.get(GROUP_NAME).getAsString();
|
||||
result.add(new UserGroup(groupName, contextMap));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -122,7 +147,6 @@ class LimboPlayerDeserializer implements JsonDeserializer<LimboPlayer> {
|
||||
* @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
|
||||
*
|
||||
* @return the number from the given JSON element, or the default value
|
||||
*/
|
||||
private static <N extends Number> N getNumberFromElement(JsonElement jsonElement,
|
||||
|
||||
@@ -4,13 +4,14 @@ import com.google.gson.Gson;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import fr.xephi.authme.data.limbo.LimboPlayer;
|
||||
import org.bukkit.Location;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Converts a LimboPlayer to a JsonElement.
|
||||
@@ -47,11 +48,19 @@ class LimboPlayerSerializer implements JsonSerializer<LimboPlayer> {
|
||||
|
||||
JsonObject obj = new JsonObject();
|
||||
obj.add(LOCATION, locationObject);
|
||||
JsonArray groups = new JsonArray();
|
||||
for (String group : limboPlayer.getGroups()) {
|
||||
groups.add(new JsonPrimitive(group));
|
||||
}
|
||||
obj.add(GROUPS, groups);
|
||||
|
||||
List<JsonObject> groups = limboPlayer.getGroups().stream().map(g -> {
|
||||
JsonObject jsonGroup = new JsonObject();
|
||||
jsonGroup.addProperty("groupName", g.getGroupName());
|
||||
if (g.getContextMap() != null) {
|
||||
jsonGroup.addProperty("contextMap", GSON.toJson(g.getContextMap()));
|
||||
}
|
||||
return jsonGroup;
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
JsonArray jsonGroups = new JsonArray();
|
||||
groups.forEach(jsonGroups::add);
|
||||
obj.add(GROUPS, jsonGroups);
|
||||
|
||||
obj.addProperty(IS_OP, limboPlayer.isOperator());
|
||||
obj.addProperty(CAN_FLY, limboPlayer.isCanFly());
|
||||
|
||||
Reference in New Issue
Block a user