fix: migrate to IDbContextFactory and remove direct AppDbContext from DI (#11)
Reviewed-on: #11 Co-authored-by: Marek Jasiński <jasins.marek@gmail.com> Co-committed-by: Marek Jasiński <jasins.marek@gmail.com>
This commit was merged in pull request #11.
This commit is contained in:
@@ -2,16 +2,19 @@ using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using NexusReader.Domain.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
using NexusReader.Data.Persistence;
|
||||
|
||||
namespace NexusReader.Application.Security.Authorization;
|
||||
|
||||
public class ProUserHandler : AuthorizationHandler<ProUserRequirement>
|
||||
{
|
||||
private readonly UserManager<NexusUser> _userManager;
|
||||
|
||||
public ProUserHandler(UserManager<NexusUser> userManager)
|
||||
private readonly IDbContextFactory<AppDbContext> _dbContextFactory;
|
||||
|
||||
public ProUserHandler(IDbContextFactory<AppDbContext> dbContextFactory)
|
||||
{
|
||||
_userManager = userManager;
|
||||
_dbContextFactory = dbContextFactory;
|
||||
}
|
||||
|
||||
protected override async Task HandleRequirementAsync(
|
||||
@@ -24,14 +27,18 @@ public class ProUserHandler : AuthorizationHandler<ProUserRequirement>
|
||||
return;
|
||||
}
|
||||
|
||||
var user = await _userManager.FindByIdAsync(userId);
|
||||
using var db = _dbContextFactory.CreateDbContext();
|
||||
var user = await db.Users
|
||||
.Include(u => u.SubscriptionPlan)
|
||||
.FirstOrDefaultAsync(u => u.Id == userId);
|
||||
|
||||
if (user == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Rule 1: Explicit Pro plan
|
||||
if (user.SubscriptionPlanId == SubscriptionPlan.ProId)
|
||||
// Rule 1: Unlimited access
|
||||
if (user.SubscriptionPlan?.IsUnlimitedTokens == true)
|
||||
{
|
||||
context.Succeed(requirement);
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user