Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Route for getting all PermissionHolders with a specific Permission #14

Open
Carlos17Kopra opened this issue Jan 7, 2024 · 0 comments

Comments

@Carlos17Kopra
Copy link

Carlos17Kopra commented Jan 7, 2024

Hello there.
I cant help myself, I added another Route :'D
This Routes gets all PermissionHolders with a specific Permission and Value (optional)
The Routes are:

/<Type>/has/{perm}
/<Type>/has/{perm}/{value}

What did I do:

First, I added the two Methods to the PermissionHolderController:

// GET /<type>/has/{permission}
void getHasPermSingle(Context ctx) throws Exception;

// GET <type>/has/{permission}/{value}
void getHasPermValue(Context ctx) throws Exception;

Here are my Implementations of the Methods
UserController

// GET /user/has/{permission}
@Override
public void getHasPermSingle(Context ctx) throws Exception {
    CompletableFuture<Set<UUID>> future = this.userManager.getUniqueUsers();
    String param = ctx.pathParam("perm");
    boolean value = true;
    ctx.future(future);
    this.hasPerm(ctx, future.get(), param, value);
}

// GET /user/has/{permission}/{value}
@Override
public void getHasPermValue(Context ctx) throws Exception {
    CompletableFuture<Set<UUID>> future = this.userManager.getUniqueUsers();
    String param = ctx.pathParam("perm");
    boolean value = Boolean.parseBoolean(ctx.pathParam("value"));;
    ctx.future(future);
    this.hasPerm(ctx, future.get(), param, value);
}

private void hasPerm(Context ctx, Set<UUID> uuids, String perm, boolean value) throws Exception {
    List<UUID> matchingUUIDs = new ArrayList<>();
    for(UUID uuid : uuids){
        CompletableFuture<User> loadFuture = loadUserCached(uuid);
        ctx.future(loadFuture);
        User user = loadFuture.get();
        if(user == null) continue;
        Collection<PermissionNode> permNodes = user.getNodes(NodeType.PERMISSION);
        if(permNodes.stream().anyMatch(node -> node.getPermission().equals(perm) && node.getValue() == value)){
            matchingUUIDs.add(uuid);
        }
    }
    ctx.json(matchingUUIDs);
}

GroupController

// GET /group/has/{perm}
@Override
public void getHasPermSingle(Context ctx) {
    String perm = ctx.pathParam("perm");
    this.hasPerm(ctx, perm, true);
}
// GET /group/has/{perm}/{value}
@Override
public void getHasPermValue(Context ctx) {
    String perm = ctx.pathParam("perm");
    boolean value = Boolean.parseBoolean(ctx.pathParam("value"));
    this.hasPerm(ctx, perm, value);
}

private void hasPerm(Context ctx, String perm, boolean value){
    CompletableFuture<List<String>> future = loadGroupsCached()
            .thenApply(groups -> groups.stream()
                    .filter(group -> {
                        Collection<PermissionNode> nodes = group.getNodes(NodeType.PERMISSION);
                        return nodes.stream().anyMatch(node -> node.getPermission().equals(perm) && node.getValue() == value);
                    })
                    .map(Group::getName)
                    .collect(Collectors.toList())
            );
    ctx.future(future);
}

Finally I added the Routes in the RestServer Class:

path("has", () -> {
    path("{perm}", () -> {
        get(controller::getHasPermSingle);
        path("{value}", () -> {
            get(controller::getHasPermValue);
        });
    });
});

This would be a cool Feature for the official API too and im open for questions or optimizations :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant