Search in sources :

Example 1 with NodeModel

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

the class SqlDao method getGroupsWithPermission.

@Override
public List<HeldPermission<String>> getGroupsWithPermission(String permission) throws SQLException {
    List<HeldPermission<String>> held = new ArrayList<>();
    try (Connection c = this.provider.getConnection()) {
        try (PreparedStatement ps = c.prepareStatement(this.prefix.apply(GROUP_PERMISSIONS_SELECT_PERMISSION))) {
            ps.setString(1, permission);
            try (ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    String holder = rs.getString("name");
                    boolean value = rs.getBoolean("value");
                    String server = rs.getString("server");
                    String world = rs.getString("world");
                    long expiry = rs.getLong("expiry");
                    String contexts = rs.getString("contexts");
                    NodeModel data = deserializeNode(permission, value, server, world, expiry, contexts);
                    held.add(NodeHeldPermission.of(holder, data));
                }
            }
        }
    }
    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) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 2 with NodeModel

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

the class SqlDao method saveGroup.

@Override
public void saveGroup(Group group) throws SQLException {
    group.getIoLock().lock();
    try {
        // Empty data, just delete.
        if (group.getEnduringNodes().isEmpty()) {
            try (Connection c = this.provider.getConnection()) {
                try (PreparedStatement ps = c.prepareStatement(this.prefix.apply(GROUP_PERMISSIONS_DELETE))) {
                    ps.setString(1, group.getName());
                    ps.execute();
                }
            }
            return;
        }
        // Get a snapshot of current data
        Set<NodeModel> remote = new HashSet<>();
        try (Connection c = this.provider.getConnection()) {
            try (PreparedStatement ps = c.prepareStatement(this.prefix.apply(GROUP_PERMISSIONS_SELECT))) {
                ps.setString(1, group.getName());
                try (ResultSet rs = ps.executeQuery()) {
                    while (rs.next()) {
                        String permission = rs.getString("permission");
                        boolean value = rs.getBoolean("value");
                        String server = rs.getString("server");
                        String world = rs.getString("world");
                        long expiry = rs.getLong("expiry");
                        String contexts = rs.getString("contexts");
                        remote.add(deserializeNode(permission, value, server, world, expiry, contexts));
                    }
                }
            }
        }
        Set<NodeModel> local = group.getEnduringNodes().values().stream().map(NodeModel::fromNode).collect(Collectors.toSet());
        Map.Entry<Set<NodeModel>, Set<NodeModel>> diff = compareSets(local, remote);
        Set<NodeModel> toAdd = diff.getKey();
        Set<NodeModel> toRemove = diff.getValue();
        if (!toRemove.isEmpty()) {
            try (Connection c = this.provider.getConnection()) {
                try (PreparedStatement ps = c.prepareStatement(this.prefix.apply(GROUP_PERMISSIONS_DELETE_SPECIFIC))) {
                    for (NodeModel nd : toRemove) {
                        ps.setString(1, group.getName());
                        ps.setString(2, nd.getPermission());
                        ps.setBoolean(3, nd.getValue());
                        ps.setString(4, nd.getServer());
                        ps.setString(5, nd.getWorld());
                        ps.setLong(6, nd.getExpiry());
                        ps.setString(7, this.gson.toJson(ContextSetJsonSerializer.serializeContextSet(nd.getContexts())));
                        ps.addBatch();
                    }
                    ps.executeBatch();
                }
            }
        }
        if (!toAdd.isEmpty()) {
            try (Connection c = this.provider.getConnection()) {
                try (PreparedStatement ps = c.prepareStatement(this.prefix.apply(GROUP_PERMISSIONS_INSERT))) {
                    for (NodeModel nd : toAdd) {
                        ps.setString(1, group.getName());
                        ps.setString(2, nd.getPermission());
                        ps.setBoolean(3, nd.getValue());
                        ps.setString(4, nd.getServer());
                        ps.setString(5, nd.getWorld());
                        ps.setLong(6, nd.getExpiry());
                        ps.setString(7, this.gson.toJson(ContextSetJsonSerializer.serializeContextSet(nd.getContexts())));
                        ps.addBatch();
                    }
                    ps.executeBatch();
                }
            }
        }
    } finally {
        group.getIoLock().unlock();
    }
}
Also used : NodeModel(me.lucko.luckperms.common.node.NodeModel) HashSet(java.util.HashSet) ResultSet(java.sql.ResultSet) Set(java.util.Set) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Map(java.util.Map) HashSet(java.util.HashSet)

