Search in sources :

Example 11 with NodeModel

use of me.lucko.luckperms.common.node.NodeModel in project LuckPerms by lucko.

the class ConfigurateDao method saveUser.

@Override
public void saveUser(User user) throws Exception {
    user.getIoLock().lock();
    try {
        if (!this.plugin.getUserManager().shouldSave(user)) {
            saveFile(StorageLocation.USER, user.getUuid().toString(), null);
        } else {
            ConfigurationNode data = SimpleConfigurationNode.root();
            data.getNode("uuid").setValue(user.getUuid().toString());
            data.getNode("name").setValue(user.getName().orElse("null"));
            data.getNode(this instanceof JsonDao ? "primaryGroup" : "primary-group").setValue(user.getPrimaryGroup().getStoredValue().orElse(NodeFactory.DEFAULT_GROUP_NAME));
            Set<NodeModel> nodes = user.getEnduringNodes().values().stream().map(NodeModel::fromNode).collect(Collectors.toCollection(LinkedHashSet::new));
            writeNodes(data, nodes);
            saveFile(StorageLocation.USER, user.getUuid().toString(), data);
        }
    } catch (Exception e) {
        throw reportException(user.getUuid().toString(), e);
    } finally {
        user.getIoLock().unlock();
    }
}
Also used : NodeModel(me.lucko.luckperms.common.node.NodeModel) ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) SimpleConfigurationNode(ninja.leaping.configurate.SimpleConfigurationNode) IOException(java.io.IOException)

Example 12 with NodeModel

use of me.lucko.luckperms.common.node.NodeModel in project LuckPerms by lucko.

the class ConfigurateDao method saveGroup.

@Override
public void saveGroup(Group group) throws Exception {
    group.getIoLock().lock();
    try {
        ConfigurationNode data = SimpleConfigurationNode.root();
        data.getNode("name").setValue(group.getName());
        Set<NodeModel> nodes = group.getEnduringNodes().values().stream().map(NodeModel::fromNode).collect(Collectors.toCollection(LinkedHashSet::new));
        writeNodes(data, nodes);
        saveFile(StorageLocation.GROUP, group.getName(), data);
    } catch (Exception e) {
        throw reportException(group.getName(), e);
    } finally {
        group.getIoLock().unlock();
    }
}
Also used : NodeModel(me.lucko.luckperms.common.node.NodeModel) ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) SimpleConfigurationNode(ninja.leaping.configurate.SimpleConfigurationNode) IOException(java.io.IOException)

Example 13 with NodeModel

use of me.lucko.luckperms.common.node.NodeModel in project LuckPerms by lucko.

the class ConfigurateDao method applyBulkUpdate.

@Override
public void applyBulkUpdate(BulkUpdate bulkUpdate) throws Exception {
    if (bulkUpdate.getDataType().isIncludingUsers()) {
        File[] files = getDirectory(StorageLocation.USER).listFiles(getFileTypeFilter());
        if (files == null) {
            throw new IllegalStateException("Users directory matched no files.");
        }
        for (File file : files) {
            try {
                registerFileAction(StorageLocation.USER, file);
                ConfigurationNode object = readFile(file);
                Set<NodeModel> nodes = readNodes(object);
                Set<NodeModel> results = nodes.stream().map(bulkUpdate::apply).filter(Objects::nonNull).collect(Collectors.toSet());
                if (!nodes.equals(results)) {
                    writeNodes(object, results);
                    saveFile(file, object);
                }
            } catch (Exception e) {
                throw reportException(file.getName(), e);
            }
        }
    }
    if (bulkUpdate.getDataType().isIncludingGroups()) {
        File[] files = getDirectory(StorageLocation.GROUP).listFiles(getFileTypeFilter());
        if (files == null) {
            throw new IllegalStateException("Groups directory matched no files.");
        }
        for (File file : files) {
            try {
                registerFileAction(StorageLocation.GROUP, file);
                ConfigurationNode object = readFile(file);
                Set<NodeModel> nodes = readNodes(object);
                Set<NodeModel> results = nodes.stream().map(bulkUpdate::apply).filter(Objects::nonNull).collect(Collectors.toSet());
                if (!nodes.equals(results)) {
                    writeNodes(object, results);
                    saveFile(file, object);
                }
            } catch (Exception e) {
                throw reportException(file.getName(), e);
            }
        }
    }
}
Also used : NodeModel(me.lucko.luckperms.common.node.NodeModel) ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) SimpleConfigurationNode(ninja.leaping.configurate.SimpleConfigurationNode) File(java.io.File) IOException(java.io.IOException)

Example 14 with NodeModel

use of me.lucko.luckperms.common.node.NodeModel in project LuckPerms by lucko.

the class ConfigurateDao method writeNodes.

