| | | 1 | | using Elsa.Authorization; |
| | | 2 | | using System.Collections.Frozen; |
| | | 3 | | using Elsa.Abstractions; |
| | | 4 | | using Elsa.Expressions.Contracts; |
| | | 5 | | using Elsa.Expressions.Models; |
| | | 6 | | using Elsa.Models; |
| | | 7 | | using JetBrains.Annotations; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Workflows.Api.Endpoints.Scripting.ExpressionDescriptors.List; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Returns a TypeScript definition that is used by the Monaco editor to display intellisense for JavaScript expressions |
| | | 13 | | /// </summary> |
| | | 14 | | [UsedImplicitly] |
| | | 15 | | internal class List(IExpressionDescriptorRegistry expressionDescriptorRegistry) : ElsaEndpointWithoutRequest<ListRespons |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// The expression types the host can switch off, which are omitted entirely rather than listed as |
| | | 19 | | /// non-browsable so a disabled language does not appear in the editor at all. Every other type is listed |
| | | 20 | | /// and carries <c>IsBrowsable</c> for the client to act on. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <remarks> |
| | | 23 | | /// This was a map from expression type to a per-author permission. The permission was only ever read to |
| | | 24 | | /// test membership -- the value went unused even before it was retired -- and the decision has always |
| | | 25 | | /// been the host switch, surfaced as <c>IsBrowsable</c>. A set says that without implying a check that |
| | | 26 | | /// does not happen. Per-author script trust was considered and declined in #7975: the host switch is the control. |
| | | 27 | | /// </remarks> |
| | | 28 | | private static readonly FrozenSet<string> HostCodeExpressionTypes = new[] { "CSharp", "Python" }.ToFrozenSet(StringC |
| | | 29 | | |
| | | 30 | | /// <inheritdoc /> |
| | | 31 | | public override void Configure() |
| | | 32 | | { |
| | | 33 | | Get("/descriptors/expression-descriptors"); |
| | | 34 | | RequirePermission(Elsa.Workflows.Api.Permissions.WorkflowPermissions.DescriptorsExpressions, CoreVerbs.View); |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <inheritdoc /> |
| | | 38 | | public override Task HandleAsync(CancellationToken cancellationToken) |
| | | 39 | | { |
| | | 40 | | var descriptors = expressionDescriptorRegistry.ListAll().Where(CanListDescriptor).ToList(); |
| | | 41 | | var models = Map(descriptors).ToList(); |
| | | 42 | | var response = new ListResponse<ExpressionDescriptorModel>(models); |
| | | 43 | | return Send.OkAsync(response, cancellationToken); |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | private static bool CanListDescriptor(ExpressionDescriptor descriptor) => |
| | | 47 | | !HostCodeExpressionTypes.Contains(descriptor.Type) || descriptor.IsBrowsable; |
| | | 48 | | |
| | | 49 | | private static IEnumerable<ExpressionDescriptorModel> Map(List<ExpressionDescriptor> descriptors) => descriptors.Sel |
| | | 50 | | |
| | | 51 | | private static ExpressionDescriptorModel Map(ExpressionDescriptor descriptor) |
| | | 52 | | { |
| | | 53 | | var properties = descriptor.Properties; |
| | | 54 | | return new ExpressionDescriptorModel( |
| | | 55 | | descriptor.Type, |
| | | 56 | | descriptor.DisplayName, |
| | | 57 | | descriptor.IsSerializable, |
| | | 58 | | descriptor.IsBrowsable, |
| | | 59 | | properties); |
| | | 60 | | } |
| | | 61 | | } |
| | | 62 | | |
| | 0 | 63 | | internal record ExpressionDescriptorModel(string Type, string DisplayName, bool IsSerializable, bool IsBrowsable, IDicti |