Files
LightlessServer/LightlessSyncServer/LightlessSyncServer/Controllers/UserController.cs
defnotken 47b2a7a9b9 boolz
2025-10-29 18:26:41 -05:00

133 lines
5.1 KiB
C#

using LightlessSync.API.Dto.User;
using LightlessSync.API.Routes;
using LightlessSyncShared.Data;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace LightlessSyncServer.Controllers;
[Route(LightlessAuth.User)]
[Authorize(Policy = "Internal")]
public class UserController : Controller
{
protected readonly ILogger Logger;
protected readonly IDbContextFactory<LightlessDbContext> LightlessDbContextFactory;
public UserController(ILogger<UserController> logger, IDbContextFactory<LightlessDbContext> lightlessDbContext)
{
Logger = logger;
LightlessDbContextFactory = lightlessDbContext;
}
[Route(LightlessAuth.Ban_Uid)]
[HttpPost]
public async Task MarkForBanUid([FromBody] BanRequest request)
{
using var dbContext = await LightlessDbContextFactory.CreateDbContextAsync();
Logger.LogInformation("Banning user with UID {UID}", request.Uid);
//Mark User as banned, and not marked for ban
var auth = await dbContext.Auth.FirstOrDefaultAsync(f => f.UserUID == request.Uid);
if (auth != null)
{
auth.MarkForBan = true;
}
await dbContext.SaveChangesAsync();
}
[Route(LightlessAuth.User_Unban_Uid)]
[HttpPost]
public async Task UnBanUserByUid([FromBody] UnbanRequest request)
{
using var dbContext = await LightlessDbContextFactory.CreateDbContextAsync();
Logger.LogInformation("Unbanning user with UID {UID}", request.Uid);
//Mark User as not banned, and not marked for ban (if marked)
var auth = await dbContext.Auth.FirstOrDefaultAsync(f => f.UserUID == request.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 == request.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 == request.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([FromBody] UnbanRequest request)
{
Logger.LogInformation("Unbanning user with discordId: {discordId}", request.DiscordId);
using var dbContext = await LightlessDbContextFactory.CreateDbContextAsync();
var userByDiscord = await dbContext.LodeStoneAuth.Include(l => l.User).FirstOrDefaultAsync(l => l.DiscordId.ToString() == request.DiscordId);
if (userByDiscord?.User == null)
{
Logger.LogInformation("Unbanning user with discordId: {discordId} but no user found", request.DiscordId);
return;
}
var bannedRegs = dbContext.BannedRegistrations.Where(b => b.DiscordIdOrLodestoneAuth == request.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();
}
[Route(LightlessAuth.Disable_Profile)]
[HttpPost]
public async Task DisableGroupProfile([FromBody] UserProfileAvailabilityRequest request)
{
using var dbContext = await LightlessDbContextFactory.CreateDbContextAsync();
Logger.LogInformation("Disabling profile for user with uid {UID}", request.UID);
var user = await dbContext.UserProfileData.FirstOrDefaultAsync(f => f.UserUID == request.UID);
if (user != null)
{
user.ProfileDisabled = true;
}
await dbContext.SaveChangesAsync();
}
[Route(LightlessAuth.Enable_Profile)]
[HttpPost]
public async Task EnableGroupProfile([FromBody] UserProfileAvailabilityRequest request)
{
using var dbContext = await LightlessDbContextFactory.CreateDbContextAsync();
Logger.LogInformation("Enabling profile for user with uid {UID}", request.UID);
var user = await dbContext.UserProfileData.FirstOrDefaultAsync(f => f.UserUID == request.UID);
if (user != null)
{
user.ProfileDisabled = false;
}
await dbContext.SaveChangesAsync();
}
}