Part 2 of the big "make this closer to a RESTful api" commit [Doesn't compile]

This commit is contained in:
Colin McDonald 2016-06-22 17:14:51 -04:00
parent 8ade7066f9
commit d47b0a6170
11 changed files with 44 additions and 36 deletions

View File

@ -268,9 +268,9 @@ public final class APIv3 extends AbstractVerticle {
http.get("/punishments/:id").handler(new GETPunishmentsId());
http.get("/punishments").handler(new GETPunishments());
http.post("/punishments").blockingHandler(new POSTUserPunish(), false);
http.post("/punishments").blockingHandler(new POSTPunishments(), false);
http.delete("/punishments/:id").blockingHandler(new DELETEPunishments(), false);
http.delete("/user/:id/activePunishment").blockingHandler(new DELETEUserActivePunishment(), false);
http.delete("/users/:id/activePunishment").blockingHandler(new DELETEUserActivePunishment(), false);
http.get("/ranks/:id").handler(new GETRanksId());
http.get("/ranks").handler(new GETRanks());
@ -295,15 +295,15 @@ public final class APIv3 extends AbstractVerticle {
http.get("/users/:id").handler(new GETUser());
http.get("/users/:id/details").blockingHandler(new GETUserDetails(), false);
http.get("/users/:id/permissions").blockingHandler(new GETUserPermissions(), false);
http.get("/users/:id/requiresTOTP").handler(new GETUserRequiresTOTP());
http.get("/users/:id/requiresTotp").handler(new GETUserRequiresTotp());
http.get("/users/:id/verifyPassword").blockingHandler(new GETUserVerifyPassword(), false);
http.post("/users/:id/changePassword").blockingHandler(new POSTUserChangePassword(), false);
http.post("/users/:id/leave").handler(new POSTUserLeave());
http.post("/users/:id/login").blockingHandler(new POSTUserLogin());
http.post("/users/:id/notify").blockingHandler(new POSTUserNotify(), false);
http.post("/users/:id/register").blockingHandler(new POSTUserRegister(), false);
http.post("/users/:id/setupTOTP").blockingHandler(new POSTUserSetupTOTP(), false);
http.post("/users/:id/verifyTOTP").blockingHandler(new POSTUserVerifyTOTP(), false);
http.post("/users/:id/setupTotp").blockingHandler(new POSTUserSetupTotp(), false);
http.post("/users/:id/verifyTotp").blockingHandler(new POSTUserVerifyTotp(), false);
http.get("/dumps/:type").handler(new GETDumps());
http.get("/whoami").handler(new GETWhoAmI());
@ -328,6 +328,9 @@ public final class APIv3 extends AbstractVerticle {
public static void respondJson(RoutingContext ctx, int code, Object response) {
ctx.response().putHeader(HttpHeaders.CONTENT_TYPE, MediaType.JSON_UTF_8.toString());
ctx.response().setStatusCode(code);
if (!ctx.request().path().contains("dumps")) {
log.info(gson.toJson(response));
}
ctx.response().end(gson.toJson(response));
}

View File

@ -34,7 +34,7 @@ public final class AuditLogEntry {
@Getter private AuditLogActionType type;
@Getter private Map<String, Object> metadata;
public static void findAllPaginated(int skip, int pageSize, SingleResultCallback<List<AuditLogEntry>> callback) {
public static void findPaginated(Document query, int skip, int pageSize, SingleResultCallback<List<AuditLogEntry>> callback) {
auditLogCollection.find().sort(new Document("performedAt", -1)).skip(skip).limit(pageSize).into(new ArrayList<>(), callback);
}

View File

@ -188,6 +188,7 @@ public final class User {
if (!newUsername.equals(lastUsername)) {
this.lastUsername = newUsername;
// TODO: FIX MOJANG API CALL
User withNewUsername;
while ((withNewUsername = User.findByLastUsernameSync(newUsername)) != null) {
@ -394,7 +395,7 @@ public final class User {
Rank highestRank = getHighestRankScoped(ServerGroup.findById(server.getServerGroup()), grants);
Map<String, Object> access = ImmutableMap.of(
"allowed", true,
"message", "Public server"
"message", ""
);
if (activeBan != null) {

View File

@ -5,6 +5,7 @@ import io.vertx.ext.web.RoutingContext;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.model.AuditLogEntry;
import net.frozenorb.apiv3.util.ErrorUtils;
import org.bson.Document;
public final class GETAuditLog implements Handler<RoutingContext> {
@ -13,7 +14,7 @@ public final class GETAuditLog implements Handler<RoutingContext> {
int skip = ctx.request().getParam("skip") == null ? 0 : Integer.parseInt(ctx.request().getParam("skip"));
int pageSize = ctx.request().getParam("pageSize") == null ? 100 : Integer.parseInt(ctx.request().getParam("pageSize"));
AuditLogEntry.findAllPaginated(skip, pageSize, (auditLog, error) -> {
AuditLogEntry.findPaginated(new Document(), skip, pageSize, (auditLog, error) -> {
if (error != null) {
ErrorUtils.respondInternalError(ctx, error);
} else {

View File

@ -8,22 +8,25 @@ import net.frozenorb.apiv3.model.Rank;
import net.frozenorb.apiv3.model.ServerGroup;
import net.frozenorb.apiv3.model.User;
import net.frozenorb.apiv3.util.ErrorUtils;
import org.bson.Document;
import java.time.Instant;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public final class POSTGrants implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) {
User target = User.findByIdSync(ctx.request().getParam("id"));
Document body = Document.parse(ctx.getBodyAsString());
User target = User.findByIdSync(body.getString("user"));
if (target == null) {
ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("id"));
ErrorUtils.respondNotFound(ctx, "User", body.getString("user"));
return;
}
String reason = ctx.request().getParam("reason");
String reason = body.getString("reason");
if (reason == null || reason.trim().isEmpty()) {
ErrorUtils.respondRequiredInput(ctx, "reason");
@ -31,10 +34,10 @@ public final class POSTGrants implements Handler<RoutingContext> {
}
Set<ServerGroup> scopes = new HashSet<>();
String scopesUnparsed = ctx.request().getParam("scopes");
List<String> scopeIds = (List<String>) body.get("scopes"); // TODO: SHOULD BE ARRAY
if (!scopesUnparsed.isEmpty()) {
for (String serverGroupId : scopesUnparsed.split(",")) {
if (!scopeIds.isEmpty()) {
for (String serverGroupId : scopeIds) {
ServerGroup serverGroup = ServerGroup.findById(serverGroupId);
if (serverGroup == null) {
@ -46,19 +49,17 @@ public final class POSTGrants implements Handler<RoutingContext> {
}
}
Rank rank = Rank.findById(ctx.request().getParam("rank"));
Rank rank = Rank.findById(body.getString("rank"));
if (rank == null) {
ErrorUtils.respondNotFound(ctx, "Rank", ctx.request().getParam("rank"));
ErrorUtils.respondNotFound(ctx, "Rank", body.getString("rank"));
return;
}
Instant expiresAt = null;
try {
expiresAt = Instant.ofEpochMilli(Long.parseLong(ctx.request().getParam("expiresAt")));
} catch (NumberFormatException ignored) {
// Just leave it null, we don't need an expiration date.
if (body.containsKey("expiresAt") && body.get("expiresAt", Number.class).longValue() != -1) {
expiresAt = Instant.ofEpochMilli(body.get("expiresAt", Number.class).longValue());
}
if (expiresAt != null && expiresAt.isBefore(Instant.now())) {
@ -67,7 +68,7 @@ public final class POSTGrants implements Handler<RoutingContext> {
}
// We purposely don't do a null check, grants don't have to have a source.
User addedBy = User.findByIdSync(ctx.request().getParam("addedBy"));
User addedBy = User.findByIdSync(body.getString("addedBy"));
Grant grant = new Grant(target, reason, scopes, rank, expiresAt, addedBy);
grant.insert();

View File

@ -9,6 +9,7 @@ import net.frozenorb.apiv3.util.ErrorUtils;
public final class GETPunishments implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) {
// USER PARAM
try {
int skip = ctx.request().getParam("skip") == null ? 0 : Integer.parseInt(ctx.request().getParam("skip"));
int pageSize = ctx.request().getParam("pageSize") == null ? 100 : Integer.parseInt(ctx.request().getParam("pageSize"));

View File

@ -15,24 +15,25 @@ import org.bson.Document;
import java.time.Instant;
import java.util.Map;
public final class POSTUserPunish implements Handler<RoutingContext> {
public final class POSTPunishments implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) {
User target = User.findByIdSync(ctx.request().getParam("id"));
Document body = Document.parse(ctx.getBodyAsString());
User target = User.findByIdSync(body.getString("user"));
if (target == null) {
ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("id"));
ErrorUtils.respondNotFound(ctx, "User", body.getString("user"));
return;
}
String reason = ctx.request().getParam("reason");
String reason = body.getString("reason");
if (reason == null || reason.trim().isEmpty()) {
ErrorUtils.respondRequiredInput(ctx, "reason");
return;
}
Punishment.PunishmentType type = Punishment.PunishmentType.valueOf(ctx.request().getParam("type"));
Punishment.PunishmentType type = Punishment.PunishmentType.valueOf(body.getString("type"));
if (type != Punishment.PunishmentType.WARN) {
for (Punishment punishment : Punishment.findByUserAndTypeSync(target, ImmutableSet.of(type))) {
@ -45,10 +46,8 @@ public final class POSTUserPunish implements Handler<RoutingContext> {
Instant expiresAt = null;
try {
expiresAt = Instant.ofEpochMilli(Long.parseLong(ctx.request().getParam("expiresAt")));
} catch (NumberFormatException ignored) {
// Just leave it null, we don't need an expiration date.
if (body.containsKey("expiresAt") && body.get("expiresAt", Number.class).longValue() != -1) {
expiresAt = Instant.ofEpochMilli(body.get("expiresAt", Number.class).longValue());
}
if (expiresAt != null && expiresAt.isBefore(Instant.now())) {
@ -56,7 +55,7 @@ public final class POSTUserPunish implements Handler<RoutingContext> {
return;
}
Map<String, Object> meta = Document.parse(ctx.getBodyAsString());
Map<String, Object> meta = (Map<String, Object>) body.get("metadata");
if (meta == null) {
ErrorUtils.respondRequiredInput(ctx, "request body meta");
@ -64,7 +63,7 @@ public final class POSTUserPunish implements Handler<RoutingContext> {
}
// We purposely don't do a null check, punishments don't have to have a source.
User addedBy = User.findByIdSync(ctx.request().getParam("addedBy"));
User addedBy = User.findByIdSync(body.getString("addedBy"));
if (target.hasPermissionAnywhere(Permissions.PROTECTED_PUNISHMENT)) {
ErrorUtils.respondGeneric(ctx, 200, target.getLastSeenOn() + " is protected from punishments.");
@ -73,7 +72,7 @@ public final class POSTUserPunish implements Handler<RoutingContext> {
Punishment punishment = new Punishment(target, reason, type, expiresAt, addedBy, ctx.get("actor"), meta);
String accessDenialReason = punishment.getAccessDenialReason();
String userIp = ctx.request().getParam("playerIp"); // TODO: YELL AT GRIFFIN FOR THIS, IT SHOULD BE USERIP
String userIp = body.getString("userIp");
if ((type == Punishment.PunishmentType.BAN || type == Punishment.PunishmentType.BLACKLIST) && userIp != null) {
IpBan ipBan = new IpBan(userIp, punishment);

View File

@ -10,7 +10,7 @@ import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.IpUtils;
import net.frozenorb.apiv3.util.TotpUtils;
public final class GETUserRequiresTOTP implements Handler<RoutingContext> {
public final class GETUserRequiresTotp implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) {
User.findById(ctx.request().getParam("id"), (user, error) -> {

View File

@ -9,7 +9,7 @@ import net.frozenorb.apiv3.model.User;
import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.TotpUtils;
public final class POSTUserSetupTOTP implements Handler<RoutingContext> {
public final class POSTUserSetupTotp implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) {
User user = User.findByIdSync(ctx.request().getParam("id"));

View File

@ -12,7 +12,7 @@ import net.frozenorb.apiv3.util.TotpUtils;
import java.util.concurrent.TimeUnit;
public final class POSTUserVerifyTOTP implements Handler<RoutingContext> {
public final class POSTUserVerifyTotp implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) {
User user = User.findByIdSync(ctx.request().getParam("id"));

View File

@ -13,6 +13,8 @@ import java.util.UUID;
public class MojangUtils {
public static void getName(UUID id, SingleResultCallback<String> callback) {
System.out.println("GET " + id.toString().replace("-", ""));
APIv3.getHttpClient().get("sessionserver.mojang.com", "/session/minecraft/profile/" + id.toString().replace("-", ""), (response) -> {
response.bodyHandler((body) -> {
try {