This commit is contained in:
Colin McDonald 2016-05-10 18:37:07 -04:00
parent afbe2a68b5
commit b4b70bafd5
12 changed files with 33 additions and 50 deletions

View File

@ -23,20 +23,20 @@ public final class AuditLogEntry {
@Getter @Indexed private Date performedAt; @Getter @Indexed private Date performedAt;
@Getter private String actorName; @Getter private String actorName;
@Getter private ActorType actorType; @Getter private ActorType actorType;
@Getter private AuditLogActionType actionType; @Getter private AuditLogActionType type;
@Getter private Map<String, Object> actionData; @Getter private Map<String, Object> metadata;
public AuditLogEntry() {} // For Morphia public AuditLogEntry() {} // For Morphia
public AuditLogEntry(User user, String userIp, Actor actor, AuditLogActionType actionType, Map<String, Object> actionData) { public AuditLogEntry(User user, String userIp, Actor actor, AuditLogActionType type, Map<String, Object> metadata) {
this.id = new ObjectId().toString(); this.id = new ObjectId().toString();
this.user = user.getId(); this.user = user.getId();
this.userIp = userIp; this.userIp = userIp;
this.performedAt = new Date(); this.performedAt = new Date();
this.actorName = actor.getName(); this.actorName = actor.getName();
this.actorType = actor.getType(); this.actorType = actor.getType();
this.actionType = actionType; this.type = type;
this.actionData = ImmutableMap.copyOf(actionData); this.metadata = ImmutableMap.copyOf(metadata);
} }
} }

View File

