using System.Reflection; using Elsa.Features.Services; using Elsa.Permissions; using FastEndpoints; using Microsoft.Extensions.DependencyInjection; // ReSharper disable once CheckNamespace namespace Elsa.Extensions; /// /// Provides extensions to . /// public static class ModuleExtensions { private static readonly object FastEndpointsAssembliesKey = new(); /// /// Registers the specified assembly for FastEndpoint assembly discovery. /// public static IModule AddFastEndpointsAssembly(this IModule module, Assembly assembly) { var assemblies = module.Properties.GetOrAdd(FastEndpointsAssembliesKey, () => new HashSet()); assemblies.Add(assembly); // Registering an endpoint assembly is what guarantees its permissions work, so the evaluator and // that assembly's descriptors are registered here rather than in AddFastEndpointsFromModule. A host // that wires FastEndpoints itself never calls that helper, and without the authorization handler // every RequirePermission endpoint would answer 403 -- fail-closed, but broken. module.Services.AddElsaAuthorization(); module.Services.AddPermissionDescriptorsFromAssembly(assembly); return module; } /// /// Registers the assembly for FastEndpoint assembly discovery using the specified marker type. /// public static IModule AddFastEndpointsAssembly(this IModule module) => module.AddFastEndpointsAssembly(typeof(T)); /// /// Registers the assembly for FastEndpoint assembly discovery using the specified marker type. /// public static IModule AddFastEndpointsAssembly(this IModule module, Type markerType) => module.AddFastEndpointsAssembly(markerType.Assembly); /// /// Returns all collected assemblies for discovery of endpoints. /// public static IEnumerable GetFastEndpointsAssembliesFromModule(this IModule module) => module.Properties.GetOrAdd(FastEndpointsAssembliesKey, () => new HashSet()); /// /// Adds FastEndpoints to the service container and registers all collected assemblies for discovery of endpoints. /// public static IModule AddFastEndpointsFromModule(this IModule module) { var assemblies = module.GetFastEndpointsAssembliesFromModule().ToList(); module.Services.AddFastEndpoints(options => { options.DisableAutoDiscovery = true; options.Assemblies = assemblies; }); return module; } }