94 lines
3.5 KiB
C#
94 lines
3.5 KiB
C#
// 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
|
|
// {
|
|
// /// <summary>
|
|
// /// Resolve Policies from Cache → Repository → Default (static) Provider ...
|
|
// /// </summary>
|
|
// public class XDynamicPolicyProvider : IAuthorizationPolicyProvider
|
|
// {
|
|
// private readonly IXAuthorizationCache cache;
|
|
// private readonly IServiceScopeFactory scopeFactory;
|
|
// private readonly DefaultAuthorizationPolicyProvider fallback;
|
|
|
|
// public XDynamicPolicyProvider(
|
|
// IXAuthorizationCache cache,
|
|
// IServiceScopeFactory scopeFactory,
|
|
// IOptions<AuthorizationOptions> options
|
|
// )
|
|
// {
|
|
// //
|
|
// this.cache = cache;
|
|
// this.scopeFactory = scopeFactory;
|
|
// fallback = new DefaultAuthorizationPolicyProvider(options);
|
|
// }
|
|
|
|
// public Task<AuthorizationPolicy> GetDefaultPolicyAsync() =>
|
|
// fallback.GetDefaultPolicyAsync();
|
|
|
|
// public async Task<AuthorizationPolicy> 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<IXPolicyProvider>();
|
|
// var resolver = scope.ServiceProvider.GetService<IXRoleHierarchyResolver>();
|
|
|
|
// //
|
|
// 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<string>();
|
|
// 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);
|
|
// }
|
|
// }
|
|
// } |