@ -17,7 +17,7 @@ import java.util.UUID;
public final class Grant { public final class Grant {
@Getter @Id private String id; @Getter @Id private String id;
@Getter @Indexed private UUID user @Getter @Indexed private UUID user;
@Getter private String reason; @Getter private String reason;
@Getter private Set<String> scopes = new HashSet<>(); // So on things w/o scopes we still load properly (Morphia drops empty sets) @Getter private Set<String> scopes = new HashSet<>(); // So on things w/o scopes we still load properly (Morphia drops empty sets)
@Getter @Indexed private String rank; @Getter @Indexed private String rank;

View File

@ -17,8 +17,8 @@ public final class IPLogEntry {
@Getter @Id private String id; @Getter @Id private String id;
@Getter @ExcludeFromReplies @Indexed private UUID user; @Getter @ExcludeFromReplies @Indexed private UUID user;
@Getter @Indexed private String userIp; @Getter @Indexed private String userIp;
@Getter private Date firstSeen; @Getter private Date firstSeenAt;
@Getter private Date lastSeen; @Getter private Date lastSeenAt;
@Getter private int uses; @Getter private int uses;
public IPLogEntry() {} // For Morphia public IPLogEntry() {} // For Morphia
@ -27,13 +27,13 @@ public final class IPLogEntry {
this.id = new ObjectId().toString(); this.id = new ObjectId().toString();
this.user = user.getId(); this.user = user.getId();
this.userIp = userIp; this.userIp = userIp;
this.firstSeen = new Date(); this.firstSeenAt = new Date();
this.lastSeen = new Date(); this.lastSeenAt = new Date();
this.uses = 0; this.uses = 0;
} }
public void used() { public void used() {
this.lastSeen = new Date(); this.lastSeenAt = new Date();
this.uses++; this.uses++;
APIv3.getDatastore().save(this); APIv3.getDatastore().save(this);

View File

@ -22,7 +22,7 @@ public final class Punishment {
@Getter private String reason; @Getter private String reason;
@Getter @Indexed private PunishmentType type; // Type is indexed for the rank dump @Getter @Indexed private PunishmentType type; // Type is indexed for the rank dump
@Getter private Date expiresAt; @Getter private Date expiresAt;
@Getter private Map<String, Object> meta; @Getter private Map<String, Object> metadata;
@Getter private UUID addedBy; @Getter private UUID addedBy;
@Getter @Indexed private Date addedAt; @Getter @Indexed private Date addedAt;
@ -39,7 +39,7 @@ public final class Punishment {
public Punishment() {} // For Morphia public Punishment() {} // For Morphia
public Punishment(User user, String reason, PunishmentType type, Date expiresAt, User addedBy, Actor actor, Map<String, Object> meta) { public Punishment(User user, String reason, PunishmentType type, Date expiresAt, User addedBy, Actor actor, Map<String, Object> metadata) {
this.id = new ObjectId().toString(); this.id = new ObjectId().toString();
this.user = user.getId(); this.user = user.getId();
this.reason = reason; this.reason = reason;
@ -49,7 +49,7 @@ public final class Punishment {
this.addedAt = new Date(); this.addedAt = new Date();
this.actorName = actor.getName(); this.actorName = actor.getName();
this.actorType = actor.getType(); this.actorType = actor.getType();
this.meta = meta; this.metadata = metadata;
} }
public void delete(User removedBy, String reason) { public void delete(User removedBy, String reason) {

View File

@ -17,9 +17,9 @@ public final class Server {
@Getter private String bungeeId; @Getter private String bungeeId;
@Getter private String displayName; @Getter private String displayName;
@Getter @ExcludeFromReplies String apiKey; @Getter @ExcludeFromReplies String apiKey;
@Getter @Indexed private String group; @Getter @Indexed private String serverGroup;
@Getter private String serverIp; @Getter private String serverIp;
@Getter @Setter private Date lastUpdate; @Getter @Setter private Date lastUpdatedAt;
@Getter @Setter private double lastTps; @Getter @Setter private double lastTps;
@Getter @Setter @ExcludeFromReplies private Set<UUID> players; @Getter @Setter @ExcludeFromReplies private Set<UUID> players;
@ -33,14 +33,14 @@ public final class Server {
public Server() {} // For Morphia public Server() {} // For Morphia
public Server(String id, String bungeeId, String displayName, String apiKey, ServerGroup group, String serverIp) { public Server(String id, String bungeeId, String displayName, String apiKey, ServerGroup serverGroup, String serverIp) {
this.id = id; this.id = id;
this.bungeeId = bungeeId; this.bungeeId = bungeeId;
this.displayName = displayName; this.displayName = displayName;
this.apiKey = apiKey; this.apiKey = apiKey;
this.group = group.getId(); this.serverGroup = serverGroup.getId();
this.serverIp = serverIp; this.serverIp = serverIp;
this.lastUpdate = new Date(); this.lastUpdatedAt = new Date();
this.lastTps = 0; this.lastTps = 0;
this.players = new HashSet<>(); this.players = new HashSet<>();
} }

View File

@ -1,5 +1,6 @@
package net.frozenorb.apiv3.models; package net.frozenorb.apiv3.models;
import com.google.gson.annotations.SerializedName;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.APIv3;
@ -16,11 +17,11 @@ public final class ServerGroup {
@Getter @Id private String id; @Getter @Id private String id;
// We rename this to public, we just can't name it that because it's a Java identifier. // We rename this to public, we just can't name it that because it's a Java identifier.
@Getter @Property("public") private boolean isPublic; @Getter @Property("public") @SerializedName("public") private boolean isPublic;
// We define these HashSets up here because, in the event they're // We define these HashSets up here because, in the event they're
// empty, Morphia will load them as null, not empty sets. // empty, Morphia will load them as null, not empty sets.
@Getter @Setter @ExcludeFromReplies private Set<String> announcements = new HashSet<>(); @Getter @Setter @ExcludeFromReplies private Set<String> announcements = new HashSet<>();
@Getter @ExcludeFromReplies private Map<String, List<String>> permissions = new HashMap<>(); @Getter @ExcludeFromReplies private Map<String, Map<String, Boolean>> permissions = new HashMap<>();
public static ServerGroup byId(String id) { public static ServerGroup byId(String id) {
return APIv3.getDatastore().createQuery(ServerGroup.class).field("id").equal(id).get(); return APIv3.getDatastore().createQuery(ServerGroup.class).field("id").equal(id).get();

View File

@ -24,7 +24,7 @@ public final class User {
@Getter @ExcludeFromReplies private Map<String, Date> aliases; @Getter @ExcludeFromReplies private Map<String, Date> aliases;
@Getter @Setter @ExcludeFromReplies private String totpSecret; @Getter @Setter @ExcludeFromReplies private String totpSecret;
@Getter @Indexed @ExcludeFromReplies @Setter private String emailToken; @Getter @Indexed @ExcludeFromReplies @Setter private String emailToken;
@Getter @ExcludeFromReplies @Setter private Date emailTokenSet; @Getter @ExcludeFromReplies @Setter private Date emailTokenSetAt;
@Getter @ExcludeFromReplies private String password; @Getter @ExcludeFromReplies private String password;
@Getter @Setter private String email; @Getter @Setter private String email;
@Getter private String phoneNumber; @Getter private String phoneNumber;
@ -249,7 +249,7 @@ public final class User {
} }
} }
ServerGroup actorGroup = ServerGroup.byId(server.getGroup()); ServerGroup actorGroup = ServerGroup.byId(server.getServerGroup());
Rank highestRank = getHighestRank(actorGroup); Rank highestRank = getHighestRank(actorGroup);
// Generics are weird, yes we have to do this. // Generics are weird, yes we have to do this.

View File

@ -20,7 +20,7 @@ public final class GETAnnouncements implements Route {
} }
Server sender = ((ServerActor) req.attribute("actor")).getServer(); Server sender = ((ServerActor) req.attribute("actor")).getServer();
ServerGroup senderGroup = ServerGroup.byId(sender.getGroup()); ServerGroup senderGroup = ServerGroup.byId(sender.getServerGroup());
return senderGroup.getAnnouncements(); return senderGroup.getAnnouncements();
} }

View File

@ -30,7 +30,7 @@ public final class POSTServerHeartbeat implements Route {
} }
Server actorServer = Server.byId(actor.getName()); Server actorServer = Server.byId(actor.getName());
ServerGroup actorServerGroup = ServerGroup.byId(actorServer.getGroup()); ServerGroup actorServerGroup = ServerGroup.byId(actorServer.getServerGroup());
Document reqJson = Document.parse(req.body()); Document reqJson = Document.parse(req.body());
Set<UUID> onlinePlayers = new HashSet<>(); Set<UUID> onlinePlayers = new HashSet<>();
Map<String, Object> playersResponse = new HashMap<>(); Map<String, Object> playersResponse = new HashMap<>();
@ -81,7 +81,7 @@ public final class POSTServerHeartbeat implements Route {
actorServer.setPlayers(onlinePlayers); actorServer.setPlayers(onlinePlayers);
actorServer.setLastTps(reqJson.getDouble("lastTps")); actorServer.setLastTps(reqJson.getDouble("lastTps"));
actorServer.setLastUpdate(new Date()); actorServer.setLastUpdatedAt(new Date());
APIv3.getDatastore().save(actorServer); APIv3.getDatastore().save(actorServer);
return ImmutableMap.of( return ImmutableMap.of(

View File

@ -35,7 +35,7 @@ public final class POSTUserConfirmRegister implements Route {
return ErrorUtils.error("User provided already has email set."); return ErrorUtils.error("User provided already has email set.");
} }
if ((System.currentTimeMillis() - user.getEmailTokenSet().getTime()) > TimeUnit.DAYS.toMillis(2)) { if ((System.currentTimeMillis() - user.getEmailTokenSetAt().getTime()) > TimeUnit.DAYS.toMillis(2)) {
return ErrorUtils.error("Email token is expired"); return ErrorUtils.error("Email token is expired");
} }

View File

@ -41,13 +41,13 @@ public final class POSTUserRegister implements Route {
return ErrorUtils.error(email + " is not a valid email."); return ErrorUtils.error(email + " is not a valid email.");
} }
if (user.getEmailToken() != null && (System.currentTimeMillis() - user.getEmailTokenSet().getTime()) < TimeUnit.DAYS.toMillis(2)) { if (user.getEmailToken() != null && (System.currentTimeMillis() - user.getEmailTokenSetAt().getTime()) < TimeUnit.DAYS.toMillis(2)) {
return ErrorUtils.error("We just recently sent you a confirmation email. Please wait before trying to register again."); return ErrorUtils.error("We just recently sent you a confirmation email. Please wait before trying to register again.");
} }
user.setEmail(email); user.setEmail(email);
user.setEmailToken(new BigInteger(130, new Random()).toString(32)); user.setEmailToken(new BigInteger(130, new Random()).toString(32));
user.setEmailTokenSet(new Date()); user.setEmailTokenSetAt(new Date());
APIv3.getDatastore().save(user); APIv3.getDatastore().save(user);
Map<String, Object> replacements = ImmutableMap.of( Map<String, Object> replacements = ImmutableMap.of(

View File

@ -5,7 +5,6 @@ import lombok.experimental.UtilityClass;
import net.frozenorb.apiv3.models.Rank; import net.frozenorb.apiv3.models.Rank;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
@UtilityClass @UtilityClass
@ -19,15 +18,14 @@ public class PermissionUtils {
return result; return result;
} }
public static Map<String, Boolean> mergeUpTo(Map<String, List<String>> unconverted, Rank upTo) { public static Map<String, Boolean> mergeUpTo(Map<String, Map<String, Boolean>> unmerged, Rank upTo) {
Map<String, Boolean> result = new HashMap<>(); Map<String, Boolean> result = new HashMap<>();
for (Rank rank : Rank.values()) { for (Rank rank : Rank.values()) {
List<String> unconvertedPermissions = unconverted.get(rank.getId()); Map<String, Boolean> rankPermissions = unmerged.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 (unconvertedPermissions != null) { if (rankPermissions != null) {
Map<String, Boolean> rankPermissions = convertToMap(unconvertedPermissions);
result = mergePermissions(result, rankPermissions); result = mergePermissions(result, rankPermissions);
} }
@ -44,20 +42,4 @@ public class PermissionUtils {
return ImmutableMap.of(); return ImmutableMap.of();
} }
private static Map<String, Boolean> convertToMap(List<String> unconvered) {
Map<String, Boolean> result = new HashMap<>();
for (String permission : unconvered) {
boolean negate = permission.startsWith("-");
if (negate) {
result.put(permission.substring(1), false);
} else {
result.put(permission, true);
}
}
return result;
}
} }