Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(app): Add dedicated scope and dbcontext to GetSubjectResources #1648

Merged
merged 4 commits into from
Jan 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
using Digdir.Domain.Dialogporten.Domain.Parties.Abstractions;
using Digdir.Domain.Dialogporten.Domain.SubjectResources;
using Digdir.Domain.Dialogporten.Infrastructure.Common.Exceptions;
using Digdir.Domain.Dialogporten.Infrastructure.Persistence;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using ZiggyCreatures.Caching.Fusion;

Expand All @@ -25,8 +27,8 @@ internal sealed class AltinnAuthorizationClient : IAltinnAuthorization
private readonly IFusionCache _partiesCache;
private readonly IFusionCache _subjectResourcesCache;
private readonly IUser _user;
private readonly IDialogDbContext _dialogDbContext;
private readonly ILogger _logger;
private readonly IServiceScopeFactory _serviceScopeFactory;

private static readonly JsonSerializerOptions SerializerOptions = new()
{
Expand All @@ -38,16 +40,16 @@ public AltinnAuthorizationClient(
HttpClient client,
IFusionCacheProvider cacheProvider,
IUser user,
IDialogDbContext dialogDbContext,
ILogger<AltinnAuthorizationClient> logger)
ILogger<AltinnAuthorizationClient> logger,
IServiceScopeFactory serviceScopeFactory)
{
_httpClient = client ?? throw new ArgumentNullException(nameof(client));
_pdpCache = cacheProvider.GetCache(nameof(Authorization)) ?? throw new ArgumentNullException(nameof(cacheProvider));
_partiesCache = cacheProvider.GetCache(nameof(AuthorizedPartiesResult)) ?? throw new ArgumentNullException(nameof(cacheProvider));
_subjectResourcesCache = cacheProvider.GetCache(nameof(SubjectResource)) ?? throw new ArgumentNullException(nameof(cacheProvider));
_user = user ?? throw new ArgumentNullException(nameof(user));
_dialogDbContext = dialogDbContext ?? throw new ArgumentNullException(nameof(dialogDbContext));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_serviceScopeFactory = serviceScopeFactory ?? throw new ArgumentNullException(nameof(serviceScopeFactory));
}

public async Task<DialogDetailsAuthorizationResult> GetDialogDetailsAuthorization(
Expand Down Expand Up @@ -180,10 +182,13 @@ await AuthorizationHelper.CollapseSubjectResources(

return dialogSearchAuthorizationResult;
}

private async Task<List<SubjectResource>> GetAllSubjectResources(CancellationToken cancellationToken) =>
await _subjectResourcesCache.GetOrSetAsync(nameof(SubjectResource), async ct
=> await _dialogDbContext.SubjectResources.ToListAsync(cancellationToken: ct),
await _subjectResourcesCache.GetOrSetAsync(nameof(SubjectResource), async ct =>
{
using var scope = _serviceScopeFactory.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<DialogDbContext>();
return await dbContext.SubjectResources.AsNoTracking().ToListAsync(cancellationToken: ct);
},
token: cancellationToken);

private async Task<DialogDetailsAuthorizationResult> PerformDialogDetailsAuthorization(
Expand Down
Loading