| | | 1 | | using Elsa.Extensions; |
| | | 2 | | using Elsa.Workflows; |
| | | 3 | | using Elsa.Workflows.Activities.Flowchart.Attributes; |
| | | 4 | | using Elsa.Workflows.Attributes; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Server.Web.Activities; |
| | | 7 | | |
| | | 8 | | [Activity("Elsa", "Authorization", "Authorizes a flow based on the configured policies.")] |
| | | 9 | | [FlowNode("Authorized", "Unauthorized", "Error")] |
| | | 10 | | public class AuthorizeFlow : Activity<string> |
| | | 11 | | { |
| | | 12 | | protected override ValueTask ExecuteAsync(ActivityExecutionContext context) |
| | | 13 | | { |
| | 0 | 14 | | var httpContext = context.GetRequiredService<IHttpContextAccessor>().HttpContext; |
| | | 15 | | |
| | 0 | 16 | | if (httpContext == null) |
| | 0 | 17 | | throw new InvalidOperationException("HttpContext is not available. Ensure that the activity is executed with |
| | | 18 | | |
| | 0 | 19 | | var bookmark = context.CreateBookmark(new AuthorizeStimulus(), OnResumeAsync); |
| | 0 | 20 | | var redirectUrl = context.ExpressionExecutionContext.GenerateBookmarkTriggerUrl(bookmark.Id); |
| | | 21 | | |
| | 0 | 22 | | Result.Set(context, redirectUrl); |
| | 0 | 23 | | return ValueTask.CompletedTask; |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | private async ValueTask OnResumeAsync(ActivityExecutionContext context) |
| | | 27 | | { |
| | 0 | 28 | | if (!context.TryGetWorkflowInput<string>("Answer", out var response)) |
| | | 29 | | { |
| | 0 | 30 | | await context.CompleteActivityWithOutcomesAsync("Unauthorized"); |
| | 0 | 31 | | return; |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | switch (response) |
| | | 35 | | { |
| | | 36 | | case "Authorized": |
| | 0 | 37 | | await context.CompleteActivityWithOutcomesAsync("Authorized"); |
| | 0 | 38 | | return; |
| | | 39 | | case "Error": |
| | 0 | 40 | | await context.CompleteActivityWithOutcomesAsync("Error"); |
| | 0 | 41 | | return; |
| | | 42 | | default: |
| | 0 | 43 | | await context.CompleteActivityWithOutcomesAsync("Unauthorized"); |
| | | 44 | | break; |
| | | 45 | | } |
| | 0 | 46 | | } |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | public record AuthorizeStimulus; |