private static void writeNodes(ConfigurationNode to, Set<NodeModel> nodes) {
    ConfigurationNode permissionsSection = SimpleConfigurationNode.root();
    ConfigurationNode parentsSection = SimpleConfigurationNode.root();
    ConfigurationNode prefixesSection = SimpleConfigurationNode.root();
    ConfigurationNode suffixesSection = SimpleConfigurationNode.root();
    ConfigurationNode metaSection = SimpleConfigurationNode.root();
    for (NodeModel node : nodes) {
        Node n = node.toNode();
        // just add a string to the list.
        if (isPlain(node)) {
            if (n.isGroupNode()) {
                parentsSection.getAppendedNode().setValue(n.getGroupName());
                continue;
            }
            if (!MetaType.ANY.matches(n)) {
                permissionsSection.getAppendedNode().setValue(node.getPermission());
                continue;
            }
        }
        ChatMetaType chatMetaType = ChatMetaType.ofNode(n).orElse(null);
        if (chatMetaType != null && n.getValuePrimitive()) {
            // handle prefixes / suffixes
            Map.Entry<Integer, String> entry = chatMetaType.getEntry(n);
            ConfigurationNode attributes = SimpleConfigurationNode.root();
            attributes.getNode("priority").setValue(entry.getKey());
            writeAttributesTo(attributes, node, false);
            ConfigurationNode appended = SimpleConfigurationNode.root();
            appended.getNode(entry.getValue()).setValue(attributes);
            switch(chatMetaType) {
                case PREFIX:
                    prefixesSection.getAppendedNode().setValue(appended);
                    break;
                case SUFFIX:
                    suffixesSection.getAppendedNode().setValue(appended);
                    break;
                default:
                    throw new AssertionError();
            }
        } else if (n.isMeta() && n.getValuePrimitive()) {
            // handle meta nodes
            Map.Entry<String, String> meta = n.getMeta();
            ConfigurationNode attributes = SimpleConfigurationNode.root();
            attributes.getNode("value").setValue(meta.getValue());
            writeAttributesTo(attributes, node, false);
            ConfigurationNode appended = SimpleConfigurationNode.root();
            appended.getNode(meta.getKey()).setValue(attributes);
            metaSection.getAppendedNode().setValue(appended);
        } else if (n.isGroupNode() && n.getValuePrimitive()) {
            // handle group nodes
            ConfigurationNode attributes = SimpleConfigurationNode.root();
            writeAttributesTo(attributes, node, false);
            ConfigurationNode appended = SimpleConfigurationNode.root();
            appended.getNode(n.getGroupName()).setValue(attributes);
            parentsSection.getAppendedNode().setValue(appended);
        } else {
            // handle regular permissions and negated meta+prefixes+suffixes
            ConfigurationNode attributes = SimpleConfigurationNode.root();
            writeAttributesTo(attributes, node, true);
            ConfigurationNode appended = SimpleConfigurationNode.root();
            appended.getNode(n.getPermission()).setValue(attributes);
            permissionsSection.getAppendedNode().setValue(appended);
        }
    }
    if (permissionsSection.hasListChildren()) {
        to.getNode("permissions").setValue(permissionsSection);
    }
    if (parentsSection.hasListChildren()) {
        to.getNode("parents").setValue(parentsSection);
    }
    if (prefixesSection.hasListChildren()) {
        to.getNode("prefixes").setValue(prefixesSection);
    }
    if (suffixesSection.hasListChildren()) {
        to.getNode("suffixes").setValue(suffixesSection);
    }
    if (metaSection.hasListChildren()) {
        to.getNode("meta").setValue(metaSection);
    }
}
Also used : NodeModel(me.lucko.luckperms.common.node.NodeModel) LogEntry(me.lucko.luckperms.api.LogEntry) ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) SimpleConfigurationNode(ninja.leaping.configurate.SimpleConfigurationNode) ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) SimpleConfigurationNode(ninja.leaping.configurate.SimpleConfigurationNode) Node(me.lucko.luckperms.api.Node) ChatMetaType(me.lucko.luckperms.api.ChatMetaType) Map(java.util.Map)

Example 15 with NodeModel

use of me.lucko.luckperms.common.node.NodeModel in project LuckPerms by lucko.

the class MongoDao method getUsersWithPermission.

@Override
public List<HeldPermission<UUID>> getUsersWithPermission(String permission) {
    List<HeldPermission<UUID>> held = new ArrayList<>();
    MongoCollection<Document> c = this.database.getCollection(this.prefix + "users");
    try (MongoCursor<Document> cursor = c.find().iterator()) {
        while (cursor.hasNext()) {
            Document d = cursor.next();
            UUID holder = d.get("_id", UUID.class);
            Set<NodeModel> nodes = new HashSet<>(nodesFromDoc(d));
            for (NodeModel e : nodes) {
                if (!e.getPermission().equalsIgnoreCase(permission)) {
                    continue;
                }
                held.add(NodeHeldPermission.of(holder, e));
            }
        }
    }
    return held;
}
Also used : NodeModel(me.lucko.luckperms.common.node.NodeModel) HeldPermission(me.lucko.luckperms.api.HeldPermission) NodeHeldPermission(me.lucko.luckperms.common.node.NodeHeldPermission) ArrayList(java.util.ArrayList) Document(org.bson.Document) UUID(java.util.UUID) HashSet(java.util.HashSet)

Aggregations

NodeModel (me.lucko.luckperms.common.node.NodeModel)22 ArrayList (java.util.ArrayList)11 ConfigurationNode (ninja.leaping.configurate.ConfigurationNode)10 SimpleConfigurationNode (ninja.leaping.configurate.SimpleConfigurationNode)10 IOException (java.io.IOException)8 HashSet (java.util.HashSet)8 HeldPermission (me.lucko.luckperms.api.HeldPermission)7 Node (me.lucko.luckperms.api.Node)7 NodeHeldPermission (me.lucko.luckperms.common.node.NodeHeldPermission)7 Connection (java.sql.Connection)6 PreparedStatement (java.sql.PreparedStatement)6 ResultSet (java.sql.ResultSet)6 Map (java.util.Map)6 UUID (java.util.UUID)5 File (java.io.File)4 Set (java.util.Set)4 Group (me.lucko.luckperms.common.model.Group)4 ImmutableContextSet (me.lucko.luckperms.api.context.ImmutableContextSet)3 Document (org.bson.Document)3 List (java.util.List)2