use of net.luckperms.api.node.Node in project LuckPerms by lucko.
the class Importer method run.
@Override
public void run() {
long startTime = System.currentTimeMillis();
this.notify.forEach(Message.IMPORT_START::send);
// start an update task in the background - we'll #join this later
CompletableFuture<Void> updateTask = CompletableFuture.runAsync(() -> this.plugin.getSyncTaskBuffer().requestDirectly());
this.notify.forEach(s -> Message.IMPORT_INFO.send(s, "Reading data..."));
Map<String, Set<Node>> groups = new HashMap<>();
Map<String, List<String>> tracks = new HashMap<>();
Map<UUID, UserData> users = new HashMap<>();
if (this.data.has("knownPermissions")) {
this.notify.forEach(s -> Message.IMPORT_INFO.send(s, "The data appears to be from a web editor upload - attempting to recover from it"));
parseWebEditorData(groups, tracks, users);
} else {
parseExportData(groups, tracks, users);
}
this.notify.forEach(s -> Message.IMPORT_INFO.send(s, "Waiting for initial update task to complete..."));
// join the update task future before scheduling command executions
updateTask.join();
this.notify.forEach(s -> Message.IMPORT_INFO.send(s, "Setting up data processor..."));
// create a threadpool for the processing
ExecutorService executor = Executors.newFixedThreadPool(16, new ThreadFactoryBuilder().setNameFormat("luckperms-importer-%d").build());
// A set of futures, which are really just the processes we need to wait for.
Set<CompletableFuture<Void>> futures = new HashSet<>();
int total = 0;
AtomicInteger processedCount = new AtomicInteger(0);
for (Map.Entry<String, Set<Node>> group : groups.entrySet()) {
futures.add(CompletableFuture.completedFuture(group).thenAcceptAsync(ent -> {
processGroup(ent.getKey(), ent.getValue());
processedCount.incrementAndGet();
}, executor));
total++;
}
for (Map.Entry<String, List<String>> track : tracks.entrySet()) {
futures.add(CompletableFuture.completedFuture(track).thenAcceptAsync(ent -> {
processTrack(ent.getKey(), ent.getValue());
processedCount.incrementAndGet();
}, executor));
total++;
}
for (Map.Entry<UUID, UserData> user : users.entrySet()) {
futures.add(CompletableFuture.completedFuture(user).thenAcceptAsync(ent -> {
processUser(ent.getKey(), ent.getValue());
processedCount.incrementAndGet();
}, executor));
total++;
}
// all of the threads have been scheduled now and are running. we just need to wait for them all to complete
CompletableFuture<Void> overallFuture = CompletableFutures.allOf(futures);
this.notify.forEach(s -> Message.IMPORT_INFO.send(s, "All data entries have been processed and scheduled for import - now waiting for the execution to complete."));
while (true) {
try {
overallFuture.get(2, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException e) {
// abnormal error - just break
e.printStackTrace();
break;
} catch (TimeoutException e) {
// still executing - send a progress report and continue waiting
sendProgress(processedCount.get(), total);
continue;
}
// process is complete
break;
}
executor.shutdown();
long endTime = System.currentTimeMillis();
double seconds = (endTime - startTime) / 1000.0;
this.notify.forEach(s -> Message.IMPORT_END_COMPLETE.send(s, seconds));
}
use of net.luckperms.api.node.Node in project LuckPerms by lucko.
the class Importer method parseWebEditorData.
private void parseWebEditorData(Map<String, Set<Node>> groups, Map<String, List<String>> tracks, Map<UUID, UserData> users) {
JsonArray holdersArray = this.data.get("permissionHolders").getAsJsonArray();
for (JsonElement holderElement : holdersArray) {
JsonObject jsonData = holderElement.getAsJsonObject();
HolderType type = HolderType.valueOf(jsonData.get("type").getAsString().toUpperCase(Locale.ROOT));
String id = jsonData.get("id").getAsString();
if (type == HolderType.GROUP) {
groups.put(id, NodeJsonSerializer.deserializeNodes(jsonData.get("nodes").getAsJsonArray()));
} else {
UUID uuid = UUID.fromString(id);
String username = null;
String displayName = jsonData.get("displayName").getAsString();
if (!Uuids.PREDICATE.test(displayName)) {
username = displayName;
}
Set<Node> nodes = NodeJsonSerializer.deserializeNodes(jsonData.get("nodes").getAsJsonArray());
users.put(uuid, new UserData(username, null, nodes));
}
}
JsonArray tracksArray = this.data.get("tracks").getAsJsonArray();
for (JsonElement trackElement : tracksArray) {
JsonObject jsonData = trackElement.getAsJsonObject();
String name = jsonData.get("id").getAsString();
JsonArray trackGroups = jsonData.get("groups").getAsJsonArray();
List<String> trackGroupsList = new ArrayList<>();
trackGroups.forEach(g -> trackGroupsList.add(g.getAsString()));
tracks.put(name, trackGroupsList);
}
}
use of net.luckperms.api.node.Node in project LuckPerms by lucko.
the class Importer method parseExportData.
private void parseExportData(Map<String, Set<Node>> groups, Map<String, List<String>> tracks, Map<UUID, UserData> users) {
for (Map.Entry<String, JsonElement> group : getDataSection("groups")) {
groups.put(group.getKey(), NodeJsonSerializer.deserializeNodes(group.getValue().getAsJsonObject().get("nodes").getAsJsonArray()));
}
for (Map.Entry<String, JsonElement> track : getDataSection("tracks")) {
JsonArray trackGroups = track.getValue().getAsJsonObject().get("groups").getAsJsonArray();
List<String> trackGroupsList = new ArrayList<>();
trackGroups.forEach(g -> trackGroupsList.add(g.getAsString()));
tracks.put(track.getKey(), trackGroupsList);
}
for (Map.Entry<String, JsonElement> user : getDataSection("users")) {
JsonObject jsonData = user.getValue().getAsJsonObject();
UUID uuid = UUID.fromString(user.getKey());
String username = null;
String primaryGroup = null;
Set<Node> nodes = NodeJsonSerializer.deserializeNodes(jsonData.get("nodes").getAsJsonArray());
if (jsonData.has("username")) {
username = jsonData.get("username").getAsString();
}
if (jsonData.has("primaryGroup")) {
primaryGroup = jsonData.get("primaryGroup").getAsString();
}
users.put(uuid, new UserData(username, primaryGroup, nodes));
}
}
use of net.luckperms.api.node.Node in project LuckPerms by lucko.
the class HolderEditor method execute.
@Override
public void execute(LuckPermsPlugin plugin, Sender sender, T target, ArgumentList args, String label) {
if (ArgumentPermissions.checkViewPerms(plugin, sender, getPermission().get(), target) || ArgumentPermissions.checkGroup(plugin, sender, target, ImmutableContextSetImpl.EMPTY)) {
Message.COMMAND_NO_PERMISSION.send(sender);
return;
}
List<PermissionHolder> holders = new ArrayList<>();
// also include users who are a member of the group
if (target instanceof Group) {
Group group = (Group) target;
ConstraintNodeMatcher<Node> matcher = StandardNodeMatchers.key(Inheritance.key(group.getName()));
WebEditorRequest.includeMatchingUsers(holders, matcher, true, plugin);
}
// include the original holder too
holders.add(target);
// remove holders which the sender doesn't have perms to view
holders.removeIf(h -> ArgumentPermissions.checkViewPerms(plugin, sender, getPermission().get(), h) || ArgumentPermissions.checkGroup(plugin, sender, h, ImmutableContextSetImpl.EMPTY));
// they don't have perms to view any of them
if (holders.isEmpty()) {
Message.COMMAND_NO_PERMISSION.send(sender);
return;
}
Message.EDITOR_START.send(sender);
WebEditorSession.createAndOpen(holders, Collections.emptyList(), sender, label, plugin);
}
use of net.luckperms.api.node.Node in project LuckPerms by lucko.
the class RegexProcessor method refresh.
@Override
public void refresh() {
ImmutableList.Builder<Map.Entry<Pattern, TristateResult>> builder = ImmutableList.builder();
for (Map.Entry<String, Node> e : this.sourceMap.entrySet()) {
RegexPermission.Builder regexPerm = RegexPermission.parse(e.getKey());
if (regexPerm == null) {
continue;
}
Pattern pattern = regexPerm.build().getPattern().orElse(null);
if (pattern == null) {
continue;
}
TristateResult value = RESULT_FACTORY.result(e.getValue());
builder.add(Maps.immutableEntry(pattern, value));
}
this.regexPermissions = builder.build();
}
Aggregations