This commit is contained in:
Colin McDonald 2016-04-27 22:41:22 -04:00
parent cd3936b0bf
commit e8bdcdc6a5
12 changed files with 82 additions and 59 deletions

View File

@ -7,6 +7,9 @@ import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import lombok.Getter;
import net.frozenorb.apiv3.models.Rank;
import net.frozenorb.apiv3.models.Server;
import net.frozenorb.apiv3.models.ServerGroup;
import net.frozenorb.apiv3.routes.GETDump;
import net.frozenorb.apiv3.routes.GETRoutes;
import net.frozenorb.apiv3.routes.announcements.GETAnnouncements;
@ -33,6 +36,10 @@ import net.frozenorb.apiv3.weirdStuff.ObjectIdTypeAdapter;
import org.bson.types.ObjectId;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Morphia;
import spark.Request;
import java.util.HashMap;
import java.util.Map;
import static spark.Spark.*;
@ -44,6 +51,36 @@ public final class APIv3 {
APIv3() {
setupDatabase();
setupHttp();
ServerGroup scope = ServerGroup.byId("HCTeams");
Map<ServerGroup, Rank> userRanks = new HashMap<>();
ServerGroup global = ServerGroup.byId("Global");
Map<String, Boolean> calculatedPermissions = global.getPermissions(userRanks.get(global));
// for global calculations
for (ServerGroup serverGroup : ServerGroup.values()) {
if (serverGroup != global) {
mergePermissions(serverGroup, userRanks.get(serverGroup), calculatedPermissions);
}
}
// for scoped calculations
mergePermissions(scope, userRanks.get(scope), calculatedPermissions);
}
public void mergePermissions(ServerGroup serverGroup, Rank rank, Map<String, Boolean> current) {
Map<String, Boolean> groupPermissions = serverGroup.getPermissions(rank);
groupPermissions.forEach((permissionNode, grant) -> {
if (!current.containsKey(permissionNode) || !current.get(permissionNode)) {
current.put(permissionNode, grant);
}
});
}
private void setupDatabase() {
@ -102,4 +139,9 @@ public final class APIv3 {
after(new ContentTypeFilter());
}
public static void requireAuthorizedActor(Request req) {
Actor actor = req.attribute("actor");
if (!req.attribute("actor").)
}
}

View File

@ -22,14 +22,6 @@ public final class AuditLogEntry {
@Getter private String description;
@Getter private Document actionData;
public static AuditLogEntry byId(String id) {
return APIv3.getDatastore().createQuery(AuditLogEntry.class).field("id").equal(new ObjectId(id)).get();
}
public static List<AuditLogEntry> values() {
return APIv3.getDatastore().createQuery(AuditLogEntry.class).asList();
}
public AuditLogEntry() {} // For Morphia
public AuditLogEntry(User performedBy, String performedFrom, String actionType, String description, Document actionData) {

View File

@ -30,10 +30,6 @@ public final class Grant {
return APIv3.getDatastore().createQuery(Grant.class).field("id").equal(new ObjectId(id)).get();
}
public static List<Grant> values() {
return APIv3.getDatastore().createQuery(Grant.class).asList();
}
public Grant() {} // For Morphia
public Grant(User target, String reason, Set<ServerGroup> scopes, Rank rank, Date expiresAt, User addedBy) {

View File

@ -1,15 +0,0 @@
package net.frozenorb.apiv3.models;
import lombok.Getter;
import org.bson.types.ObjectId;
import org.mongodb.morphia.annotations.Entity;
import org.mongodb.morphia.annotations.Id;
@Entity(value = "ipBans", noClassnameStored = true)
public final class IPBan {
@Getter @Id private ObjectId id;
public IPBan() {} // For Morphia
}

View File

@ -20,14 +20,6 @@ public final class IPLogEntry {
@Getter private Date lastSeen;
@Getter private int uses;
public static IPLogEntry byId(String id) {
return APIv3.getDatastore().createQuery(IPLogEntry.class).field("id").equal(new ObjectId(id)).get();
}
public static List<IPLogEntry> values() {
return APIv3.getDatastore().createQuery(IPLogEntry.class).asList();
}
public IPLogEntry() {} // For Morphia
public IPLogEntry(User user, String ip) {

View File

@ -23,10 +23,6 @@ public final class NotificationTemplate {
return APIv3.getDatastore().createQuery(NotificationTemplate.class).field("id").equalIgnoreCase(id).get();
}
public static List<NotificationTemplate> values() {
return APIv3.getDatastore().createQuery(NotificationTemplate.class).asList();
}
public NotificationTemplate() {} // For Morphia
public NotificationTemplate(String subject, String body, User creator) {

View File

@ -31,10 +31,6 @@ public final class Punishment {
return APIv3.getDatastore().createQuery(Punishment.class).field("id").equal(new ObjectId(id)).get();
}
public static List<Punishment> values() {
return APIv3.getDatastore().createQuery(Punishment.class).asList();
}
public Punishment() {} // For Morphia
public Punishment(User target, String reason, PunishmentType type, Date expiresAt, User addedBy, Server addedOn) {

View File

@ -5,9 +5,7 @@ import net.frozenorb.apiv3.APIv3;
import org.mongodb.morphia.annotations.Entity;
import org.mongodb.morphia.annotations.Id;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;
@Entity(value = "serverGroups", noClassnameStored = true)
public final class ServerGroup {
@ -44,4 +42,9 @@ public final class ServerGroup {
APIv3.getDatastore().save(this);
}
// TODO: DO THIS
public Map<String, Boolean> getPermissions(Rank rank) {
return new HashMap<>();
}
}

View File

@ -59,10 +59,6 @@ public final class User {
return APIv3.getDatastore().createQuery(User.class).field("emailToken").equal(name).get();
}
public static List<User> values() {
return APIv3.getDatastore().createQuery(User.class).asList();
}
public User() {} // For Morphia
public User(UUID id, String lastName) {

View File

@ -19,14 +19,6 @@ public final class UserMetaEntry {
@Getter private String serverGroup;
@Getter @Setter private Document data;
public static UserMetaEntry byId(String id) {
return APIv3.getDatastore().createQuery(UserMetaEntry.class).field("id").equal(new ObjectId(id)).get();
}
public static List<UserMetaEntry> values() {
return APIv3.getDatastore().createQuery(UserMetaEntry.class).asList();
}
public UserMetaEntry() {} // For Morphia
public UserMetaEntry(User user, ServerGroup serverGroup, Document data) {

View File

@ -0,0 +1,15 @@
package net.frozenorb.apiv3.weirdStuff;
public interface Actor {
boolean isAuthorized();
String getName();
Actor.Type getType();
enum Type {
WEBSITE, SERVER, UNKNOWN
}
}

View File

@ -7,7 +7,25 @@ import spark.Response;
public final class ActorAttributeFilter implements Filter {
public void handle(Request req, Response res) {
//req.attribute("server", Server.byId(req.queryParams("server")));
// TODO: Auth!
req.attribute("actor", new Actor() {
@Override
public boolean isAuthorized() {
return true;
}
@Override
public String getName() {
return "HCTeams";
}
@Override
public Actor.Type getType() {
return Actor.Type.SERVER;
}
});
}
}