Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

regenerate activity URL when importing Workflows #15291

Merged
merged 8 commits into from
Feb 10, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@
using System.Linq;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Routing;
using OrchardCore.Recipes.Models;
using OrchardCore.Recipes.Services;
using OrchardCore.Workflows.Http.Activities;
using OrchardCore.Workflows.Http.Controllers;
using OrchardCore.Workflows.Http.Models;
using OrchardCore.Workflows.Models;
using OrchardCore.Workflows.Services;

Expand All @@ -12,10 +18,17 @@ namespace OrchardCore.Workflows.Recipes
public class WorkflowTypeStep : IRecipeStepHandler
{
private readonly IWorkflowTypeStore _workflowTypeStore;
private readonly ISecurityTokenService _securityTokenService;
private readonly IUrlHelper _urlHelper;

public WorkflowTypeStep(IWorkflowTypeStore workflowTypeStore)
public WorkflowTypeStep(IWorkflowTypeStore workflowTypeStore,
ISecurityTokenService securityTokenService,
IActionContextAccessor actionContextAccessor,
IUrlHelperFactory urlHelperFactory)
{
_workflowTypeStore = workflowTypeStore;
_securityTokenService = securityTokenService;
_urlHelper = urlHelperFactory.GetUrlHelper(actionContextAccessor.ActionContext);
}

public async Task ExecuteAsync(RecipeExecutionContext context)
Expand All @@ -31,6 +44,15 @@ public async Task ExecuteAsync(RecipeExecutionContext context)
{
var workflow = token.ToObject<WorkflowType>();

foreach (var activity in workflow.Activities.Where(a => a.Name == nameof(HttpRequestEvent)))
{
var tokenLifeSpan = activity.Properties["TokenLifeSpan"];
if (tokenLifeSpan != null)
{
activity.Properties["Url"] = ReGenerateHttpRequestEventUrl(workflow, activity, tokenLifeSpan.ToObject<int>());
}
}

var existing = await _workflowTypeStore.GetAsync(workflow.WorkflowTypeId);

if (existing == null)
Expand All @@ -45,6 +67,14 @@ public async Task ExecuteAsync(RecipeExecutionContext context)
await _workflowTypeStore.SaveAsync(workflow);
}
}

private string ReGenerateHttpRequestEventUrl(WorkflowType workflow, ActivityRecord activity, int tokenLifeSpan)
{
var token = _securityTokenService.CreateToken(new WorkflowPayload(workflow.WorkflowTypeId, activity.ActivityId),
TimeSpan.FromDays(tokenLifeSpan == 0 ? HttpWorkflowController.NoExpiryTokenLifespan : tokenLifeSpan));

return _urlHelper.Action("Invoke", "HttpWorkflow", new { token });
}
}

public class WorkflowStepModel
Expand Down
Loading