Search in sources :

Example 16 with NodeModel

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

the class MongoDao method nodesFromDoc.

private static List<NodeModel> nodesFromDoc(Document document) {
    List<NodeModel> nodes = new ArrayList<>();
    // legacy
    if (document.containsKey("perms") && document.get("perms") instanceof Map) {
        // noinspection unchecked
        Map<String, Boolean> permsMap = (Map<String, Boolean>) document.get("perms");
        for (Map.Entry<String, Boolean> e : permsMap.entrySet()) {
            // legacy permission key deserialisation
            String permission = e.getKey().replace("[**DOT**]", ".").replace("[**DOLLAR**]", "$");
            nodes.add(NodeModel.fromNode(LegacyNodeFactory.fromLegacyString(permission, e.getValue())));
        }
    }
    // new format
    if (document.containsKey("permissions") && document.get("permissions") instanceof List) {
        // noinspection unchecked
        List<Document> permsList = (List<Document>) document.get("permissions");
        for (Document d : permsList) {
            nodes.add(nodeFromDoc(d));
        }
    }
    return nodes;
}
Also used : NodeModel(me.lucko.luckperms.common.node.NodeModel) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Document(org.bson.Document) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 17 with NodeModel

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

the class MongoDao method applyBulkUpdate.

@Override
public void applyBulkUpdate(BulkUpdate bulkUpdate) {
    if (bulkUpdate.getDataType().isIncludingUsers()) {
        MongoCollection<Document> c = this.database.getCollection(this.prefix + "users");
        try (MongoCursor<Document> cursor = c.find().iterator()) {
            while (cursor.hasNext()) {
                Document d = cursor.next();
                UUID uuid = d.get("_id", UUID.class);
                Set<NodeModel> nodes = new HashSet<>(nodesFromDoc(d));
                Set<NodeModel> results = nodes.stream().map(bulkUpdate::apply).filter(Objects::nonNull).collect(Collectors.toSet());
                if (!nodes.equals(results)) {
                    List<Document> newNodes = results.stream().map(MongoDao::nodeToDoc).collect(Collectors.toList());
                    d.append("permissions", newNodes).remove("perms");
                    c.replaceOne(new Document("_id", uuid), d);
                }
            }
        }
    }
    if (bulkUpdate.getDataType().isIncludingGroups()) {
        MongoCollection<Document> c = this.database.getCollection(this.prefix + "groups");
        try (MongoCursor<Document> cursor = c.find().iterator()) {
            while (cursor.hasNext()) {
                Document d = cursor.next();
                String holder = d.getString("_id");
                Set<NodeModel> nodes = new HashSet<>(nodesFromDoc(d));
                Set<NodeModel> results = nodes.stream().map(bulkUpdate::apply).filter(Objects::nonNull).collect(Collectors.toSet());
                if (!nodes.equals(results)) {
                    List<Document> newNodes = results.stream().map(MongoDao::nodeToDoc).collect(Collectors.toList());
                    d.append("permissions", newNodes).remove("perms");
                    c.replaceOne(new Document("_id", holder), d);
                }
            }
        }
    }
}
Also used : NodeModel(me.lucko.luckperms.common.node.NodeModel) Document(org.bson.Document) UUID(java.util.UUID) HashSet(java.util.HashSet)

Example 18 with NodeModel

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

the class SqlDao method loadGroup.

@Override
public Optional<Group> loadGroup(String name) throws SQLException {
    // Check the group actually exists
    List<String> groups = new ArrayList<>();
    try (Connection c = this.provider.getConnection()) {
        try (PreparedStatement ps = c.prepareStatement(this.prefix.apply(GROUP_SELECT_ALL))) {
            try (ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    groups.add(rs.getString("name").toLowerCase());
                }
            }
        }
    }
    if (!groups.contains(name)) {
        return Optional.empty();
    }
    Group group = this.plugin.getGroupManager().getOrMake(name);
    group.getIoLock().lock();
    try {
        List<NodeModel> data = new ArrayList<>();
        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");
                        data.add(deserializeNode(permission, value, server, world, expiry, contexts));
                    }
                }
            }
        }
        if (!data.isEmpty()) {
            Set<Node> nodes = data.stream().map(NodeModel::toNode).collect(Collectors.toSet());
            group.setEnduringNodes(nodes);
        } else {
            group.clearNodes();
        }
    } finally {
        group.getIoLock().unlock();
    }
    group.getRefreshBuffer().requestDirectly();
    return Optional.of(group);
}
Also used : Group(me.lucko.luckperms.common.model.Group) Node(me.lucko.luckperms.api.Node) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) NodeModel(me.lucko.luckperms.common.node.NodeModel) ResultSet(java.sql.ResultSet)

Example 19 with NodeModel

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

the class SqlDao method loadUser.

@Override
public User loadUser(UUID uuid, String username) throws SQLException {
    User user = this.plugin.getUserManager().getOrMake(UserIdentifier.of(uuid, username));
    user.getIoLock().lock();
    try {
        List<NodeModel> data = new ArrayList<>();
        String primaryGroup = null;
        String userName = null;
        // Collect user permissions
        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");
                        data.add(deserializeNode(permission, value, server, world, expiry, contexts));
                    }
                }
            }
        }
        // Collect user meta (username & primary group)
        try (Connection c = this.provider.getConnection()) {
            try (PreparedStatement ps = c.prepareStatement(this.prefix.apply(PLAYER_SELECT_BY_UUID))) {
                ps.setString(1, user.getUuid().toString());
                try (ResultSet rs = ps.executeQuery()) {
                    if (rs.next()) {
                        userName = rs.getString("username");
                        primaryGroup = rs.getString("primary_group");
                    }
                }
            }
        }
        // update username & primary group
        if (primaryGroup == null) {
            primaryGroup = NodeFactory.DEFAULT_GROUP_NAME;
        }
        user.getPrimaryGroup().setStoredValue(primaryGroup);
        // Update their username to what was in the storage if the one in the local instance is null
        user.setName(userName, true);
        // If the user has any data in storage
        if (!data.isEmpty()) {
            Set<Node> nodes = data.stream().map(NodeModel::toNode).collect(Collectors.toSet());
            user.setEnduringNodes(nodes);
            // Save back to the store if data they were given any defaults or had permissions expire
            if (this.plugin.getUserManager().giveDefaultIfNeeded(user, false) | user.auditTemporaryPermissions()) {
                // This should be fine, as the lock will be acquired by the same thread.
                saveUser(user);
            }
        } else {
            // User has no data in storage.
            if (this.plugin.getUserManager().shouldSave(user)) {
                user.clearNodes();
                user.getPrimaryGroup().setStoredValue(null);
                this.plugin.getUserManager().giveDefaultIfNeeded(user, false);
            }
        }
    } finally {
        user.getIoLock().unlock();
    }
    user.getRefreshBuffer().requestDirectly();
    return user;
}
Also used : NodeModel(me.lucko.luckperms.common.node.NodeModel) User(me.lucko.luckperms.common.model.User) Node(me.lucko.luckperms.api.Node) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 20 with NodeModel

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

the class SqlDao method getUsersWithPermission.

@Override
public List<HeldPermission<UUID>> getUsersWithPermission(String permission) throws SQLException {
    List<HeldPermission<UUID>> held = new ArrayList<>();
    try (Connection c = this.provider.getConnection()) {
        try (PreparedStatement ps = c.prepareStatement(this.prefix.apply(USER_PERMISSIONS_SELECT_PERMISSION))) {
            ps.setString(1, permission);
            try (ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    UUID holder = UUID.fromString(rs.getString("uuid"));
                    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) UUID(java.util.UUID)

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