Per-world permissions

This commit is contained in:
Luck
2016-07-25 18:19:36 +01:00
Unverified
parent 9ad40be210
commit ebeb69dd3a
38 changed files with 950 additions and 296 deletions
@@ -11,7 +11,9 @@ import me.lucko.luckperms.groups.Group;
import me.lucko.luckperms.utils.Patterns;
import me.lucko.luckperms.utils.PermissionObject;
import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
@ToString(of = {"uuid"})
@@ -74,6 +76,17 @@ public abstract class User extends PermissionObject {
return hasPermission("group." + group.getName(), true, server);
}
/**
* Check to see if a user is a member of a group on a specific server
* @param group The group to check membership of
* @param server The server to check on
* @param world The world to check on
* @return true if the user is a member of the group
*/
public boolean isInGroup(Group group, String server, String world) {
return hasPermission("group." + group.getName(), true, server, world);
}
/**
* Add a user to a group
* @param group The group to add the user to
@@ -97,6 +110,21 @@ public abstract class User extends PermissionObject {
setPermission("group." + group.getName(), true, server);
}
/**
* Add a user to a group on a specific server
* @param group The group to add the user to
* @param server The server to add the group on
* @param world The world to add the group on
* @throws ObjectAlreadyHasException if the user is already a member of the group on that server
*/
public void addGroup(Group group, String server, String world) throws ObjectAlreadyHasException {
if (server == null) {
server = "global";
}
setPermission("group." + group.getName(), true, server, world);
}
/**
* Add a user to a group on a specific server
* @param group The group to add the user to
@@ -122,6 +150,22 @@ public abstract class User extends PermissionObject {
setPermission("group." + group.getName(), true, server, expireAt);
}
/**
* Add a user to a group on a specific server
* @param group The group to add the user to
* @param server The server to add the group on
* @param world The world to add the group on
* @param expireAt when the group should expire
* @throws ObjectAlreadyHasException if the user is already a member of the group on that server
*/
public void addGroup(Group group, String server, String world, long expireAt) throws ObjectAlreadyHasException {
if (server == null) {
server = "global";
}
setPermission("group." + group.getName(), true, server, world, expireAt);
}
/**
* Remove the user from a group
* @param group the group to remove the user from
@@ -155,6 +199,21 @@ public abstract class User extends PermissionObject {
unsetPermission("group." + group.getName(), server);
}
/**
* Remove the user from a group
* @param group The group to remove the user from
* @param server The server to remove the group on
* @param world The world to remove the group on
* @throws ObjectLacksException if the user isn't a member of the group
*/
public void removeGroup(Group group, String server, String world) throws ObjectLacksException {
if (server == null) {
server = "global";
}
unsetPermission("group." + group.getName(), server, world);
}
/**
* Remove the user from a group
* @param group The group to remove the user from
@@ -170,6 +229,22 @@ public abstract class User extends PermissionObject {
unsetPermission("group." + group.getName(), server, temporary);
}
/**
* Remove the user from a group
* @param group The group to remove the user from
* @param server The server to remove the group on
* @param world The world to remove the group on
* @param temporary if the group being removed is temporary
* @throws ObjectLacksException if the user isn't a member of the group
*/
public void removeGroup(Group group, String server, String world, boolean temporary) throws ObjectLacksException {
if (server == null) {
server = "global";
}
unsetPermission("group." + group.getName(), server, world, temporary);
}
/**
* Clear all of the users permission nodes
*/
@@ -187,96 +262,38 @@ public abstract class User extends PermissionObject {
return getGroups(null, null, true);
}
/**
* Get a {@link List} of the groups the user is a member of on a specific server
* @param server the server to check
* @return a {@link List} of group names
*/
public List<String> getLocalGroups(String server, String world) {
return getGroups(server, world, false);
}
/**
* Get a {@link List} of the groups the user is a member of on a specific server
* @param server the server to check
* @return a {@link List} of group names
*/
public List<String> getLocalGroups(String server) {
return getGroups(server, null, false);
return getLocalGroups(server, null);
}
/**
* Get a {@link List} of the groups the user is a member of on a specific server with the option to include global
* groups or all groups
* @param server Which server to check on
* @param excludedGroups groups to exclude (prevents circular inheritance issues)
* @param world Which world to check on
* @param includeGlobal Whether to include global groups
* @return a {@link List} of group names
*/
private List<String> getGroups(String server, List<String> excludedGroups, boolean includeGlobal) {
if (excludedGroups == null) {
excludedGroups = new ArrayList<>();
}
excludedGroups.add(getObjectName());
List<String> groups = new ArrayList<>();
if (server == null || server.equals("")) {
server = "global";
}
/*
Priority:
1. server specific group nodes
2. group nodes
*/
final Map<String, Boolean> serverSpecificGroups = new HashMap<>();
final Map<String, Boolean> groupNodes = new HashMap<>();
// Sorts the permissions and puts them into a priority order
for (Map.Entry<String, Boolean> node : convertTemporaryPerms().entrySet()) {
serverSpecific:
if (node.getKey().contains("/")) {
String[] parts = Patterns.SERVER_SPLIT.split(node.getKey(), 2);
if (parts[0].equalsIgnoreCase("global")) {
// REGULAR
break serverSpecific;
}
if (!parts[0].equalsIgnoreCase(server)) {
// SERVER SPECIFIC BUT DOES NOT APPLY
continue;
}
if (Patterns.GROUP_MATCH.matcher(parts[1]).matches()) {
// SERVER SPECIFIC AND GROUP
serverSpecificGroups.put(node.getKey(), node.getValue());
continue;
}
continue;
}
// Skip adding global permissions if they are not requested
if (!includeGlobal) continue;
if (Patterns.GROUP_MATCH.matcher(node.getKey()).matches()) {
// GROUP
groupNodes.put(node.getKey(), node.getValue());
}
}
// If a group is negated at a higher priority, the group should not then be applied at a lower priority
serverSpecificGroups.entrySet().stream().filter(node -> !node.getValue()).forEach(node -> {
groupNodes.remove(node.getKey());
groupNodes.remove(Patterns.SERVER_SPLIT.split(node.getKey(), 2)[1]);
});
groups.addAll(serverSpecificGroups.entrySet().stream()
.filter(Map.Entry::getValue)
.map(e -> Patterns.DOT_SPLIT.split(e.getKey(), 2)[1])
.collect(Collectors.toList())
);
groups.addAll(groupNodes.entrySet().stream()
.filter(Map.Entry::getValue)
.map(e -> Patterns.DOT_SPLIT.split(e.getKey(), 2)[1])
.collect(Collectors.toList())
);
return groups;
private List<String> getGroups(String server, String world, boolean includeGlobal) {
// Call super #getPermissions method, and just sort through those
Map<String, Boolean> perms = getPermissions(server, world, null, includeGlobal);
return perms.keySet().stream()
.filter(s -> Patterns.GROUP_MATCH.matcher(s).matches())
.map(s -> Patterns.DOT_SPLIT.split(s, 2)[1])
.collect(Collectors.toList());
}
}