comment uncomplete authorization services ...
This commit is contained in:
@@ -1,94 +1,94 @@
|
||||
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;
|
||||
// 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;
|
||||
// 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 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 Task<AuthorizationPolicy> GetDefaultPolicyAsync() =>
|
||||
// fallback.GetDefaultPolicyAsync();
|
||||
|
||||
public async Task<AuthorizationPolicy> GetPolicyAsync(string policyName)
|
||||
{
|
||||
//
|
||||
if (policyName.IsNullOrEmpty())
|
||||
{
|
||||
return await fallback.GetPolicyAsync(policyName);
|
||||
}
|
||||
// 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;
|
||||
}
|
||||
// //
|
||||
// // 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>();
|
||||
// //
|
||||
// // 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
|
||||
);
|
||||
// //
|
||||
// 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 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// //
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
// //
|
||||
// // 3. Fallback to Static Policies (LocalApi.PolicyName, XPolicies.*) ...
|
||||
// return await fallback.GetPolicyAsync(policyName);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
Reference in New Issue
Block a user