From c4497db06c8fc8b37ce3ca8834f30e5604fd33c5 Mon Sep 17 00:00:00 2001 From: Luck Date: Fri, 26 Aug 2016 16:58:47 +0100 Subject: [PATCH] progress towards permissionservice --- .../luckperms/core/PermissionHolder.java | 2 +- .../service/wrapping/LuckPermsSubject.java | 387 ++++++++++++++---- 2 files changed, 299 insertions(+), 90 deletions(-) diff --git a/common/src/main/java/me/lucko/luckperms/core/PermissionHolder.java b/common/src/main/java/me/lucko/luckperms/core/PermissionHolder.java index 8bcc927b..03198de4 100644 --- a/common/src/main/java/me/lucko/luckperms/core/PermissionHolder.java +++ b/common/src/main/java/me/lucko/luckperms/core/PermissionHolder.java @@ -547,7 +547,7 @@ public abstract class PermissionHolder { return !toExpire.isEmpty(); } - private Map convertTemporaryPerms() { + public Map convertTemporaryPerms() { auditTemporaryPermissions(); Map nodes = new HashMap<>(); diff --git a/sponge/src/main/java/me/lucko/luckperms/service/wrapping/LuckPermsSubject.java b/sponge/src/main/java/me/lucko/luckperms/service/wrapping/LuckPermsSubject.java index a5663004..a30dd1d5 100644 --- a/sponge/src/main/java/me/lucko/luckperms/service/wrapping/LuckPermsSubject.java +++ b/sponge/src/main/java/me/lucko/luckperms/service/wrapping/LuckPermsSubject.java @@ -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 getCommandSource() { - if (holder instanceof User) { - final UUID uuid = ((User) holder).getUuid(); + if (enduringData.getHolder() instanceof User) { + final UUID uuid = ((User) enduringData.getHolder()).getUuid(); Optional 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 applyContexts(@NonNull Set set) { - final Map map = new HashMap<>(); - - String world = null; - String server = null; - - Map 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 local = holder.getLocalPermissions(server, world, null, service.getPossiblePermissions()); - perms: - for (Map.Entry 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 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 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 contexts, @NonNull String node) { + return null; + /* final Map 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 getParents(@NonNull Set contexts) { - final Set parents = new HashSet<>(); - final Map nodes = applyContexts(contexts); - - for (Map.Entry 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 getActiveContexts() { return SubjectData.GLOBAL_CONTEXT; } + + @AllArgsConstructor + public static class EnduringData implements SubjectData { + private final LuckPermsService service; + + @Getter + private final PermissionHolder holder; + + @Override + public Map, Map> getAllPermissions() { + Map nodes = holder.convertTemporaryPerms(); + Map, Map> permissions = new HashMap<>(); + + for (Map.Entry 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 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 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 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 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 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())) { + permissions.put(new HashSet<>(), new HashMap<>()); + } + permissions.get(new HashSet()).put(node, e.getValue()); + } + } + } + + return permissions; + } + + @Override + public Map getPermissions(Set set) { + return getAllPermissions().getOrDefault(set, Collections.emptyMap()); + } + + @Override + public boolean setPermission(Set set, String s, Tristate tristate) { + return false; + } + + @Override + public boolean clearPermissions() { + return false; + } + + @Override + public boolean clearPermissions(Set set) { + return false; + } + + @Override + public Map, List> getAllParents() { + return null; + } + + @Override + public List getParents(Set contexts) { + final Set parents = new HashSet<>(); + final Map nodes = applyContexts(contexts); + + for (Map.Entry 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 set, Subject subject) { + return false; + } + + @Override + public boolean removeParent(Set set, Subject subject) { + return false; + } + + @Override + public boolean clearParents() { + return false; + } + + @Override + public boolean clearParents(Set set) { + return false; + } + + private Map applyContexts(@NonNull Set set) { + final Map map = new HashMap<>(); + + String world = null; + String server = null; + + Map 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 local = holder.getLocalPermissions(server, world, null, service.getPossiblePermissions()); + perms: + for (Map.Entry 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 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 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; + } + } }