use more dbcontext factories or something idk

This commit is contained in:
Stanley Dimant
2024-11-06 16:08:41 +01:00
parent 33d5f44754
commit ac640c2f0c
4 changed files with 17 additions and 14 deletions

View File

@@ -27,7 +27,7 @@ public abstract class AuthControllerBase : Controller
private readonly GeoIPService _geoIPProvider;
protected AuthControllerBase(ILogger logger,
IHttpContextAccessor accessor, IDbContextFactory<MareDbContext> mareDbContext,
IHttpContextAccessor accessor, IDbContextFactory<MareDbContext> mareDbContextFactory,
SecretKeyAuthenticatorService secretKeyAuthenticatorService,
IConfigurationService<AuthServiceConfiguration> 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;
}

View File

@@ -17,11 +17,11 @@ namespace MareSynchronosAuthService.Controllers;
public class JwtController : AuthControllerBase
{
public JwtController(ILogger<JwtController> logger,
IHttpContextAccessor accessor, IDbContextFactory<MareDbContext> mareDbContext,
IHttpContextAccessor accessor, IDbContextFactory<MareDbContext> mareDbContextFactory,
SecretKeyAuthenticatorService secretKeyAuthenticatorService,
IConfigurationService<AuthServiceConfiguration> configuration,
IRedisDatabase redisDb, GeoIPService geoIPProvider)
: base(logger, accessor, mareDbContext, secretKeyAuthenticatorService,
: base(logger, accessor, mareDbContextFactory, secretKeyAuthenticatorService,
configuration, redisDb, geoIPProvider)
{
}

View File

@@ -7,12 +7,12 @@ using Microsoft.Extensions.Logging;
namespace MareSynchronosShared.RequirementHandlers;
public class ExistingUserRequirementHandler : AuthorizationHandler<ExistingUserRequirement>
{
private readonly MareDbContext _dbContext;
private readonly IDbContextFactory<MareDbContext> _dbContextFactory;
private readonly ILogger<UserRequirementHandler> _logger;
public ExistingUserRequirementHandler(MareDbContext dbContext, ILogger<UserRequirementHandler> logger)
public ExistingUserRequirementHandler(IDbContextFactory<MareDbContext> dbContext, ILogger<UserRequirementHandler> logger)
{
_dbContext = dbContext;
_dbContextFactory = dbContext;
_logger = logger;
}
@@ -24,12 +24,13 @@ public class ExistingUserRequirementHandler : AuthorizationHandler<ExistingUserR
var discordIdString = context.User.Claims.SingleOrDefault(g => 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);

View File

@@ -12,13 +12,13 @@ namespace MareSynchronosShared.RequirementHandlers;
public class UserRequirementHandler : AuthorizationHandler<UserRequirement, HubInvocationContext>
{
private readonly MareDbContext _dbContext;
private readonly IDbContextFactory<MareDbContext> _dbContextFactory;
private readonly ILogger<UserRequirementHandler> _logger;
private readonly IRedisDatabase _redis;
public UserRequirementHandler(MareDbContext dbContext, ILogger<UserRequirementHandler> logger, IRedisDatabase redisDb)
public UserRequirementHandler(IDbContextFactory<MareDbContext> dbContextFactory, ILogger<UserRequirementHandler> logger, IRedisDatabase redisDb)
{
_dbContext = dbContext;
_dbContextFactory = dbContextFactory;
_logger = logger;
_redis = redisDb;
}
@@ -37,14 +37,16 @@ public class UserRequirementHandler : AuthorizationHandler<UserRequirement, HubI
if ((requirement.Requirements & UserRequirements.Administrator) is UserRequirements.Administrator)
{
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) 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);
}