Push a couple model changes from last night

This commit is contained in:
Colin McDonald 2016-02-27 14:46:33 -05:00
parent 2884ce209a
commit 0b13eed247
8 changed files with 45 additions and 57 deletions

View File

@ -4,7 +4,6 @@ import com.mongodb.async.SingleResultCallback;
import lombok.experimental.UtilityClass; import lombok.experimental.UtilityClass;
import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.model.AuditLogEntry; import net.frozenorb.apiv3.model.AuditLogEntry;
import net.frozenorb.apiv3.util.IPUtils;
import net.frozenorb.apiv3.util.MongoUtils; import net.frozenorb.apiv3.util.MongoUtils;
import org.bson.Document; import org.bson.Document;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
@ -12,7 +11,6 @@ import org.bson.types.ObjectId;
import java.util.Collection; import java.util.Collection;
import java.util.Date; import java.util.Date;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.Callable;
@UtilityClass @UtilityClass
public class AuditLog { public class AuditLog {
@ -28,21 +26,25 @@ public class AuditLog {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document("performedBy", performer.toString()), TIME_BASED_SORT, AuditLogEntry::new, callback); MongoUtils.findAndTransform(COLLECTION_NAME, new Document("performedBy", performer.toString()), TIME_BASED_SORT, AuditLogEntry::new, callback);
} }
public static void findByPerformerIp(long machineIp, SingleResultCallback<Collection<AuditLogEntry>> callback) { public static void findByPerformerIp(String performerIp, SingleResultCallback<Collection<AuditLogEntry>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document("performedFrom", machineIp), TIME_BASED_SORT, AuditLogEntry::new, callback); MongoUtils.findAndTransform(COLLECTION_NAME, new Document("performedFrom", performerIp), TIME_BASED_SORT, AuditLogEntry::new, callback);
} }
public static void findByActionType(String actionType, SingleResultCallback<Collection<AuditLogEntry>> callback) { public static void findByActionType(String actionType, SingleResultCallback<Collection<AuditLogEntry>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document("actionType", actionType), TIME_BASED_SORT, AuditLogEntry::new, callback); MongoUtils.findAndTransform(COLLECTION_NAME, new Document("actionType", actionType), TIME_BASED_SORT, AuditLogEntry::new, callback);
} }
public static void findByEntryId(String entryId, SingleResultCallback<AuditLogEntry> callback) {
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", entryId), AuditLogEntry::new, callback);
}
public static void log(UUID user, String userIp, String actionType, Document data, SingleResultCallback<Void> callback) { public static void log(UUID user, String userIp, String actionType, Document data, SingleResultCallback<Void> callback) {
Document insert = new Document(); Document insert = new Document();
insert.put("_id", new ObjectId().toString()); insert.put("_id", new ObjectId().toString());
insert.put("performedBy", user.toString()); insert.put("performedBy", user.toString());
insert.put("performedAt", new Date()); insert.put("performedAt", new Date());
insert.put("performedFrom", IPUtils.humanToMachine(userIp)); insert.put("performedFrom", userIp);
insert.put("actionType", actionType); insert.put("actionType", actionType);
insert.put("actionData", data); insert.put("actionData", data);

View File

@ -4,14 +4,11 @@ import com.mongodb.async.SingleResultCallback;
import lombok.experimental.UtilityClass; import lombok.experimental.UtilityClass;
import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.model.EmailTemplate; import net.frozenorb.apiv3.model.EmailTemplate;
import net.frozenorb.apiv3.util.IPUtils;
import net.frozenorb.apiv3.util.MongoUtils; import net.frozenorb.apiv3.util.MongoUtils;
import org.bson.Document; import org.bson.Document;
import org.bson.types.ObjectId;
import java.time.Instant; import java.time.Instant;
import java.util.Collection; import java.util.Collection;
import java.util.Date;
import java.util.UUID; import java.util.UUID;
@UtilityClass @UtilityClass

View File

@ -2,11 +2,16 @@ package net.frozenorb.apiv3.accessor;
import com.mongodb.async.SingleResultCallback; import com.mongodb.async.SingleResultCallback;
import lombok.experimental.UtilityClass; import lombok.experimental.UtilityClass;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.model.Grant; import net.frozenorb.apiv3.model.Grant;
import net.frozenorb.apiv3.util.MongoUtils; import net.frozenorb.apiv3.util.MongoUtils;
import org.bson.Document; import org.bson.Document;
import org.bson.types.ObjectId;
import java.time.Instant;
import java.util.Collection; import java.util.Collection;
import java.util.Date;
import java.util.Set;
import java.util.UUID; import java.util.UUID;
@UtilityClass @UtilityClass
@ -31,4 +36,20 @@ public class Grants {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document("addedBy", addedBy.toString()), TIME_BASED_SORT, Grant::new, callback); MongoUtils.findAndTransform(COLLECTION_NAME, new Document("addedBy", addedBy.toString()), TIME_BASED_SORT, Grant::new, callback);
} }
public static void createGrant(UUID target, Set<String> scopes, String rank, Instant expiresAt, UUID addedBy, String reason, SingleResultCallback<Void> callback) {
Document insert = new Document();
insert.put("_id", new ObjectId().toString());
insert.put("target", target.toString());
insert.put("reason", reason);
insert.put("scopes", scopes);
insert.put("rank", rank);
insert.put("expiresAt", expiresAt);
insert.put("addedBy", addedBy.toString());
insert.put("addedAt", Instant.now());
APIv3.getMongo().getCollection(COLLECTION_NAME).insertOne(insert, callback);
}
} }

