-
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.
Nå ble dette litt mer enn noen forslag om akkurat PRen din. Jeg så noe lavt hengende frukt i PaginatedList også som jeg håper du syns er greit at jeg bare slenger inn her 🙏
- Loading branch information
1 parent
1c0ac9d
commit 72b2f68
Showing
4 changed files
with
89 additions
and
46 deletions.
There are no files selected for viewing
62 changes: 35 additions & 27 deletions
62
src/Digdir.Domain.Dialogporten.Application/Common/Extensions/DbSetExtensions.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 |
---|---|---|
@@ -1,49 +1,57 @@ | ||
using System.Globalization; | ||
using System.Text; | ||
using Digdir.Domain.Dialogporten.Application.Externals.AltinnAuthorization; | ||
using Digdir.Domain.Dialogporten.Domain.Dialogs.Entities; | ||
using Digdir.Domain.Dialogporten.Domain.SubjectResources; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace Digdir.Domain.Dialogporten.Application.Common.Extensions; | ||
|
||
public static class DbSetExtensions | ||
{ | ||
public static IQueryable<DialogEntity> PrefilterAuthorizedDialogs(this DbSet<DialogEntity> dialogs, | ||
Func<Task<DialogSearchAuthorizationResult?>> authorizedResourcesProvider) | ||
{ | ||
var authorizedResources = authorizedResourcesProvider().GetAwaiter().GetResult(); | ||
return authorizedResources is null ? dialogs : dialogs.PrefilterAuthorizedDialogs(authorizedResources); | ||
} | ||
|
||
public static IQueryable<DialogEntity> PrefilterAuthorizedDialogs(this DbSet<DialogEntity> dialogs, DialogSearchAuthorizationResult authorizedResources) | ||
{ | ||
var parameters = new List<object>(); | ||
var sb = new StringBuilder().Append("SELECT * FROM \"Dialog\" WHERE 1=1"); | ||
|
||
foreach (var item in authorizedResources.ResourcesByParties) | ||
// lang=sql | ||
var sb = new StringBuilder() | ||
.AppendLine(CultureInfo.InvariantCulture, $""" | ||
SELECT * | ||
FROM "Dialog" | ||
WHERE "Id" = ANY(@p{parameters.Count}) | ||
"""); | ||
parameters.Add(authorizedResources.DialogIds); | ||
|
||
foreach (var (party, resources) in authorizedResources.ResourcesByParties) | ||
{ | ||
sb.Append(" OR (\"Party\" = @p") | ||
.Append(parameters.Count) | ||
.Append(" AND \"ServiceResource\" = ANY(@p") | ||
.Append(parameters.Count + 1).Append("))"); | ||
parameters.Add(item.Key); | ||
parameters.Add(item.Value); | ||
// lang=sql | ||
sb.AppendLine(CultureInfo.InvariantCulture, $""" | ||
OR ( | ||
"{nameof(DialogEntity.Party)}" = @p{parameters.Count} | ||
AND "{nameof(DialogEntity.ServiceResource)}" = ANY(@p{parameters.Count + 1}) | ||
) | ||
"""); | ||
parameters.Add(party); | ||
parameters.Add(resources); | ||
} | ||
|
||
foreach (var item in authorizedResources.SubjectsByParties) | ||
foreach (var (party, subjects) in authorizedResources.SubjectsByParties) | ||
{ | ||
sb.Append(" OR (\"Party\" = @p") | ||
.Append(parameters.Count) | ||
.Append(" AND \"ServiceResource\" = ANY(SELECT r.\"Resource\" FROM \"SubjectResource\" r WHERE r.\"Subject\" = ANY(@p") | ||
.Append(parameters.Count + 1).Append(")))"); | ||
parameters.Add(item.Key); | ||
parameters.Add(item.Value); | ||
// lang=sql | ||
sb.AppendLine(CultureInfo.InvariantCulture, $""" | ||
OR ( | ||
"{nameof(DialogEntity.Party)}" = @p{parameters.Count} | ||
AND "{nameof(DialogEntity.ServiceResource)}" = ANY( | ||
SELECT "{nameof(SubjectResource.Resource)}" | ||
FROM "{nameof(SubjectResource)}" | ||
WHERE "{nameof(SubjectResource.Subject)}" = ANY(@p{parameters.Count + 1}) | ||
) | ||
) | ||
"""); | ||
parameters.Add(party); | ||
parameters.Add(subjects); | ||
} | ||
|
||
sb.Append(" OR \"Id\" = ANY(@p") | ||
.Append(parameters.Count) | ||
.Append(')'); | ||
parameters.Add(authorizedResources.DialogIds); | ||
|
||
return dialogs.FromSqlRaw(sb.ToString(), parameters.ToArray()); | ||
} | ||
} |
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