Role bans (#6703)
This commit is contained in:
@@ -306,6 +306,37 @@ namespace Content.Server.Database
|
||||
public abstract Task AddServerUnbanAsync(ServerUnbanDef serverUnban);
|
||||
#endregion
|
||||
|
||||
#region Role Bans
|
||||
/*
|
||||
* ROLE BANS
|
||||
*/
|
||||
/// <summary>
|
||||
/// Looks up a role ban by id.
|
||||
/// This will return a pardoned role ban as well.
|
||||
/// </summary>
|
||||
/// <param name="id">The role ban id to look for.</param>
|
||||
/// <returns>The role ban with the given id or null if none exist.</returns>
|
||||
public abstract Task<ServerRoleBanDef?> GetServerRoleBanAsync(int id);
|
||||
|
||||
/// <summary>
|
||||
/// Looks up an user's role ban history.
|
||||
/// This will return pardoned role bans based on the <see cref="includeUnbanned"/> bool.
|
||||
/// Requires one of <see cref="address"/>, <see cref="userId"/>, or <see cref="hwId"/> to not be null.
|
||||
/// </summary>
|
||||
/// <param name="address">The IP address of the user.</param>
|
||||
/// <param name="userId">The NetUserId of the user.</param>
|
||||
/// <param name="hwId">The Hardware Id of the user.</param>
|
||||
/// <param name="includeUnbanned">Whether expired and pardoned bans are included.</param>
|
||||
/// <returns>The user's role ban history.</returns>
|
||||
public abstract Task<List<ServerRoleBanDef>> GetServerRoleBansAsync(IPAddress? address,
|
||||
NetUserId? userId,
|
||||
ImmutableArray<byte>? hwId,
|
||||
bool includeUnbanned);
|
||||
|
||||
public abstract Task AddServerRoleBanAsync(ServerRoleBanDef serverRoleBan);
|
||||
public abstract Task AddServerRoleUnbanAsync(ServerRoleUnbanDef serverRoleUnban);
|
||||
#endregion
|
||||
|
||||
#region Player Records
|
||||
/*
|
||||
* PLAYER RECORDS
|
||||
|
||||
@@ -90,6 +90,35 @@ namespace Content.Server.Database
|
||||
Task AddServerUnbanAsync(ServerUnbanDef serverBan);
|
||||
#endregion
|
||||
|
||||
#region Role Bans
|
||||
/// <summary>
|
||||
/// Looks up a role ban by id.
|
||||
/// This will return a pardoned role ban as well.
|
||||
/// </summary>
|
||||
/// <param name="id">The role ban id to look for.</param>
|
||||
/// <returns>The role ban with the given id or null if none exist.</returns>
|
||||
Task<ServerRoleBanDef?> GetServerRoleBanAsync(int id);
|
||||
|
||||
/// <summary>
|
||||
/// Looks up an user's role ban history.
|
||||
/// This will return pardoned role bans based on the <see cref="includeUnbanned"/> bool.
|
||||
/// Requires one of <see cref="address"/>, <see cref="userId"/>, or <see cref="hwId"/> to not be null.
|
||||
/// </summary>
|
||||
/// <param name="address">The IP address of the user.</param>
|
||||
/// <param name="userId">The NetUserId of the user.</param>
|
||||
/// <param name="hwId">The Hardware Id of the user.</param>
|
||||
/// <param name="includeUnbanned">Whether expired and pardoned bans are included.</param>
|
||||
/// <returns>The user's role ban history.</returns>
|
||||
Task<List<ServerRoleBanDef>> GetServerRoleBansAsync(
|
||||
IPAddress? address,
|
||||
NetUserId? userId,
|
||||
ImmutableArray<byte>? hwId,
|
||||
bool includeUnbanned = true);
|
||||
|
||||
Task AddServerRoleBanAsync(ServerRoleBanDef serverBan);
|
||||
Task AddServerRoleUnbanAsync(ServerRoleUnbanDef serverBan);
|
||||
#endregion
|
||||
|
||||
#region Player Records
|
||||
Task UpdatePlayerRecordAsync(
|
||||
NetUserId userId,
|
||||
@@ -264,6 +293,32 @@ namespace Content.Server.Database
|
||||
return _db.AddServerUnbanAsync(serverUnban);
|
||||
}
|
||||
|
||||
#region Role Ban
|
||||
public Task<ServerRoleBanDef?> GetServerRoleBanAsync(int id)
|
||||
{
|
||||
return _db.GetServerRoleBanAsync(id);
|
||||
}
|
||||
|
||||
public Task<List<ServerRoleBanDef>> GetServerRoleBansAsync(
|
||||
IPAddress? address,
|
||||
NetUserId? userId,
|
||||
ImmutableArray<byte>? hwId,
|
||||
bool includeUnbanned = true)
|
||||
{
|
||||
return _db.GetServerRoleBansAsync(address, userId, hwId, includeUnbanned);
|
||||
}
|
||||
|
||||
public Task AddServerRoleBanAsync(ServerRoleBanDef serverRoleBan)
|
||||
{
|
||||
return _db.AddServerRoleBanAsync(serverRoleBan);
|
||||
}
|
||||
|
||||
public Task AddServerRoleUnbanAsync(ServerRoleUnbanDef serverRoleUnban)
|
||||
{
|
||||
return _db.AddServerRoleUnbanAsync(serverRoleUnban);
|
||||
}
|
||||
#endregion
|
||||
|
||||
public Task UpdatePlayerRecordAsync(
|
||||
NetUserId userId,
|
||||
string userName,
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Collections.Immutable;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
@@ -34,6 +32,7 @@ namespace Content.Server.Database
|
||||
});
|
||||
}
|
||||
|
||||
#region Ban
|
||||
public override async Task<ServerBanDef?> GetServerBanAsync(int id)
|
||||
{
|
||||
await using var db = await GetDbImpl();
|
||||
@@ -52,9 +51,9 @@ namespace Content.Server.Database
|
||||
NetUserId? userId,
|
||||
ImmutableArray<byte>? hwId)
|
||||
{
|
||||
if (address == null && userId == null)
|
||||
if (address == null && userId == null && hwId == null)
|
||||
{
|
||||
throw new ArgumentException("Address and userId cannot both be null");
|
||||
throw new ArgumentException("Address, userId, and hwId cannot all be null");
|
||||
}
|
||||
|
||||
await using var db = await GetDbImpl();
|
||||
@@ -73,7 +72,7 @@ namespace Content.Server.Database
|
||||
{
|
||||
if (address == null && userId == null && hwId == null)
|
||||
{
|
||||
throw new ArgumentException("Address and userId cannot both be null");
|
||||
throw new ArgumentException("Address, userId, and hwId cannot all be null");
|
||||
}
|
||||
|
||||
await using var db = await GetDbImpl();
|
||||
@@ -225,6 +224,191 @@ namespace Content.Server.Database
|
||||
|
||||
await db.PgDbContext.SaveChangesAsync();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Role Ban
|
||||
public override async Task<ServerRoleBanDef?> GetServerRoleBanAsync(int id)
|
||||
{
|
||||
await using var db = await GetDbImpl();
|
||||
|
||||
var query = db.PgDbContext.RoleBan
|
||||
.Include(p => p.Unban)
|
||||
.Where(p => p.Id == id);
|
||||
|
||||
var ban = await query.SingleOrDefaultAsync();
|
||||
|
||||
return ConvertRoleBan(ban);
|
||||
|
||||
}
|
||||
|
||||
public override async Task<List<ServerRoleBanDef>> GetServerRoleBansAsync(IPAddress? address,
|
||||
NetUserId? userId,
|
||||
ImmutableArray<byte>? hwId,
|
||||
bool includeUnbanned)
|
||||
{
|
||||
if (address == null && userId == null && hwId == null)
|
||||
{
|
||||
throw new ArgumentException("Address, userId, and hwId cannot all be null");
|
||||
}
|
||||
|
||||
await using var db = await GetDbImpl();
|
||||
|
||||
var query = MakeRoleBanLookupQuery(address, userId, hwId, db, includeUnbanned)
|
||||
.OrderByDescending(b => b.BanTime);
|
||||
|
||||
return await QueryRoleBans(query);
|
||||
}
|
||||
|
||||
private static async Task<List<ServerRoleBanDef>> QueryRoleBans(IQueryable<ServerRoleBan> query)
|
||||
{
|
||||
var queryRoleBans = await query.ToArrayAsync();
|
||||
var bans = new List<ServerRoleBanDef>(queryRoleBans.Length);
|
||||
|
||||
foreach (var ban in queryRoleBans)
|
||||
{
|
||||
var banDef = ConvertRoleBan(ban);
|
||||
|
||||
if (banDef != null)
|
||||
{
|
||||
bans.Add(banDef);
|
||||
}
|
||||
}
|
||||
|
||||
return bans;
|
||||
}
|
||||
|
||||
private static IQueryable<ServerRoleBan> MakeRoleBanLookupQuery(
|
||||
IPAddress? address,
|
||||
NetUserId? userId,
|
||||
ImmutableArray<byte>? hwId,
|
||||
DbGuardImpl db,
|
||||
bool includeUnbanned)
|
||||
{
|
||||
IQueryable<ServerRoleBan>? query = null;
|
||||
|
||||
if (userId is { } uid)
|
||||
{
|
||||
var newQ = db.PgDbContext.RoleBan
|
||||
.Include(p => p.Unban)
|
||||
.Where(b => b.UserId == uid.UserId);
|
||||
|
||||
query = query == null ? newQ : query.Union(newQ);
|
||||
}
|
||||
|
||||
if (address != null)
|
||||
{
|
||||
var newQ = db.PgDbContext.RoleBan
|
||||
.Include(p => p.Unban)
|
||||
.Where(b => b.Address != null && EF.Functions.ContainsOrEqual(b.Address.Value, address));
|
||||
|
||||
query = query == null ? newQ : query.Union(newQ);
|
||||
}
|
||||
|
||||
if (hwId != null)
|
||||
{
|
||||
var newQ = db.PgDbContext.RoleBan
|
||||
.Include(p => p.Unban)
|
||||
.Where(b => b.HWId!.SequenceEqual(hwId.Value.ToArray()));
|
||||
|
||||
query = query == null ? newQ : query.Union(newQ);
|
||||
}
|
||||
|
||||
if (!includeUnbanned)
|
||||
{
|
||||
query = query?.Where(p =>
|
||||
p.Unban == null && (p.ExpirationTime == null || p.ExpirationTime.Value > DateTime.Now));
|
||||
}
|
||||
|
||||
query = query!.Distinct();
|
||||
return query;
|
||||
}
|
||||
|
||||
private static ServerRoleBanDef? ConvertRoleBan(ServerRoleBan? ban)
|
||||
{
|
||||
if (ban == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
NetUserId? uid = null;
|
||||
if (ban.UserId is {} guid)
|
||||
{
|
||||
uid = new NetUserId(guid);
|
||||
}
|
||||
|
||||
NetUserId? aUid = null;
|
||||
if (ban.BanningAdmin is {} aGuid)
|
||||
{
|
||||
aUid = new NetUserId(aGuid);
|
||||
}
|
||||
|
||||
var unbanDef = ConvertRoleUnban(ban.Unban);
|
||||
|
||||
return new ServerRoleBanDef(
|
||||
ban.Id,
|
||||
uid,
|
||||
ban.Address,
|
||||
ban.HWId == null ? null : ImmutableArray.Create(ban.HWId),
|
||||
ban.BanTime,
|
||||
ban.ExpirationTime,
|
||||
ban.Reason,
|
||||
aUid,
|
||||
unbanDef,
|
||||
ban.RoleId);
|
||||
}
|
||||
|
||||
private static ServerRoleUnbanDef? ConvertRoleUnban(ServerRoleUnban? unban)
|
||||
{
|
||||
if (unban == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
NetUserId? aUid = null;
|
||||
if (unban.UnbanningAdmin is {} aGuid)
|
||||
{
|
||||
aUid = new NetUserId(aGuid);
|
||||
}
|
||||
|
||||
return new ServerRoleUnbanDef(
|
||||
unban.Id,
|
||||
aUid,
|
||||
unban.UnbanTime);
|
||||
}
|
||||
|
||||
public override async Task AddServerRoleBanAsync(ServerRoleBanDef serverRoleBan)
|
||||
{
|
||||
await using var db = await GetDbImpl();
|
||||
|
||||
db.PgDbContext.RoleBan.Add(new ServerRoleBan
|
||||
{
|
||||
Address = serverRoleBan.Address,
|
||||
HWId = serverRoleBan.HWId?.ToArray(),
|
||||
Reason = serverRoleBan.Reason,
|
||||
BanningAdmin = serverRoleBan.BanningAdmin?.UserId,
|
||||
BanTime = serverRoleBan.BanTime.UtcDateTime,
|
||||
ExpirationTime = serverRoleBan.ExpirationTime?.UtcDateTime,
|
||||
UserId = serverRoleBan.UserId?.UserId,
|
||||
RoleId = serverRoleBan.Role,
|
||||
});
|
||||
|
||||
await db.PgDbContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public override async Task AddServerRoleUnbanAsync(ServerRoleUnbanDef serverRoleUnban)
|
||||
{
|
||||
await using var db = await GetDbImpl();
|
||||
|
||||
db.PgDbContext.RoleUnban.Add(new ServerRoleUnban
|
||||
{
|
||||
BanId = serverRoleUnban.BanId,
|
||||
UnbanningAdmin = serverRoleUnban.UnbanningAdmin?.UserId,
|
||||
UnbanTime = serverRoleUnban.UnbanTime.UtcDateTime
|
||||
});
|
||||
|
||||
await db.PgDbContext.SaveChangesAsync();
|
||||
}
|
||||
#endregion
|
||||
|
||||
protected override PlayerRecord MakePlayerRecord(Player record)
|
||||
{
|
||||
|
||||
@@ -46,6 +46,7 @@ namespace Content.Server.Database
|
||||
}
|
||||
}
|
||||
|
||||
#region Ban
|
||||
public override async Task<ServerBanDef?> GetServerBanAsync(int id)
|
||||
{
|
||||
await using var db = await GetDbImpl();
|
||||
@@ -159,6 +160,162 @@ namespace Content.Server.Database
|
||||
|
||||
await db.SqliteDbContext.SaveChangesAsync();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Role Ban
|
||||
public override async Task<ServerRoleBanDef?> GetServerRoleBanAsync(int id)
|
||||
{
|
||||
await using var db = await GetDbImpl();
|
||||
|
||||
var ban = await db.SqliteDbContext.RoleBan
|
||||
.Include(p => p.Unban)
|
||||
.Where(p => p.Id == id)
|
||||
.SingleOrDefaultAsync();
|
||||
|
||||
return ConvertRoleBan(ban);
|
||||
}
|
||||
|
||||
public override async Task<List<ServerRoleBanDef>> GetServerRoleBansAsync(IPAddress? address,
|
||||
NetUserId? userId,
|
||||
ImmutableArray<byte>? hwId,
|
||||
bool includeUnbanned)
|
||||
{
|
||||
await using var db = await GetDbImpl();
|
||||
|
||||
// SQLite can't do the net masking stuff we need to match IP address ranges.
|
||||
// So just pull down the whole list into memory.
|
||||
var queryBans = await GetAllRoleBans(db.SqliteDbContext, includeUnbanned);
|
||||
|
||||
return queryBans
|
||||
.Where(b => BanMatches(b, address, userId, hwId))
|
||||
.Select(ConvertRoleBan)
|
||||
.ToList()!;
|
||||
}
|
||||
|
||||
private static async Task<List<ServerRoleBan>> GetAllRoleBans(
|
||||
SqliteServerDbContext db,
|
||||
bool includeUnbanned)
|
||||
{
|
||||
IQueryable<ServerRoleBan> query = db.RoleBan.Include(p => p.Unban);
|
||||
if (!includeUnbanned)
|
||||
{
|
||||
query = query.Where(p =>
|
||||
p.Unban == null && (p.ExpirationTime == null || p.ExpirationTime.Value > DateTime.UtcNow));
|
||||
}
|
||||
|
||||
return await query.ToListAsync();
|
||||
}
|
||||
|
||||
private static bool BanMatches(
|
||||
ServerRoleBan ban,
|
||||
IPAddress? address,
|
||||
NetUserId? userId,
|
||||
ImmutableArray<byte>? hwId)
|
||||
{
|
||||
if (address != null && ban.Address is not null && IPAddressExt.IsInSubnet(address, ban.Address.Value))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (userId is { } id && ban.UserId == id.UserId)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (hwId is { } hwIdVar && hwIdVar.AsSpan().SequenceEqual(ban.HWId))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public override async Task AddServerRoleBanAsync(ServerRoleBanDef serverBan)
|
||||
{
|
||||
await using var db = await GetDbImpl();
|
||||
|
||||
db.SqliteDbContext.RoleBan.Add(new ServerRoleBan
|
||||
{
|
||||
Address = serverBan.Address,
|
||||
Reason = serverBan.Reason,
|
||||
BanningAdmin = serverBan.BanningAdmin?.UserId,
|
||||
HWId = serverBan.HWId?.ToArray(),
|
||||
BanTime = serverBan.BanTime.UtcDateTime,
|
||||
ExpirationTime = serverBan.ExpirationTime?.UtcDateTime,
|
||||
UserId = serverBan.UserId?.UserId,
|
||||
RoleId = serverBan.Role,
|
||||
});
|
||||
|
||||
await db.SqliteDbContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public override async Task AddServerRoleUnbanAsync(ServerRoleUnbanDef serverUnban)
|
||||
{
|
||||
await using var db = await GetDbImpl();
|
||||
|
||||
db.SqliteDbContext.RoleUnban.Add(new ServerRoleUnban
|
||||
{
|
||||
BanId = serverUnban.BanId,
|
||||
UnbanningAdmin = serverUnban.UnbanningAdmin?.UserId,
|
||||
UnbanTime = serverUnban.UnbanTime.UtcDateTime
|
||||
});
|
||||
|
||||
await db.SqliteDbContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
private static ServerRoleBanDef? ConvertRoleBan(ServerRoleBan? ban)
|
||||
{
|
||||
if (ban == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
NetUserId? uid = null;
|
||||
if (ban.UserId is { } guid)
|
||||
{
|
||||
uid = new NetUserId(guid);
|
||||
}
|
||||
|
||||
NetUserId? aUid = null;
|
||||
if (ban.BanningAdmin is { } aGuid)
|
||||
{
|
||||
aUid = new NetUserId(aGuid);
|
||||
}
|
||||
|
||||
var unban = ConvertRoleUnban(ban.Unban);
|
||||
|
||||
return new ServerRoleBanDef(
|
||||
ban.Id,
|
||||
uid,
|
||||
ban.Address,
|
||||
ban.HWId == null ? null : ImmutableArray.Create(ban.HWId),
|
||||
ban.BanTime,
|
||||
ban.ExpirationTime,
|
||||
ban.Reason,
|
||||
aUid,
|
||||
unban,
|
||||
ban.RoleId);
|
||||
}
|
||||
|
||||
private static ServerRoleUnbanDef? ConvertRoleUnban(ServerRoleUnban? unban)
|
||||
{
|
||||
if (unban == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
NetUserId? aUid = null;
|
||||
if (unban.UnbanningAdmin is { } aGuid)
|
||||
{
|
||||
aUid = new NetUserId(aGuid);
|
||||
}
|
||||
|
||||
return new ServerRoleUnbanDef(
|
||||
unban.Id,
|
||||
aUid,
|
||||
unban.UnbanTime);
|
||||
}
|
||||
#endregion
|
||||
|
||||
protected override PlayerRecord MakePlayerRecord(Player record)
|
||||
{
|
||||
|
||||
56
Content.Server/Database/ServerRoleBanDef.cs
Normal file
56
Content.Server/Database/ServerRoleBanDef.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using System.Collections.Immutable;
|
||||
using System.Net;
|
||||
using Robust.Shared.Network;
|
||||
|
||||
namespace Content.Server.Database;
|
||||
|
||||
public sealed class ServerRoleBanDef
|
||||
{
|
||||
public int? Id { get; }
|
||||
public NetUserId? UserId { get; }
|
||||
public (IPAddress address, int cidrMask)? Address { get; }
|
||||
public ImmutableArray<byte>? HWId { get; }
|
||||
|
||||
public DateTimeOffset BanTime { get; }
|
||||
public DateTimeOffset? ExpirationTime { get; }
|
||||
public string Reason { get; }
|
||||
public NetUserId? BanningAdmin { get; }
|
||||
public ServerRoleUnbanDef? Unban { get; }
|
||||
public string Role { get; }
|
||||
|
||||
public ServerRoleBanDef(
|
||||
int? id,
|
||||
NetUserId? userId,
|
||||
(IPAddress, int)? address,
|
||||
ImmutableArray<byte>? hwId,
|
||||
DateTimeOffset banTime,
|
||||
DateTimeOffset? expirationTime,
|
||||
string reason,
|
||||
NetUserId? banningAdmin,
|
||||
ServerRoleUnbanDef? unban,
|
||||
string role)
|
||||
{
|
||||
if (userId == null && address == null && hwId == null)
|
||||
{
|
||||
throw new ArgumentException("Must have at least one of banned user, banned address or hardware ID");
|
||||
}
|
||||
|
||||
if (address is {} addr && addr.Item1.IsIPv4MappedToIPv6)
|
||||
{
|
||||
// Fix IPv6-mapped IPv4 addresses
|
||||
// So that IPv4 addresses are consistent between separate-socket and dual-stack socket modes.
|
||||
address = (addr.Item1.MapToIPv4(), addr.Item2 - 96);
|
||||
}
|
||||
|
||||
Id = id;
|
||||
UserId = userId;
|
||||
Address = address;
|
||||
HWId = hwId;
|
||||
BanTime = banTime;
|
||||
ExpirationTime = expirationTime;
|
||||
Reason = reason;
|
||||
BanningAdmin = banningAdmin;
|
||||
Unban = unban;
|
||||
Role = role;
|
||||
}
|
||||
}
|
||||
19
Content.Server/Database/ServerRoleUnbanDef.cs
Normal file
19
Content.Server/Database/ServerRoleUnbanDef.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using Robust.Shared.Network;
|
||||
|
||||
namespace Content.Server.Database;
|
||||
|
||||
public sealed class ServerRoleUnbanDef
|
||||
{
|
||||
public int BanId { get; }
|
||||
|
||||
public NetUserId? UnbanningAdmin { get; }
|
||||
|
||||
public DateTimeOffset UnbanTime { get; }
|
||||
|
||||
public ServerRoleUnbanDef(int banId, NetUserId? unbanningAdmin, DateTimeOffset unbanTime)
|
||||
{
|
||||
BanId = banId;
|
||||
UnbanningAdmin = unbanningAdmin;
|
||||
UnbanTime = unbanTime;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user