Add scope flag to GET /dumps/grant to limit the scope of returned grants

This commit is contained in:
Colin McDonald 2016-08-19 02:20:57 -04:00
parent 96c55ea6c8
commit 256d12c1d4
1 changed files with 42 additions and 13 deletions

View File

@ -6,10 +6,9 @@ import com.google.common.collect.ImmutableSet;
import io.vertx.core.Handler;
import io.vertx.ext.web.RoutingContext;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.model.Grant;
import net.frozenorb.apiv3.model.IpBan;
import net.frozenorb.apiv3.model.IpIntel;
import net.frozenorb.apiv3.model.Punishment;
import net.frozenorb.apiv3.actor.Actor;
import net.frozenorb.apiv3.actor.ActorType;
import net.frozenorb.apiv3.model.*;
import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.GeoJsonPoint;
@ -24,7 +23,7 @@ public final class GETDumpsType implements Handler<RoutingContext> {
private static List<UUID> banCache = ImmutableList.of();
private static List<UUID> blacklistCache = ImmutableList.of();
private static List<String> ipBanCache = ImmutableList.of();
private static Map<String, List<UUID>> grantCache = ImmutableMap.of();
private static Map<String, Map<String, List<UUID>>> grantCache = ImmutableMap.of();
private static Map<String, GeoJsonPoint> ipIntelCache = ImmutableMap.of();
static {
@ -66,21 +65,32 @@ public final class GETDumpsType implements Handler<RoutingContext> {
return;
}
Map<String, List<UUID>> grantCache = new HashMap<>();
Map<String, Map<String, List<UUID>>> grantCache = new HashMap<>();
for (ServerGroup serverGroup : ServerGroup.findAll()) {
grantCache.put(serverGroup.getId().toLowerCase(), new HashMap<>());
}
for (Grant grant : grants) {
if (!grant.isActive()) {
continue;
}
List<UUID> users = grantCache.get(grant.getRank());
for (ServerGroup serverGroup : ServerGroup.findAll()) {
if (!serverGroup.getId().equals(ServerGroup.DEFAULT_GROUP_ID) && !grant.appliesOn(serverGroup)) {
continue;
}
if (users == null) {
users = new LinkedList<>();
grantCache.put(grant.getRank(), users);
Map<String, List<UUID>> serverGroupGrantDump = grantCache.get(serverGroup.getId().toLowerCase());
List<UUID> users = serverGroupGrantDump.get(grant.getRank());
if (users == null) {
users = new LinkedList<>();
serverGroupGrantDump.put(grant.getRank(), users);
}
users.add(grant.getUser());
}
users.add(grant.getUser());
}
GETDumpsType.grantCache = grantCache;
@ -148,7 +158,26 @@ public final class GETDumpsType implements Handler<RoutingContext> {
APIv3.respondJson(ctx, 200, ipBanCache);
return;
case "grant":
APIv3.respondJson(ctx, 200, grantCache);
String serverGroup = ServerGroup.DEFAULT_GROUP_ID;
String customScope = ctx.request().getParam("scope");
if (customScope != null) {
if (customScope.equalsIgnoreCase("me")) {
Actor actor = ctx.get("actor");
if (actor.getType() != ActorType.SERVER) {
ErrorUtils.respondOther(ctx, 403, "This action can only be performed when requested by a server.", "serverOnly", ImmutableMap.of());
return;
}
Server actorServer = Server.findById(actor.getName());
serverGroup = actorServer.getServerGroup();
} else {
serverGroup = customScope;
}
}
APIv3.respondJson(ctx, 200, grantCache.get(serverGroup.toLowerCase()));
return;
case "ipintel":
APIv3.respondJson(ctx, 200, ipIntelCache.values());