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);
if (!ctx.request().path().contains("dumps")) {
//log.info(gson.toJson(response));
log.info(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;
@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;

View File

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

View File

@ -491,7 +491,7 @@ public final class User {
globalPermissions = PermissionUtils.mergePermissions(
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) {
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<Void> callback = new BlockingCallback<>();
rank.insert(callback);
callback.get();

View File

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

View File

@ -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<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<>();
List<Rank> mergeQueue = new LinkedList<>();
Rank merge = upTo;
for (Rank rank : Rank.findAll()) {
Map<String, Boolean> rankPermissions = convertToMap(unmerged.get(rank.getId()));
while (merge != null) {
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 (!rankPermissions.isEmpty()) {
result = mergePermissions(result, rankPermissions);
}
if (upTo.getId().equals(rank.getId())) {
break;
}
}
return result;