From 9cd77d2b5529b9c8c368325b75d471f75ef8b7c6 Mon Sep 17 00:00:00 2001 From: defnotken Date: Fri, 5 Sep 2025 15:20:43 -0500 Subject: [PATCH] moving bancontroller --- .../Controllers/UserController.cs | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 LightlessSyncServer/LightlessSyncServer/Controllers/UserController.cs diff --git a/LightlessSyncServer/LightlessSyncServer/Controllers/UserController.cs b/LightlessSyncServer/LightlessSyncServer/Controllers/UserController.cs new file mode 100644 index 0000000..c84ff49 --- /dev/null +++ b/LightlessSyncServer/LightlessSyncServer/Controllers/UserController.cs @@ -0,0 +1,98 @@ +using LightlessSync.API.Routes; +using LightlessSyncShared.Data; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; + +namespace LightlessSyncAuthService.Controllers; + +[Route(LightlessAuth.User)] +[Authorize(Policy = "Internal")] +public class UserController : Controller +{ + protected readonly ILogger Logger; + protected readonly IDbContextFactory LightlessDbContextFactory; + public UserController(ILogger logger, IDbContextFactory lightlessDbContext) + { + Logger = logger; + LightlessDbContextFactory = lightlessDbContext; + } + + [Route(LightlessAuth.Ban_Uid)] + [HttpPost] + public async Task MarkForBanUid(string uid) + { + using var dbContext = await LightlessDbContextFactory.CreateDbContextAsync(); + + Logger.LogInformation("Banning user with UID {UID}", uid); + + //Mark User as banned, and not marked for ban + var auth = await dbContext.Auth.FirstOrDefaultAsync(f => f.UserUID == uid); + if (auth != null) + { + auth.MarkForBan = true; + } + + await dbContext.SaveChangesAsync(); + } + + [Route(LightlessAuth.User_Unban_Uid)] + [HttpPost] + public async Task UnBanUserByUid(string uid) + { + using var dbContext = await LightlessDbContextFactory.CreateDbContextAsync(); + + Logger.LogInformation("Unbanning user with UID {UID}", uid); + + //Mark User as not banned, and not marked for ban (if marked) + var auth = await dbContext.Auth.FirstOrDefaultAsync(f => f.UserUID == uid); + if (auth != null) + { + auth.IsBanned = false; + auth.MarkForBan = false; + } + + // Remove all bans associated with this user + var bannedFromLightlessIds = dbContext.BannedUsers.Where(b => b.BannedUid == uid); + dbContext.BannedUsers.RemoveRange(bannedFromLightlessIds); + + // Remove all character/discord bans associated with this user + var lodestoneAuths = dbContext.LodeStoneAuth.Where(l => l.User != null && l.User.UID == uid).ToList(); + foreach (var lodestoneAuth in lodestoneAuths) + { + var bannedRegs = dbContext.BannedRegistrations.Where(b => b.DiscordIdOrLodestoneAuth == lodestoneAuth.HashedLodestoneId || b.DiscordIdOrLodestoneAuth == lodestoneAuth.DiscordId.ToString()); + dbContext.BannedRegistrations.RemoveRange(bannedRegs); + } + + await dbContext.SaveChangesAsync(); + } + + [Route(LightlessAuth.User_Unban_Discord)] + [HttpPost] + public async Task UnBanUserByDiscordId(string discordId) + { + Logger.LogInformation("Unbanning user with discordId: {discordId}", discordId); + using var dbContext = await LightlessDbContextFactory.CreateDbContextAsync(); + + var userByDiscord = await dbContext.LodeStoneAuth.Include(l => l.User).FirstOrDefaultAsync(l => l.DiscordId.ToString() == discordId); + + if (userByDiscord?.User == null) + { + Logger.LogInformation("Unbanning user with discordId: {discordId} but no user found", discordId); + return; + } + var bannedRegs = dbContext.BannedRegistrations.Where(b => b.DiscordIdOrLodestoneAuth == discordId || b.DiscordIdOrLodestoneAuth == userByDiscord.HashedLodestoneId); + //Mark User as not banned, and not marked for ban (if marked) + var auth = await dbContext.Auth.FirstOrDefaultAsync(f => f.UserUID == userByDiscord.User.UID); + if (auth != null) + { + auth.IsBanned = false; + auth.MarkForBan = false; + } + // Remove all bans associated with this user + var bannedFromLightlessIds = dbContext.BannedUsers.Where(b => b.BannedUid == auth.UserUID || b.BannedUid == auth.PrimaryUserUID); + dbContext.BannedUsers.RemoveRange(bannedFromLightlessIds); + + await dbContext.SaveChangesAsync(); + } +} \ No newline at end of file