< Summary

Information
Class: Elsa.Extensions.AuthorizationServiceCollectionExtensions
Assembly: Elsa.Api.Common
File(s): /home/runner/work/elsa-core/elsa-core/src/common/Elsa.Api.Common/Extensions/AuthorizationServiceCollectionExtensions.cs
Line coverage
47%
Covered lines: 10
Uncovered lines: 11
Coverable lines: 21
Total lines: 80
Line coverage: 47.6%
Branch coverage
57%
Covered branches: 8
Total branches: 14
Branch coverage: 57.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddElsaAuthorization(...)100%11100%
AddPermissionDescriptorsFromAssembly(...)100%88100%
AddPermissionDescriptorsFromLoadedAssemblies(...)0%4260%
AddPermissionDescriptors(...)100%210%

File(s)

/home/runner/work/elsa-core/elsa-core/src/common/Elsa.Api.Common/Extensions/AuthorizationServiceCollectionExtensions.cs

#LineLine coverage
 1using System.Reflection;
 2using Elsa.Authorization;
 3using Elsa.Permissions;
 4using Microsoft.AspNetCore.Authorization;
 5using Microsoft.Extensions.DependencyInjection;
 6using Microsoft.Extensions.DependencyInjection.Extensions;
 7
 8namespace Elsa.Extensions;
 9
 10/// <summary>Registers Elsa's permission model: the evaluator, the descriptor catalog, and the authorization handler.</s
 11public static class AuthorizationServiceCollectionExtensions
 12{
 13    /// <summary>
 14    /// Adds the single permission evaluator and the descriptor registry. Safe to call more than once, and
 15    /// safe to call before any module has contributed descriptors.
 16    /// </summary>
 17    public static IServiceCollection AddElsaAuthorization(this IServiceCollection services)
 18    {
 1819        services.TryAddSingleton<IPermissionEvaluator, PermissionEvaluator>();
 1820        services.TryAddSingleton<IPermissionDescriptorRegistry, DefaultPermissionDescriptorRegistry>();
 1821        services.TryAddSingleton<IPermissionGrantValidator, PermissionGrantValidator>();
 1822        services.TryAddEnumerable(ServiceDescriptor.Scoped<IAuthorizationHandler, PermissionAuthorizationHandler>());
 23
 1824        return services;
 25    }
 26
 27    /// <summary>
 28    /// Contributes every <see cref="IPermissionDescriptorProvider"/> declared in <paramref name="assembly"/>.
 29    /// Discovery is by convention so that a module cannot ship endpoints without the catalog describing them.
 30    /// </summary>
 31    public static IServiceCollection AddPermissionDescriptorsFromAssembly(this IServiceCollection services, Assembly ass
 32    {
 1833        var providerTypes = assembly.GetTypes()
 332134            .Where(x => x is { IsClass: true, IsAbstract: false } && typeof(IPermissionDescriptorProvider).IsAssignableF
 35
 7236        foreach (var providerType in providerTypes)
 1837            services.TryAddEnumerable(ServiceDescriptor.Singleton(typeof(IPermissionDescriptorProvider), providerType));
 38
 1839        return services;
 40    }
 41
 42    /// <summary>
 43    /// Contributes every <see cref="IPermissionDescriptorProvider"/> declared in the loaded Elsa assemblies.
 44    /// </summary>
 45    /// <remarks>
 46    /// The module path discovers descriptors from the assemblies it registers with FastEndpoints. The shell
 47    /// path does not register assemblies that way -- CShells discovers endpoints from features implementing
 48    /// its own marker interface -- so without this the catalog comes up empty on a shell host, taking role
 49    /// authoring, introspection and the stored-permission validator with it. Scanning is bounded to loaded
 50    /// Elsa assemblies and runs once per shell.
 51    /// </remarks>
 52    public static IServiceCollection AddPermissionDescriptorsFromLoadedAssemblies(this IServiceCollection services)
 53    {
 054        var assemblies = AppDomain.CurrentDomain
 055            .GetAssemblies()
 056            .Where(x => x.GetName().Name?.StartsWith("Elsa.", StringComparison.Ordinal) == true && !x.IsDynamic);
 57
 058        foreach (var assembly in assemblies)
 59        {
 60            try
 61            {
 062                services.AddPermissionDescriptorsFromAssembly(assembly);
 063            }
 064            catch (ReflectionTypeLoadException)
 65            {
 66                // An assembly whose types cannot all be loaded contributes nothing rather than failing startup.
 067            }
 68        }
 69
 070        return services;
 71    }
 72
 73    /// <summary>Contributes <typeparamref name="T"/>'s permission descriptors to the catalog.</summary>
 74    public static IServiceCollection AddPermissionDescriptors<T>(this IServiceCollection services) where T : class, IPer
 75    {
 076        services.TryAddEnumerable(ServiceDescriptor.Singleton<IPermissionDescriptorProvider, T>());
 77
 078        return services;
 79    }
 80}