last ...
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
using xIdentityService.Interfaces;
|
||||
|
||||
namespace xIdentityService.Providers
|
||||
{
|
||||
public class XAuthorizationProvider : IXAuthorizationProvider
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +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;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using xCommons.Extensions;
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.Generic;
|
||||
using xIdentityService.Interfaces;
|
||||
|
||||
namespace xIdentityService.Providers
|
||||
{
|
||||
public class XRoleHierarchyResolver : IXRoleHierarchyResolver
|
||||
{
|
||||
private readonly IXRoleProvider provider;
|
||||
|
||||
public XRoleHierarchyResolver(
|
||||
IXRoleProvider provider
|
||||
)
|
||||
{
|
||||
this.provider = provider;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<string>> GetRolesByMinWeightAsync(
|
||||
int minWeight,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var roles = await provider.FindManyAsync(
|
||||
predicate: r => r.IsEnabled && r.Weight >= minWeight,
|
||||
orderBuilder: q => q.OrderBy(r => r.Weight),
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
var result = roles
|
||||
.Select(r => r.Name.ToNormalString())
|
||||
.Distinct()
|
||||
.ToList();
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<string>> ResolveEffectiveRolesAsync(
|
||||
IEnumerable<string> explicitRoles,
|
||||
int? minRoleWeight,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
|
||||
|
||||
//
|
||||
// Explicit Roles ...
|
||||
if (explicitRoles.HasChild())
|
||||
{
|
||||
//
|
||||
foreach (var role in explicitRoles)
|
||||
{
|
||||
result.Add(role.ToNormalString());
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Roles by Weight ...
|
||||
if (minRoleWeight.HasValue)
|
||||
{
|
||||
//
|
||||
var byWeight = await GetRolesByMinWeightAsync(
|
||||
minRoleWeight.Value,
|
||||
cancellationToken
|
||||
);
|
||||
foreach (var role in byWeight)
|
||||
{
|
||||
result.Add(role);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Parent Chain: any Role whose ParentRoleName is in result inherits it ...
|
||||
var all = await provider.GetAllAsync(cancellationToken: cancellationToken);
|
||||
bool changed;
|
||||
do
|
||||
{
|
||||
//
|
||||
changed = false;
|
||||
foreach (var role in all.Where(r => r.IsEnabled && !r.ParentRoleName.IsNullOrEmpty()))
|
||||
{
|
||||
//
|
||||
if (result.Contains(role.ParentRoleName) &&
|
||||
result.Add(role.Name.ToNormalString()))
|
||||
{
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
} while (changed);
|
||||
|
||||
//
|
||||
return [.. result];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user