From a45a91723ac7add5af307fb6f67b26f7be39a617 Mon Sep 17 00:00:00 2001 From: Alexander von Trostorff Date: Thu, 1 Nov 2018 16:54:03 +0100 Subject: [PATCH] Ban Revoke Structure Fixes #95 --- .../legacy/LegacyServicesActivators.java | 3 +- .../faforever/server/security/BanDetails.java | 31 +++++++++++++------ .../server/security/FafUserDetails.java | 7 +---- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/faf-java-server-app/src/main/java/com/faforever/server/integration/legacy/LegacyServicesActivators.java b/faf-java-server-app/src/main/java/com/faforever/server/integration/legacy/LegacyServicesActivators.java index 4608dc3..cc700f0 100644 --- a/faf-java-server-app/src/main/java/com/faforever/server/integration/legacy/LegacyServicesActivators.java +++ b/faf-java-server-app/src/main/java/com/faforever/server/integration/legacy/LegacyServicesActivators.java @@ -27,7 +27,6 @@ import org.springframework.transaction.annotation.Transactional; import javax.inject.Inject; -import java.time.Instant; import java.util.Comparator; import static com.faforever.server.integration.MessageHeaders.CLIENT_CONNECTION; @@ -93,7 +92,7 @@ public void loginRequest(LegacyLoginRequest loginRequest, @Header(CLIENT_CONNECT /** Throws an exception if the account has an active ban. */ private void checkBan(FafUserDetails userDetails) { userDetails.getPlayer().getBanDetails().stream() - .filter(banDetail -> !banDetail.isRevoked() && (banDetail.getExpiresAt() == null || banDetail.getExpiresAt().toInstant().isAfter(Instant.now()))) + .filter(BanDetails::isActive) .max(Comparator.comparing(BanDetails::getId)) .ifPresent(banDetails -> { if (banDetails.getExpiresAt() != null) { diff --git a/faf-java-server-app/src/main/java/com/faforever/server/security/BanDetails.java b/faf-java-server-app/src/main/java/com/faforever/server/security/BanDetails.java index c0d1569..ff8c470 100644 --- a/faf-java-server-app/src/main/java/com/faforever/server/security/BanDetails.java +++ b/faf-java-server-app/src/main/java/com/faforever/server/security/BanDetails.java @@ -3,7 +3,6 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; -import org.hibernate.annotations.Formula; import javax.persistence.Column; import javax.persistence.Entity; @@ -13,7 +12,8 @@ import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; -import java.sql.Timestamp; +import javax.validation.constraints.NotNull; +import java.time.OffsetDateTime; @Entity @Table(name = "ban") @@ -25,27 +25,38 @@ public class BanDetails { @Id @Column(name = "id") private Integer id; - @ManyToOne @JoinColumn(name = "player_id") + @NotNull private User user; - @ManyToOne @JoinColumn(name = "author_id") private User author; - @Column(name = "reason") + @NotNull private String reason; - @Column(name = "expires_at") - private Timestamp expiresAt; - + private OffsetDateTime expiresAt; @Column(name = "level") @Enumerated(EnumType.STRING) private BanScope scope; + @Column(name = "revoke_reason") + private String revokeReason; + @ManyToOne + @JoinColumn(name = "revoke_author_id") + private User revokeAuthor; + @Column(name = "revoke_time") + private OffsetDateTime revokeTime; - @Formula(value = "(SELECT count(1) FROM ban_revoke WHERE ban_revoke.ban_id = id)") - private boolean revoked; + public boolean isActive() { + if (revokeTime != null && revokeTime.isBefore(OffsetDateTime.now())) { + return false; + } + if (expiresAt == null) { + return true; + } + return expiresAt.isAfter(OffsetDateTime.now()); + } public enum BanScope { CHAT, GLOBAL diff --git a/faf-java-server-app/src/main/java/com/faforever/server/security/FafUserDetails.java b/faf-java-server-app/src/main/java/com/faforever/server/security/FafUserDetails.java index f55225a..62958bd 100644 --- a/faf-java-server-app/src/main/java/com/faforever/server/security/FafUserDetails.java +++ b/faf-java-server-app/src/main/java/com/faforever/server/security/FafUserDetails.java @@ -11,8 +11,6 @@ import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; -import java.sql.Timestamp; -import java.time.Instant; import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -62,9 +60,6 @@ private static List getRoles(User user) { private static boolean isNonLocked(Set banDetails) { return banDetails.stream() - .noneMatch(details -> - !details.isRevoked() - && (details.getExpiresAt() == null || details.getExpiresAt().after(Timestamp.from(Instant.now()))) - ); + .noneMatch(BanDetails::isActive); } }