using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Options;
using System;
using System.Threading.Tasks;
namespace LINGYUN.Abp.MicroService.WorkflowService;
///
/// Root for Shells hosting. Elsa 3.8.0-rc2 endpoints
/// declare FastEndpoints permission policies named "epPolicy:<EndpointType>" that CShells
/// registers inside the shell's own container, so ASP.NET's root policy provider (ABP's) cannot
/// resolve them. ABP authenticates /elsa/api with the AuthServer JWT and every validated token
/// carries Elsa's "all permissions" claim (see ),
/// so epPolicy names are bridged to that grant; other policy names fall back to the defaults.
///
public sealed class ElsaShellPermissionPolicyProvider : IAuthorizationPolicyProvider
{
// Allow-all policy (always-true assertion; AuthorizationPolicy rejects zero requirements).
private static readonly AuthorizationPolicy AllowAll = new AuthorizationPolicyBuilder().RequireAssertion(_ => true).Build();
private readonly DefaultAuthorizationPolicyProvider _default = new(Options.Create(new AuthorizationOptions()));
///
public Task GetPolicyAsync(string policyName)
{
if (!policyName.StartsWith("epPolicy:", StringComparison.Ordinal))
return _default.GetPolicyAsync(policyName);
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.RequireAssertion(context =>
context.User.HasClaim(Elsa.PermissionNames.ClaimType, Elsa.PermissionNames.All))
.Build();
return Task.FromResult(policy);
}
///
public Task GetDefaultPolicyAsync() => Task.FromResult(AllowAll);
///
public Task GetFallbackPolicyAsync() => Task.FromResult(AllowAll);
}