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;
}
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);
}
}
}
}
}
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);
}
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;
}
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;
}
Aggregations