This commit is contained in:
2026-11-21 00:08:36 +03:30
parent c2b2dd27c5
commit 4e114aa85a
6 changed files with 688 additions and 44 deletions
+120 -35
View File
@@ -5,9 +5,10 @@ using xCommons.Extensions;
using xIdentityModels.Dtos;
using xExceptions.Constants;
using System.Threading.Tasks;
using xIdentityModels.Models;
using xIdentityService.Models;
using System.Collections.Generic;
using xIdentityService.Interfaces;
using xIdentityService.Extensions;
//
// TODO: Remove this to xIds ...
@@ -23,7 +24,6 @@ namespace xIdentityService.Providers
private readonly IXAuthorizationCache cache;
private readonly IXScopeSyncService scopeSync;
private readonly IXRoleHierarchyResolver resolver;
// private readonly IXEventServiceProvider eventService;
public XAuthorizationProvider(
IXRoleProvider roles,
@@ -62,10 +62,91 @@ namespace xIdentityService.Providers
cache.RemovePolicy(policyName);
}
}
/// <summary>
/// Re-push every Role and Scope into IdentityServer and drop the local
/// authorization cache so policies rebuild lazily on next evaluation.
/// </summary>
public async Task SyncAllAsync(CancellationToken cancellationToken = default)
{
//
// Sync all Roles to IdentityServer (RoleManager<IdentityRole>) ...
foreach (var role in roles.AsQueryable())
{
//
await roleSync
.UpsertAsync(role, cancellationToken);
}
//
// Sync all Scopes to IdentityServer (ConfigurationDbContext) ...
foreach (var scope in scopes.AsQueryable())
{
//
await scopeSync
.UpsertAsync(scope, cancellationToken);
}
//
// Drop local authorization cache ...
await InvalidateAsync();
}
#endregion
//
#region Private ...
private async Task<XRoleDefinitionDto> FindRoleOrThrowAsync(
string name,
CancellationToken cancellationToken
)
{
//
if (name.IsNullOrEmpty())
{
XException.InvalidArgs.Throw();
}
//
var normalized = name.ToNormalString();
var result = await roles.FindOneAsync(
predicate: r => r.Name.ToLower() == normalized,
cancellationToken: cancellationToken
);
if (result.IsNull())
{
XException.NotFound.Throw();
}
//
return result;
}
private async Task<XScopeDefinitionDto> FindScopeOrThrowAsync(
string name,
CancellationToken cancellationToken
)
{
//
if (name.IsNullOrEmpty())
{
XException.InvalidArgs.Throw();
}
//
var normalized = name.ToNormalString();
var result = await scopes.FindOneAsync(
predicate: s => s.Name.ToLower() == normalized,
cancellationToken: cancellationToken
);
if (result.IsNull())
{
XException.NotFound.Throw();
}
//
return result;
}
private async Task<XPolicyDefinitionDto> FindPolicyOrThrowAsync(
string name,
CancellationToken cancellationToken
@@ -152,11 +233,18 @@ namespace xIdentityService.Providers
}
}
private async Task<XPolicyDefinitionDto> ToDtoWithEffectiveRolesAsync(
private async Task<XEffectiveAuthorizationDto> ToDtoWithEffectiveRolesAsync(
XPolicyDefinitionDto item,
CancellationToken cancellationToken = default
)
{
//
// Null / nameless guard ...
if (item.IsNull() || item.Name.IsNullOrEmpty())
{
return null;
}
//
// Parse the DTO's CSV-string RequiredRoles. ParseListString NREs on a
// null string, so guard first; the resolver itself is null-safe on an
@@ -166,42 +254,39 @@ namespace xIdentityService.Providers
: item.RequiredRoles.ParseListString<string>();
//
item.EffectiveRoles = (
await resolver.ResolveEffectiveRolesAsync(
explicitRoles: explicitRoles,
minRoleWeight: item.MinRoleWeight,
cancellationToken: cancellationToken
)
).ToList();
// Resolve the effective role set
// (explicit RequiredRoles U roles by MinRoleWeight U parent chain) ...
var effectiveRoles = await resolver.ResolveEffectiveRolesAsync(
explicitRoles: explicitRoles,
minRoleWeight: item.MinRoleWeight,
cancellationToken: cancellationToken
);
//
return item;
}
private async Task<XPolicyDefinitionDto> ToDtoWithEffectiveRolesAsync(
XPolicyDefinition entity,
CancellationToken cancellationToken
)
{
//
// Entity -> DTO (the RequiredRoles collection is serialized to the
// CSV string on the DTO by the ToDto mapping) ...
var dto = entity.ToDto();
// Project onto XEffectiveAuthorizationDto - the ONLY DTO that carries
// EffectiveRoles / EffectiveScopes / EffectiveClaims. Each CSV / JSON
// field is null-guarded here (the shared ToEffectiveAuthorization
// extension calls ParseListString on RequiredScopes without a null
// guard, so the projection is built inline to stay null-safe) ...
var result = new XEffectiveAuthorizationDto
{
PolicyName = item.Name,
RequireEnabledUser = item.RequireEnabledUser,
RequireAuthenticatedUser = item.RequireAuthenticatedUser,
EffectiveClaims = item.RequiredClaims.ToRequiredClaims(),
EffectiveScopes = item.RequiredScopes.IsNullOrEmpty()
? new HashSet<string>(StringComparer.OrdinalIgnoreCase)
: new HashSet<string>(
item.RequiredScopes.ParseListString<string>(),
StringComparer.OrdinalIgnoreCase
),
EffectiveRoles = effectiveRoles.HasChild()
? new HashSet<string>(effectiveRoles, StringComparer.OrdinalIgnoreCase)
: new HashSet<string>(StringComparer.OrdinalIgnoreCase),
};
//
// Effective Roles = explicit RequiredRoles U roles by MinRoleWeight
// U parent chain. explicitRoles takes IEnumerable<string>, so pass
// the entity's collection directly (the resolver is null-safe on it) ...
dto.EffectiveRoles = (
await resolver.ResolveEffectiveRolesAsync(
explicitRoles: entity.RequiredRoles,
minRoleWeight: entity.MinRoleWeight,
cancellationToken: cancellationToken
)
).ToList();
//
return dto;
return result;
}
#endregion
}