| | | 1 | | using Elsa.Workflows.Management.Entities; |
| | | 2 | | using Elsa.Workflows.Management.Options; |
| | | 3 | | using JetBrains.Annotations; |
| | | 4 | | using Microsoft.AspNetCore.Authorization; |
| | | 5 | | using Microsoft.Extensions.Options; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Workflows.Api.Requirements; |
| | | 8 | | |
| | | 9 | | public record NotReadOnlyResource(WorkflowDefinition? WorkflowDefinition = default); |
| | | 10 | | |
| | | 11 | | |
| | 1 | 12 | | public record NotReadOnlyRequirement() : IAuthorizationRequirement; |
| | | 13 | | |
| | | 14 | | |
| | | 15 | | /// <inheritdoc /> |
| | | 16 | | [PublicAPI] |
| | | 17 | | public class NotReadOnlyRequirementHandler : AuthorizationHandler<NotReadOnlyRequirement, NotReadOnlyResource> |
| | | 18 | | { |
| | | 19 | | private readonly IOptions<ManagementOptions> _managementOptions; |
| | | 20 | | |
| | | 21 | | /// <inheritdoc /> |
| | | 22 | | public NotReadOnlyRequirementHandler( |
| | | 23 | | IOptions<ManagementOptions> managementOptions) |
| | | 24 | | { |
| | | 25 | | _managementOptions = managementOptions; |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <inheritdoc /> |
| | | 29 | | protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, NotReadOnlyRequirement requireme |
| | | 30 | | { |
| | | 31 | | if (_managementOptions.Value.IsReadOnlyMode) |
| | | 32 | | { |
| | | 33 | | context.Fail(new(this, "Workflow edit is not allowed when the read-only mode is enabled.")); |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | if (resource.WorkflowDefinition != null && (resource.WorkflowDefinition.IsReadonly || resource.WorkflowDefinitio |
| | | 37 | | { |
| | | 38 | | context.Fail(new(this, "Workflow edit is not allowed for a readonly or system workflow.")); |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | context.Succeed(requirement); |
| | | 42 | | return Task.CompletedTask; |
| | | 43 | | } |
| | | 44 | | } |