| | | 1 | | using System.Collections.Frozen; |
| | | 2 | | using Elsa.Abstractions; |
| | | 3 | | using Elsa.Expressions.Contracts; |
| | | 4 | | using Elsa.Expressions.Models; |
| | | 5 | | using Elsa.Models; |
| | | 6 | | using JetBrains.Annotations; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Workflows.Api.Endpoints.Scripting.ExpressionDescriptors.List; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Returns a TypeScript definition that is used by the Monaco editor to display intellisense for JavaScript expressions |
| | | 12 | | /// </summary> |
| | | 13 | | [UsedImplicitly] |
| | 3 | 14 | | internal class List(IExpressionDescriptorRegistry expressionDescriptorRegistry) : ElsaEndpointWithoutRequest<ListRespons |
| | | 15 | | { |
| | 0 | 16 | | private static readonly IReadOnlyDictionary<string, string> PrivilegedExpressionPermissions = new Dictionary<string, |
| | 0 | 17 | | { |
| | 0 | 18 | | ["CSharp"] = PermissionNames.ExecuteCSharpExpressions, |
| | 0 | 19 | | ["Python"] = PermissionNames.ExecutePythonExpressions |
| | 0 | 20 | | }.ToFrozenDictionary(StringComparer.Ordinal); |
| | | 21 | | |
| | | 22 | | /// <inheritdoc /> |
| | | 23 | | public override void Configure() |
| | | 24 | | { |
| | 3 | 25 | | Get("/descriptors/expression-descriptors"); |
| | 3 | 26 | | ConfigurePermissions("read:*", "read:expression-descriptors"); |
| | 3 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <inheritdoc /> |
| | | 30 | | public override Task HandleAsync(CancellationToken cancellationToken) |
| | | 31 | | { |
| | 0 | 32 | | var descriptors = expressionDescriptorRegistry.ListAll().Where(CanListDescriptor).ToList(); |
| | 0 | 33 | | var models = Map(descriptors).ToList(); |
| | 0 | 34 | | var response = new ListResponse<ExpressionDescriptorModel>(models); |
| | 0 | 35 | | return Send.OkAsync(response, cancellationToken); |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | private bool CanListDescriptor(ExpressionDescriptor descriptor) |
| | | 39 | | { |
| | 0 | 40 | | if (!PrivilegedExpressionPermissions.TryGetValue(descriptor.Type, out var permission)) |
| | 0 | 41 | | return true; |
| | | 42 | | |
| | 0 | 43 | | return descriptor.IsBrowsable && User.Claims.Any(x => x.Type == PermissionNames.ClaimType && (x.Value == Permiss |
| | | 44 | | } |
| | | 45 | | |
| | 0 | 46 | | private static IEnumerable<ExpressionDescriptorModel> Map(List<ExpressionDescriptor> descriptors) => descriptors.Sel |
| | | 47 | | |
| | | 48 | | private static ExpressionDescriptorModel Map(ExpressionDescriptor descriptor) |
| | | 49 | | { |
| | 0 | 50 | | var properties = descriptor.Properties; |
| | 0 | 51 | | return new ExpressionDescriptorModel( |
| | 0 | 52 | | descriptor.Type, |
| | 0 | 53 | | descriptor.DisplayName, |
| | 0 | 54 | | descriptor.IsSerializable, |
| | 0 | 55 | | descriptor.IsBrowsable, |
| | 0 | 56 | | properties); |
| | | 57 | | } |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | internal record ExpressionDescriptorModel(string Type, string DisplayName, bool IsSerializable, bool IsBrowsable, IDicti |