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

feat(api): add allow and disallow method for controlling permissi… #8

Merged
merged 1 commit into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions api/src/main/java/net/luxcube/minecraft/server/PteroServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.luxcube.minecraft.server.status.StatusType;
import net.luxcube.minecraft.server.usage.ServerUsage;
import net.luxcube.minecraft.user.PteroUser;
import org.jetbrains.annotations.NotNull;

import java.util.UUID;
Expand Down Expand Up @@ -65,6 +66,20 @@ public interface PteroServer {
*/
CompletableFuture<ServerUsage> getUsage();

/**
* Allow the user to change the state of the server.
* @param pteroUser The user that will change the state of the server.
* @return A future of the completable state.
*/
CompletableFuture<Void> allow(@NotNull PteroUser pteroUser);

/**
* Deny the user to change the state of the server.
* @param pteroUser The user that will change the state of the server.
* @return A future of the completable state.
*/
CompletableFuture<Void> disallow(@NotNull PteroUser pteroUser);

/**
* Execute the start command of the server.
* @return A future of the completable server.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public static void severe(String message) {
LOGGER.error(message);
}

public static void severe(String message, Object... args) {
LOGGER.error(message, args);
}

public static void debug(String message) {
LOGGER.debug(message);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package net.luxcube.minecraft.server;

import com.mattmalec.pterodactyl4j.Permission;
import com.mattmalec.pterodactyl4j.client.entities.ClientServer;
import com.mattmalec.pterodactyl4j.client.entities.ClientSubuser;
import com.mattmalec.pterodactyl4j.client.entities.Utilization;
import com.mattmalec.pterodactyl4j.exceptions.NotFoundException;
import com.mattmalec.pterodactyl4j.exceptions.PteroException;
import net.luxcube.minecraft.exception.ServerDoesntExistException;
import net.luxcube.minecraft.logger.PteroLogger;
import net.luxcube.minecraft.server.status.StatusType;
import net.luxcube.minecraft.server.usage.ServerUsage;
import net.luxcube.minecraft.server.usage.ServerUsageImpl;
import net.luxcube.minecraft.user.PteroUser;
import net.luxcube.minecraft.util.Pair;
import net.luxcube.minecraft.util.Try;
import net.luxcube.minecraft.vo.PteroBridgeVO;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -134,6 +139,99 @@ public CompletableFuture<ServerUsage> getUsage() {
});
}

@Override
public CompletableFuture<Void> allow(@NotNull PteroUser pteroUser) {
PteroLogger.debug("Allowing user %s to access server %s", pteroUser.getId(), identifier);

return CompletableFuture.supplyAsync(() -> {
return bridge.getClient()
.retrieveServerByIdentifier(identifier)
.execute();
}, bridge.getWorker()).thenApply(clientServer -> {
Try<ClientSubuser> catching = Try.catching(() -> {
return clientServer.retrieveSubuser(pteroUser.getUniqueId())
.execute();
});

catching.catching(PteroException.class, e -> {
throw new ServerDoesntExistException(identifier);
});

return new Pair<>(clientServer, catching.unwrap());
}).thenAccept(pair -> {
ClientSubuser user = pair.second();

if (user.hasPermission(Permission.CONTROL_PERMISSIONS)) {
PteroLogger.debug("User %s already has permission to access server %s", pteroUser.getId(), identifier);
return;
}

Permission[] permissions = new Permission[user.getPermissions().size() + Permission.CONTROL_PERMISSIONS.length];

// Clone permissions
System.arraycopy(user.getPermissions().toArray(), 0, permissions, 0, user.getPermissions().size());

// Add control permissions
System.arraycopy(Permission.CONTROL_PERMISSIONS, 0, permissions, user.getPermissions().size(), Permission.CONTROL_PERMISSIONS.length);

ClientServer server = pair.first();
server.getSubuserManager()
.editUser(user)
.setPermissions(permissions)
.executeAsync();

PteroLogger.debug("User %s now has permission to access server %s", pteroUser.getId(), identifier);
});
}

@Override
public CompletableFuture<Void> disallow(@NotNull PteroUser pteroUser) {
return CompletableFuture.supplyAsync(() -> {
return bridge.getClient()
.retrieveServerByIdentifier(identifier)
.execute();
}, bridge.getWorker()).thenApply(clientServer -> {
Try<ClientSubuser> catching = Try.catching(() -> {
return clientServer.retrieveSubuser(pteroUser.getUniqueId())
.execute();
});

catching.catching(PteroException.class, e -> {
throw new ServerDoesntExistException(identifier);
});

return new Pair<>(clientServer, catching.unwrap());
}).thenAccept(pair -> {
ClientSubuser user = pair.second();

if (!user.hasPermission(Permission.CONTROL_PERMISSIONS)) {
PteroLogger.debug("User %s already doesn't have permission to access server %s", pteroUser.getId(), identifier);
return;
}

Permission[] permissions = user.getPermissions()
.toArray(new Permission[0]);

// Remove control permissions
for (Permission permission : Permission.CONTROL_PERMISSIONS) {
for (int i = 0; i < permissions.length; i++) {
if (permissions[i] == permission) {
permissions[i] = null;
break;
}
}
}

ClientServer server = pair.first();
server.getSubuserManager()
.editUser(user)
.setPermissions(permissions)
.executeAsync();

PteroLogger.debug("User %s now doesn't have permission to access server %s", pteroUser.getId(), identifier);
});
}

@Override
public CompletableFuture<Void> start() {
PteroLogger.debug("Starting server %s", identifier);
Expand Down