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