| | | 1 | | using Elsa.Abstractions; |
| | | 2 | | using Elsa.Common.Models; |
| | | 3 | | using Elsa.Workflows.Api.Constants; |
| | | 4 | | using Elsa.Workflows.Api.Requirements; |
| | | 5 | | using Elsa.Workflows.Management; |
| | | 6 | | using Elsa.Workflows.Management.Filters; |
| | | 7 | | using JetBrains.Annotations; |
| | | 8 | | using Microsoft.AspNetCore.Authorization; |
| | | 9 | | |
| | | 10 | | namespace Elsa.Workflows.Api.Endpoints.WorkflowDefinitions.Publish; |
| | | 11 | | |
| | | 12 | | [PublicAPI] |
| | 1 | 13 | | internal class Publish(IWorkflowDefinitionStore store, IWorkflowDefinitionPublisher workflowDefinitionPublisher, IWorkfl |
| | | 14 | | : ElsaEndpoint<Request, Response> |
| | | 15 | | { |
| | | 16 | | public override void Configure() |
| | | 17 | | { |
| | 1 | 18 | | Post("/workflow-definitions/{definitionId}/publish"); |
| | 1 | 19 | | ConfigurePermissions("publish:workflow-definitions"); |
| | 1 | 20 | | } |
| | | 21 | | |
| | | 22 | | public override async Task HandleAsync(Request request, CancellationToken cancellationToken) |
| | | 23 | | { |
| | 0 | 24 | | var filter = new WorkflowDefinitionFilter |
| | 0 | 25 | | { |
| | 0 | 26 | | DefinitionId = request.DefinitionId, |
| | 0 | 27 | | VersionOptions = VersionOptions.Latest |
| | 0 | 28 | | }; |
| | | 29 | | |
| | 0 | 30 | | var definition = await store.FindAsync(filter, cancellationToken); |
| | | 31 | | |
| | 0 | 32 | | if (definition == null) |
| | | 33 | | { |
| | 0 | 34 | | await Send.NotFoundAsync(cancellationToken); |
| | 0 | 35 | | return; |
| | | 36 | | } |
| | | 37 | | |
| | 0 | 38 | | var authorizationResult = await authorizationService.AuthorizeAsync(User, new NotReadOnlyResource(definition), A |
| | | 39 | | |
| | 0 | 40 | | if (!authorizationResult.Succeeded) |
| | | 41 | | { |
| | 0 | 42 | | await Send.ForbiddenAsync(cancellationToken); |
| | 0 | 43 | | return; |
| | | 44 | | } |
| | | 45 | | |
| | 0 | 46 | | var isPublished = definition.IsPublished; |
| | 0 | 47 | | var result = !isPublished ? await workflowDefinitionPublisher.PublishAsync(definition, cancellationToken) : null |
| | 0 | 48 | | var mappedDefinition = await linker.MapAsync(definition, cancellationToken); |
| | 0 | 49 | | var response = new Response(mappedDefinition, isPublished, result?.AffectedWorkflows.WorkflowDefinitions.Count ? |
| | 0 | 50 | | await Send.OkAsync(response, cancellationToken); |
| | 0 | 51 | | } |
| | | 52 | | } |