Files
LuckPerms/common/src/main/java/me/lucko/luckperms/users/User.java
T

282 lines
9.1 KiB
Java
Raw Normal View History

2016-05-22 01:57:10 +01:00
package me.lucko.luckperms.users;
import lombok.Getter;
import lombok.Setter;
import me.lucko.luckperms.LuckPermsPlugin;
import me.lucko.luckperms.exceptions.ObjectAlreadyHasException;
2016-07-15 22:44:25 +01:00
import me.lucko.luckperms.exceptions.ObjectLacksException;
2016-05-22 01:57:10 +01:00
import me.lucko.luckperms.groups.Group;
import me.lucko.luckperms.utils.Patterns;
2016-05-22 01:57:10 +01:00
import me.lucko.luckperms.utils.PermissionObject;
2016-06-22 17:24:30 +01:00
import java.util.*;
import java.util.stream.Collectors;
2016-05-22 01:57:10 +01:00
public abstract class User extends PermissionObject {
/**
* The users Mojang UUID
*/
@Getter
private final UUID uuid;
/**
* The last known username of a player
*/
@Getter
@Setter
private String name;
2016-06-24 18:38:05 +01:00
/**
* The users primary group
*/
@Getter
@Setter
private String primaryGroup = null;
2016-06-23 13:29:51 +01:00
User(UUID uuid, LuckPermsPlugin plugin) {
2016-05-22 01:57:10 +01:00
super(plugin, uuid.toString());
this.uuid = uuid;
this.name = null;
}
2016-06-23 13:29:51 +01:00
User(UUID uuid, String name, LuckPermsPlugin plugin) {
2016-05-22 01:57:10 +01:00
super(plugin, uuid.toString());
this.uuid = uuid;
this.name = name;
}
/**
* Refresh and re-assign the users permissions
*/
public abstract void refreshPermissions();
/**
* Check to see if the user is a member of a group
* @param group The group to check membership of
* @return true if the user is a member of the group
*/
public boolean isInGroup(Group group) {
return isInGroup(group, "global");
}
/**
* 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
* @return true if the user is a member of the group
*/
public boolean isInGroup(Group group, String server) {
return hasPermission("group." + group.getName(), true, server);
2016-05-22 01:57:10 +01:00
}
/**
* Add a user to a group
* @param group The group to add the user to
* @throws ObjectAlreadyHasException if the user is already a member of the group
*/
public void addGroup(Group group) throws ObjectAlreadyHasException {
addGroup(group, "global");
}
/**
* 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
* @throws ObjectAlreadyHasException if the user is already a member of the group on that server
*/
public void addGroup(Group group, String server) throws ObjectAlreadyHasException {
if (server == null) {
server = "global";
}
setPermission("group." + group.getName(), true, server);
2016-05-22 01:57:10 +01:00
}
2016-07-19 12:54:39 +01:00
/**
* Add a user to a group on a specific server
* @param group The group to add the user to
* @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, long expireAt) throws ObjectAlreadyHasException {
setPermission("group." + group.getName(), true, 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 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, long expireAt) throws ObjectAlreadyHasException {
if (server == null) {
server = "global";
}
setPermission("group." + group.getName(), true, server, expireAt);
}
2016-05-22 01:57:10 +01:00
/**
* Remove the user from a group
* @param group the group to remove the user from
2016-07-15 22:44:25 +01:00
* @throws ObjectLacksException if the user isn't a member of the group
2016-05-22 01:57:10 +01:00
*/
2016-07-15 22:44:25 +01:00
public void removeGroup(Group group) throws ObjectLacksException {
2016-05-22 01:57:10 +01:00
removeGroup(group, "global");
}
2016-07-19 12:54:39 +01:00
/**
* Remove the user from a group
* @param group the group to remove the user from
* @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, boolean temporary) throws ObjectLacksException {
removeGroup(group, "global", temporary);
}
2016-05-22 01:57:10 +01:00
/**
* Remove the user from a group
* @param group The group to remove the user from
* @param server The server to remove the group on
2016-07-15 22:44:25 +01:00
* @throws ObjectLacksException if the user isn't a member of the group
2016-05-22 01:57:10 +01:00
*/
2016-07-15 22:44:25 +01:00
public void removeGroup(Group group, String server) throws ObjectLacksException {
2016-05-22 01:57:10 +01:00
if (server == null) {
server = "global";
}
unsetPermission("group." + group.getName(), server);
2016-05-22 01:57:10 +01:00
}
2016-07-19 12:54:39 +01:00
/**
* 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 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, boolean temporary) throws ObjectLacksException {
if (server == null) {
server = "global";
}
unsetPermission("group." + group.getName(), server, temporary);
}
2016-05-22 01:57:10 +01:00
/**
* Clear all of the users permission nodes
*/
public void clearNodes() {
String defaultGroupNode = getPlugin().getConfiguration().getDefaultGroupNode();
getNodes().clear();
getNodes().put(defaultGroupNode, true);
}
/**
* Get a {@link List} of all of the groups the user is a member of, on all servers
* @return a {@link List} of group names
*/
public List<String> getGroupNames() {
2016-06-22 17:24:30 +01:00
return getGroups(null, null, true);
2016-05-22 01:57:10 +01:00
}
/**
* 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) {
2016-06-22 17:24:30 +01:00
return getGroups(server, null, false);
2016-05-22 01:57:10 +01:00
}
/**
* 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
2016-06-22 17:24:30 +01:00
* @param excludedGroups groups to exclude (prevents circular inheritance issues)
2016-05-22 01:57:10 +01:00
* @param includeGlobal Whether to include global groups
* @return a {@link List} of group names
*/
2016-06-22 17:24:30 +01:00
private List<String> getGroups(String server, List<String> excludedGroups, boolean includeGlobal) {
if (excludedGroups == null) {
excludedGroups = new ArrayList<>();
}
excludedGroups.add(getObjectName());
2016-05-22 01:57:10 +01:00
List<String> groups = new ArrayList<>();
if (server == null || server.equals("")) {
server = "global";
}
2016-06-22 17:24:30 +01:00
/*
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
2016-07-19 12:54:39 +01:00
for (Map.Entry<String, Boolean> node : convertTemporaryPerms().entrySet()) {
2016-06-22 17:24:30 +01:00
serverSpecific:
if (node.getKey().contains("/")) {
2016-07-19 12:54:39 +01:00
String[] parts = Patterns.SERVER_SPLIT.split(node.getKey(), 2);
2016-06-22 17:24:30 +01:00
if (parts[0].equalsIgnoreCase("global")) {
// REGULAR
break serverSpecific;
}
if (!parts[0].equalsIgnoreCase(server)) {
// SERVER SPECIFIC BUT DOES NOT APPLY
2016-05-22 01:57:10 +01:00
continue;
}
2016-06-22 17:24:30 +01:00
if (Patterns.GROUP_MATCH.matcher(parts[1]).matches()) {
2016-06-22 17:24:30 +01:00
// SERVER SPECIFIC AND GROUP
serverSpecificGroups.put(node.getKey(), node.getValue());
2016-05-22 01:57:10 +01:00
continue;
}
2016-06-22 17:24:30 +01:00
continue;
2016-05-22 01:57:10 +01:00
}
2016-06-22 17:24:30 +01:00
// Skip adding global permissions if they are not requested
if (!includeGlobal) continue;
if (Patterns.GROUP_MATCH.matcher(node.getKey()).matches()) {
2016-06-22 17:24:30 +01:00
// GROUP
groupNodes.put(node.getKey(), node.getValue());
2016-05-22 01:57:10 +01:00
}
2016-06-22 17:24:30 +01:00
2016-05-22 01:57:10 +01:00
}
2016-06-22 17:24:30 +01:00
// 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]);
2016-06-22 17:24:30 +01:00
});
groups.addAll(serverSpecificGroups.entrySet().stream()
.filter(Map.Entry::getValue)
2016-07-15 22:44:25 +01:00
.map(e -> Patterns.DOT_SPLIT.split(e.getKey(), 2)[1])
.collect(Collectors.toList())
);
groups.addAll(groupNodes.entrySet().stream()
.filter(Map.Entry::getValue)
2016-07-15 22:44:25 +01:00
.map(e -> Patterns.DOT_SPLIT.split(e.getKey(), 2)[1])
.collect(Collectors.toList())
);
2016-05-22 01:57:10 +01:00
return groups;
}
@Override
public String toString() {
return getUuid().toString();
}
}