Don't require removedBy field when deleting grants, ip bans, and punishments

This commit is contained in:
Colin McDonald 2016-07-11 22:53:31 -04:00
parent e48a0c1736
commit 254d5336bf
7 changed files with 54 additions and 58 deletions

View File

@ -113,7 +113,7 @@ public final class Grant {
}
public boolean isRemoved() {
return removedBy != null;
return removedAt != null;
}
public boolean appliesOn(ServerGroup serverGroup) {
@ -129,7 +129,7 @@ public final class Grant {
}
public void delete(User removedBy, String reason, SingleResultCallback<Void> callback) {
this.removedBy = removedBy.getId();
this.removedBy = removedBy == null ? null : removedBy.getId();
this.removedAt = Instant.now();
this.removalReason = reason;

View File

@ -103,7 +103,7 @@ public final class IpBan {
}
public boolean isRemoved() {
return removedBy != null;
return removedAt != null;
}
public void getAccessDenialReason(SingleResultCallback<String> callback) {
@ -150,7 +150,7 @@ public final class IpBan {
}
public void delete(User removedBy, String reason, SingleResultCallback<Void> callback) {
this.removedBy = removedBy.getId();
this.removedBy = removedBy == null ? null : removedBy.getId();
this.removedAt = Instant.now();
this.removalReason = reason;

View File

@ -122,7 +122,7 @@ public final class Punishment {
}
public boolean isRemoved() {
return removedBy != null;
return removedAt != null;
}
public String getAccessDenialReason() {
@ -153,7 +153,7 @@ public final class Punishment {
}
public void delete(User removedBy, String reason, SingleResultCallback<Void> callback) {
this.removedBy = removedBy.getId();
this.removedBy = removedBy == null ? null : removedBy.getId();
this.removedAt = Instant.now();
this.removalReason = reason;

View File

@ -26,13 +26,8 @@ public final class DELETEGrantsId implements Handler<RoutingContext> {
}
JsonObject requestBody = ctx.getBodyAsJson();
// We purposely don't do a null check, grant removals don't have to have a user/ip.
User removedBy = SyncUtils.runBlocking(v -> User.findById(requestBody.getString("removedBy"), v));
if (removedBy == null) {
ErrorUtils.respondNotFound(ctx, "User", requestBody.getString("removedBy"));
return;
}
String reason = requestBody.getString("reason");
if (reason == null || reason.trim().isEmpty()) {
@ -42,6 +37,7 @@ public final class DELETEGrantsId implements Handler<RoutingContext> {
SyncUtils.<Void>runBlocking(v -> grant.delete(removedBy, reason, v));
if (removedBy != null) {
AuditLog.log(removedBy.getId(), requestBody.getString("removedByIp"), ctx, AuditLogActionType.GRANT_DELETE, ImmutableMap.of("grantId", grant.getId()), (ignored, error) -> {
if (error != null) {
ErrorUtils.respondInternalError(ctx, error);
@ -49,6 +45,9 @@ public final class DELETEGrantsId implements Handler<RoutingContext> {
APIv3.respondJson(ctx, 200, grant);
}
});
} else {
APIv3.respondJson(ctx, 200, grant);
}
}
}

View File

@ -26,13 +26,8 @@ public final class DELETEIpBansId implements Handler<RoutingContext> {
}
JsonObject requestBody = ctx.getBodyAsJson();
// We purposely don't do a null check, ip ban removals don't have to have a user/ip.
User removedBy = SyncUtils.runBlocking(v -> User.findById(requestBody.getString("removedBy"), v));
if (removedBy == null) {
ErrorUtils.respondNotFound(ctx, "User", requestBody.getString("removedBy"));
return;
}
String reason = requestBody.getString("reason");
if (reason == null || reason.trim().isEmpty()) {
@ -42,6 +37,7 @@ public final class DELETEIpBansId implements Handler<RoutingContext> {
SyncUtils.<Void>runBlocking(v -> ipBan.delete(removedBy, reason, v));
if (removedBy != null) {
AuditLog.log(removedBy.getId(), requestBody.getString("removedByIp"), ctx, AuditLogActionType.IP_BAN_DELETE, ImmutableMap.of("punishmentId", ipBan.getId()), (ignored, error) -> {
if (error != null) {
ErrorUtils.respondInternalError(ctx, error);
@ -49,6 +45,9 @@ public final class DELETEIpBansId implements Handler<RoutingContext> {
APIv3.respondJson(ctx, 200, ipBan);
}
});
} else {
APIv3.respondJson(ctx, 200, ipBan);
}
}
}

View File

@ -26,13 +26,8 @@ public final class DELETEPunishmentsId implements Handler<RoutingContext> {
}
JsonObject requestBody = ctx.getBodyAsJson();
// We purposely don't do a null check, punishment removals don't have to have a user/ip.
User removedBy = SyncUtils.runBlocking(v -> User.findById(requestBody.getString("removedBy"), v));
if (removedBy == null) {
ErrorUtils.respondNotFound(ctx, "User", requestBody.getString("removedBy"));
return;
}
String reason = requestBody.getString("reason");
if (reason == null || reason.trim().isEmpty()) {
@ -42,6 +37,7 @@ public final class DELETEPunishmentsId implements Handler<RoutingContext> {
SyncUtils.<Void>runBlocking(v -> punishment.delete(removedBy, reason, v));
if (removedBy != null) {
AuditLog.log(removedBy.getId(), requestBody.getString("removedByIp"), ctx, AuditLogActionType.PUNISHMENT_DELETE, ImmutableMap.of("punishmentId", punishment.getId()), (ignored, error) -> {
if (error != null) {
ErrorUtils.respondInternalError(ctx, error);
@ -49,6 +45,9 @@ public final class DELETEPunishmentsId implements Handler<RoutingContext> {
APIv3.respondJson(ctx, 200, punishment);
}
});
} else {
APIv3.respondJson(ctx, 200, punishment);
}
}
}

View File

@ -27,13 +27,8 @@ public final class DELETEUsersIdActivePunishment implements Handler<RoutingConte
JsonObject requestBody = ctx.getBodyAsJson();
Punishment.PunishmentType type = Punishment.PunishmentType.valueOf(requestBody.getString("type").toUpperCase());
// We purposely don't do a null check, punishment removals don't have to have a user/ip.
User removedBy = SyncUtils.runBlocking(v -> User.findById(requestBody.getString("removedBy"), v));
if (removedBy == null) {
ErrorUtils.respondNotFound(ctx, "User", requestBody.getString("removedBy"));
return;
}
String reason = requestBody.getString("reason");
if (reason == null || reason.trim().isEmpty()) {
@ -59,6 +54,7 @@ public final class DELETEUsersIdActivePunishment implements Handler<RoutingConte
Punishment finalActivePunishment = activePunishment;
SyncUtils.<Void>runBlocking(v -> finalActivePunishment.delete(removedBy, reason, v));
if (removedBy != null) {
AuditLog.log(removedBy.getId(), requestBody.getString("removedByIp"), ctx, AuditLogActionType.PUNISHMENT_DELETE, ImmutableMap.of("punishmentId", activePunishment.getId()), (ignored, error) -> {
if (error != null) {
ErrorUtils.respondInternalError(ctx, error);
@ -66,6 +62,9 @@ public final class DELETEUsersIdActivePunishment implements Handler<RoutingConte
APIv3.respondJson(ctx, 200, finalActivePunishment);
}
});
} else {
APIv3.respondJson(ctx, 200, finalActivePunishment);
}
}
}