| | | 1 | | using Elsa.Abstractions; |
| | | 2 | | using Elsa.Workflows.Api.Constants; |
| | | 3 | | using Elsa.Workflows.Api.Requirements; |
| | | 4 | | using Elsa.Workflows.Management; |
| | | 5 | | using Elsa.Workflows.Management.Models; |
| | | 6 | | using JetBrains.Annotations; |
| | | 7 | | using Microsoft.AspNetCore.Authorization; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Workflows.Api.Endpoints.WorkflowDefinitions.Import; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Imports JSON and/or ZIP files containing a workflow definitions. |
| | | 13 | | /// </summary> |
| | | 14 | | [PublicAPI] |
| | | 15 | | internal class Import : ElsaEndpoint<WorkflowDefinitionModel> |
| | | 16 | | { |
| | | 17 | | private readonly IWorkflowDefinitionImporter _workflowDefinitionImporter; |
| | | 18 | | private readonly IWorkflowDefinitionLinker _linker; |
| | | 19 | | private readonly IAuthorizationService _authorizationService; |
| | | 20 | | |
| | | 21 | | /// <inheritdoc /> |
| | 1 | 22 | | public Import( |
| | 1 | 23 | | IWorkflowDefinitionImporter workflowDefinitionImporter, |
| | 1 | 24 | | IWorkflowDefinitionLinker linker, |
| | 1 | 25 | | IAuthorizationService authorizationService) |
| | | 26 | | { |
| | 1 | 27 | | _workflowDefinitionImporter = workflowDefinitionImporter; |
| | 1 | 28 | | _linker = linker; |
| | 1 | 29 | | _authorizationService = authorizationService; |
| | 1 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <inheritdoc /> |
| | | 33 | | public override void Configure() |
| | | 34 | | { |
| | 1 | 35 | | Routes("workflow-definitions/import", "workflow-definitions/{definitionId}/import"); |
| | 1 | 36 | | Verbs(FastEndpoints.Http.POST, FastEndpoints.Http.PUT); |
| | 1 | 37 | | ConfigurePermissions("write:workflow-definitions"); |
| | 1 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <inheritdoc /> |
| | | 41 | | public override async Task HandleAsync(WorkflowDefinitionModel model, CancellationToken cancellationToken) |
| | | 42 | | { |
| | 0 | 43 | | var definitionId = model.DefinitionId; |
| | 0 | 44 | | var isNew = string.IsNullOrWhiteSpace(definitionId); |
| | 0 | 45 | | var result = await ImportSingleWorkflowDefinitionAsync(model, cancellationToken); |
| | 0 | 46 | | var definition = result.WorkflowDefinition; |
| | | 47 | | |
| | 0 | 48 | | var authorizationResult = await _authorizationService.AuthorizeAsync(User, new NotReadOnlyResource(definition), |
| | | 49 | | |
| | 0 | 50 | | if (!authorizationResult.Succeeded) |
| | | 51 | | { |
| | 0 | 52 | | await Send.ForbiddenAsync(cancellationToken); |
| | 0 | 53 | | return; |
| | | 54 | | } |
| | | 55 | | |
| | 0 | 56 | | var updatedModel = await _linker.MapAsync(definition, cancellationToken); |
| | | 57 | | |
| | 0 | 58 | | if (result.Succeeded) |
| | | 59 | | { |
| | 0 | 60 | | if (isNew) |
| | 0 | 61 | | await Send.CreatedAtAsync<GetByDefinitionId.GetByDefinitionId>(new { DefinitionId = definitionId }, upda |
| | | 62 | | else |
| | 0 | 63 | | await Send.OkAsync(updatedModel, cancellationToken); |
| | | 64 | | } |
| | | 65 | | |
| | 0 | 66 | | if (ValidationFailed) |
| | 0 | 67 | | await Send.ErrorsAsync(400, cancellationToken); |
| | 0 | 68 | | } |
| | | 69 | | |
| | | 70 | | private async Task<ImportWorkflowResult> ImportSingleWorkflowDefinitionAsync(WorkflowDefinitionModel model, Cancella |
| | | 71 | | { |
| | | 72 | | // Import workflow |
| | 0 | 73 | | var saveWorkflowRequest = new SaveWorkflowDefinitionRequest |
| | 0 | 74 | | { |
| | 0 | 75 | | Model = model, |
| | 0 | 76 | | Publish = false, |
| | 0 | 77 | | }; |
| | | 78 | | |
| | 0 | 79 | | var result = await _workflowDefinitionImporter.ImportAsync(saveWorkflowRequest, cancellationToken); |
| | | 80 | | |
| | 0 | 81 | | if (result.Succeeded) |
| | 0 | 82 | | return result; |
| | | 83 | | |
| | 0 | 84 | | foreach (var validationError in result.ValidationErrors) |
| | 0 | 85 | | AddError(validationError.Message); |
| | | 86 | | |
| | 0 | 87 | | return result; |
| | 0 | 88 | | } |
| | | 89 | | } |