Skip to content

Commit

Permalink
Implement new enum based permission system
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Dec 16, 2024
1 parent 6c84de7 commit 0334edd
Show file tree
Hide file tree
Showing 19 changed files with 127 additions and 187 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package com.soulfiremc.generator;

import net.fabricmc.api.ModInitializer;
import net.minecraft.FileUtil;
import net.minecraft.client.multiplayer.ClientPacketListener;

public class Main implements ModInitializer {
@Override
Expand All @@ -27,5 +29,7 @@ public void onInitialize() {
// Player.class;
// AbstractClientPlayer.class;
// LocalPlayer.class;
ClientPacketListener;
FileUtil
}
}
22 changes: 22 additions & 0 deletions proto/src/main/proto/soulfire/common.proto
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,25 @@ message SettingsNamespace {
string namespace = 1;
repeated SettingsEntry entries = 2;
}

enum GlobalPermission {
CREATE_INSTANCE = 0;
SUBSCRIBE_LOGS = 1;
SERVER_CONFIG = 2;
}

enum InstancePermission {
COMMAND_EXECUTION = 0;
COMMAND_COMPLETION = 1;
READ_INSTANCE = 2;
UPDATE_INSTANCE = 3;
DELETE_INSTANCE = 4;
CHANGE_INSTANCE_STATE = 5;
AUTHENTICATE_MC_ACCOUNT = 6;
CHECK_PROXY = 7;
DOWNLOAD_URL = 8;
UPLOAD_OBJECT_STORAGE = 9;
DOWNLOAD_OBJECT_STORAGE = 10;
DELETE_OBJECT_STORAGE = 11;
LIST_OBJECT_STORAGE = 12;
}
9 changes: 4 additions & 5 deletions proto/src/main/proto/soulfire/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,9 @@ message SettingsPage {
string iconId = 6;
}

