| | | 1 | | using Elsa.Abstractions; |
| | | 2 | | using Elsa.Authorization; |
| | | 3 | | using Elsa.Identity.Permissions; |
| | | 4 | | using Elsa.Permissions; |
| | | 5 | | using FastEndpoints; |
| | | 6 | | using JetBrains.Annotations; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Identity.Endpoints.Permissions.Reach; |
| | | 9 | | |
| | | 10 | | /// <summary>What a wildcard grant covers right now.</summary> |
| | | 11 | | /// <param name="Covers"> |
| | | 12 | | /// A point-in-time snapshot. A wildcard grant also covers resources registered later, so a role editor |
| | | 13 | | /// should present this as "currently covers", not as a fixed list. |
| | | 14 | | /// </param> |
| | | 15 | | public record Response(string Resource, IReadOnlyCollection<string> Covers, int Count); |
| | | 16 | | |
| | | 17 | | /// <summary>The resource pattern to report on, for example <c>workflows/*</c>.</summary> |
| | | 18 | | public class Request |
| | | 19 | | { |
| | | 20 | | /// <summary>The resource pattern.</summary> |
| | | 21 | | public string Resource { get; set; } = null!; |
| | | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Reports the resources a grant currently covers. This is the mitigation for forward reach on the |
| | | 26 | | /// resource axis: a wildcard is convenient precisely because it covers things that do not exist yet, |
| | | 27 | | /// so an author needs a way to see what it reaches today. |
| | | 28 | | /// </summary> |
| | | 29 | | [PublicAPI] |
| | 3 | 30 | | internal class Reach(IPermissionDescriptorRegistry registry) : ElsaEndpoint<Request, Response> |
| | | 31 | | { |
| | | 32 | | /// <inheritdoc /> |
| | | 33 | | public override void Configure() |
| | | 34 | | { |
| | 3 | 35 | | Get("/identity/permissions/reach"); |
| | 3 | 36 | | RequirePermission(IdentityPermissions.Roles, CoreVerbs.View); |
| | 3 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <inheritdoc /> |
| | | 40 | | public override async Task HandleAsync(Request request, CancellationToken cancellationToken) |
| | | 41 | | { |
| | 0 | 42 | | if (string.IsNullOrWhiteSpace(request.Resource)) |
| | | 43 | | { |
| | 0 | 44 | | AddError(nameof(request.Resource), "A resource pattern is required."); |
| | 0 | 45 | | await Send.ErrorsAsync(cancellation: cancellationToken); |
| | 0 | 46 | | return; |
| | | 47 | | } |
| | | 48 | | |
| | 0 | 49 | | var covers = registry.Reach(request.Resource.Trim()); |
| | | 50 | | |
| | 0 | 51 | | await Send.OkAsync(new Response(request.Resource.Trim(), covers, covers.Count), cancellationToken); |
| | 0 | 52 | | } |
| | | 53 | | } |