Example 3 with NodeModel

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

the class SqlDao method saveUser.

@Override
public void saveUser(User user) throws SQLException {
    user.getIoLock().lock();
    try {
        // Empty data - just delete from the DB.
        if (!this.plugin.getUserManager().shouldSave(user)) {
            try (Connection c = this.provider.getConnection()) {
                try (PreparedStatement ps = c.prepareStatement(this.prefix.apply(USER_PERMISSIONS_DELETE))) {
                    ps.setString(1, user.getUuid().toString());
                    ps.execute();
                }
                try (PreparedStatement ps = c.prepareStatement(this.prefix.apply(PLAYER_UPDATE_PRIMARY_GROUP_BY_UUID))) {
                    ps.setString(1, NodeFactory.DEFAULT_GROUP_NAME);
                    ps.setString(2, user.getUuid().toString());
                    ps.execute();
                }
            }
            return;
        }
        // Get a snapshot of current data.
        Set<NodeModel> remote = new HashSet<>();
        try (Connection c = this.provider.getConnection()) {
            try (PreparedStatement ps = c.prepareStatement(this.prefix.apply(USER_PERMISSIONS_SELECT))) {
                ps.setString(1, user.getUuid().toString());
                try (ResultSet rs = ps.executeQuery()) {
                    while (rs.next()) {
                        String permission = rs.getString("permission");
                        boolean value = rs.getBoolean("value");
                        String server = rs.getString("server");
                        String world = rs.getString("world");
                        long expiry = rs.getLong("expiry");
                        String contexts = rs.getString("contexts");
                        remote.add(deserializeNode(permission, value, server, world, expiry, contexts));
                    }
                }
            }
        }
        Set<NodeModel> local = user.getEnduringNodes().values().stream().map(NodeModel::fromNode).collect(Collectors.toSet());
        Map.Entry<Set<NodeModel>, Set<NodeModel>> diff = compareSets(local, remote);
        Set<NodeModel> toAdd = diff.getKey();
        Set<NodeModel> toRemove = diff.getValue();
        if (!toRemove.isEmpty()) {
            try (Connection c = this.provider.getConnection()) {
                try (PreparedStatement ps = c.prepareStatement(this.prefix.apply(USER_PERMISSIONS_DELETE_SPECIFIC))) {
                    for (NodeModel nd : toRemove) {
                        ps.setString(1, user.getUuid().toString());
                        ps.setString(2, nd.getPermission());
                        ps.setBoolean(3, nd.getValue());
                        ps.setString(4, nd.getServer());
                        ps.setString(5, nd.getWorld());
                        ps.setLong(6, nd.getExpiry());
                        ps.setString(7, this.gson.toJson(ContextSetJsonSerializer.serializeContextSet(nd.getContexts())));
                        ps.addBatch();
                    }
                    ps.executeBatch();
                }
            }
        }
        if (!toAdd.isEmpty()) {
            try (Connection c = this.provider.getConnection()) {
                try (PreparedStatement ps = c.prepareStatement(this.prefix.apply(USER_PERMISSIONS_INSERT))) {
                    for (NodeModel nd : toAdd) {
                        ps.setString(1, user.getUuid().toString());
                        ps.setString(2, nd.getPermission());
                        ps.setBoolean(3, nd.getValue());
                        ps.setString(4, nd.getServer());
                        ps.setString(5, nd.getWorld());
                        ps.setLong(6, nd.getExpiry());
                        ps.setString(7, this.gson.toJson(ContextSetJsonSerializer.serializeContextSet(nd.getContexts())));
                        ps.addBatch();
                    }
                    ps.executeBatch();
                }
            }
        }
        try (Connection c = this.provider.getConnection()) {
            boolean hasPrimaryGroupSaved;
            try (PreparedStatement ps = c.prepareStatement(this.prefix.apply(PLAYER_SELECT_PRIMARY_GROUP_BY_UUID))) {
                ps.setString(1, user.getUuid().toString());
                try (ResultSet rs = ps.executeQuery()) {
                    hasPrimaryGroupSaved = rs.next();
                }
            }
            if (hasPrimaryGroupSaved) {
                // update
                try (PreparedStatement ps = c.prepareStatement(this.prefix.apply(PLAYER_UPDATE_PRIMARY_GROUP_BY_UUID))) {
                    ps.setString(1, user.getPrimaryGroup().getStoredValue().orElse(NodeFactory.DEFAULT_GROUP_NAME));
                    ps.setString(2, user.getUuid().toString());
                    ps.execute();
                }
            } else {
                // insert
                try (PreparedStatement ps = c.prepareStatement(this.prefix.apply(PLAYER_INSERT))) {
                    ps.setString(1, user.getUuid().toString());
                    ps.setString(2, user.getName().orElse("null"));
                    ps.setString(3, user.getPrimaryGroup().getStoredValue().orElse(NodeFactory.DEFAULT_GROUP_NAME));
                    ps.execute();
                }
            }
        }
    } finally {
        user.getIoLock().unlock();
    }
}
Also used : NodeModel(me.lucko.luckperms.common.node.NodeModel) HashSet(java.util.HashSet) ResultSet(java.sql.ResultSet) Set(java.util.Set) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Map(java.util.Map) HashSet(java.util.HashSet)

