// using xCommons.Extensions; // using System.Threading.Tasks; // using xIdentityService.Interfaces; // using xIdentityService.Extensions; // using Microsoft.Extensions.Options; // using Microsoft.AspNetCore.Authorization; // using Microsoft.Extensions.DependencyInjection; // namespace xIdentityService.Providers // { // /// // /// Resolve Policies from Cache → Repository → Default (static) Provider ... // /// // public class XDynamicPolicyProvider : IAuthorizationPolicyProvider // { // private readonly IXAuthorizationCache cache; // private readonly IServiceScopeFactory scopeFactory; // private readonly DefaultAuthorizationPolicyProvider fallback; // public XDynamicPolicyProvider( // IXAuthorizationCache cache, // IServiceScopeFactory scopeFactory, // IOptions options // ) // { // // // this.cache = cache; // this.scopeFactory = scopeFactory; // fallback = new DefaultAuthorizationPolicyProvider(options); // } // public Task GetDefaultPolicyAsync() => // fallback.GetDefaultPolicyAsync(); // public async Task GetPolicyAsync(string policyName) // { // // // if (policyName.IsNullOrEmpty()) // { // return await fallback.GetPolicyAsync(policyName); // } // // // // 1. Cache ... // if (cache.TryGetPolicy(policyName, out var cached)) // { // return cached; // } // // // // 2. Repository (needs a Scope because Provider is Singleton) ... // using (var scope = scopeFactory.CreateScope()) // { // // // var repository = scope.ServiceProvider.GetService(); // var resolver = scope.ServiceProvider.GetService(); // // // if (!repository.IsNull() && !resolver.IsNull()) // { // // // var normalized = policyName.ToNormalString(); // var definition = await repository.FindOneAsync( // predicate: p => p.IsEnabled && p.Name.ToLower() == normalized // ); // // // var requiredRoles = definition.RequiredRoles // .ParseListString(); // if (!definition.IsNull()) // { // // // var roles = await resolver.ResolveEffectiveRolesAsync( // explicitRoles: requiredRoles, // minRoleWeight: definition.MinRoleWeight // ); // // // var policy = definition.ToAuthorizationPolicy(roles); // if (!policy.IsNull()) // { // cache.SetPolicy(policyName, policy); // return policy; // } // } // } // } // // // // 3. Fallback to Static Policies (LocalApi.PolicyName, XPolicies.*) ... // return await fallback.GetPolicyAsync(policyName); // } // } // }