Refactor permissions in prep for ranks not on the inheritance tree

This commit is contained in:
Colin McDonald 2016-06-25 18:20:32 -04:00
parent 3381d74a8b
commit a648fb7f30
7 changed files with 21 additions and 14 deletions

View File

@ -321,7 +321,7 @@ public final class APIv3 extends AbstractVerticle {
ctx.response().setStatusCode(code); ctx.response().setStatusCode(code);
if (!ctx.request().path().contains("dumps")) { if (!ctx.request().path().contains("dumps")) {
//log.info(gson.toJson(response)); log.info(gson.toJson(response));
} }
ctx.response().end(gson.toJson(response)); ctx.response().end(gson.toJson(response));

View File

@ -26,6 +26,7 @@ public final class Rank {
private static List<Rank> rankCache = null; private static List<Rank> rankCache = null;
@Getter @Id private String id; @Getter @Id private String id;
@Getter private String inheritsFromId;
@Getter private int weight; @Getter private int weight;
@Getter private String displayName; @Getter private String displayName;
@Getter private String gameColor; @Getter private String gameColor;
@ -68,8 +69,9 @@ public final class Rank {
private Rank() {} // For Jackson 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.id = id;
this.inheritsFromId = inheritsFromId;
this.weight = weight; this.weight = weight;
this.displayName = displayName; this.displayName = displayName;
this.gameColor = gameColor; this.gameColor = gameColor;

View File

@ -72,8 +72,8 @@ public final class ServerGroup {
this.image = image; this.image = image;
} }
public Map<String, Boolean> calculatePermissions(Rank userRank) { public Map<String, Boolean> calculateScopedPermissions(Rank rank) {
return PermissionUtils.mergeUpTo(permissions, userRank); return PermissionUtils.mergeUpTo(permissions, rank);
} }
public void insert(SingleResultCallback<Void> callback) { public void insert(SingleResultCallback<Void> callback) {

View File

@ -491,7 +491,7 @@ public final class User {
globalPermissions = PermissionUtils.mergePermissions( globalPermissions = PermissionUtils.mergePermissions(
globalPermissions, globalPermissions,
serverGroup.calculatePermissions(rank) serverGroup.calculateScopedPermissions(rank)
); );
} }

View File

@ -12,13 +12,14 @@ public final class POSTRanks implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) { public void handle(RoutingContext ctx) {
JsonObject requestBody = ctx.getBodyAsJson(); JsonObject requestBody = ctx.getBodyAsJson();
String id = requestBody.getString("id"); String id = requestBody.getString("id");
String inheritsFromId = requestBody.getString("inheritsFromId");
int weight = requestBody.getInteger("weight"); int weight = requestBody.getInteger("weight");
String displayName = requestBody.getString("displayName"); String displayName = requestBody.getString("displayName");
String gameColor = requestBody.getString("gameColor"); String gameColor = requestBody.getString("gameColor");
String websiteColor = requestBody.getString("websiteColor"); String websiteColor = requestBody.getString("websiteColor");
boolean staffRank = requestBody.getBoolean("staffRank"); 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<Void> callback = new BlockingCallback<>(); BlockingCallback<Void> callback = new BlockingCallback<>();
rank.insert(callback); rank.insert(callback);
callback.get(); callback.get();

View File

@ -133,7 +133,7 @@ public final class POSTServersHeartbeat implements Handler<RoutingContext> {
for (Rank rank : Rank.findAll()) { for (Rank rank : Rank.findAll()) {
Map<String, Boolean> scopedPermissions = PermissionUtils.mergePermissions( Map<String, Boolean> scopedPermissions = PermissionUtils.mergePermissions(
PermissionUtils.getDefaultPermissions(rank), PermissionUtils.getDefaultPermissions(rank),
serverGroup.calculatePermissions(rank) serverGroup.calculateScopedPermissions(rank)
); );
permissionsResponse.put(rank.getId(), scopedPermissions); permissionsResponse.put(rank.getId(), scopedPermissions);

View File

@ -5,6 +5,7 @@ import lombok.experimental.UtilityClass;
import net.frozenorb.apiv3.model.Rank; import net.frozenorb.apiv3.model.Rank;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -19,20 +20,23 @@ public class PermissionUtils {
return result; return result;
} }
public static Map<String, Boolean> mergeUpTo(Map<String, List<String>> unmerged, Rank upTo) { public static Map<String, Boolean> mergeUpTo(Map<String, List<String>> raw, Rank upTo) {
Map<String, Boolean> result = new HashMap<>(); Map<String, Boolean> result = new HashMap<>();
List<Rank> mergeQueue = new LinkedList<>();
Rank merge = upTo;
for (Rank rank : Rank.findAll()) { while (merge != null) {
Map<String, Boolean> rankPermissions = convertToMap(unmerged.get(rank.getId())); mergeQueue.add(0, merge);
merge = Rank.findById(merge.getInheritsFromId());
}
for (Rank rank : mergeQueue) {
Map<String, Boolean> rankPermissions = convertToMap(raw.get(rank.getId()));
// If there's no permissions defined for this rank just skip it. // If there's no permissions defined for this rank just skip it.
if (!rankPermissions.isEmpty()) { if (!rankPermissions.isEmpty()) {
result = mergePermissions(result, rankPermissions); result = mergePermissions(result, rankPermissions);
} }
if (upTo.getId().equals(rank.getId())) {
break;
}
} }
return result; return result;