Example 4 with NodeModel

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

the class ConfigurateDao method readAttributes.

private static Collection<NodeModel> readAttributes(ConfigurationNode attributes, String permission) {
    boolean value = attributes.getNode("value").getBoolean(true);
    String server = attributes.getNode("server").getString("global");
    String world = attributes.getNode("world").getString("global");
    long expiry = attributes.getNode("expiry").getLong(0L);
    ImmutableContextSet context = ImmutableContextSet.empty();
    ConfigurationNode contextMap = attributes.getNode("context");
    if (!contextMap.isVirtual() && contextMap.hasMapChildren()) {
        context = ContextSetConfigurateSerializer.deserializeContextSet(contextMap).makeImmutable();
    }
    ConfigurationNode batchAttribute = attributes.getNode("permissions");
    if (permission.startsWith("luckperms.batch") && !batchAttribute.isVirtual() && batchAttribute.hasListChildren()) {
        List<NodeModel> nodes = new ArrayList<>();
        for (ConfigurationNode element : batchAttribute.getChildrenList()) {
            nodes.add(NodeModel.of(element.getString(), value, server, world, expiry, context));
        }
        return nodes;
    } else {
        return Collections.singleton(NodeModel.of(permission, value, server, world, expiry, context));
    }
}
Also used : NodeModel(me.lucko.luckperms.common.node.NodeModel) ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) SimpleConfigurationNode(ninja.leaping.configurate.SimpleConfigurationNode) ArrayList(java.util.ArrayList) ImmutableContextSet(me.lucko.luckperms.api.context.ImmutableContextSet)

Example 5 with NodeModel

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

the class ConfigurateDao method createAndLoadGroup.

@Override
public Group createAndLoadGroup(String name) throws Exception {
    Group group = this.plugin.getGroupManager().getOrMake(name);
    group.getIoLock().lock();
    try {
        ConfigurationNode object = readFile(StorageLocation.GROUP, name);
        if (object != null) {
            Set<Node> nodes = readNodes(object).stream().map(NodeModel::toNode).collect(Collectors.toSet());
            group.setEnduringNodes(nodes);
        } else {
            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, name, data);
        }
    } catch (Exception e) {
        throw reportException(name, e);
    } finally {
        group.getIoLock().unlock();
    }
    group.getRefreshBuffer().requestDirectly();
    return group;
}
Also used : Group(me.lucko.luckperms.common.model.Group) NodeModel(me.lucko.luckperms.common.node.NodeModel) 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) IOException(java.io.IOException)

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