private void populateMap(ImmutableMap.Builder<String, Boolean> values, String prefix, Node currentNode) { if (currentNode.value != Tristate.UNDEFINED) { values.put(prefix, currentNode.value.asBoolean()); } for (Map.Entry<String, Node> ent : currentNode.children.entrySet()) { populateMap(values, prefix + '.' + ent.getKey(), ent.getValue()); } }
/** * Create a new node tree with the given values, and the specified root * fallback value. * * @param values The values to be contained in this node tree * @param defaultValue The fallback value for any completely undefined nodes * @return The newly created node tree */ public static NodeTree of(Map<String, Boolean> values, Tristate defaultValue) { NodeTree newTree = new NodeTree(defaultValue); for (Map.Entry<String, Boolean> value : values.entrySet()) { Iterable<String> parts = NODE_SPLITTER.split(value.getKey().toLowerCase()); Node currentNode = newTree.rootNode; for (String part : parts) { if (currentNode.children.containsKey(part)) { currentNode = currentNode.children.get(part); } else { Node newNode = new Node(new HashMap<>()); currentNode.children.put(part, newNode); currentNode = newNode; } } currentNode.value = Tristate.fromBoolean(value.getValue()); } return newTree; }
/** * Test whether the subject is permitted to perform an action corresponding * to the given permission string. * * <p>This must return the same boolean equivalent as * {@link #getPermissionValue(Set, String)}.</p> * * @param contexts The set of contexts that represents the subject's current * environment * @param permission The permission string * @return True if permission is granted */ default boolean hasPermission(Set<Context> contexts, String permission) { return getPermissionValue(contexts, permission).asBoolean(); }
/** * Create a new node tree with the given values, and the specified root fallback value. * * @param values The values to be contained in this node tree * @param defaultValue The fallback value for any completely undefined nodes * @return The newly created node tree */ public static NodeTree of(Map<String, Boolean> values, Tristate defaultValue) { NodeTree newTree = new NodeTree(defaultValue); for (Map.Entry<String, Boolean> value : values.entrySet()) { String[] parts = SPLIT_REGEX.split(value.getKey().toLowerCase()); Node currentNode = newTree.rootNode; for (String part : parts) { if (currentNode.children.containsKey(part)) { currentNode = currentNode.children.get(part); } else { Node newNode = new Node(new HashMap<>()); currentNode.children.put(part, newNode); currentNode = newNode; } } currentNode.value = Tristate.fromBoolean(value.getValue()); } return newTree; }
@Override public CompletableFuture<Boolean> setPermission(Set<Context> contexts, String permission, Tristate value) { contexts = ImmutableSet.copyOf(contexts); while (true) { NodeTree oldTree = this.permissions.get(contexts); if (oldTree != null && oldTree.get(permission) == value) { return CompletableFuture.completedFuture(false); } if (oldTree == null && value != Tristate.UNDEFINED) { if (this.permissions.putIfAbsent(contexts, NodeTree.of(ImmutableMap.of(permission, value.asBoolean()))) == null) { break; } } else { if (oldTree == null || this.permissions.replace(contexts, oldTree, oldTree.withValue(permission, value))) { break; } } } return CompletableFuture.completedFuture(true); }
public void unprotect(Player player) { //notify BungeeCord plugins for the login channel.sendTo(player, buf -> buf.writeUTF(LOGIN_ACTION)); ProtectionData data = protections.remove(player.getUniqueId()); if (data == null) { return; } //teleport if (config.getGeneral().getTeleport().isEnabled()) { safeTeleport(player, data.getOldLocation()); } //restore permissions SubjectData subjectData = player.getTransientSubjectData(); Map<Set<Context>, Map<String, Boolean>> oldPermissions = data.getPermissions(); for (Entry<Set<Context>, Map<String, Boolean>> permission : oldPermissions.entrySet()) { Set<Context> context = permission.getKey(); for (Entry<String, Boolean> perm : permission.getValue().entrySet()) { subjectData.setPermission(context, perm.getKey(), Tristate.fromBoolean(perm.getValue())); } } }
@Override public boolean hasPermission(Set<Context> contexts, String permission) { return getPermissionValue(contexts, permission).asBoolean(); }
private void populateMap(ImmutableMap.Builder<String, Boolean> values, String prefix, Node currentNode) { if (currentNode.value != Tristate.UNDEFINED) { values.put(prefix, currentNode.value.asBoolean()); } for (Map.Entry<String, Node> ent : currentNode.children.entrySet()) { populateMap(values, prefix + '.' + ent.getKey(), ent.getValue()); } }
/** * Test whether the subject is permitted to perform an action corresponding to the * given permission string. This must return the same boolean equivalent as * {@link #getPermissionValue(Set, String)}. * * @param contexts The set of contexts that represents the subject's current environment * @param permission The permission string * @return True if permission is granted */ default boolean hasPermission(Set<Context> contexts, String permission) { return getPermissionValue(contexts, permission).asBoolean(); }
@Override public Map<Subject, Boolean> getAllWithPermission(Set<Context> contexts, String permission) { final ImmutableMap.Builder<Subject, Boolean> ret = ImmutableMap.builder(); for (PEXSubject subject : subjectCache.asMap().values()) { Tristate permissionValue = subject.getPermissionValue(contexts == null ? subject.getActiveContexts() : contexts, permission); if (permissionValue != Tristate.UNDEFINED) { ret.put(subject, permissionValue.asBoolean()); } } return ret.build(); }
@Override public boolean setPermission(Set<Context> contexts, String permission, Tristate value) { contexts = ImmutableSet.copyOf(contexts); while (true) { NodeTree oldTree = this.permissions.get(contexts); if (oldTree != null && oldTree.get(permission) == value) { return false; } if (oldTree == null && value != Tristate.UNDEFINED) { if (this.permissions.putIfAbsent(contexts, NodeTree.of(ImmutableMap.of(permission, value.asBoolean()))) == null) { break; } } else { if (oldTree == null || this.permissions.replace(contexts, oldTree, oldTree.withValue(permission, value))) { break; } } } return true; }