-
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.
feat: Implement scalable dialog search authorization (#875)
## Description This implements a authorized-parties based approach to list/search authorization, which should scale better than the current approach. This builds on the syncronization of data from RR, which contains a map of subjects (role codes and eventually access packages) and resources. This data is persisted in Dialogporten DB, and used as a cache. A new predicate builder `PrefilterAuthorizedDialogs` replaces`WhereUserIsAuthorizedFor`, and constructs a SQL manually in order to propertly handle the new property `SubjectsByParties` in `DialogSearchAuthorizationResult`, which is a dict of party->subjects. Each of the roles grant access to a list of resources. This also removes legacy system users, as they cannot be authorized this way (not possible to get a list of parties from Authorization APIs for a legacy system user). ## Related Issue(s) - #42 ## Verification - [x] **Your** code builds clean without any errors or warnings - [x] Manual testing done (required) - [ ] Relevant automated test added (if you find this hard, leave it and we'll help out) ## Documentation - [ ] Documentation is updated (either in `docs`-directory, Altinnpedia or a separate linked PR in [altinn-studio-docs.](https://github.com/Altinn/altinn-studio-docs), if applicable) --------- Co-authored-by: Magnus Sandgren <5285192+MagnusSandgren@users.noreply.github.com>
- Loading branch information
1 parent
b408d41
commit aa8f84d
Showing
30 changed files
with
6,076 additions
and
495 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
57 changes: 57 additions & 0 deletions
57
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 |
---|---|---|
@@ -0,0 +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, DialogSearchAuthorizationResult authorizedResources) | ||
{ | ||
var parameters = new List<object>(); | ||
|
||
// 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) | ||
{ | ||
// 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 (party, subjects) in authorizedResources.SubjectsByParties) | ||
{ | ||
// 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); | ||
} | ||
|
||
return dialogs.FromSqlRaw(sb.ToString(), parameters.ToArray()); | ||
} | ||
} |
69 changes: 0 additions & 69 deletions
69
src/Digdir.Domain.Dialogporten.Application/Common/Extensions/QueryableExtensions.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,78 +1,9 @@ | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using Digdir.Domain.Dialogporten.Application.Externals.AltinnAuthorization; | ||
using Digdir.Domain.Dialogporten.Domain.Dialogs.Entities; | ||
|
||
namespace Digdir.Domain.Dialogporten.Application.Common.Extensions; | ||
|
||
public static class QueryableExtensions | ||
{ | ||
public static IQueryable<TSource> WhereIf<TSource>(this IQueryable<TSource> source, bool predicate, Expression<Func<TSource, bool>> queryPredicate) | ||
=> predicate ? source.Where(queryPredicate) : source; | ||
|
||
private static readonly Type DialogType = typeof(DialogEntity); | ||
private static readonly PropertyInfo DialogIdPropertyInfo = DialogType.GetProperty(nameof(DialogEntity.Id))!; | ||
private static readonly PropertyInfo DialogPartyPropertyInfo = DialogType.GetProperty(nameof(DialogEntity.Party))!; | ||
private static readonly PropertyInfo DialogServiceResourcePropertyInfo = DialogType.GetProperty(nameof(DialogEntity.ServiceResource))!; | ||
|
||
private static readonly MethodInfo StringListContainsMethodInfo = typeof(List<string>).GetMethod(nameof(List<object>.Contains))!; | ||
private static readonly MethodInfo GuidListContainsMethodInfo = typeof(List<Guid>).GetMethod(nameof(List<object>.Contains))!; | ||
|
||
private static readonly Type KeyValueType = typeof(KeyValuePair<string, List<string>>); | ||
private static readonly PropertyInfo KeyPropertyInfo = KeyValueType.GetProperty(nameof(KeyValuePair<object, object>.Key))!; | ||
private static readonly PropertyInfo ValuePropertyInfo = KeyValueType.GetProperty(nameof(KeyValuePair<object, object>.Value))!; | ||
|
||
private static readonly Type DialogSearchAuthorizationResultType = typeof(DialogSearchAuthorizationResult); | ||
private static readonly PropertyInfo DialogIdsPropertyInfo = DialogSearchAuthorizationResultType.GetProperty(nameof(DialogSearchAuthorizationResult.DialogIds))!; | ||
|
||
public static IQueryable<DialogEntity> WhereUserIsAuthorizedFor( | ||
this IQueryable<DialogEntity> source, | ||
DialogSearchAuthorizationResult authorizedResources) | ||
{ | ||
if (authorizedResources.HasNoAuthorizations) | ||
{ | ||
return source.Where(x => false); | ||
} | ||
|
||
var dialogParameter = Expression.Parameter(DialogType); | ||
var id = Expression.MakeMemberAccess(dialogParameter, DialogIdPropertyInfo); | ||
var party = Expression.MakeMemberAccess(dialogParameter, DialogPartyPropertyInfo); | ||
var serviceResource = Expression.MakeMemberAccess(dialogParameter, DialogServiceResourcePropertyInfo); | ||
|
||
var partyResourceExpressions = new List<Expression>(); | ||
|
||
foreach (var item in authorizedResources.ResourcesByParties) | ||
{ | ||
var itemArg = Expression.Constant(item, KeyValueType); | ||
var partyAccess = Expression.MakeMemberAccess(itemArg, KeyPropertyInfo); | ||
var resourcesAccess = Expression.MakeMemberAccess(itemArg, ValuePropertyInfo); | ||
var partyEquals = Expression.Equal(partyAccess, party); | ||
var resourceContains = Expression.Call(resourcesAccess, StringListContainsMethodInfo, serviceResource); | ||
partyResourceExpressions.Add(Expression.AndAlso(partyEquals, resourceContains)); | ||
} | ||
|
||
foreach (var item in authorizedResources.PartiesByResources) | ||
{ | ||
var itemArg = Expression.Constant(item, KeyValueType); | ||
var resourceAccess = Expression.MakeMemberAccess(itemArg, KeyPropertyInfo); | ||
var partiesAccess = Expression.MakeMemberAccess(itemArg, ValuePropertyInfo); | ||
var resourceEquals = Expression.Equal(resourceAccess, serviceResource); | ||
var partiesContains = Expression.Call(partiesAccess, StringListContainsMethodInfo, party); | ||
partyResourceExpressions.Add(Expression.AndAlso(resourceEquals, partiesContains)); | ||
} | ||
|
||
if (authorizedResources.DialogIds.Count > 0) | ||
{ | ||
var itemArg = Expression.Constant(authorizedResources, DialogSearchAuthorizationResultType); | ||
var dialogIdsAccess = Expression.MakeMemberAccess(itemArg, DialogIdsPropertyInfo); | ||
var dialogIdsContains = Expression.Call(dialogIdsAccess, GuidListContainsMethodInfo, id); | ||
partyResourceExpressions.Add(dialogIdsContains); | ||
} | ||
|
||
var predicate = partyResourceExpressions | ||
.DefaultIfEmpty(Expression.Constant(false)) | ||
.Aggregate(Expression.OrElse); | ||
|
||
return source.Where(Expression.Lambda<Func<DialogEntity, bool>>(predicate, dialogParameter)); | ||
} | ||
} |
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
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
Oops, something went wrong.