progress towards permissionservice

This commit is contained in:
Luck 2016-08-26 16:58:47 +01:00
parent 9873415303
commit c4497db06c
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
2 changed files with 299 additions and 90 deletions

View File

@ -547,7 +547,7 @@ public abstract class PermissionHolder {
return !toExpire.isEmpty();
}
private Map<String, Boolean> convertTemporaryPerms() {
public Map<String, Boolean> convertTemporaryPerms() {
auditTemporaryPermissions();
Map<String, Boolean> nodes = new HashMap<>();

View File

@ -22,7 +22,9 @@
package me.lucko.luckperms.service.wrapping;
import com.google.common.collect.Sets;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NonNull;
import me.lucko.luckperms.constants.Patterns;
import me.lucko.luckperms.core.PermissionHolder;
@ -41,20 +43,29 @@ import org.spongepowered.api.util.Tristate;
import java.util.*;
import java.util.stream.Collectors;
/**
* TODO
* this class is aids rn
*/
@AllArgsConstructor
public class LuckPermsSubject implements Subject {
private final PermissionHolder holder;
private final EnduringData enduringData;
private final LuckPermsService service;
public LuckPermsSubject(PermissionHolder holder, LuckPermsService service) {
this.enduringData = new EnduringData(service, holder);
this.service = service;
}
@Override
public String getIdentifier() {
return holder.getObjectName();
return enduringData.getHolder().getObjectName();
}
@Override
public Optional<CommandSource> getCommandSource() {
if (holder instanceof User) {
final UUID uuid = ((User) holder).getUuid();
if (enduringData.getHolder() instanceof User) {
final UUID uuid = ((User) enduringData.getHolder()).getUuid();
Optional<Player> p = Sponge.getServer().getPlayer(uuid);
if (p.isPresent()) {
@ -67,7 +78,7 @@ public class LuckPermsSubject implements Subject {
@Override
public SubjectCollection getContainingCollection() {
if (holder instanceof Group) {
if (enduringData.getHolder() instanceof Group) {
return service.getGroupSubjects();
} else {
return service.getUserSubjects();
@ -94,77 +105,10 @@ public class LuckPermsSubject implements Subject {
return getPermissionValue(getActiveContexts(), permission).asBoolean();
}
private Map<String, Boolean> applyContexts(@NonNull Set<Context> set) {
final Map<String, Boolean> map = new HashMap<>();
String world = null;
String server = null;
Map<String, String> contexts = new HashMap<>();
for (Context context : set) {
if (context.getType().equals(Context.WORLD_KEY)) {
world = context.getName();
continue;
}
if (context.getType().equals(LuckPermsService.SERVER_CONTEXT)) {
server = context.getName();
continue;
}
contexts.put(context.getType(), context.getName());
}
Map<String, Boolean> local = holder.getLocalPermissions(server, world, null, service.getPossiblePermissions());
perms:
for (Map.Entry<String, Boolean> e : local.entrySet()) {
if (!contexts.isEmpty()) {
if (!Patterns.NODE_CONTEXTS.matcher(e.getKey()).matches()) {
continue;
}
String[] parts = e.getKey().substring(1).split("\\)", 2);
// 0 = context, 1 = node
// Parse the context values from this node
Map<String, String> contextValues = new HashMap<>();
for (String s : parts[0].split("\\,")) {
if (!s.contains("=")) {
// Not valid
continue;
}
// contextKey=value
String[] con = s.split("\\=", 2);
contextValues.put(con[0], con[1]);
}
// Check that all of the requested contexts are met
for (Map.Entry<String, String> req : contexts.entrySet()) {
if (!contextValues.containsKey(e.getKey())) {
continue;
}
if (!contextValues.get(req.getKey()).equalsIgnoreCase(req.getValue())) {
// Not valid within the current contexts
continue perms;
}
}
// Passed all da tests.
map.put(parts[1], e.getValue());
} else {
map.put(e.getKey(), e.getValue());
}
}
return map;
}
@Override
public Tristate getPermissionValue(@NonNull Set<Context> contexts, @NonNull String node) {
return null;
/*
final Map<String, Boolean> nodes = applyContexts(contexts);
if (nodes.containsKey(node)) {
@ -172,6 +116,7 @@ public class LuckPermsSubject implements Subject {
} else {
return Tristate.UNDEFINED;
}
*/
}
@Override
@ -191,25 +136,289 @@ public class LuckPermsSubject implements Subject {
@Override
public List<Subject> getParents(@NonNull Set<Context> contexts) {
final Set<String> parents = new HashSet<>();
final Map<String, Boolean> nodes = applyContexts(contexts);
for (Map.Entry<String, Boolean> e : nodes.entrySet()) {
if (!e.getValue()) {
continue;
}
if (Patterns.GROUP_MATCH.matcher(e.getKey()).matches()) {
final String groupName = e.getKey().substring("group.".length());
parents.add(groupName);
}
}
return parents.stream().map(s -> service.getGroupSubjects().get(s)).collect(Collectors.toList());
return null;
}
@Override
public Set<Context> getActiveContexts() {
return SubjectData.GLOBAL_CONTEXT;
}
@AllArgsConstructor
public static class EnduringData implements SubjectData {
private final LuckPermsService service;
@Getter
private final PermissionHolder holder;
@Override
public Map<Set<Context>, Map<String, Boolean>> getAllPermissions() {
Map<String, Boolean> nodes = holder.convertTemporaryPerms();
Map<Set<Context>, Map<String, Boolean>> permissions = new HashMap<>();
for (Map.Entry<String, Boolean> e : nodes.entrySet()) {
String node = e.getKey();
if (node.contains("/")) {
// server and/or world specific
String[] parts = Patterns.SERVER_DELIMITER.split(node, 2);
// 0 = server+world 1 = node
node = parts[1];
String server = null;
String world = null;
if (parts[0].contains("-")) {
String[] serverParts = Patterns.WORLD_DELIMITER.split(parts[0], 2);
world = serverParts[0];
server = serverParts[1];
} else {
server = parts[0];
}
if (world == null) {
if (Patterns.NODE_CONTEXTS.matcher(node).matches()) {
// Has special context
Set<Context> c = Sets.newHashSet(new Context(LuckPermsService.SERVER_CONTEXT, server));
String[] contextParts = e.getKey().substring(1).split("\\)", 2);
// 0 = context, 1 = node
node = contextParts[1];
// Parse the context values from this node
for (String s : contextParts[0].split("\\,")) {
if (!s.contains("=")) {
// Not valid
continue;
}
// contextKey=value
String[] con = s.split("\\=", 2);
c.add(new Context(con[0], con[1]));
}
if (!permissions.containsKey(c)) {
permissions.put(c, new HashMap<>());
}
permissions.get(c).put(node, e.getValue());
} else {
// No special context
Set<Context> c = Sets.newHashSet(new Context(LuckPermsService.SERVER_CONTEXT, server));
if (!permissions.containsKey(c)) {
permissions.put(c, new HashMap<>());
}
permissions.get(c).put(node, e.getValue());
}
} else {
if (Patterns.NODE_CONTEXTS.matcher(node).matches()) {
// Has special context
Set<Context> c = Sets.newHashSet(new Context(Context.WORLD_KEY, world), new Context(LuckPermsService.SERVER_CONTEXT, server));
String[] contextParts = e.getKey().substring(1).split("\\)", 2);
// 0 = context, 1 = node
node = contextParts[1];
// Parse the context values from this node
for (String s : contextParts[0].split("\\,")) {
if (!s.contains("=")) {
// Not valid
continue;
}
// contextKey=value
String[] con = s.split("\\=", 2);
c.add(new Context(con[0], con[1]));
}
if (!permissions.containsKey(c)) {
permissions.put(c, new HashMap<>());
}
permissions.get(c).put(node, e.getValue());
} else {
// No special context
Set<Context> c = Sets.newHashSet(new Context(Context.WORLD_KEY, world), new Context(LuckPermsService.SERVER_CONTEXT, server));
if (!permissions.containsKey(c)) {
permissions.put(c, new HashMap<>());
}
permissions.get(c).put(node, e.getValue());
}
}
} else {
// Plain node
if (Patterns.NODE_CONTEXTS.matcher(e.getKey()).matches()) {
// Has special context
Set<Context> c = Sets.newHashSet();
String[] contextParts = e.getKey().substring(1).split("\\)", 2);
// 0 = context, 1 = node
node = contextParts[1];
// Parse the context values from this node
for (String s : contextParts[0].split("\\,")) {
if (!s.contains("=")) {
// Not valid
continue;
}
// contextKey=value
String[] con = s.split("\\=", 2);
c.add(new Context(con[0], con[1]));
}
if (!permissions.containsKey(c)) {
permissions.put(c, new HashMap<>());
}
permissions.get(c).put(node, e.getValue());
} else {
if (!permissions.containsKey(new HashSet<Context>())) {
permissions.put(new HashSet<>(), new HashMap<>());
}
permissions.get(new HashSet<Context>()).put(node, e.getValue());
}
}
}
return permissions;
}
@Override
public Map<String, Boolean> getPermissions(Set<Context> set) {
return getAllPermissions().getOrDefault(set, Collections.emptyMap());
}
@Override
public boolean setPermission(Set<Context> set, String s, Tristate tristate) {
return false;
}
@Override
public boolean clearPermissions() {
return false;
}
@Override
public boolean clearPermissions(Set<Context> set) {
return false;
}
@Override
public Map<Set<Context>, List<Subject>> getAllParents() {
return null;
}
@Override
public List<Subject> getParents(Set<Context> contexts) {
final Set<String> parents = new HashSet<>();
final Map<String, Boolean> nodes = applyContexts(contexts);
for (Map.Entry<String, Boolean> e : nodes.entrySet()) {
if (!e.getValue()) {
continue;
}
if (Patterns.GROUP_MATCH.matcher(e.getKey()).matches()) {
final String groupName = e.getKey().substring("group.".length());
parents.add(groupName);
}
}
return parents.stream().map(s -> service.getGroupSubjects().get(s)).collect(Collectors.toList());
}
@Override
public boolean addParent(Set<Context> set, Subject subject) {
return false;
}
@Override
public boolean removeParent(Set<Context> set, Subject subject) {
return false;
}
@Override
public boolean clearParents() {
return false;
}
@Override
public boolean clearParents(Set<Context> set) {
return false;
}
private Map<String, Boolean> applyContexts(@NonNull Set<Context> set) {
final Map<String, Boolean> map = new HashMap<>();
String world = null;
String server = null;
Map<String, String> contexts = new HashMap<>();
for (Context context : set) {
if (context.getType().equals(Context.WORLD_KEY)) {
world = context.getName();
continue;
}
if (context.getType().equals(LuckPermsService.SERVER_CONTEXT)) {
server = context.getName();
continue;
}
contexts.put(context.getType(), context.getName());
}
Map<String, Boolean> local = holder.getLocalPermissions(server, world, null, service.getPossiblePermissions());
perms:
for (Map.Entry<String, Boolean> e : local.entrySet()) {
if (!contexts.isEmpty()) {
if (!Patterns.NODE_CONTEXTS.matcher(e.getKey()).matches()) {
continue;
}
String[] parts = e.getKey().substring(1).split("\\)", 2);
// 0 = context, 1 = node
// Parse the context values from this node
Map<String, String> contextValues = new HashMap<>();
for (String s : parts[0].split("\\,")) {
if (!s.contains("=")) {
// Not valid
continue;
}
// contextKey=value
String[] con = s.split("\\=", 2);
contextValues.put(con[0], con[1]);
}
// Check that all of the requested contexts are met
for (Map.Entry<String, String> req : contexts.entrySet()) {
if (!contextValues.containsKey(e.getKey())) {
continue;
}
if (!contextValues.get(req.getKey()).equalsIgnoreCase(req.getValue())) {
// Not valid within the current contexts
continue perms;
}
}
// Passed all da tests.
map.put(parts[1], e.getValue());
} else {
map.put(e.getKey(), e.getValue());
}
}
return map;
}
}
}