elsa-core/src/modules/Elsa.Http/Extensions/EventExpressionExecutionContextExtensions.cs

74 lines
3.4 KiB
C#
Raw Normal View History

using Elsa.Expressions.Models;
Implement multitenant HTTP routing (#6031) * Add tenant awareness to bookmark handling and route resolution Added tenant ID support across various components, including bookmark updates, route resolution, and middleware processing. This ensures that bookmark and route operations can now appropriately handle tenant-specific data, improving the system's multitenancy capabilities. * Add Multitenant HTTP Routing feature to Tenants module Introduced a new MultitenantHttpRoutingFeature class to the Elsa.Tenants.AspNetCore module, enhancing the tenant resolution capabilities. Moved RoutePrefixTenantResolver from Elsa.Http to Elsa.Tenants.AspNetCore and updated relevant project references and namespaces accordingly. This refactor improves modularity and separation of concerns between HTTP and tenancy features. * Refactor route handling and tenant configuration Removed redundant `RouteTableExtensions` and replaced with new route providers and updaters, enhancing flexibility and modularity. Introduced tenant-specific HTTP endpoint configurations for better customization and configuration management. * Rename HttpEndpointBookmarkStimulus to HttpEndpointBookmarkPayload Refactor various classes and methods to reflect the renaming from `HttpEndpointBookmarkStimulus` to `HttpEndpointBookmarkPayload`. Add and configure new extension methods for tenant route handling, update the route provider to support multi-tenancy, and adjust the tenants provider to bind configuration properly. * Add HeaderTenantResolver and refactor Http namespace. Introduce HeaderTenantResolver to resolve tenants via HTTP headers. Refactor multiple classes and interfaces to move from the Elsa.Http.Models namespace directly into Elsa.Http for clarity and consistency. * Add Host-based tenant resolution Implemented a HostTenantResolver to resolve tenants based on the request's host and updated tenant configurations with host information. Modified the tenant resolver pipeline and added the new host resolver to the service registrations. * Add tenant-aware caching and accessor support Enhanced caching by incorporating tenant identifiers into cache keys for more granular cache management. Introduced ITenantAccessor dependencies in various services to retrieve the current tenant information. This ensures that cache entries are correctly isolated per tenant. * Reorder tenant resolvers for pipeline setup. Reordered the tenant resolvers in the pipeline to prioritize HostTenantResolver before RoutePrefixTenantResolver. This ensures that tenant resolution is correctly aligned with host-based resolving before checking the route prefix. * Remove unused imports This commit eliminates redundant `using` directives across multiple files to streamline the codebase. This cleanup helps improve code readability and maintainability by removing unnecessary dependencies.
2024-10-14 19:27:11 +00:00
using Elsa.Http;
using Elsa.SasTokens.Contracts;
using Elsa.Workflows.Api;
using Elsa.Workflows.Runtime;
using Microsoft.Extensions.Options;
// ReSharper disable once CheckNamespace
namespace Elsa.Extensions;
2023-09-24 22:55:29 +00:00
/// <summary>
/// Provides extension methods for working with <see cref="ExpressionExecutionContext"/>.
2023-09-24 22:55:29 +00:00
/// </summary>
public static class EventExpressionExecutionContextExtensions
{
2023-09-24 22:55:29 +00:00
/// <summary>
/// Generates a URL that can be used to trigger an event.
/// </summary>
/// <param name="context">The expression execution context.</param>
/// <param name="eventName">The name of the event to trigger.</param>
/// <param name="lifetime">The lifetime of the event trigger token.</param>
/// <returns>A URL that can be used to trigger an event.</returns>
public static string GenerateEventTriggerUrl(this ExpressionExecutionContext context, string eventName, TimeSpan lifetime)
{
var token = context.GenerateEventTriggerTokenInternal(eventName, lifetime);
return context.GenerateEventTriggerUrlInternal(token);
}
2023-09-24 22:55:29 +00:00
/// <summary>
/// Generates a URL that can be used to trigger an event.
/// </summary>
/// <param name="context">The expression execution context.</param>
/// <param name="eventName">The name of the event to trigger.</param>
/// <param name="expiresAt">The expiration date of the event trigger token.</param>
/// <returns>A URL that can be used to trigger an event.</returns>
public static string GenerateEventTriggerUrl(this ExpressionExecutionContext context, string eventName, DateTimeOffset expiresAt)
{
var token = context.GenerateEventTriggerTokenInternal(eventName, expiresAt: expiresAt);
return context.GenerateEventTriggerUrlInternal(token);
}
2023-09-24 22:55:29 +00:00
/// <summary>
/// Generates a URL that can be used to trigger an event.
/// </summary>
/// <param name="context">The expression execution context.</param>
/// <param name="eventName">The name of the event to trigger.</param>
/// <returns>A URL that can be used to trigger an event.</returns>
public static string GenerateEventTriggerUrl(this ExpressionExecutionContext context, string eventName)
{
var token = context.GenerateEventTriggerTokenInternal(eventName);
return context.GenerateEventTriggerUrlInternal(token);
}
private static string GenerateEventTriggerUrlInternal(this ExpressionExecutionContext context, string token)
{
var options = context.GetRequiredService<IOptions<ApiEndpointOptions>>().Value;
var url = $"{options.RoutePrefix}/events/trigger?t={token}";
var absoluteUrlProvider = context.GetRequiredService<IAbsoluteUrlProvider>();
return absoluteUrlProvider.ToAbsoluteUrl(url).ToString();
}
private static string GenerateEventTriggerTokenInternal(this ExpressionExecutionContext context, string eventName, TimeSpan? lifetime = null, DateTimeOffset? expiresAt = null)
{
var workflowInstanceId = context.GetWorkflowExecutionContext().Id;
var payload = new EventTokenPayload(eventName, workflowInstanceId);
var tokenService = context.GetRequiredService<ITokenService>();
return lifetime != null
? tokenService.CreateToken(payload, lifetime.Value)
: expiresAt != null
? tokenService.CreateToken(payload, expiresAt.Value)
: tokenService.CreateToken(payload);
}
}