Unban account & ip
This commit is contained in:
@@ -5,8 +5,10 @@ import database.ban.IpBanRepository;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.jcip.annotations.ThreadSafe;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -16,7 +18,7 @@ import java.util.Set;
|
||||
@Slf4j
|
||||
public class IpBanManager {
|
||||
private final IpBanRepository ipBanRepository;
|
||||
private final Set<String> bannedIps = new HashSet<>();
|
||||
private final Map<String, Integer> bannedIps = new HashMap<>();
|
||||
|
||||
public IpBanManager(IpBanRepository ipBanRepository) {
|
||||
this.ipBanRepository = ipBanRepository;
|
||||
@@ -25,11 +27,11 @@ public class IpBanManager {
|
||||
public synchronized void loadIpBans() {
|
||||
List<IpBan> ipBans = ipBanRepository.getAllIpBans();
|
||||
log.debug("Loaded {} ip bans", ipBans.size());
|
||||
bannedIps.addAll(ipBans.stream().map(IpBan::ip).toList());
|
||||
ipBans.forEach(ipBan -> bannedIps.put(ipBan.ip(), ipBan.accountId()));
|
||||
}
|
||||
|
||||
public synchronized boolean isBanned(String ip) {
|
||||
return bannedIps.contains(ip);
|
||||
return bannedIps.containsKey(ip);
|
||||
}
|
||||
|
||||
public synchronized void banIp(String ip, int accountId) {
|
||||
@@ -38,8 +40,24 @@ public class IpBanManager {
|
||||
}
|
||||
// TODO: validate ip format. Or create "Ip" model class.
|
||||
|
||||
bannedIps.add(ip);
|
||||
ipBanRepository.saveIpBan(accountId, ip);
|
||||
bannedIps.put(ip, accountId);
|
||||
ipBanRepository.saveIpBan(ip, accountId);
|
||||
}
|
||||
|
||||
public synchronized void unbanAccountIps(int accountId) {
|
||||
Set<String> ipsToUnban = new HashSet<>();
|
||||
for (Map.Entry<String, Integer> bannedIp : bannedIps.entrySet()) {
|
||||
if (bannedIp.getValue() == accountId) {
|
||||
ipsToUnban.add(bannedIp.getKey());
|
||||
}
|
||||
}
|
||||
|
||||
ipsToUnban.forEach(this::unbanIp);
|
||||
}
|
||||
|
||||
private void unbanIp(String ip) {
|
||||
bannedIps.remove(ip);
|
||||
ipBanRepository.deleteIpBan(ip);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user