| | | 1 | | using Elsa.Authorization; |
| | | 2 | | using Elsa.Labels.Contracts; |
| | | 3 | | using Elsa.Labels.Permissions; |
| | | 4 | | using Elsa.Workflows.Management; |
| | | 5 | | using Elsa.Workflows.Management.Exceptions; |
| | | 6 | | using Elsa.Workflows.Management.Filters; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Labels.Services; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Applies label relationships to workflow definition filters. A version qualifies when it has any requested label ID; |
| | | 12 | | /// </summary> |
| | 0 | 13 | | public class WorkflowDefinitionLabelFilterProvider(IWorkflowDefinitionLabelStore store) : IWorkflowDefinitionFilterProvi |
| | | 14 | | { |
| | | 15 | | /// <inheritdoc /> |
| | 0 | 16 | | public bool CanApply(WorkflowDefinitionFilter filter) => filter.LabelIds is { Count: > 0 }; |
| | | 17 | | |
| | | 18 | | /// <summary>Requires workflow-definition-label view permission when a label filter is present.</summary> |
| | | 19 | | public IEnumerable<Permission> GetRequiredPermissions(WorkflowDefinitionFilter filter) => |
| | 0 | 20 | | CanApply(filter) ? [new(LabelPermissions.WorkflowDefinitionLabels, CoreVerbs.View)] : []; |
| | | 21 | | |
| | | 22 | | /// <inheritdoc /> |
| | | 23 | | public async Task ApplyAsync(WorkflowDefinitionFilter filter, CancellationToken cancellationToken = default) |
| | | 24 | | { |
| | 0 | 25 | | if (filter.LabelIds is not { Count: > 0 }) |
| | | 26 | | { |
| | 0 | 27 | | return; |
| | | 28 | | } |
| | | 29 | | |
| | 0 | 30 | | if (store is not IWorkflowDefinitionLabelQuery query) |
| | | 31 | | { |
| | 0 | 32 | | throw new WorkflowDefinitionFilterNotSupportedException("Filtering workflow definitions by labels is not sup |
| | | 33 | | } |
| | | 34 | | |
| | 0 | 35 | | var labelIds = filter.LabelIds.Distinct().ToList(); |
| | 0 | 36 | | var matchingVersionIds = (await query.FindByLabelIdsAsync(labelIds, cancellationToken)) |
| | 0 | 37 | | .Select(x => x.WorkflowDefinitionVersionId) |
| | 0 | 38 | | .Distinct() |
| | 0 | 39 | | .ToList(); |
| | | 40 | | |
| | 0 | 41 | | filter.Ids = filter.Ids == null |
| | 0 | 42 | | ? matchingVersionIds |
| | 0 | 43 | | : filter.Ids.Intersect(matchingVersionIds).ToList(); |
| | 0 | 44 | | filter.LabelIds = null; |
| | 0 | 45 | | } |
| | | 46 | | } |