View File

@ -3,7 +3,6 @@ package net.frozenorb.apiv3.model;
import lombok.Getter; import lombok.Getter;
import lombok.ToString; import lombok.ToString;
import net.frozenorb.apiv3.accessor.AuditLog; import net.frozenorb.apiv3.accessor.AuditLog;
import net.frozenorb.apiv3.util.IPUtils;
import org.bson.Document; import org.bson.Document;
import java.time.Instant; import java.time.Instant;
@ -15,7 +14,7 @@ public final class AuditLogEntry extends BaseModel {
@Getter private String id; @Getter private String id;
@Getter private UUID performedBy; @Getter private UUID performedBy;
@Getter private Instant performedAt; @Getter private Instant performedAt;
@Getter private long performedFrom; @Getter private String performedFrom;
@Getter private String actionType; @Getter private String actionType;
@Getter private Document actionData; @Getter private Document actionData;
@ -25,7 +24,7 @@ public final class AuditLogEntry extends BaseModel {
this.id = json.getString("_id"); this.id = json.getString("_id");
this.performedBy = UUID.fromString(json.getString("performedBy")); this.performedBy = UUID.fromString(json.getString("performedBy"));
this.performedAt = (Instant) json.get("performedAt"); this.performedAt = (Instant) json.get("performedAt");
this.performedFrom = json.getLong("performedFrom"); this.performedFrom = json.getString("performedFrom");
this.actionType = json.getString("actionType"); this.actionType = json.getString("actionType");
this.actionData = (Document) json.get("actionData"); this.actionData = (Document) json.get("actionData");
@ -38,7 +37,7 @@ public final class AuditLogEntry extends BaseModel {
json.put("id", id); json.put("id", id);
json.put("performedBy", performedBy.toString()); json.put("performedBy", performedBy.toString());
json.put("performedAt", performedAt.toString()); json.put("performedAt", performedAt.toString());
json.put("performedFrom", IPUtils.machineToHuman(performedFrom)); json.put("performedFrom", performedFrom);
json.put("actionType", actionType); json.put("actionType", actionType);
return json; return json;

View File

@ -1,5 +1,8 @@
package net.frozenorb.apiv3.model; package net.frozenorb.apiv3.model;
import com.mongodb.async.SingleResultCallback;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import lombok.Getter; import lombok.Getter;
import lombok.ToString; import lombok.ToString;
import net.frozenorb.apiv3.accessor.EmailTemplates; import net.frozenorb.apiv3.accessor.EmailTemplates;
@ -42,7 +45,7 @@ public final class EmailTemplate extends BaseModel {
return json; return json;
} }
public void update(String title, String body, UUID updatedBy) { public void update(String title, String body, UUID updatedBy, SingleResultCallback<UpdateResult> callback) {
this.title = title; this.title = title;
this.body = body; this.body = body;
this.lastUpdatedAt = Instant.now(); this.lastUpdatedAt = Instant.now();
@ -55,11 +58,11 @@ public final class EmailTemplate extends BaseModel {
set.put("lastUpdatedAt", lastUpdatedAt.toString()); set.put("lastUpdatedAt", lastUpdatedAt.toString());
set.put("lastUpdatedBy", lastUpdatedBy.toString()); set.put("lastUpdatedBy", lastUpdatedBy.toString());
super.update(new Document("$set", set), (result, error) -> {}); super.update(new Document("$set", set), callback);
} }
public void delete() { public void delete(SingleResultCallback<DeleteResult> callback) {
super.delete((result, error) -> {}); super.delete(callback);
} }
public String fillTitle(Map<String, Object> replacements) { public String fillTitle(Map<String, Object> replacements) {

View File

@ -1,6 +1,9 @@
package net.frozenorb.apiv3.model; package net.frozenorb.apiv3.model;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.mongodb.async.SingleResultCallback;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import lombok.Getter; import lombok.Getter;
import lombok.ToString; import lombok.ToString;
import net.frozenorb.apiv3.accessor.Grants; import net.frozenorb.apiv3.accessor.Grants;
@ -50,7 +53,7 @@ public final class Grant extends BaseModel {
setId(id); setId(id);
} }
public void delete(UUID removedBy, String reason) { public void delete(UUID removedBy, String reason, SingleResultCallback<UpdateResult> callback) {
this.removedBy = removedBy; this.removedBy = removedBy;
this.removedAt = Instant.now(); this.removedAt = Instant.now();
this.removalReason = reason; this.removalReason = reason;
@ -61,7 +64,7 @@ public final class Grant extends BaseModel {
set.put("removedAt", removedAt); set.put("removedAt", removedAt);
set.put("removalReason", removalReason); set.put("removalReason", removalReason);
super.update(new Document("$set", set), (result, error) -> {}); super.update(new Document("$set", set), callback);
} }
public boolean isActive() { public boolean isActive() {

View File

@ -14,7 +14,7 @@ public final class IPLogEntry extends BaseModel {
@Getter private String id; @Getter private String id;
@Getter private UUID user; @Getter private UUID user;
@Getter private long ip; @Getter private String ip;
@Getter private Instant firstSeen; @Getter private Instant firstSeen;
@Getter private Instant lastSeen; @Getter private Instant lastSeen;
@Getter private int uses; @Getter private int uses;
@ -24,7 +24,7 @@ public final class IPLogEntry extends BaseModel {
this.id = json.getString("_id"); this.id = json.getString("_id");
this.user = UUID.fromString(json.getString("user")); this.user = UUID.fromString(json.getString("user"));
this.ip = json.getLong("ip"); this.ip = json.getString("ip");
this.lastSeen = (Instant) json.get("lastSeen"); this.lastSeen = (Instant) json.get("lastSeen");
this.firstSeen = (Instant) json.get("firstSeen"); this.firstSeen = (Instant) json.get("firstSeen");
this.uses = json.getInteger("uses"); this.uses = json.getInteger("uses");

View File

@ -1,37 +0,0 @@
package net.frozenorb.apiv3.util;
import lombok.experimental.UtilityClass;
@UtilityClass
public class IPUtils {
public static String machineToHuman(long machine) {
StringBuilder builder = new StringBuilder(15); // All IPv4 addresses are 15 characters
for (int i = 0; i < 4; i++) {
builder.insert(0, Long.toString(machine & 0xFF));
if (i < 3) {
builder.insert(0, '.');
}
machine >>= 8;
}
return builder.toString();
}
public static long humanToMachine(String human) {
long result = 0;
String[] octets = human.split("\\.");
for (int i = 3; i >= 0; i--) {
long ip = Long.parseLong(octets[3 - i]);
result |= ip << (i * 8);
}
return result;
}
}