Skip to content

Commit

Permalink
stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
burdoto committed Dec 19, 2023
1 parent 14c4279 commit d6902d3
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 11 deletions.
9 changes: 4 additions & 5 deletions src/agent/main/java/org/comroid/mcsd/agent/AgentRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,10 @@ public Object status(String[] args, ConsoleController.Connection con) {
@Command(usage = "<name> (experimental)")
public Object reload(String[] args, ConsoleController.Connection con) {
var srv = getServer(args);
srv.requirePermission(con.getUser(), AbstractEntity.Permission.ForceOP);
var tree = manager.tree(srv);
tree.terminate();
tree.initialize();
return srv+" modules have been reloaded";
srv.requirePermission(con.getUser(), AbstractEntity.Permission.Administrate);
var tree = manager.get(srv).orElseThrow(()->new Command.Error("Could not load entry " + srv));
var load = tree.reloadProtos();
return load+" modules have been reloaded";
}

@Command(usage = "<name> [-na]")
Expand Down
14 changes: 12 additions & 2 deletions src/core/main/java/org/comroid/mcsd/core/ServerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,25 @@ public long cleanupModules() {
.count();
}

private Stream<ModulePrototype> streamProtos() {
return Streams.of(moduleRepo.findAllByServerId(server.getId()));
}

/**
* Reloads all module configurations without restarting anything
*/
public long reloadProtos() {
return 0; //todo
return streamProtos()
.filter(tree::containsKey)
.peek(proto -> tree.get(proto).setProto(proto))
.count();
}

/**
* Loads all modules that are in DB but are not loaded as a module
*/
public long refreshModules() {
return Streams.of(moduleRepo.findAllByServerId(server.getId()))
return streamProtos()
.filter(not(tree::containsKey))
.<ServerModule<?>>map(proto -> {
log.fine("Loading proto " + proto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public final boolean hasAnyPermission(@NotNull User user) {
return hasPermission(user, Permission.Any);
}

public final boolean hasPermission(@NotNull User user, AbstractEntity.Permission... permissions) {
public boolean hasPermission(@NotNull User user, AbstractEntity.Permission... permissions) {
Constraint.Length.min(1, permissions, "permissions").run();
final var mask = this.permissions.getOrDefault(user, 0);
return (owner != null && user.getId().equals(owner.getId()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.comroid.mcsd.core.entity.module.status.UpdateModulePrototype;
import org.comroid.mcsd.core.entity.module.status.UptimeModulePrototype;
import org.comroid.mcsd.core.entity.server.Server;
import org.comroid.mcsd.core.entity.system.User;
import org.comroid.mcsd.core.module.ServerModule;
import org.comroid.mcsd.core.module.console.McsdCommandModule;
import org.comroid.mcsd.core.module.discord.DiscordModule;
Expand All @@ -38,6 +39,7 @@
import org.comroid.mcsd.core.module.status.UpdateModule;
import org.comroid.mcsd.core.module.status.UptimeModule;
import org.comroid.mcsd.core.repo.module.ModuleRepo;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;
Expand Down Expand Up @@ -71,6 +73,11 @@ public <T extends ServerModule<P>, P extends ModulePrototype> T toModule(Server
return Polyfill.uncheckedCast(module);
}

@Override
public boolean hasPermission(@NotNull User user, Permission... permissions) {
return super.hasPermission(user, permissions) || server.hasPermission(user, permissions);
}

@Getter
public enum Type implements Named {
// console
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.comroid.api.Component;
import org.comroid.api.Named;
import org.comroid.mcsd.core.ServerManager;
Expand All @@ -13,7 +11,7 @@

import static org.comroid.mcsd.core.util.ApplicationContextProvider.bean;

@Getter
@Data
@AllArgsConstructor
public abstract class ServerModule<T extends ModulePrototype> extends Component.Base implements Named {
protected final Server server;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.comroid.mcsd.core.MCSD;
import org.comroid.mcsd.core.ServerManager;
import org.comroid.mcsd.core.entity.*;
import org.comroid.mcsd.core.entity.module.ModulePrototype;
import org.comroid.mcsd.core.entity.server.Server;
import org.comroid.mcsd.core.entity.system.*;
import org.comroid.mcsd.core.exception.EntityNotFoundException;
Expand All @@ -34,6 +35,7 @@
import java.time.Instant;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
@Controller
Expand Down Expand Up @@ -73,6 +75,12 @@ public List<Server> servers(HttpSession session) {
.toList();
}

@ResponseBody
@GetMapping("/webapp/modules")
public List<ModulePrototype.Type> modules() {
return Stream.of(ModulePrototype.Type.values()).toList();
}

@ResponseBody
@GetMapping("/webapp/server/{id}/status")
public StatusMessage fetchServerStatus(@PathVariable("id") UUID serverId) {
Expand Down

0 comments on commit d6902d3

Please sign in to comment.