From 42ed164c54c770e7c5c25be9db2b758cdfad4ac8 Mon Sep 17 00:00:00 2001 From: defnotken Date: Fri, 5 Sep 2025 16:02:00 -0500 Subject: [PATCH] Registering Controller --- .../Controllers/UserController.cs | 98 ------------------- .../LightlessSyncAuthService/Startup.cs | 5 +- .../LightlessSyncServer/Startup.cs | 41 ++++---- 3 files changed, 22 insertions(+), 122 deletions(-) delete mode 100644 LightlessSyncServer/LightlessSyncAuthService/Controllers/UserController.cs diff --git a/LightlessSyncServer/LightlessSyncAuthService/Controllers/UserController.cs b/LightlessSyncServer/LightlessSyncAuthService/Controllers/UserController.cs deleted file mode 100644 index c84ff49..0000000 --- a/LightlessSyncServer/LightlessSyncAuthService/Controllers/UserController.cs +++ /dev/null @@ -1,98 +0,0 @@ -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 diff --git a/LightlessSyncServer/LightlessSyncAuthService/Startup.cs b/LightlessSyncServer/LightlessSyncAuthService/Startup.cs index 2f81451..20d8d89 100644 --- a/LightlessSyncServer/LightlessSyncAuthService/Startup.cs +++ b/LightlessSyncServer/LightlessSyncAuthService/Startup.cs @@ -3,8 +3,6 @@ using LightlessSyncShared.Metrics; using LightlessSyncShared.Services; using LightlessSyncShared.Utils; using Microsoft.AspNetCore.Mvc.Controllers; -using StackExchange.Redis.Extensions.Core.Configuration; -using StackExchange.Redis.Extensions.System.Text.Json; using StackExchange.Redis; using System.Net; using LightlessSyncAuthService.Services; @@ -17,7 +15,6 @@ using LightlessSyncShared.Data; using Microsoft.EntityFrameworkCore; using Prometheus; using LightlessSyncShared.Utils.Configuration; -using StackExchange.Redis.Extensions.Core.Abstractions; namespace LightlessSyncAuthService; @@ -88,7 +85,7 @@ public class Startup services.AddControllers().ConfigureApplicationPartManager(a => { a.FeatureProviders.Remove(a.FeatureProviders.OfType().First()); - a.FeatureProviders.Add(new AllowedControllersFeatureProvider(typeof(JwtController), typeof(OAuthController), typeof(UserController))); + a.FeatureProviders.Add(new AllowedControllersFeatureProvider(typeof(JwtController), typeof(OAuthController))); }); } diff --git a/LightlessSyncServer/LightlessSyncServer/Startup.cs b/LightlessSyncServer/LightlessSyncServer/Startup.cs index 4a2a2ba..d9bab3f 100644 --- a/LightlessSyncServer/LightlessSyncServer/Startup.cs +++ b/LightlessSyncServer/LightlessSyncServer/Startup.cs @@ -1,29 +1,30 @@ -using Microsoft.EntityFrameworkCore; -using LightlessSyncServer.Hubs; -using Microsoft.AspNetCore.Http.Connections; -using Microsoft.AspNetCore.SignalR; -using Microsoft.AspNetCore.Authorization; using AspNetCoreRateLimit; +using LightlessSync.API.SignalR; +using LightlessSyncAuthService.Controllers; +using LightlessSyncServer.Controllers; +using LightlessSyncServer.Hubs; +using LightlessSyncServer.Services; using LightlessSyncShared.Data; using LightlessSyncShared.Metrics; -using LightlessSyncServer.Services; -using LightlessSyncShared.Utils; +using LightlessSyncShared.RequirementHandlers; using LightlessSyncShared.Services; -using Prometheus; -using Microsoft.AspNetCore.Authentication.JwtBearer; -using Microsoft.IdentityModel.Tokens; -using System.Text; -using StackExchange.Redis; -using StackExchange.Redis.Extensions.Core.Configuration; -using System.Net; -using StackExchange.Redis.Extensions.System.Text.Json; -using LightlessSync.API.SignalR; +using LightlessSyncShared.Utils; +using LightlessSyncShared.Utils.Configuration; using MessagePack; using MessagePack.Resolvers; +using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http.Connections; using Microsoft.AspNetCore.Mvc.Controllers; -using LightlessSyncServer.Controllers; -using LightlessSyncShared.RequirementHandlers; -using LightlessSyncShared.Utils.Configuration; +using Microsoft.AspNetCore.SignalR; +using Microsoft.EntityFrameworkCore; +using Microsoft.IdentityModel.Tokens; +using Prometheus; +using StackExchange.Redis; +using StackExchange.Redis.Extensions.Core.Configuration; +using StackExchange.Redis.Extensions.System.Text.Json; +using System.Net; +using System.Text; namespace LightlessSyncServer; @@ -71,7 +72,7 @@ public class Startup a.FeatureProviders.Remove(a.FeatureProviders.OfType().First()); if (lightlessConfig.GetValue(nameof(ServerConfiguration.MainServerAddress), defaultValue: null) == null) { - a.FeatureProviders.Add(new AllowedControllersFeatureProvider(typeof(LightlessServerConfigurationController), typeof(LightlessBaseConfigurationController), typeof(ClientMessageController))); + a.FeatureProviders.Add(new AllowedControllersFeatureProvider(typeof(LightlessServerConfigurationController), typeof(LightlessBaseConfigurationController), typeof(ClientMessageController), typeof(UserController))); } else {