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 05296e5f23.

* Cleanup imports

* Fix test

Co-authored-by: David Maes <david.maes@kbc.be>
This commit is contained in:
David Maes 2021-08-21 22:19:45 +02:00 committed by GitHub
parent b1e58fa0b8
commit d969d314b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 303 additions and 135 deletions

View File

@ -1,5 +1,6 @@
package fr.xephi.authme.command.executable.authme.debug;
import fr.xephi.authme.data.limbo.UserGroup;
import fr.xephi.authme.permission.DebugSectionPermissions;
import fr.xephi.authme.permission.PermissionNode;
import fr.xephi.authme.permission.PermissionsManager;
@ -11,6 +12,8 @@ import org.bukkit.entity.Player;
import javax.inject.Inject;
import java.util.List;
import static java.util.stream.Collectors.toList;
/**
* Outputs the permission groups of a player.
*/
@ -37,8 +40,11 @@ class PermissionGroups implements DebugSection {
if (player == null) {
sender.sendMessage("Player " + name + " could not be found");
} else {
sender.sendMessage("Player " + name + " has permission groups: "
+ String.join(", ", permissionsManager.getGroups(player)));
List<String> groupNames = permissionsManager.getGroups(player).stream()
.map(UserGroup::getGroupName)
.collect(toList());
sender.sendMessage("Player " + name + " has permission groups: " + String.join(", ", groupNames));
sender.sendMessage("Primary group is: " + permissionsManager.getGroups(player));
}
}

View File

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

View File

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

View File

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

View File

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

View File

@ -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,

View File

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

View File

