-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Collapse subject resource mappings before building sql query (#1579
) ## Description This implements a few optimizations for search authorization, which particularly improves performance for users with access to a large number of parties. - SubjectsByParties gets collapsed and merged into ResourcesByParties. This lets us avoid doing subselects in the OR-clauses added to the search SQL - The entire SubjectResources-table is cached, which lets us do the above without an additional database hit. - When constructing the SQL, parties with identical resource lists are grouped, further collapsing multiple OR clauses into single ones with two ANY checks. This vastly reduces the number of clauses for large number of reportees, which in turn reduces the number of (identical) query parameters being sent over the network. - Minor improvements to data structures (mostly replacing List<T> with HashSet<T>) ## Related Issue(s) - N/A ## Verification - [x] **Your** code builds clean without any errors or warnings - [x] Manual testing done (required) - [x] Relevant automated test added
- Loading branch information
Showing
8 changed files
with
271 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/Digdir.Domain.Dialogporten.Infrastructure/Altinn/Authorization/AuthorizationHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using Digdir.Domain.Dialogporten.Application.Externals.AltinnAuthorization; | ||
using Digdir.Domain.Dialogporten.Domain.SubjectResources; | ||
|
||
namespace Digdir.Domain.Dialogporten.Infrastructure.Altinn.Authorization; | ||
|
||
internal static class AuthorizationHelper | ||
{ | ||
public static async Task CollapseSubjectResources( | ||
DialogSearchAuthorizationResult dialogSearchAuthorizationResult, | ||
AuthorizedPartiesResult authorizedParties, | ||
List<string> constraintResources, | ||
Func<CancellationToken, Task<List<SubjectResource>>> getAllSubjectResources, | ||
CancellationToken cancellationToken) | ||
{ | ||
var authorizedPartiesWithRoles = authorizedParties.AuthorizedParties | ||
.Where(p => p.AuthorizedRoles.Count != 0) | ||
.ToList(); | ||
|
||
var uniqueSubjects = authorizedPartiesWithRoles | ||
.SelectMany(p => p.AuthorizedRoles) | ||
.ToHashSet(); | ||
|
||
var subjectResources = (await getAllSubjectResources(cancellationToken)) | ||
.Where(x => uniqueSubjects.Contains(x.Subject) && (constraintResources.Count == 0 || constraintResources.Contains(x.Resource))).ToList(); | ||
|
||
var subjectToResources = subjectResources | ||
.GroupBy(sr => sr.Subject) | ||
.ToDictionary(g => g.Key, g => g.Select(sr => sr.Resource).ToHashSet()); | ||
|
||
foreach (var partyEntry in authorizedPartiesWithRoles) | ||
{ | ||
if (!dialogSearchAuthorizationResult.ResourcesByParties.TryGetValue(partyEntry.Party, out var resourceList)) | ||
{ | ||
resourceList = new HashSet<string>(); | ||
dialogSearchAuthorizationResult.ResourcesByParties[partyEntry.Party] = resourceList; | ||
} | ||
|
||
foreach (var subject in partyEntry.AuthorizedRoles) | ||
{ | ||
if (subjectToResources.TryGetValue(subject, out var subjectResourceSet)) | ||
{ | ||
resourceList.UnionWith(subjectResourceSet); | ||
} | ||
} | ||
|
||
if (resourceList.Count == 0) | ||
{ | ||
dialogSearchAuthorizationResult.ResourcesByParties.Remove(partyEntry.Party); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
tests/Digdir.Domain.Dialogporten.Application.Unit.Tests/DbSetExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using Digdir.Domain.Dialogporten.Application.Common.Extensions; | ||
using Digdir.Domain.Dialogporten.Application.Externals.AltinnAuthorization; | ||
using FluentAssertions; | ||
|
||
namespace Digdir.Domain.Dialogporten.Application.Unit.Tests; | ||
|
||
public class DbSetExtensionsTests | ||
{ | ||
[Fact] | ||
public void PrefilterAuthorizedDialogs_GeneratesExpectedSql_ForGroupedParties() | ||
{ | ||
// Arrange | ||
var authorizedResources = new DialogSearchAuthorizationResult | ||
{ | ||
DialogIds = [Guid.CreateVersion7()], | ||
ResourcesByParties = new Dictionary<string, HashSet<string>> | ||
{ | ||
{ "Party1", ["Resource1", "Resource2"] }, | ||
{ "Party2", ["Resource1", "Resource2"] }, | ||
{ "Party3", ["Resource1", "Resource2", "Resource3"] }, | ||
{ "Party4", ["Resource3"] }, | ||
{ "Party5", ["Resource4"] } | ||
} | ||
}; | ||
|
||
var expectedSql = """ | ||
SELECT * | ||
FROM "Dialog" | ||
WHERE "Id" = ANY(@p0) | ||
OR ( | ||
"Party" = ANY(@p1) | ||
AND "ServiceResource" = ANY(@p2) | ||
) | ||
OR ( | ||
"Party" = ANY(@p3) | ||
AND "ServiceResource" = ANY(@p4) | ||
) | ||
OR ( | ||
"Party" = ANY(@p5) | ||
AND "ServiceResource" = ANY(@p6) | ||
) | ||
OR ( | ||
"Party" = ANY(@p7) | ||
AND "ServiceResource" = ANY(@p8) | ||
) | ||
"""; | ||
var expectedParameters = new object[] | ||
{ | ||
authorizedResources.DialogIds, | ||
new HashSet<string> { "Party1", "Party2" }, | ||
new HashSet<string> { "Resource1", "Resource2" }, | ||
new HashSet<string> { "Party3" }, | ||
new HashSet<string> { "Resource1", "Resource2", "Resource3" }, | ||
new HashSet<string> { "Party4" }, | ||
new HashSet<string> { "Resource3" }, | ||
new HashSet<string> { "Party5" }, | ||
new HashSet<string> { "Resource4" } | ||
}; | ||
|
||
// Act | ||
var (actualSql, actualParameters) = DbSetExtensions.GeneratePrefilterAuthorizedDialogsSql(authorizedResources); | ||
|
||
// Assert | ||
RemoveWhitespace(actualSql).Should().Be(RemoveWhitespace(expectedSql)); | ||
actualParameters.Should().BeEquivalentTo(expectedParameters); | ||
} | ||
|
||
private static string RemoveWhitespace(string input) | ||
{ | ||
return string.Concat(input.Where(c => !char.IsWhiteSpace(c))); | ||
} | ||
} |
Oops, something went wrong.