-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update builder to standard set in correspondance (#909)
* update builder to standard set in correspondance (1/2) * restructure: add builders folder * formatting * add TryGet method to retrieve app resource id * cleanup * formatting * add custom exceptions * use IOptions for plattformsettings * simplify builder * format * trailing comma * more formatting
- Loading branch information
Showing
12 changed files
with
342 additions
and
131 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
54 changes: 54 additions & 0 deletions
54
src/Altinn.App.Core/Internal/AccessManagement/Builders/AccessRightBuilder.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,54 @@ | ||
using Altinn.App.Core.Internal.AccessManagement.Models; | ||
using Altinn.App.Core.Internal.AccessManagement.Models.Shared; | ||
|
||
namespace Altinn.App.Core.Internal.AccessManagement.Builders; | ||
|
||
internal interface IAccessRightBuilderStart | ||
{ | ||
IAccessRightBuilderAction WithAction(string value); | ||
} | ||
|
||
internal interface IAccessRightBuilderAction | ||
{ | ||
IAccessRightBuilder WithResource(string value); | ||
IAccessRightBuilder WithResources(List<Resource> resources); | ||
} | ||
|
||
internal interface IAccessRightBuilder : IAccessRightBuilderStart, IAccessRightBuilderAction | ||
{ | ||
RightRequest Build(); | ||
} | ||
|
||
internal sealed class AccessRightBuilder : IAccessRightBuilder | ||
{ | ||
private AltinnAction? _action; | ||
private List<Resource>? _resources; | ||
|
||
private AccessRightBuilder() { } | ||
|
||
public static IAccessRightBuilderStart Create() => new AccessRightBuilder(); | ||
|
||
public IAccessRightBuilderAction WithAction(string value) | ||
{ | ||
_action = new AltinnAction { Type = DelegationConst.ActionId, Value = value }; | ||
return this; | ||
} | ||
|
||
public IAccessRightBuilder WithResource(string value) | ||
{ | ||
_resources = [new Resource { Type = DelegationConst.Resource, Value = value }]; | ||
|
||
return this; | ||
} | ||
|
||
public IAccessRightBuilder WithResources(List<Resource> resources) | ||
{ | ||
_resources = [.. _resources ?? [], .. resources]; | ||
return this; | ||
} | ||
|
||
public RightRequest Build() | ||
{ | ||
return new RightRequest { Action = _action, Resource = _resources ?? [] }; | ||
} | ||
} |
137 changes: 137 additions & 0 deletions
137
src/Altinn.App.Core/Internal/AccessManagement/Builders/DelegationBuilder.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,137 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using Altinn.App.Core.Helpers; | ||
using Altinn.App.Core.Internal.AccessManagement.Exceptions; | ||
using Altinn.App.Core.Internal.AccessManagement.Models; | ||
using Altinn.App.Core.Internal.AccessManagement.Models.Shared; | ||
|
||
namespace Altinn.App.Core.Internal.AccessManagement.Builders; | ||
|
||
internal abstract class DelegationBuilderBase | ||
{ | ||
internal static void NotNullOrEmpty([NotNull] object? value, string? errorMessage = null) | ||
{ | ||
if ( | ||
value is null | ||
|| value is string str && string.IsNullOrWhiteSpace(str) | ||
|| value is ReadOnlyMemory<byte> { IsEmpty: true } | ||
) | ||
{ | ||
throw new AccessManagementArgumentException(errorMessage); // TODO: add custom exception | ||
} | ||
} | ||
} | ||
|
||
internal interface IDelegationBuilderStart | ||
{ | ||
IDelegationBuilderApplicationId WithApplicationId(string applicationId); | ||
} | ||
|
||
internal interface IDelegationBuilderApplicationId | ||
{ | ||
IDelegationBuilderInstanceId WithInstanceId(string instanceId); | ||
} | ||
|
||
internal interface IDelegationBuilderInstanceId | ||
{ | ||
IDelegationBuilderDelegator WithDelegator(Delegator delegator); | ||
} | ||
|
||
internal interface IDelegationBuilderDelegator | ||
{ | ||
IDelegationBuilderRecipient WithRecipient(Delegatee recipient); | ||
} | ||
|
||
internal interface IDelegationBuilderRecipient | ||
{ | ||
IDelegationBuilder WithRight(RightRequest rightRequest); | ||
IDelegationBuilder WithRights(List<RightRequest> rightRequests); | ||
} | ||
|
||
internal interface IDelegationBuilder | ||
: IDelegationBuilderStart, | ||
IDelegationBuilderApplicationId, | ||
IDelegationBuilderInstanceId, | ||
IDelegationBuilderDelegator, | ||
IDelegationBuilderRecipient | ||
{ | ||
DelegationRequest Build(); | ||
} | ||
|
||
internal sealed class DelegationBuilder : DelegationBuilderBase, IDelegationBuilder | ||
{ | ||
private string? _applicationId; | ||
private string? _instanceId; | ||
private Delegator? _delegator; | ||
private Delegatee? _recipient; | ||
private List<RightRequest>? _rights; | ||
|
||
private DelegationBuilder() { } | ||
|
||
public static IDelegationBuilderStart Create() => new DelegationBuilder(); | ||
|
||
public IDelegationBuilderApplicationId WithApplicationId(string applicationId) | ||
{ | ||
NotNullOrEmpty(applicationId, nameof(applicationId)); | ||
_applicationId = AppIdHelper.TryGetResourceId(applicationId, out string? resourceId) | ||
? resourceId | ||
: throw new ArgumentException("Invalid application ID", nameof(applicationId)); | ||
return this; | ||
} | ||
|
||
public IDelegationBuilderInstanceId WithInstanceId(string instanceId) | ||
{ | ||
NotNullOrEmpty(instanceId, nameof(instanceId)); | ||
_instanceId = instanceId; | ||
return this; | ||
} | ||
|
||
public IDelegationBuilderDelegator WithDelegator(Delegator delegator) | ||
{ | ||
NotNullOrEmpty(delegator, nameof(delegator)); | ||
_delegator = delegator; | ||
return this; | ||
} | ||
|
||
public IDelegationBuilderRecipient WithRecipient(Delegatee recipient) | ||
{ | ||
NotNullOrEmpty(recipient, nameof(recipient)); | ||
_recipient = recipient; | ||
return this; | ||
} | ||
|
||
public IDelegationBuilder WithRight(RightRequest rightRequest) | ||
{ | ||
_rights = [rightRequest]; | ||
return this; | ||
} | ||
|
||
public IDelegationBuilder WithRights(List<RightRequest> rightRequests) | ||
{ | ||
_rights = [.. _rights ?? [], .. rightRequests]; | ||
return this; | ||
} | ||
|
||
public IDelegationBuilder WithRight(AccessRightBuilder rightBuilder) | ||
{ | ||
_rights = [rightBuilder.Build()]; | ||
return this; | ||
} | ||
|
||
public DelegationRequest Build() | ||
{ | ||
NotNullOrEmpty(_applicationId, nameof(_applicationId)); | ||
NotNullOrEmpty(_instanceId, nameof(_instanceId)); | ||
NotNullOrEmpty(_delegator, nameof(_delegator)); | ||
NotNullOrEmpty(_recipient, nameof(_recipient)); | ||
NotNullOrEmpty(_rights, nameof(_rights)); | ||
|
||
return new DelegationRequest | ||
{ | ||
ResourceId = _applicationId, | ||
InstanceId = _instanceId, | ||
From = _delegator, | ||
To = _recipient, | ||
Rights = _rights, | ||
}; | ||
} | ||
} |
Oops, something went wrong.