message PermissionMessage {
string id = 1;
string description = 2;
bool granted = 3;
message GlobalPermissionState {
GlobalPermission globalPermission = 1;
bool granted = 2;
}

message ServerPlugin {
Expand All @@ -137,7 +136,7 @@ message ServerPlugin {

message ClientDataResponse {
string username = 1;
repeated PermissionMessage serverPermissions = 2;
repeated GlobalPermissionState serverPermissions = 2;
repeated ServerPlugin plugins = 3;
repeated SettingsPage settings = 5;
}
Expand Down
8 changes: 6 additions & 2 deletions proto/src/main/proto/soulfire/instance.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ option java_multiple_files = true;
package soulfire.v1;

import "soulfire/common.proto";
import "soulfire/config.proto";

message InstanceConfig {
repeated SettingsNamespace settings = 1;
Expand Down Expand Up @@ -50,6 +49,11 @@ message InstanceListResponse {
repeated Instance instances = 1;
}

message InstancePermissionState {
InstancePermission instancePermission = 1;
bool granted = 2;
}

message InstanceInfoRequest {
string id = 1;
}
Expand All @@ -58,7 +62,7 @@ message InstanceInfoResponse {
string friendlyName = 1;
InstanceConfig config = 2;
InstanceState state = 3;
repeated PermissionMessage instancePermissions = 4;
repeated InstancePermissionState instancePermissions = 4;
}

message InstanceUpdateFriendlyNameRequest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package com.soulfiremc.server.brigadier;

import com.soulfiremc.brigadier.ClientConsoleCommandSource;
import com.soulfiremc.server.user.Permission;
import com.soulfiremc.server.user.PermissionContext;
import com.soulfiremc.server.user.ServerCommandSource;
import net.kyori.adventure.util.TriState;

Expand All @@ -40,7 +40,7 @@ public String getUsername() {
}

@Override
public TriState getPermission(Permission.Context permission) {
public TriState getPermission(PermissionContext permission) {
return TriState.TRUE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import com.soulfiremc.grpc.generated.*;
import com.soulfiremc.server.ServerCommandManager;
import com.soulfiremc.server.user.Permissions;
import com.soulfiremc.server.user.PermissionContext;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import io.grpc.stub.StreamObserver;
Expand All @@ -39,7 +39,7 @@ public class CommandServiceImpl extends CommandServiceGrpc.CommandServiceImplBas
public void executeCommand(
CommandRequest request, StreamObserver<CommandResponse> responseObserver) {
var instanceId = UUID.fromString(request.getInstanceId());
ServerRPCConstants.USER_CONTEXT_KEY.get().hasPermissionOrThrow(Permissions.COMMAND_EXECUTION.context(instanceId));
ServerRPCConstants.USER_CONTEXT_KEY.get().hasPermissionOrThrow(PermissionContext.instance(InstancePermission.COMMAND_EXECUTION, instanceId));

try {
ServerCommandManager.putInstanceIds(List.of(instanceId));
Expand All @@ -58,7 +58,7 @@ public void tabCompleteCommand(
CommandCompletionRequest request,
StreamObserver<CommandCompletionResponse> responseObserver) {
var instanceId = UUID.fromString(request.getInstanceId());
ServerRPCConstants.USER_CONTEXT_KEY.get().hasPermissionOrThrow(Permissions.COMMAND_COMPLETION.context(instanceId));
ServerRPCConstants.USER_CONTEXT_KEY.get().hasPermissionOrThrow(PermissionContext.instance(InstancePermission.COMMAND_COMPLETION, instanceId));

try {
ServerCommandManager.putInstanceIds(List.of(instanceId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,28 @@
import com.soulfiremc.server.api.Plugin;
import com.soulfiremc.server.api.PluginInfo;
import com.soulfiremc.server.api.SoulFireAPI;
import com.soulfiremc.server.user.Permission;
import com.soulfiremc.server.user.Permissions;
import com.soulfiremc.server.user.PermissionContext;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import io.grpc.stub.StreamObserver;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import javax.inject.Inject;
import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Stream;

@Slf4j
@RequiredArgsConstructor(onConstructor_ = @Inject)
public class ConfigServiceImpl extends ConfigServiceGrpc.ConfigServiceImplBase {
private final SoulFireServer soulFireServer;

private Collection<PermissionMessage> getGlobalPermissions() {
private Collection<GlobalPermissionState> getGlobalPermissions() {
var user = ServerRPCConstants.USER_CONTEXT_KEY.get();
return Permissions.VALUES.stream().flatMap(p -> p instanceof Permission.Global global ? Stream.of(global) : Stream.empty()).map(permission -> PermissionMessage.newBuilder()
.setId(permission.id())
.setDescription(permission.description())
.setGranted(user.hasPermission(permission.context()))
return Arrays.stream(GlobalPermission.values())
.map(permission -> GlobalPermissionState.newBuilder()
.setGlobalPermission(permission)
.setGranted(user.hasPermission(PermissionContext.global(permission)))
.build())
.toList();
}
Expand All @@ -59,7 +58,7 @@ private Collection<ServerPlugin> getPlugins() {
@Override
public void getClientData(
ClientDataRequest request, StreamObserver<ClientDataResponse> responseObserver) {
ServerRPCConstants.USER_CONTEXT_KEY.get().hasPermissionOrThrow(Permissions.SERVER_CONFIG.context());
ServerRPCConstants.USER_CONTEXT_KEY.get().hasPermissionOrThrow(PermissionContext.global(GlobalPermission.SERVER_CONFIG));

try {
responseObserver.onNext(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import com.soulfiremc.server.plugins.ChatMessageLogger;
import com.soulfiremc.server.user.AuthSystem;
import com.soulfiremc.server.user.Permission;
import com.soulfiremc.server.user.PermissionContext;
import com.soulfiremc.server.user.ServerCommandSource;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.util.TriState;
Expand Down Expand Up @@ -50,7 +50,7 @@ public String getUsername() {
}

@Override
public TriState getPermission(Permission.Context permission) {
public TriState getPermission(PermissionContext permission) {
return TriState.TRUE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@
package com.soulfiremc.server.grpc;

import com.google.protobuf.ByteString;
import com.soulfiremc.grpc.generated.DownloadRequest;
import com.soulfiremc.grpc.generated.DownloadResponse;
import com.soulfiremc.grpc.generated.DownloadServiceGrpc;
import com.soulfiremc.grpc.generated.HeaderPair;
import com.soulfiremc.server.user.Permissions;
import com.soulfiremc.grpc.generated.*;
import com.soulfiremc.server.user.PermissionContext;
import com.soulfiremc.server.util.ReactorHttpHelper;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
Expand All @@ -37,7 +34,7 @@ public class DownloadServiceImpl extends DownloadServiceGrpc.DownloadServiceImpl
@Override
public void download(DownloadRequest request, StreamObserver<DownloadResponse> responseObserver) {
var instanceId = UUID.fromString(request.getInstanceId());
ServerRPCConstants.USER_CONTEXT_KEY.get().hasPermissionOrThrow(Permissions.DOWNLOAD_URL.context(instanceId));
ServerRPCConstants.USER_CONTEXT_KEY.get().hasPermissionOrThrow(PermissionContext.instance(InstancePermission.DOWNLOAD_URL, instanceId));

try {
var proxy = RPCUtils.convertProxy(request::hasProxy, request::getProxy);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,36 @@
import com.soulfiremc.server.SoulFireServer;
import com.soulfiremc.server.api.AttackLifecycle;
import com.soulfiremc.server.settings.lib.SettingsImpl;
import com.soulfiremc.server.user.Permission;
import com.soulfiremc.server.user.Permissions;
import com.soulfiremc.server.user.PermissionContext;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import io.grpc.stub.StreamObserver;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import javax.inject.Inject;
import java.util.Arrays;
import java.util.Collection;
import java.util.UUID;
import java.util.stream.Stream;

@Slf4j
@RequiredArgsConstructor(onConstructor_ = @Inject)
public class InstanceServiceImpl extends InstanceServiceGrpc.InstanceServiceImplBase {
private final SoulFireServer soulFireServer;

private Collection<PermissionMessage> getInstancePermissions(UUID instanceId) {
private Collection<InstancePermissionState> getInstancePermissions(UUID instanceId) {
var user = ServerRPCConstants.USER_CONTEXT_KEY.get();
return Permissions.VALUES.stream().flatMap(p -> p instanceof Permission.Instance instance ? Stream.of(instance) : Stream.empty()).map(permission -> PermissionMessage.newBuilder()
.setId(permission.id())
.setDescription(permission.description())
.setGranted(user.hasPermission(permission.context(instanceId)))
return Arrays.stream(InstancePermission.values())
.map(permission -> InstancePermissionState.newBuilder()
.setInstancePermission(permission)
.setGranted(user.hasPermission(PermissionContext.instance(permission, instanceId)))
.build())
.toList();
}

@Override
public void createInstance(InstanceCreateRequest request, StreamObserver<InstanceCreateResponse> responseObserver) {
ServerRPCConstants.USER_CONTEXT_KEY.get().hasPermissionOrThrow(Permissions.CREATE_INSTANCE.context());
ServerRPCConstants.USER_CONTEXT_KEY.get().hasPermissionOrThrow(PermissionContext.global(GlobalPermission.CREATE_INSTANCE));

try {
var id = soulFireServer.createInstance(request.getFriendlyName());
Expand All @@ -67,7 +66,7 @@ public void createInstance(InstanceCreateRequest request, StreamObserver<Instanc
@Override
public void deleteInstance(InstanceDeleteRequest request, StreamObserver<InstanceDeleteResponse> responseObserver) {
var instanceId = UUID.fromString(request.getId());
ServerRPCConstants.USER_CONTEXT_KEY.get().hasPermissionOrThrow(Permissions.DELETE_INSTANCE.context(instanceId));
ServerRPCConstants.USER_CONTEXT_KEY.get().hasPermissionOrThrow(PermissionContext.instance(InstancePermission.DELETE_INSTANCE, instanceId));

try {
var optionalDeletion = soulFireServer.deleteInstance(instanceId);
Expand All @@ -89,7 +88,7 @@ public void listInstances(InstanceListRequest request, StreamObserver<InstanceLi
try {
responseObserver.onNext(InstanceListResponse.newBuilder()
.addAllInstances(soulFireServer.instances().values().stream()
.filter(instance -> ServerRPCConstants.USER_CONTEXT_KEY.get().hasPermission(Permissions.READ_INSTANCE.context(instance.id())))
.filter(instance -> ServerRPCConstants.USER_CONTEXT_KEY.get().hasPermission(PermissionContext.instance(InstancePermission.READ_INSTANCE, instance.id())))
.map(InstanceManager::toProto)
.toList())
.build());
Expand All @@ -103,7 +102,7 @@ public void listInstances(InstanceListRequest request, StreamObserver<InstanceLi
@Override
public void getInstanceInfo(InstanceInfoRequest request, StreamObserver<InstanceInfoResponse> responseObserver) {
var instanceId = UUID.fromString(request.getId());
ServerRPCConstants.USER_CONTEXT_KEY.get().hasPermissionOrThrow(Permissions.READ_INSTANCE.context(instanceId));
ServerRPCConstants.USER_CONTEXT_KEY.get().hasPermissionOrThrow(PermissionContext.instance(InstancePermission.READ_INSTANCE, instanceId));

try {
var optionalInstance = soulFireServer.getInstance(instanceId);
Expand All @@ -128,7 +127,7 @@ public void getInstanceInfo(InstanceInfoRequest request, StreamObserver<Instance
@Override
public void updateInstanceFriendlyName(InstanceUpdateFriendlyNameRequest request, StreamObserver<InstanceUpdateFriendlyNameResponse> responseObserver) {
var instanceId = UUID.fromString(request.getId());
ServerRPCConstants.USER_CONTEXT_KEY.get().hasPermissionOrThrow(Permissions.UPDATE_INSTANCE.context(instanceId));
ServerRPCConstants.USER_CONTEXT_KEY.get().hasPermissionOrThrow(PermissionContext.instance(InstancePermission.UPDATE_INSTANCE, instanceId));

try {
var optionalInstance = soulFireServer.getInstance(instanceId);
Expand All @@ -150,7 +149,7 @@ public void updateInstanceFriendlyName(InstanceUpdateFriendlyNameRequest request
@Override
public void updateInstanceConfig(InstanceUpdateConfigRequest request, StreamObserver<InstanceUpdateConfigResponse> responseObserver) {
var instanceId = UUID.fromString(request.getId());
ServerRPCConstants.USER_CONTEXT_KEY.get().hasPermissionOrThrow(Permissions.UPDATE_INSTANCE.context(instanceId));
ServerRPCConstants.USER_CONTEXT_KEY.get().hasPermissionOrThrow(PermissionContext.instance(InstancePermission.UPDATE_INSTANCE, instanceId));

try {
var optionalInstance = soulFireServer.getInstance(instanceId);
Expand All @@ -172,7 +171,7 @@ public void updateInstanceConfig(InstanceUpdateConfigRequest request, StreamObse
@Override
public void changeInstanceState(InstanceStateChangeRequest request, StreamObserver<InstanceStateChangeResponse> responseObserver) {
var instanceId = UUID.fromString(request.getId());
ServerRPCConstants.USER_CONTEXT_KEY.get().hasPermissionOrThrow(Permissions.CHANGE_INSTANCE_STATE.context(instanceId));
ServerRPCConstants.USER_CONTEXT_KEY.get().hasPermissionOrThrow(PermissionContext.instance(InstancePermission.CHANGE_INSTANCE_STATE, instanceId));

try {
var optionalInstance = soulFireServer.getInstance(instanceId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package com.soulfiremc.server.grpc;

import com.soulfiremc.grpc.generated.*;
import com.soulfiremc.server.user.Permissions;
import com.soulfiremc.server.user.PermissionContext;
import com.soulfiremc.server.util.structs.SFLogAppender;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
Expand Down Expand Up @@ -53,7 +53,7 @@ public static void sendMessage(UUID uuid, String message) {

@Override
public void getPrevious(PreviousLogRequest request, StreamObserver<PreviousLogResponse> responseObserver) {
ServerRPCConstants.USER_CONTEXT_KEY.get().hasPermissionOrThrow(Permissions.SUBSCRIBE_LOGS.context());
ServerRPCConstants.USER_CONTEXT_KEY.get().hasPermissionOrThrow(PermissionContext.global(GlobalPermission.SUBSCRIBE_LOGS));

try {
responseObserver.onNext(PreviousLogResponse.newBuilder()
Expand All @@ -68,7 +68,7 @@ public void getPrevious(PreviousLogRequest request, StreamObserver<PreviousLogRe

@Override
public void subscribe(LogRequest request, StreamObserver<LogResponse> responseObserver) {
ServerRPCConstants.USER_CONTEXT_KEY.get().hasPermissionOrThrow(Permissions.SUBSCRIBE_LOGS.context());
ServerRPCConstants.USER_CONTEXT_KEY.get().hasPermissionOrThrow(PermissionContext.global(GlobalPermission.SUBSCRIBE_LOGS));

try {
var sender = new ConnectionMessageSender((ServerCallStreamObserver<LogResponse>) responseObserver);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.soulfiremc.server.account.MCAuthService;
import com.soulfiremc.server.account.MinecraftAccount;
import com.soulfiremc.server.settings.AccountSettings;
import com.soulfiremc.server.user.Permissions;
import com.soulfiremc.server.user.PermissionContext;
import com.soulfiremc.server.util.SFHelpers;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
Expand All @@ -42,7 +42,7 @@ public class MCAuthServiceImpl extends MCAuthServiceGrpc.MCAuthServiceImplBase {
@Override
public void loginCredentials(CredentialsAuthRequest request, StreamObserver<CredentialsAuthResponse> responseObserver) {
var instanceId = UUID.fromString(request.getInstanceId());
ServerRPCConstants.USER_CONTEXT_KEY.get().hasPermissionOrThrow(Permissions.AUTHENTICATE_MC_ACCOUNT.context(instanceId));
ServerRPCConstants.USER_CONTEXT_KEY.get().hasPermissionOrThrow(PermissionContext.instance(InstancePermission.AUTHENTICATE_MC_ACCOUNT, instanceId));

var optionalInstance = soulFireServer.getInstance(instanceId);
if (optionalInstance.isEmpty()) {
Expand Down Expand Up @@ -76,7 +76,7 @@ public void loginCredentials(CredentialsAuthRequest request, StreamObserver<Cred
@Override
public void loginDeviceCode(DeviceCodeAuthRequest request, StreamObserver<DeviceCodeAuthResponse> responseObserver) {
var instanceId = UUID.fromString(request.getInstanceId());
ServerRPCConstants.USER_CONTEXT_KEY.get().hasPermissionOrThrow(Permissions.AUTHENTICATE_MC_ACCOUNT.context(instanceId));
ServerRPCConstants.USER_CONTEXT_KEY.get().hasPermissionOrThrow(PermissionContext.instance(InstancePermission.AUTHENTICATE_MC_ACCOUNT, instanceId));

var optionalInstance = soulFireServer.getInstance(instanceId);
if (optionalInstance.isEmpty()) {
Expand Down Expand Up @@ -112,7 +112,7 @@ public void loginDeviceCode(DeviceCodeAuthRequest request, StreamObserver<Device
@Override
public void refresh(RefreshRequest request, StreamObserver<RefreshResponse> responseObserver) {
var instanceId = UUID.fromString(request.getInstanceId());
ServerRPCConstants.USER_CONTEXT_KEY.get().hasPermissionOrThrow(Permissions.AUTHENTICATE_MC_ACCOUNT.context(instanceId));
ServerRPCConstants.USER_CONTEXT_KEY.get().hasPermissionOrThrow(PermissionContext.instance(InstancePermission.AUTHENTICATE_MC_ACCOUNT, instanceId));

var optionalInstance = soulFireServer.getInstance(instanceId);
if (optionalInstance.isEmpty()) {
Expand Down
Loading

0 comments on commit 0334edd

Please sign in to comment.