From a648fb7f30fb689fbca43b36e90514067f4a84cc Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Sat, 25 Jun 2016 18:20:32 -0400 Subject: [PATCH] Refactor permissions in prep for ranks not on the inheritance tree --- src/main/java/net/frozenorb/apiv3/APIv3.java | 2 +- .../java/net/frozenorb/apiv3/model/Rank.java | 4 +++- .../net/frozenorb/apiv3/model/ServerGroup.java | 4 ++-- .../java/net/frozenorb/apiv3/model/User.java | 2 +- .../frozenorb/apiv3/route/ranks/POSTRanks.java | 3 ++- .../route/servers/POSTServersHeartbeat.java | 2 +- .../frozenorb/apiv3/util/PermissionUtils.java | 18 +++++++++++------- 7 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/main/java/net/frozenorb/apiv3/APIv3.java b/src/main/java/net/frozenorb/apiv3/APIv3.java index 919abd0..7aee78f 100644 --- a/src/main/java/net/frozenorb/apiv3/APIv3.java +++ b/src/main/java/net/frozenorb/apiv3/APIv3.java @@ -321,7 +321,7 @@ public final class APIv3 extends AbstractVerticle { ctx.response().setStatusCode(code); if (!ctx.request().path().contains("dumps")) { - //log.info(gson.toJson(response)); + log.info(gson.toJson(response)); } ctx.response().end(gson.toJson(response)); diff --git a/src/main/java/net/frozenorb/apiv3/model/Rank.java b/src/main/java/net/frozenorb/apiv3/model/Rank.java index ec1e86b..3aef1da 100644 --- a/src/main/java/net/frozenorb/apiv3/model/Rank.java +++ b/src/main/java/net/frozenorb/apiv3/model/Rank.java @@ -26,6 +26,7 @@ public final class Rank { private static List rankCache = null; @Getter @Id private String id; + @Getter private String inheritsFromId; @Getter private int weight; @Getter private String displayName; @Getter private String gameColor; @@ -68,8 +69,9 @@ public final class Rank { private Rank() {} // For Jackson - public Rank(String id, int weight, String displayName, String gameColor, String websiteColor, boolean staffRank) { + public Rank(String id, String inheritsFromId, int weight, String displayName, String gameColor, String websiteColor, boolean staffRank) { this.id = id; + this.inheritsFromId = inheritsFromId; this.weight = weight; this.displayName = displayName; this.gameColor = gameColor; diff --git a/src/main/java/net/frozenorb/apiv3/model/ServerGroup.java b/src/main/java/net/frozenorb/apiv3/model/ServerGroup.java index b64d812..b808085 100644 --- a/src/main/java/net/frozenorb/apiv3/model/ServerGroup.java +++ b/src/main/java/net/frozenorb/apiv3/model/ServerGroup.java @@ -72,8 +72,8 @@ public final class ServerGroup { this.image = image; } - public Map calculatePermissions(Rank userRank) { - return PermissionUtils.mergeUpTo(permissions, userRank); + public Map calculateScopedPermissions(Rank rank) { + return PermissionUtils.mergeUpTo(permissions, rank); } public void insert(SingleResultCallback callback) { diff --git a/src/main/java/net/frozenorb/apiv3/model/User.java b/src/main/java/net/frozenorb/apiv3/model/User.java index 8db0360..53827b6 100644 --- a/src/main/java/net/frozenorb/apiv3/model/User.java +++ b/src/main/java/net/frozenorb/apiv3/model/User.java @@ -491,7 +491,7 @@ public final class User { globalPermissions = PermissionUtils.mergePermissions( globalPermissions, - serverGroup.calculatePermissions(rank) + serverGroup.calculateScopedPermissions(rank) ); } diff --git a/src/main/java/net/frozenorb/apiv3/route/ranks/POSTRanks.java b/src/main/java/net/frozenorb/apiv3/route/ranks/POSTRanks.java index c8b553e..dfa53fb 100644 --- a/src/main/java/net/frozenorb/apiv3/route/ranks/POSTRanks.java +++ b/src/main/java/net/frozenorb/apiv3/route/ranks/POSTRanks.java @@ -12,13 +12,14 @@ public final class POSTRanks implements Handler { public void handle(RoutingContext ctx) { JsonObject requestBody = ctx.getBodyAsJson(); String id = requestBody.getString("id"); + String inheritsFromId = requestBody.getString("inheritsFromId"); int weight = requestBody.getInteger("weight"); String displayName = requestBody.getString("displayName"); String gameColor = requestBody.getString("gameColor"); String websiteColor = requestBody.getString("websiteColor"); boolean staffRank = requestBody.getBoolean("staffRank"); - Rank rank = new Rank(id, weight, displayName, gameColor, websiteColor, staffRank); + Rank rank = new Rank(id, inheritsFromId, weight, displayName, gameColor, websiteColor, staffRank); BlockingCallback callback = new BlockingCallback<>(); rank.insert(callback); callback.get(); diff --git a/src/main/java/net/frozenorb/apiv3/route/servers/POSTServersHeartbeat.java b/src/main/java/net/frozenorb/apiv3/route/servers/POSTServersHeartbeat.java index 435d4c8..5269943 100644 --- a/src/main/java/net/frozenorb/apiv3/route/servers/POSTServersHeartbeat.java +++ b/src/main/java/net/frozenorb/apiv3/route/servers/POSTServersHeartbeat.java @@ -133,7 +133,7 @@ public final class POSTServersHeartbeat implements Handler { for (Rank rank : Rank.findAll()) { Map scopedPermissions = PermissionUtils.mergePermissions( PermissionUtils.getDefaultPermissions(rank), - serverGroup.calculatePermissions(rank) + serverGroup.calculateScopedPermissions(rank) ); permissionsResponse.put(rank.getId(), scopedPermissions); diff --git a/src/main/java/net/frozenorb/apiv3/util/PermissionUtils.java b/src/main/java/net/frozenorb/apiv3/util/PermissionUtils.java index e490279..7186b39 100644 --- a/src/main/java/net/frozenorb/apiv3/util/PermissionUtils.java +++ b/src/main/java/net/frozenorb/apiv3/util/PermissionUtils.java @@ -5,6 +5,7 @@ import lombok.experimental.UtilityClass; import net.frozenorb.apiv3.model.Rank; import java.util.HashMap; +import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -19,20 +20,23 @@ public class PermissionUtils { return result; } - public static Map mergeUpTo(Map> unmerged, Rank upTo) { + public static Map mergeUpTo(Map> raw, Rank upTo) { Map result = new HashMap<>(); + List mergeQueue = new LinkedList<>(); + Rank merge = upTo; - for (Rank rank : Rank.findAll()) { - Map rankPermissions = convertToMap(unmerged.get(rank.getId())); + while (merge != null) { + mergeQueue.add(0, merge); + merge = Rank.findById(merge.getInheritsFromId()); + } + + for (Rank rank : mergeQueue) { + Map rankPermissions = convertToMap(raw.get(rank.getId())); // If there's no permissions defined for this rank just skip it. if (!rankPermissions.isEmpty()) { result = mergePermissions(result, rankPermissions); } - - if (upTo.getId().equals(rank.getId())) { - break; - } } return result;