From 256d12c1d4a9dc3b70fd7008fbdf91cb73eec711 Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Fri, 19 Aug 2016 02:20:57 -0400 Subject: [PATCH] Add scope flag to GET /dumps/grant to limit the scope of returned grants --- .../frozenorb/apiv3/route/GETDumpsType.java | 55 ++++++++++++++----- 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/src/main/java/net/frozenorb/apiv3/route/GETDumpsType.java b/src/main/java/net/frozenorb/apiv3/route/GETDumpsType.java index f7117aa..30684a4 100644 --- a/src/main/java/net/frozenorb/apiv3/route/GETDumpsType.java +++ b/src/main/java/net/frozenorb/apiv3/route/GETDumpsType.java @@ -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 { private static List banCache = ImmutableList.of(); private static List blacklistCache = ImmutableList.of(); private static List ipBanCache = ImmutableList.of(); - private static Map> grantCache = ImmutableMap.of(); + private static Map>> grantCache = ImmutableMap.of(); private static Map ipIntelCache = ImmutableMap.of(); static { @@ -66,21 +65,32 @@ public final class GETDumpsType implements Handler { return; } - Map> grantCache = new HashMap<>(); + Map>> grantCache = new HashMap<>(); + + for (ServerGroup serverGroup : ServerGroup.findAll()) { + grantCache.put(serverGroup.getId().toLowerCase(), new HashMap<>()); + } for (Grant grant : grants) { if (!grant.isActive()) { continue; } - List 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> serverGroupGrantDump = grantCache.get(serverGroup.getId().toLowerCase()); + List 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 { 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());