@ -2,6 +2,7 @@ package fr.xephi.authme.permission;
import com.google.common.annotations.VisibleForTesting;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.data.limbo.UserGroup;
import fr.xephi.authme.initialization.Reloadable;
import fr.xephi.authme.output.ConsoleLoggerFactory;
import fr.xephi.authme.permission.handlers.LuckPermsHandler;
@ -285,7 +286,7 @@ public class PermissionsManager implements Reloadable {
*
* @return Permission groups, or an empty collection if this feature is not supported.
*/
public Collection<String> getGroups(OfflinePlayer player) {
public Collection<UserGroup> getGroups(OfflinePlayer player) {
return isEnabled() ? handler.getGroups(player) : Collections.emptyList();
}
@ -296,7 +297,7 @@ public class PermissionsManager implements Reloadable {
*
* @return The name of the primary permission group. Or null.
*/
public String getPrimaryGroup(OfflinePlayer player) {
public UserGroup getPrimaryGroup(OfflinePlayer player) {
return isEnabled() ? handler.getPrimaryGroup(player) : null;
}
@ -309,7 +310,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 isInGroup(OfflinePlayer player, String groupName) {
public boolean isInGroup(OfflinePlayer player, UserGroup groupName) {
return isEnabled() && handler.isInGroup(player, groupName);
}
@ -322,8 +323,8 @@ 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 addGroup(OfflinePlayer player, String groupName) {
if (!isEnabled() || StringUtils.isEmpty(groupName)) {
public boolean addGroup(OfflinePlayer player, UserGroup groupName) {
if (!isEnabled() || StringUtils.isEmpty(groupName.getGroupName())) {
return false;
}
return handler.addToGroup(player, groupName);
@ -338,7 +339,7 @@ public class PermissionsManager implements Reloadable {
* @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(OfflinePlayer player, Collection<String> groupNames) {
public boolean addGroups(OfflinePlayer player, Collection<UserGroup> groupNames) {
// If no permissions system is used, return false
if (!isEnabled()) {
return false;
@ -346,9 +347,9 @@ public class PermissionsManager implements Reloadable {
// Add each group to the user
boolean result = false;
for (String groupName : groupNames) {
if (!groupName.isEmpty()) {
result |= handler.addToGroup(player, groupName);
for (UserGroup group : groupNames) {
if (!group.getGroupName().isEmpty()) {
result |= handler.addToGroup(player, group);
}
}
@ -360,13 +361,13 @@ public class PermissionsManager implements Reloadable {
* Remove the permission group of a player, if supported.
*
* @param player The player
* @param groupName The name of the group.
* @param group The name of the group.
*
* @return True if succeed, false otherwise.
* False is also returned if this feature isn't supported for the current permissions system.
*/
public boolean removeGroup(OfflinePlayer player, String groupName) {
return isEnabled() && handler.removeFromGroup(player, groupName);
public boolean removeGroup(OfflinePlayer player, UserGroup group) {
return isEnabled() && handler.removeFromGroup(player, group);
}
/**
@ -378,7 +379,7 @@ public class PermissionsManager implements Reloadable {
* @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(OfflinePlayer player, Collection<String> groupNames) {
public boolean removeGroups(OfflinePlayer player, Collection<UserGroup> groupNames) {
// If no permissions system is used, return false
if (!isEnabled()) {
return false;
@ -386,9 +387,9 @@ public class PermissionsManager implements Reloadable {
// Add each group to the user
boolean result = false;
for (String groupName : groupNames) {
if (!groupName.isEmpty()) {
result |= handler.removeFromGroup(player, groupName);
for (UserGroup group : groupNames) {
if (!group.getGroupName().isEmpty()) {
result |= handler.removeFromGroup(player, group);
}
}
@ -401,13 +402,13 @@ public class PermissionsManager implements Reloadable {
* This clears the current groups of the player.
*
* @param player The player
* @param groupName The name of the group.
* @param group The name of the group.
*
* @return True if succeed, false otherwise.
* False is also returned if this feature isn't supported for the current permissions system.
*/
public boolean setGroup(OfflinePlayer player, String groupName) {
return isEnabled() && handler.setGroup(player, groupName);
public boolean setGroup(OfflinePlayer player, UserGroup group) {
return isEnabled() && handler.setGroup(player, group);
}
/**
@ -427,10 +428,10 @@ public class PermissionsManager implements Reloadable {
}
// Get a list of current groups
Collection<String> groupNames = getGroups(player);
Collection<UserGroup> groups = getGroups(player);
// Remove each group
return removeGroups(player, groupNames);
return removeGroups(player, groups);
}
/**

View File

@ -0,0 +1,23 @@
package fr.xephi.authme.permission.handlers;
import net.luckperms.api.context.ImmutableContextSet;
import net.luckperms.api.model.group.Group;
public class LuckPermGroup {
private Group group;
private ImmutableContextSet contexts;
public LuckPermGroup(Group group, ImmutableContextSet contexts) {
this.group = group;
this.contexts = contexts;
}
public Group getGroup() {
return group;
}
public ImmutableContextSet getContexts() {
return contexts;
}
}

View File

@ -1,12 +1,14 @@
package fr.xephi.authme.permission.handlers;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.data.limbo.UserGroup;
import fr.xephi.authme.output.ConsoleLoggerFactory;
import fr.xephi.authme.permission.PermissionNode;
import fr.xephi.authme.permission.PermissionsSystemType;
import net.luckperms.api.LuckPerms;
import net.luckperms.api.LuckPermsProvider;
import net.luckperms.api.cacheddata.CachedPermissionData;
import net.luckperms.api.context.ContextSetFactory;
import net.luckperms.api.model.data.DataMutateResult;
import net.luckperms.api.model.group.Group;
import net.luckperms.api.model.user.User;
@ -15,6 +17,7 @@ import net.luckperms.api.node.types.InheritanceNode;
import net.luckperms.api.query.QueryMode;
import net.luckperms.api.query.QueryOptions;
import org.bukkit.OfflinePlayer;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.List;
@ -45,8 +48,8 @@ public class LuckPermsHandler implements PermissionHandler {
}
@Override
public boolean addToGroup(OfflinePlayer player, String group) {
Group newGroup = luckPerms.getGroupManager().getGroup(group);
public boolean addToGroup(OfflinePlayer player, UserGroup group) {
Group newGroup = luckPerms.getGroupManager().getGroup(group.getGroupName());
if (newGroup == null) {
return false;
}
@ -60,7 +63,8 @@ public class LuckPermsHandler implements PermissionHandler {
return false;
}
InheritanceNode node = InheritanceNode.builder(group).build();
InheritanceNode node = buildGroupNode(group);
DataMutateResult result = user.data().add(node);
if (result == DataMutateResult.FAIL) {
return false;
@ -85,12 +89,12 @@ public class LuckPermsHandler implements PermissionHandler {
}
CachedPermissionData permissionData = user.getCachedData()
.getPermissionData(QueryOptions.builder(QueryMode.NON_CONTEXTUAL).build());
.getPermissionData(QueryOptions.builder(QueryMode.CONTEXTUAL).build());
return permissionData.checkPermission(node.getNode()).asBoolean();
}
@Override
public boolean isInGroup(OfflinePlayer player, String group) {
public boolean isInGroup(OfflinePlayer player, UserGroup group) {
String playerName = player.getName();
if (playerName == null) {
return false;
@ -102,12 +106,12 @@ public class LuckPermsHandler implements PermissionHandler {
return false;
}
InheritanceNode inheritanceNode = InheritanceNode.builder(group).build();
InheritanceNode inheritanceNode = InheritanceNode.builder(group.getGroupName()).build();
return user.data().contains(inheritanceNode, NodeEqualityPredicate.EXACT).asBoolean();
}
@Override
public boolean removeFromGroup(OfflinePlayer player, String group) {
public boolean removeFromGroup(OfflinePlayer player, UserGroup group) {
String playerName = player.getName();
if (playerName == null) {
return false;
@ -119,7 +123,7 @@ public class LuckPermsHandler implements PermissionHandler {
return false;
}
InheritanceNode groupNode = InheritanceNode.builder(group).build();
InheritanceNode groupNode = InheritanceNode.builder(group.getGroupName()).build();
boolean result = user.data().remove(groupNode) != DataMutateResult.FAIL;
luckPerms.getUserManager().saveUser(user);
@ -127,7 +131,7 @@ public class LuckPermsHandler implements PermissionHandler {
}
@Override
public boolean setGroup(OfflinePlayer player, String group) {
public boolean setGroup(OfflinePlayer player, UserGroup group) {
String playerName = player.getName();
if (playerName == null) {
return false;
@ -138,7 +142,9 @@ public class LuckPermsHandler implements PermissionHandler {
+ player.getName() + " but it isn't loaded!");
return false;
}
InheritanceNode groupNode = InheritanceNode.builder(group).build();
InheritanceNode groupNode = buildGroupNode(group);
DataMutateResult result = user.data().add(groupNode);
if (result == DataMutateResult.FAIL) {
return false;
@ -156,7 +162,7 @@ public class LuckPermsHandler implements PermissionHandler {
}
@Override
public List<String> getGroups(OfflinePlayer player) {
public List<UserGroup> getGroups(OfflinePlayer player) {
String playerName = player.getName();
if (playerName == null) {
return Collections.emptyList();
@ -171,17 +177,16 @@ public class LuckPermsHandler implements PermissionHandler {
return user.getDistinctNodes().stream()
.filter(node -> node instanceof InheritanceNode)
.map(node -> (InheritanceNode) node)
.map(node -> luckPerms.getGroupManager().getGroup(node.getGroupName()))
.filter(Objects::nonNull)
.sorted((o1, o2) -> {
if (o1.getName().equals(user.getPrimaryGroup()) || o2.getName().equals(user.getPrimaryGroup())) {
return o1.getName().equals(user.getPrimaryGroup()) ? 1 : -1;
.map(node -> {
Group group = luckPerms.getGroupManager().getGroup(node.getGroupName());
if (group == null) {
return null;
}
int i = Integer.compare(o2.getWeight().orElse(0), o1.getWeight().orElse(0));
return i != 0 ? i : o1.getName().compareToIgnoreCase(o2.getName());
return new LuckPermGroup(group, node.getContexts());
})
.map(Group::getName)
.filter(Objects::nonNull)
.sorted((o1, o2) -> sortGroups(user, o1, o2))
.map(g -> new UserGroup(g.getGroup().getName(), g.getContexts().toFlattenedMap()))
.collect(Collectors.toList());
}
@ -199,4 +204,24 @@ public class LuckPermsHandler implements PermissionHandler {
}
}
@NotNull
private InheritanceNode buildGroupNode(UserGroup group) {
ContextSetFactory contextSetFactory = luckPerms.getContextManager().getContextSetFactory();
InheritanceNode.Builder builder = InheritanceNode.builder(group.getGroupName());
if (group.getContextMap() != null) {
group.getContextMap().forEach((k, v) -> builder.withContext((contextSetFactory.immutableOf(k, v))));
}
return builder.build();
}
private int sortGroups(User user, LuckPermGroup o1, LuckPermGroup o2) {
Group group1 = o1.getGroup();
Group group2 = o2.getGroup();
if (group1.getName().equals(user.getPrimaryGroup()) || group2.getName().equals(user.getPrimaryGroup())) {
return group1.getName().equals(user.getPrimaryGroup()) ? 1 : -1;
}
int i = Integer.compare(group2.getWeight().orElse(0), group1.getWeight().orElse(0));
return i != 0 ? i : group1.getName().compareToIgnoreCase(group2.getName());
}
}

View File

@ -1,5 +1,6 @@
package fr.xephi.authme.permission.handlers;
import fr.xephi.authme.data.limbo.UserGroup;
import fr.xephi.authme.permission.PermissionNode;
import fr.xephi.authme.permission.PermissionsSystemType;
import fr.xephi.authme.util.Utils;
@ -19,7 +20,7 @@ public interface PermissionHandler {
* @return True if succeed, false otherwise.
* False is also returned if this feature isn't supported for the current permissions system.
*/
boolean addToGroup(OfflinePlayer player, String group);
boolean addToGroup(OfflinePlayer player, UserGroup group);
/**
* Check whether the current permissions system has group support.
@ -49,7 +50,7 @@ public interface PermissionHandler {
* @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.
*/
default boolean isInGroup(OfflinePlayer player, String group) {
default boolean isInGroup(OfflinePlayer player, UserGroup group) {
return getGroups(player).contains(group);
}
@ -62,7 +63,7 @@ public interface PermissionHandler {
* @return True if succeed, false otherwise.
* False is also returned if this feature isn't supported for the current permissions system.
*/
boolean removeFromGroup(OfflinePlayer player, String group);
boolean removeFromGroup(OfflinePlayer player, UserGroup group);
/**
* Set the permission group of a player, if supported.
@ -74,7 +75,7 @@ public interface PermissionHandler {
* @return True if succeed, false otherwise.
* False is also returned if this feature isn't supported for the current permissions system.
*/
boolean setGroup(OfflinePlayer player, String group);
boolean setGroup(OfflinePlayer player, UserGroup group);
/**
* Get the permission groups of a player, if available.
@ -83,7 +84,7 @@ public interface PermissionHandler {
*
* @return Permission groups, or an empty list if this feature is not supported.
*/
Collection<String> getGroups(OfflinePlayer player);
Collection<UserGroup> getGroups(OfflinePlayer player);
/**
* Get the primary group of a player, if available.
@ -92,8 +93,8 @@ public interface PermissionHandler {
*
* @return The name of the primary permission group. Or null.
*/
default String getPrimaryGroup(OfflinePlayer player) {
Collection<String> groups = getGroups(player);
default UserGroup getPrimaryGroup(OfflinePlayer player) {
Collection<UserGroup> groups = getGroups(player);
if (Utils.isCollectionEmpty(groups)) {
return null;
}

View File

@ -1,5 +1,6 @@
package fr.xephi.authme.permission.handlers;
import fr.xephi.authme.data.limbo.UserGroup;
import fr.xephi.authme.permission.PermissionNode;
import fr.xephi.authme.permission.PermissionsSystemType;
import org.bukkit.OfflinePlayer;
@ -10,6 +11,8 @@ import ru.tehkode.permissions.bukkit.PermissionsEx;
import java.util.ArrayList;
import java.util.List;
import static java.util.stream.Collectors.toList;
/**
* Handler for PermissionsEx.
*
@ -28,13 +31,13 @@ public class PermissionsExHandler implements PermissionHandler {
}
@Override
public boolean addToGroup(OfflinePlayer player, String group) {
public boolean addToGroup(OfflinePlayer player, UserGroup group) {
if (!PermissionsEx.getPermissionManager().getGroupNames().contains(group)) {
return false;
}
PermissionUser user = PermissionsEx.getUser(player.getName());
user.addGroup(group);
user.addGroup(group.getGroupName());
return true;
}
@ -50,22 +53,22 @@ public class PermissionsExHandler implements PermissionHandler {
}
@Override
public boolean isInGroup(OfflinePlayer player, String group) {
public boolean isInGroup(OfflinePlayer player, UserGroup group) {
PermissionUser user = permissionManager.getUser(player.getName());
return user.inGroup(group);
return user.inGroup(group.getGroupName());
}
@Override
public boolean removeFromGroup(OfflinePlayer player, String group) {
public boolean removeFromGroup(OfflinePlayer player, UserGroup group) {
PermissionUser user = permissionManager.getUser(player.getName());
user.removeGroup(group);
user.removeGroup(group.getGroupName());
return true;
}
@Override
public boolean setGroup(OfflinePlayer player, String group) {
public boolean setGroup(OfflinePlayer player, UserGroup group) {
List<String> groups = new ArrayList<>();
groups.add(group);
groups.add(group.getGroupName());
PermissionUser user = permissionManager.getUser(player.getName());
user.setParentsIdentifier(groups);
@ -73,9 +76,11 @@ public class PermissionsExHandler implements PermissionHandler {
}
@Override
public List<String> getGroups(OfflinePlayer player) {
public List<UserGroup> getGroups(OfflinePlayer player) {
PermissionUser user = permissionManager.getUser(player.getName());
return user.getParentIdentifiers(null);
return user.getParentIdentifiers(null).stream()
.map(i -> new UserGroup(i, null))
.collect(toList());
}
@Override

View File

@ -1,6 +1,7 @@
package fr.xephi.authme.permission.handlers;
import com.google.common.annotations.VisibleForTesting;
import fr.xephi.authme.data.limbo.UserGroup;
import fr.xephi.authme.permission.PermissionNode;
import fr.xephi.authme.permission.PermissionsSystemType;
import net.milkbowl.vault.permission.Permission;
@ -12,6 +13,8 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static java.util.stream.Collectors.toList;
/**
* Handler for permissions via Vault.
*
@ -51,8 +54,8 @@ public class VaultHandler implements PermissionHandler {
}
@Override
public boolean addToGroup(OfflinePlayer player, String group) {
return vaultProvider.playerAddGroup(null, player, group);
public boolean addToGroup(OfflinePlayer player, UserGroup group) {
return vaultProvider.playerAddGroup(null, player, group.getGroupName());
}
@Override
@ -66,33 +69,33 @@ public class VaultHandler implements PermissionHandler {
}
@Override
public boolean isInGroup(OfflinePlayer player, String group) {
return vaultProvider.playerInGroup(null, player, group);
public boolean isInGroup(OfflinePlayer player, UserGroup group) {
return vaultProvider.playerInGroup(null, player, group.getGroupName());
}
@Override
public boolean removeFromGroup(OfflinePlayer player, String group) {
return vaultProvider.playerRemoveGroup(null, player, group);
public boolean removeFromGroup(OfflinePlayer player, UserGroup group) {
return vaultProvider.playerRemoveGroup(null, player, group.getGroupName());
}
@Override
public boolean setGroup(OfflinePlayer player, String group) {
for (String groupName : getGroups(player)) {
removeFromGroup(player, groupName);
public boolean setGroup(OfflinePlayer player, UserGroup group) {
for (UserGroup g : getGroups(player)) {
removeFromGroup(player, g);
}
return vaultProvider.playerAddGroup(null, player, group);
return vaultProvider.playerAddGroup(null, player, group.getGroupName());
}
@Override
public List<String> getGroups(OfflinePlayer player) {
public List<UserGroup> getGroups(OfflinePlayer player) {
String[] groups = vaultProvider.getPlayerGroups(null, player);
return groups == null ? Collections.emptyList() : Arrays.asList(groups);
return groups == null ? Collections.emptyList() : Arrays.stream(groups).map(UserGroup::new).collect(toList());
}
@Override
public String getPrimaryGroup(OfflinePlayer player) {
return vaultProvider.getPrimaryGroup(null, player);
public UserGroup getPrimaryGroup(OfflinePlayer player) {
return new UserGroup(vaultProvider.getPrimaryGroup(null, player));
}
@Override

View File

@ -1,5 +1,6 @@
package fr.xephi.authme.permission.handlers;
import fr.xephi.authme.data.limbo.UserGroup;
import fr.xephi.authme.permission.PermissionNode;
import fr.xephi.authme.permission.PermissionsSystemType;
import org.bukkit.Bukkit;
@ -9,6 +10,8 @@ import org.tyrannyofheaven.bukkit.zPermissions.ZPermissionsService;
import java.util.Collection;
import java.util.Map;
import static java.util.stream.Collectors.toList;
/**
* Handler for zPermissions.
*
@ -29,9 +32,9 @@ public class ZPermissionsHandler implements PermissionHandler {
}
@Override
public boolean addToGroup(OfflinePlayer player, String group) {
public boolean addToGroup(OfflinePlayer player, UserGroup group) {
return Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
"permissions player " + player.getName() + " addgroup " + group);
"permissions player " + player.getName() + " addgroup " + group.getGroupName());
}
@Override
@ -46,25 +49,27 @@ public class ZPermissionsHandler implements PermissionHandler {
}
@Override
public boolean removeFromGroup(OfflinePlayer player, String group) {
public boolean removeFromGroup(OfflinePlayer player, UserGroup group) {
return Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
"permissions player " + player.getName() + " removegroup " + group);
"permissions player " + player.getName() + " removegroup " + group.getGroupName());
}
@Override
public boolean setGroup(OfflinePlayer player, String group) {
public boolean setGroup(OfflinePlayer player, UserGroup group) {
return Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
"permissions player " + player.getName() + " setgroup " + group);
"permissions player " + player.getName() + " setgroup " + group.getGroupName());
}
@Override
public Collection<String> getGroups(OfflinePlayer player) {
return zPermissionsService.getPlayerGroups(player.getName());
public Collection<UserGroup> getGroups(OfflinePlayer player) {
return zPermissionsService.getPlayerGroups(player.getName()).stream()
.map(UserGroup::new)
.collect(toList());
}
@Override
public String getPrimaryGroup(OfflinePlayer player) {
return zPermissionsService.getPlayerPrimaryGroup(player.getName());
public UserGroup getPrimaryGroup(OfflinePlayer player) {
return new UserGroup(zPermissionsService.getPlayerPrimaryGroup(player.getName()));
}
@Override

View File

@ -6,9 +6,12 @@ import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import static java.lang.String.format;
import static java.util.stream.Collectors.toList;
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
/**
@ -21,12 +24,12 @@ public final class LimboPlayerMatchers {
}
public static Matcher<LimboPlayer> isLimbo(LimboPlayer limbo) {
String[] groups = limbo.getGroups().toArray(new String[limbo.getGroups().size()]);
UserGroup[] groups = limbo.getGroups().toArray(new UserGroup[limbo.getGroups().size()]);
return isLimbo(limbo.isOperator(), limbo.isCanFly(), limbo.getWalkSpeed(), limbo.getFlySpeed(), groups);
}
public static Matcher<LimboPlayer> isLimbo(boolean isOp, boolean canFly, float walkSpeed, float flySpeed,
String... groups) {
UserGroup... groups) {
return new TypeSafeMatcher<LimboPlayer>() {
@Override
protected boolean matchesSafely(LimboPlayer item) {
@ -39,14 +42,19 @@ public final class LimboPlayerMatchers {
@Override
public void describeTo(Description description) {
List<String> groupNames = Arrays.stream(groups).map(UserGroup::getGroupName).collect(toList());
description.appendText(format("Limbo with isOp=%s, groups={%s}, canFly=%s, walkSpeed=%f, flySpeed=%f",
isOp, String.join(" ,", groups), canFly, walkSpeed, flySpeed));
isOp, String.join(" ,", groupNames), canFly, walkSpeed, flySpeed));
}
@Override
public void describeMismatchSafely(LimboPlayer item, Description description) {
List<String> groupNames = item.getGroups().stream()
.map(UserGroup::getGroupName)
.collect(toList());
description.appendText(format("Limbo with isOp=%s, groups={%s}, canFly=%s, walkSpeed=%f, flySpeed=%f",
item.isOperator(), String.join(" ,", item.getGroups()), item.isCanFly(),
item.isOperator(), String.join(" ,", groupNames), item.isCanFly(),
item.getWalkSpeed(), item.getFlySpeed()));
}
};
@ -119,7 +127,7 @@ public final class LimboPlayerMatchers {
}
// Hamcrest's contains() doesn't like it when there are no items, so we need to check for the empty case explicitly
private static boolean collectionContains(Collection<String> givenItems, String... expectedItems) {
private static boolean collectionContains(Collection<UserGroup> givenItems, UserGroup... expectedItems) {
if (expectedItems.length == 0) {
return givenItems.isEmpty();
}

View File

@ -106,7 +106,7 @@ public class LimboPlayerTaskManagerTest {
String name = "rats";
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
LimboPlayer limboPlayer = new LimboPlayer(null, true, Collections.singletonList("grp"), false, 0.1f, 0.0f);
LimboPlayer limboPlayer = new LimboPlayer(null, true, Collections.singletonList(new UserGroup("grp")), false, 0.1f, 0.0f);
MessageTask existingMessageTask = mock(MessageTask.class);
limboPlayer.setMessageTask(existingMessageTask);
given(settings.getProperty(RegistrationSettings.MESSAGE_INTERVAL)).willReturn(8);
@ -129,7 +129,7 @@ public class LimboPlayerTaskManagerTest {
String name = "race";
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
LimboPlayer limboPlayer = new LimboPlayer(null, true, Collections.singletonList("grp"), false, 0.1f, 0.0f);
LimboPlayer limboPlayer = new LimboPlayer(null, true, Collections.singletonList(new UserGroup("grp")), false, 0.1f, 0.0f);
given(settings.getProperty(RegistrationSettings.MESSAGE_INTERVAL)).willReturn(12);
given(registrationCaptchaManager.isCaptchaRequired(name)).willReturn(true);
String captcha = "M032";

View File

@ -37,9 +37,9 @@ public class LimboServiceHelperTest {
public void shouldMergeLimboPlayers() {
// given
Location newLocation = mock(Location.class);
LimboPlayer newLimbo = new LimboPlayer(newLocation, false, Collections.singletonList("grp-new"), false, 0.0f, 0.0f);
LimboPlayer newLimbo = new LimboPlayer(newLocation, false, Collections.singletonList(new UserGroup("grp-new")), false, 0.0f, 0.0f);
Location oldLocation = mock(Location.class);
LimboPlayer oldLimbo = new LimboPlayer(oldLocation, true, Collections.singletonList("grp-old"), true, 0.1f, 0.8f);
LimboPlayer oldLimbo = new LimboPlayer(oldLocation, true, Collections.singletonList(new UserGroup("grp-old")), true, 0.1f, 0.8f);
// when
LimboPlayer result = limboServiceHelper.merge(newLimbo, oldLimbo);
@ -47,7 +47,7 @@ public class LimboServiceHelperTest {
// then
assertThat(result.getLocation(), equalTo(oldLocation));
assertThat(result.isOperator(), equalTo(true));
assertThat(result.getGroups(), contains("grp-old"));
assertThat(result.getGroups(), contains(new UserGroup("grp-old")));
assertThat(result.isCanFly(), equalTo(true));
assertThat(result.getWalkSpeed(), equalTo(0.1f));
assertThat(result.getFlySpeed(), equalTo(0.8f));
@ -57,7 +57,7 @@ public class LimboServiceHelperTest {
public void shouldFallBackToNewLimboForMissingData() {
// given
Location newLocation = mock(Location.class);
LimboPlayer newLimbo = new LimboPlayer(newLocation, false, Collections.singletonList("grp-new"), true, 0.3f, 0.0f);
LimboPlayer newLimbo = new LimboPlayer(newLocation, false, Collections.singletonList(new UserGroup("grp-new")), true, 0.3f, 0.0f);
LimboPlayer oldLimbo = new LimboPlayer(null, false, Collections.emptyList(), false, 0.1f, 0.1f);
// when
@ -66,7 +66,7 @@ public class LimboServiceHelperTest {
// then
assertThat(result.getLocation(), equalTo(newLocation));
assertThat(result.isOperator(), equalTo(false));
assertThat(result.getGroups(), contains("grp-new"));
assertThat(result.getGroups(), contains(new UserGroup("grp-new")));
assertThat(result.isCanFly(), equalTo(true));
assertThat(result.getWalkSpeed(), equalTo(0.3f));
assertThat(result.getFlySpeed(), equalTo(0.1f));

View File

@ -83,7 +83,7 @@ public class LimboServiceTest {
Location playerLoc = mock(Location.class);
given(spawnLoader.getPlayerLocationOrSpawn(player)).willReturn(playerLoc);
given(permissionsManager.hasGroupSupport()).willReturn(true);
given(permissionsManager.getGroups(player)).willReturn(Collections.singletonList("permgrwp"));
given(permissionsManager.getGroups(player)).willReturn(Collections.singletonList(new UserGroup("permgrwp")));
given(settings.getProperty(LimboSettings.RESTORE_ALLOW_FLIGHT)).willReturn(AllowFlightRestoreType.ENABLE);
// when
@ -105,7 +105,7 @@ public class LimboServiceTest {
assertThat(limbo.isCanFly(), equalTo(false));
assertThat(limbo.getFlySpeed(), equalTo(0.2f));
assertThat(limbo.getLocation(), equalTo(playerLoc));
assertThat(limbo.getGroups(), equalTo(Collections.singletonList("permgrwp")));
assertThat(limbo.getGroups(), equalTo(Collections.singletonList(new UserGroup("permgrwp"))));
}
@Test
@ -241,7 +241,7 @@ public class LimboServiceTest {
return player;
}
private static LimboPlayer convertToLimboPlayer(Player player, Location location, Collection<String> groups) {
private static LimboPlayer convertToLimboPlayer(Player player, Location location, Collection<UserGroup> groups) {
return new LimboPlayer(location, player.isOp(), groups, player.getAllowFlight(),
player.getWalkSpeed(), player.getFlySpeed());
}

View File

@ -6,6 +6,7 @@ import ch.jalu.injector.testing.InjectDelayed;
import com.google.common.io.Files;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.data.limbo.LimboPlayer;
import fr.xephi.authme.data.limbo.UserGroup;
import fr.xephi.authme.initialization.DataFolder;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.settings.Settings;
@ -48,22 +49,22 @@ public class DistributedFilesPersistenceHandlerTest {
/** Player is in seg32-10110 and should be migrated into seg16-f. */
private static final UUID MIGRATED_UUID = fromString("f6a97c88-7c8f-c12e-4931-6206d4ca067d");
private static final Matcher<LimboPlayer> MIGRATED_LIMBO_MATCHER =
isLimbo(false, true, 0.2f, 0.1f, "noob");
isLimbo(false, true, 0.2f, 0.1f, new UserGroup("noob"));
/** Existing player in seg16-f. */
private static final UUID UUID_FAB69 = fromString("fab69c88-2cd0-1fed-f00d-dead14ca067d");
private static final Matcher<LimboPlayer> FAB69_MATCHER =
isLimbo(false, false, 0.2f, 0.1f, "");
isLimbo(false, false, 0.2f, 0.1f, new UserGroup(""));
/** Player in seg16-8. */
private static final UUID UUID_STAFF = fromString("88897c88-7c8f-c12e-4931-6206d4ca067d");
private static final Matcher<LimboPlayer> STAFF_MATCHER =
isLimbo(true, false, 0.3f, 0.1f, "staff", "mod");
isLimbo(true, false, 0.3f, 0.1f, new UserGroup("staff"), new UserGroup("mod"));
/** Player in seg16-8. */
private static final UUID UUID_8C679 = fromString("8c679491-1234-abcd-9102-1fa6e0cc3f81");
private static final Matcher<LimboPlayer> SC679_MATCHER =
isLimbo(false, true, 0.1f, 0.0f, "primary");
isLimbo(false, true, 0.1f, 0.0f, new UserGroup("primary"));
/** UUID for which no data is stored (belongs to a segment file that does not exist, seg16-4). */
private static final UUID UNKNOWN_UUID = fromString("42d1cc0b-8f12-d04a-e7ba-a067d05cdc39");
@ -156,7 +157,7 @@ public class DistributedFilesPersistenceHandlerTest {
// given
Player uuidToAdd1 = mockPlayerWithUuid(UNKNOWN_UUID);
Location location1 = mockLocation("1world");
LimboPlayer limbo1 = new LimboPlayer(location1, false, Collections.singletonList("group-1"), true, 0.1f, 0.2f);
LimboPlayer limbo1 = new LimboPlayer(location1, false, Collections.singletonList(new UserGroup("group-1")), true, 0.1f, 0.2f);
Player uuidToAdd2 = mockPlayerWithUuid(UNKNOWN_UUID2);
Location location2 = mockLocation("2world");
LimboPlayer limbo2 = new LimboPlayer(location2, true, Collections.emptyList(), false, 0.0f, 0.25f);

View File

@ -5,6 +5,7 @@ import ch.jalu.injector.testing.DelayedInjectionRunner;
import ch.jalu.injector.testing.InjectDelayed;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.data.limbo.LimboPlayer;
import fr.xephi.authme.data.limbo.UserGroup;
import fr.xephi.authme.initialization.DataFolder;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.util.FileUtils;
@ -80,7 +81,7 @@ public class IndividualFilesPersistenceHandlerTest {
assertThat(data.isCanFly(), equalTo(true));
assertThat(data.getWalkSpeed(), equalTo(0.2f));
assertThat(data.getFlySpeed(), equalTo(0.1f));
assertThat(data.getGroups(), contains("players"));
assertThat(data.getGroups(), contains(new UserGroup("players")));
Location location = data.getLocation();
assertThat(location.getX(), equalTo(-113.219));
assertThat(location.getY(), equalTo(72.0));
@ -114,7 +115,7 @@ public class IndividualFilesPersistenceHandlerTest {
World world = mock(World.class);
given(world.getName()).willReturn("player-world");
Location location = new Location(world, 0.2, 102.25, -89.28, 3.02f, 90.13f);
LimboPlayer limbo = new LimboPlayer(location, true, Collections.singletonList("primary-grp"), true, 1.2f, 0.8f);
LimboPlayer limbo = new LimboPlayer(location, true, Collections.singletonList(new UserGroup("primary-grp")), true, 1.2f, 0.8f);
// when
handler.saveLimboPlayer(player, limbo);

View File

@ -1,12 +1,15 @@
package fr.xephi.authme.permission.handlers;
import fr.xephi.authme.data.limbo.UserGroup;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.junit.Test;
import java.util.List;
import java.util.stream.Collectors;
import static java.util.stream.Collectors.toList;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.empty;
import static org.junit.Assert.assertThat;
@ -29,10 +32,11 @@ public class VaultHandlerTest {
given(permissionMock.getPlayerGroups(null, player)).willReturn(new String[]{"abc", "test"});
// when
List<String> result = vaultHandlerTest.getGroups(player);
List<UserGroup> result = vaultHandlerTest.getGroups(player);
// then
assertThat(result, contains("abc", "test"));
List<String> groupNames = result.stream().map(UserGroup::getGroupName).collect(toList());
assertThat(groupNames, contains("abc", "test"));
verify(permissionMock).getPlayerGroups(null, player);
}
@ -47,7 +51,7 @@ public class VaultHandlerTest {
given(permissionMock.getPlayerGroups(null, player)).willReturn(null);
// when
List<String> result = vaultHandlerTest.getGroups(player);
List<UserGroup> result = vaultHandlerTest.getGroups(player);
// then
assertThat(result, empty());