Skip to content

Commit

Permalink
Ban Revoke Structure
Browse files Browse the repository at this point in the history
Fixes #95
  • Loading branch information
1-alex98 committed Nov 1, 2018
1 parent 414b82b commit a45a917
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,9 +60,6 @@ private static List<GrantedAuthority> getRoles(User user) {

private static boolean isNonLocked(Set<BanDetails> banDetails) {
return banDetails.stream()
.noneMatch(details ->
!details.isRevoked()
&& (details.getExpiresAt() == null || details.getExpiresAt().after(Timestamp.from(Instant.now())))
);
.noneMatch(BanDetails::isActive);
}
}

0 comments on commit a45a917

Please sign in to comment.