-
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: add support for serviceowner admin scope (#1002)
<!--- Provide a general summary of your changes in the Title above --> ## Description <!--- Describe your changes in detail --> ## Related Issue(s) - #272 ## Verification - [ ] **Your** code builds clean without any errors or warnings - [ ] 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: Knut Haug <154342485+knuhau@users.noreply.github.com> Co-authored-by: Knut Haug <knut.espen.haug@digdir.no> Co-authored-by: Magnus Sandgren <5285192+MagnusSandgren@users.noreply.github.com>
- Loading branch information
1 parent
8e74368
commit 2638b48
Showing
19 changed files
with
414 additions
and
341 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
99 changes: 99 additions & 0 deletions
99
...Digdir.Domain.Dialogporten.Application/Common/Authorization/IServiceResourceAuthorizer.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,99 @@ | ||
using Digdir.Domain.Dialogporten.Application.Common.ReturnTypes; | ||
using Digdir.Domain.Dialogporten.Application.Externals; | ||
using Digdir.Domain.Dialogporten.Application.Features.V1.ServiceOwner.Dialogs.Commands.Create; | ||
using Digdir.Domain.Dialogporten.Domain.Dialogs.Entities; | ||
using OneOf; | ||
using OneOf.Types; | ||
|
||
namespace Digdir.Domain.Dialogporten.Application.Common.Authorization; | ||
|
||
public interface IServiceResourceAuthorizer | ||
{ | ||
Task<AuthorizeServiceResourcesResult> AuthorizeServiceResources( | ||
DialogEntity dialog, | ||
CancellationToken cancellationToken); | ||
|
||
Task<SetResourceTypeResult> SetResourceType( | ||
DialogEntity dialog, | ||
CancellationToken cancellationToken); | ||
} | ||
|
||
[GenerateOneOf] | ||
public partial class AuthorizeServiceResourcesResult : OneOfBase<Success, Forbidden>; | ||
|
||
[GenerateOneOf] | ||
public partial class SetResourceTypeResult : OneOfBase<Success, DomainContextInvalidated>; | ||
|
||
public struct DomainContextInvalidated; | ||
|
||
internal sealed class ServiceResourceAuthorizer : IServiceResourceAuthorizer | ||
{ | ||
private readonly IUserResourceRegistry _userResourceRegistry; | ||
private readonly IResourceRegistry _resourceRegistry; | ||
private readonly IDomainContext _domainContext; | ||
|
||
public ServiceResourceAuthorizer( | ||
IUserResourceRegistry userResourceRegistry, | ||
IResourceRegistry resourceRegistry, | ||
IDomainContext domainContext) | ||
{ | ||
_userResourceRegistry = userResourceRegistry ?? throw new ArgumentNullException(nameof(userResourceRegistry)); | ||
_resourceRegistry = resourceRegistry ?? throw new ArgumentNullException(nameof(resourceRegistry)); | ||
_domainContext = domainContext ?? throw new ArgumentNullException(nameof(domainContext)); | ||
} | ||
|
||
public async Task<AuthorizeServiceResourcesResult> AuthorizeServiceResources( | ||
DialogEntity dialog, | ||
CancellationToken cancellationToken) | ||
{ | ||
if (_userResourceRegistry.IsCurrentUserServiceOwnerAdmin()) | ||
{ | ||
return new Success(); | ||
} | ||
|
||
var ownedResources = await _userResourceRegistry.GetCurrentUserResourceIds(cancellationToken); | ||
var notOwnedResources = GetPrimaryServiceResourceReferences(dialog) | ||
.Except(ownedResources) | ||
.ToList(); | ||
|
||
if (notOwnedResources.Count != 0) | ||
{ | ||
return new Forbidden($"Not allowed to reference the following unowned resources: [{string.Join(", ", notOwnedResources)}]."); | ||
} | ||
|
||
if (!_userResourceRegistry.UserCanModifyResourceType(dialog.ServiceResourceType)) | ||
{ | ||
return new Forbidden($"User cannot create or modify a dialog with resource type {dialog.ServiceResourceType}."); | ||
} | ||
|
||
return new Success(); | ||
} | ||
|
||
public async Task<SetResourceTypeResult> SetResourceType(DialogEntity dialog, CancellationToken cancellationToken) | ||
{ | ||
var serviceResourceInformation = await _resourceRegistry.GetResourceInformation(dialog.ServiceResource, cancellationToken); | ||
if (serviceResourceInformation is null) | ||
{ | ||
_domainContext.AddError(nameof(CreateDialogCommand.ServiceResource), | ||
$"Service resource '{dialog.ServiceResource}' does not exist in the resource registry."); | ||
return new DomainContextInvalidated(); | ||
} | ||
|
||
dialog.ServiceResourceType = serviceResourceInformation.ResourceType; | ||
return new Success(); | ||
} | ||
|
||
private static IEnumerable<string> GetPrimaryServiceResourceReferences(DialogEntity dialog) => | ||
Enumerable.Empty<string>() | ||
.Append(dialog.ServiceResource) | ||
.Concat(dialog.ApiActions.Select(action => action.AuthorizationAttribute!)) | ||
.Concat(dialog.GuiActions.Select(action => action.AuthorizationAttribute!)) | ||
.Concat(dialog.Transmissions.Select(transmission => transmission.AuthorizationAttribute!)) | ||
.Select(x => x.ToLowerInvariant()) | ||
.Distinct() | ||
.Where(IsPrimaryResource); | ||
|
||
private static bool IsPrimaryResource(string? resource) => | ||
resource is not null | ||
&& resource.StartsWith(Domain.Common.Constants.ServiceResourcePrefix, StringComparison.OrdinalIgnoreCase); | ||
} |
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
1 change: 0 additions & 1 deletion
1
...ication/Features/V1/EndUser/DialogTransmissions/Queries/Get/GetDialogTransmissionQuery.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
1 change: 0 additions & 1 deletion
1
...n/Features/V1/EndUser/DialogTransmissions/Queries/Search/SearchDialogTransmissionQuery.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
1 change: 0 additions & 1 deletion
1
...in.Dialogporten.Application/Features/V1/EndUser/Dialogs/Queries/Search/SearchDialogDto.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
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.