Role bans (#6703)
This commit is contained in:
@@ -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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user