| | | 1 | | using Elsa.Authorization; |
| | | 2 | | using Elsa.Abstractions; |
| | | 3 | | using Elsa.Common.Entities; |
| | | 4 | | using Elsa.Common.Models; |
| | | 5 | | using Elsa.Models; |
| | | 6 | | using Elsa.Workflows.Api.Models; |
| | | 7 | | using Elsa.Workflows.Management; |
| | | 8 | | using Elsa.Workflows.Management.Exceptions; |
| | | 9 | | using Elsa.Workflows.Management.Filters; |
| | | 10 | | using Elsa.Workflows.Management.Models; |
| | | 11 | | using JetBrains.Annotations; |
| | | 12 | | using Microsoft.AspNetCore.Http; |
| | | 13 | | using Microsoft.Extensions.DependencyInjection; |
| | | 14 | | |
| | | 15 | | namespace Elsa.Workflows.Api.Endpoints.WorkflowDefinitions.List; |
| | | 16 | | |
| | | 17 | | [PublicAPI] |
| | 14 | 18 | | internal class List(IWorkflowDefinitionStore store, IWorkflowDefinitionLinker linker, IEnumerable<IWorkflowDefinitionFil |
| | | 19 | | { |
| | | 20 | | public override void Configure() |
| | | 21 | | { |
| | 14 | 22 | | Get("/workflow-definitions"); |
| | 14 | 23 | | RequirePermission(Elsa.Workflows.Api.Permissions.WorkflowPermissions.Definitions, CoreVerbs.View); |
| | 14 | 24 | | } |
| | | 25 | | |
| | | 26 | | public override async Task<PagedListResponse<LinkedWorkflowDefinitionSummary>> ExecuteAsync(Request request, Cancell |
| | | 27 | | { |
| | 11 | 28 | | var pageArgs = PageArgs.FromPage(request.Page, request.PageSize); |
| | 11 | 29 | | var filter = CreateFilter(request); |
| | | 30 | | try |
| | | 31 | | { |
| | 40 | 32 | | foreach (var filterProvider in filterProviders ?? []) |
| | | 33 | | { |
| | 10 | 34 | | if (!filterProvider.CanApply(filter)) |
| | | 35 | | { |
| | | 36 | | continue; |
| | | 37 | | } |
| | | 38 | | |
| | 9 | 39 | | if (EndpointSecurityOptions.SecurityIsEnabled && !HasRequiredPermissions(filterProvider, filter)) |
| | | 40 | | { |
| | 1 | 41 | | AddError("You do not have permission to filter workflow definitions by labels."); |
| | 1 | 42 | | await Send.ErrorsAsync(StatusCodes.Status403Forbidden, cancellationToken); |
| | 1 | 43 | | return default!; |
| | | 44 | | } |
| | | 45 | | |
| | 8 | 46 | | await filterProvider.ApplyAsync(filter, cancellationToken); |
| | | 47 | | } |
| | 9 | 48 | | } |
| | 1 | 49 | | catch (WorkflowDefinitionFilterNotSupportedException exception) |
| | | 50 | | { |
| | 1 | 51 | | AddError(exception.Message); |
| | 1 | 52 | | await Send.ErrorsAsync(StatusCodes.Status501NotImplemented, cancellationToken); |
| | 1 | 53 | | return default!; |
| | | 54 | | } |
| | | 55 | | |
| | 9 | 56 | | if (filter.LabelIds is { Count: > 0 }) |
| | | 57 | | { |
| | 2 | 58 | | AddError("Filtering workflow definitions by labels is not supported by the configured modules."); |
| | 2 | 59 | | await Send.ErrorsAsync(StatusCodes.Status501NotImplemented, cancellationToken); |
| | 2 | 60 | | return default!; |
| | | 61 | | } |
| | | 62 | | |
| | 7 | 63 | | var summaries = await FindAsync(request, filter, pageArgs, cancellationToken); |
| | 7 | 64 | | var pagedList = new PagedListResponse<WorkflowDefinitionSummary>(summaries); |
| | 7 | 65 | | var response = linker.MapAsync(pagedList); |
| | 7 | 66 | | return response; |
| | 11 | 67 | | } |
| | | 68 | | |
| | | 69 | | private bool HasRequiredPermissions(IWorkflowDefinitionFilterProvider filterProvider, WorkflowDefinitionFilter filte |
| | | 70 | | { |
| | 9 | 71 | | var evaluator = HttpContext.RequestServices.GetService<IPermissionEvaluator>() ?? PermissionEvaluator.Shared; |
| | 17 | 72 | | return filterProvider.GetRequiredPermissions(filter).All(permission => evaluator.HasPermission(HttpContext.User, |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | private WorkflowDefinitionFilter CreateFilter(Request request) |
| | | 76 | | { |
| | 11 | 77 | | var versionOptions = string.IsNullOrWhiteSpace(request.VersionOptions) ? default(VersionOptions?) : VersionOptio |
| | | 78 | | |
| | 11 | 79 | | return new() |
| | 11 | 80 | | { |
| | 11 | 81 | | IsSystem = request.IsSystem, |
| | 11 | 82 | | VersionOptions = versionOptions, |
| | 11 | 83 | | SearchTerm = request.SearchTerm?.Trim(), |
| | 11 | 84 | | MaterializerName = request.MaterializerName, |
| | 11 | 85 | | DefinitionIds = request.DefinitionIds, |
| | 11 | 86 | | Ids = request.Ids, |
| | 11 | 87 | | LabelIds = request.Labels |
| | 11 | 88 | | }; |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | private async Task<Page<WorkflowDefinitionSummary>> FindAsync(Request request, WorkflowDefinitionFilter filter, Page |
| | | 92 | | { |
| | 7 | 93 | | request.OrderBy ??= OrderByWorkflowDefinition.Name; |
| | | 94 | | |
| | 7 | 95 | | var direction = request.OrderBy == OrderByWorkflowDefinition.Name ? request.OrderDirection ?? OrderDirection.Asc |
| | | 96 | | |
| | 7 | 97 | | switch (request.OrderBy) |
| | | 98 | | { |
| | | 99 | | default: |
| | | 100 | | { |
| | 0 | 101 | | var order = new WorkflowDefinitionOrder<DateTimeOffset> |
| | 0 | 102 | | { |
| | 0 | 103 | | KeySelector = p => p.CreatedAt, |
| | 0 | 104 | | Direction = direction |
| | 0 | 105 | | }; |
| | | 106 | | |
| | 0 | 107 | | return await store.FindSummariesAsync(filter, order, pageArgs, cancellationToken); |
| | | 108 | | } |
| | | 109 | | case OrderByWorkflowDefinition.Name: |
| | | 110 | | { |
| | 7 | 111 | | var order = new WorkflowDefinitionOrder<string> |
| | 7 | 112 | | { |
| | 7 | 113 | | KeySelector = p => p.Name!, |
| | 7 | 114 | | Direction = direction |
| | 7 | 115 | | }; |
| | | 116 | | |
| | 7 | 117 | | return await store.FindSummariesAsync(filter, order, pageArgs, cancellationToken); |
| | | 118 | | } |
| | | 119 | | case OrderByWorkflowDefinition.Version: |
| | | 120 | | { |
| | 0 | 121 | | var order = new WorkflowDefinitionOrder<int> |
| | 0 | 122 | | { |
| | 0 | 123 | | KeySelector = p => p.Version, |
| | 0 | 124 | | Direction = direction |
| | 0 | 125 | | }; |
| | | 126 | | |
| | 0 | 127 | | return await store.FindSummariesAsync(filter, order, pageArgs, cancellationToken); |
| | | 128 | | } |
| | | 129 | | } |
| | 7 | 130 | | } |
| | | 131 | | } |