From ac640c2f0c0dc9f64e4eb43b9a91bd78c5e4e1cb Mon Sep 17 00:00:00 2001 From: Stanley Dimant Date: Wed, 6 Nov 2024 16:08:41 +0100 Subject: [PATCH] use more dbcontext factories or something idk --- .../Controllers/AuthControllerBase.cs | 4 ++-- .../Controllers/JwtController.cs | 4 ++-- .../ExistingUserRequirementHandler.cs | 11 ++++++----- .../RequirementHandlers/UserRequirementHandler.cs | 12 +++++++----- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/MareSynchronosServer/MareSynchronosAuthService/Controllers/AuthControllerBase.cs b/MareSynchronosServer/MareSynchronosAuthService/Controllers/AuthControllerBase.cs index 30ccf7b..cbb1f30 100644 --- a/MareSynchronosServer/MareSynchronosAuthService/Controllers/AuthControllerBase.cs +++ b/MareSynchronosServer/MareSynchronosAuthService/Controllers/AuthControllerBase.cs @@ -27,7 +27,7 @@ public abstract class AuthControllerBase : Controller private readonly GeoIPService _geoIPProvider; protected AuthControllerBase(ILogger logger, - IHttpContextAccessor accessor, IDbContextFactory mareDbContext, + IHttpContextAccessor accessor, IDbContextFactory mareDbContextFactory, SecretKeyAuthenticatorService secretKeyAuthenticatorService, IConfigurationService configuration, IRedisDatabase redisDb, GeoIPService geoIPProvider) @@ -36,7 +36,7 @@ public abstract class AuthControllerBase : Controller HttpAccessor = accessor; _redis = redisDb; _geoIPProvider = geoIPProvider; - MareDbContextFactory = mareDbContext; + MareDbContextFactory = mareDbContextFactory; SecretKeyAuthenticatorService = secretKeyAuthenticatorService; Configuration = configuration; } diff --git a/MareSynchronosServer/MareSynchronosAuthService/Controllers/JwtController.cs b/MareSynchronosServer/MareSynchronosAuthService/Controllers/JwtController.cs index fad2ed4..cca415e 100644 --- a/MareSynchronosServer/MareSynchronosAuthService/Controllers/JwtController.cs +++ b/MareSynchronosServer/MareSynchronosAuthService/Controllers/JwtController.cs @@ -17,11 +17,11 @@ namespace MareSynchronosAuthService.Controllers; public class JwtController : AuthControllerBase { public JwtController(ILogger logger, - IHttpContextAccessor accessor, IDbContextFactory mareDbContext, + IHttpContextAccessor accessor, IDbContextFactory mareDbContextFactory, SecretKeyAuthenticatorService secretKeyAuthenticatorService, IConfigurationService configuration, IRedisDatabase redisDb, GeoIPService geoIPProvider) - : base(logger, accessor, mareDbContext, secretKeyAuthenticatorService, + : base(logger, accessor, mareDbContextFactory, secretKeyAuthenticatorService, configuration, redisDb, geoIPProvider) { } diff --git a/MareSynchronosServer/MareSynchronosShared/RequirementHandlers/ExistingUserRequirementHandler.cs b/MareSynchronosServer/MareSynchronosShared/RequirementHandlers/ExistingUserRequirementHandler.cs index fac67ab..c81ff92 100644 --- a/MareSynchronosServer/MareSynchronosShared/RequirementHandlers/ExistingUserRequirementHandler.cs +++ b/MareSynchronosServer/MareSynchronosShared/RequirementHandlers/ExistingUserRequirementHandler.cs @@ -7,12 +7,12 @@ using Microsoft.Extensions.Logging; namespace MareSynchronosShared.RequirementHandlers; public class ExistingUserRequirementHandler : AuthorizationHandler { - private readonly MareDbContext _dbContext; + private readonly IDbContextFactory _dbContextFactory; private readonly ILogger _logger; - public ExistingUserRequirementHandler(MareDbContext dbContext, ILogger logger) + public ExistingUserRequirementHandler(IDbContextFactory dbContext, ILogger logger) { - _dbContext = dbContext; + _dbContextFactory = dbContext; _logger = logger; } @@ -24,12 +24,13 @@ public class ExistingUserRequirementHandler : AuthorizationHandler string.Equals(g.Type, MareClaimTypes.DiscordId, StringComparison.Ordinal))?.Value; if (discordIdString == null) context.Fail(); - var user = await _dbContext.Users.AsNoTracking().SingleOrDefaultAsync(b => b.UID == uid).ConfigureAwait(false); + using var dbContext = await _dbContextFactory.CreateDbContextAsync().ConfigureAwait(false); + var user = await dbContext.Users.AsNoTracking().SingleOrDefaultAsync(b => b.UID == uid).ConfigureAwait(false); if (user == null) context.Fail(); if (!ulong.TryParse(discordIdString, out ulong discordId)) context.Fail(); - var discordUser = _dbContext.LodeStoneAuth.AsNoTracking().SingleOrDefaultAsync(b => b.DiscordId == discordId); + var discordUser = dbContext.LodeStoneAuth.AsNoTracking().SingleOrDefaultAsync(b => b.DiscordId == discordId); if (discordUser == null) context.Fail(); context.Succeed(requirement); diff --git a/MareSynchronosServer/MareSynchronosShared/RequirementHandlers/UserRequirementHandler.cs b/MareSynchronosServer/MareSynchronosShared/RequirementHandlers/UserRequirementHandler.cs index b51ee71..954473a 100644 --- a/MareSynchronosServer/MareSynchronosShared/RequirementHandlers/UserRequirementHandler.cs +++ b/MareSynchronosServer/MareSynchronosShared/RequirementHandlers/UserRequirementHandler.cs @@ -12,13 +12,13 @@ namespace MareSynchronosShared.RequirementHandlers; public class UserRequirementHandler : AuthorizationHandler { - private readonly MareDbContext _dbContext; + private readonly IDbContextFactory _dbContextFactory; private readonly ILogger _logger; private readonly IRedisDatabase _redis; - public UserRequirementHandler(MareDbContext dbContext, ILogger logger, IRedisDatabase redisDb) + public UserRequirementHandler(IDbContextFactory dbContextFactory, ILogger logger, IRedisDatabase redisDb) { - _dbContext = dbContext; + _dbContextFactory = dbContextFactory; _logger = logger; _redis = redisDb; } @@ -37,14 +37,16 @@ public class UserRequirementHandler : AuthorizationHandler b.UID == uid).ConfigureAwait(false); + using var dbContext = await _dbContextFactory.CreateDbContextAsync().ConfigureAwait(false); + var user = await dbContext.Users.AsNoTracking().SingleOrDefaultAsync(b => b.UID == uid).ConfigureAwait(false); if (user == null || !user.IsAdmin) context.Fail(); _logger.LogInformation("Admin {uid} authenticated", uid); } if ((requirement.Requirements & UserRequirements.Moderator) is UserRequirements.Moderator) { - var user = await _dbContext.Users.AsNoTracking().SingleOrDefaultAsync(b => b.UID == uid).ConfigureAwait(false); + using var dbContext = await _dbContextFactory.CreateDbContextAsync().ConfigureAwait(false); + var user = await dbContext.Users.AsNoTracking().SingleOrDefaultAsync(b => b.UID == uid).ConfigureAwait(false); if (user == null || !user.IsAdmin && !user.IsModerator) context.Fail(); _logger.LogInformation("Admin/Moderator {uid} authenticated", uid); }