Search in sources :

Example 21 with Node

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));
}
Also used : ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) JsonObject(com.google.gson.JsonObject) TimeoutException(java.util.concurrent.TimeoutException) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) CreationCause(net.luckperms.api.event.cause.CreationCause) Message(me.lucko.luckperms.common.locale.Message) ArrayList(java.util.ArrayList) JsonElement(com.google.gson.JsonElement) HashSet(java.util.HashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Locale(java.util.Locale) Map(java.util.Map) Node(net.luckperms.api.node.Node) LuckPermsPlugin(me.lucko.luckperms.common.plugin.LuckPermsPlugin) HolderType(me.lucko.luckperms.common.model.HolderType) ExecutorService(java.util.concurrent.ExecutorService) ImmutableSet(com.google.common.collect.ImmutableSet) NodeJsonSerializer(me.lucko.luckperms.common.node.utils.NodeJsonSerializer) Uuids(me.lucko.luckperms.common.util.Uuids) Set(java.util.Set) Sender(me.lucko.luckperms.common.sender.Sender) UUID(java.util.UUID) Executors(java.util.concurrent.Executors) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) JsonArray(com.google.gson.JsonArray) Track(me.lucko.luckperms.common.model.Track) List(java.util.List) CompletableFutures(me.lucko.luckperms.common.util.CompletableFutures) Group(me.lucko.luckperms.common.model.Group) Collections(java.util.Collections) User(me.lucko.luckperms.common.model.User) DataType(net.luckperms.api.model.data.DataType) HashSet(java.util.HashSet) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) ArrayList(java.util.ArrayList) List(java.util.List) UUID(java.util.UUID) ExecutionException(java.util.concurrent.ExecutionException) HashSet(java.util.HashSet) TimeoutException(java.util.concurrent.TimeoutException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) HashMap(java.util.HashMap) Map(java.util.Map)

Example 22 with Node

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);
    }
}
Also used : JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) Node(net.luckperms.api.node.Node) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) UUID(java.util.UUID) HolderType(me.lucko.luckperms.common.model.HolderType)

Example 23 with Node

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));
    }
}
Also used : JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) Node(net.luckperms.api.node.Node) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) UUID(java.util.UUID) HashMap(java.util.HashMap) Map(java.util.Map)

Example 24 with Node

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);
}
Also used : Group(me.lucko.luckperms.common.model.Group) Node(net.luckperms.api.node.Node) ArrayList(java.util.ArrayList) PermissionHolder(me.lucko.luckperms.common.model.PermissionHolder)

Example 25 with Node

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();
}
Also used : Pattern(java.util.regex.Pattern) ImmutableList(com.google.common.collect.ImmutableList) Node(net.luckperms.api.node.Node) RegexPermission(me.lucko.luckperms.common.node.types.RegexPermission) Map(java.util.Map) TristateResult(me.lucko.luckperms.common.cacheddata.result.TristateResult)

Aggregations

Node (net.luckperms.api.node.Node)74 InheritanceNode (net.luckperms.api.node.types.InheritanceNode)28 ArrayList (java.util.ArrayList)23 Map (java.util.Map)16 UUID (java.util.UUID)16 ImmutableContextSet (net.luckperms.api.context.ImmutableContextSet)13 NodeEntry (me.lucko.luckperms.common.storage.misc.NodeEntry)12 MetaNode (net.luckperms.api.node.types.MetaNode)12 Group (me.lucko.luckperms.common.model.Group)11 HashMap (java.util.HashMap)10 List (java.util.List)10 PermissionHolder (me.lucko.luckperms.common.model.PermissionHolder)9 Difference (me.lucko.luckperms.common.util.Difference)9 DataType (net.luckperms.api.model.data.DataType)9 Connection (java.sql.Connection)7 User (me.lucko.luckperms.common.model.User)7 MutableContextSet (net.luckperms.api.context.MutableContextSet)7 PrefixNode (net.luckperms.api.node.types.PrefixNode)7 SuffixNode (net.luckperms.api.node.types.SuffixNode)7 IOException (java.io.IOException)6