Compare commits
2
Commits
384cffac36
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
49483d1d39 | ||
|
|
9d913146e1 |
@@ -14,12 +14,13 @@ using xMessageService.DI;
|
|||||||
using xStorageService.DI;
|
using xStorageService.DI;
|
||||||
using xCommons.Extensions;
|
using xCommons.Extensions;
|
||||||
using xIds.Configurations;
|
using xIds.Configurations;
|
||||||
using xIdentityService.DI;
|
|
||||||
using xExceptions.Constants;
|
using xExceptions.Constants;
|
||||||
using xCommons.Authorization;
|
using xCommons.Authorization;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using xMessageService.Interfaces;
|
using xMessageService.Interfaces;
|
||||||
using xStorageService.Interfaces;
|
using xStorageService.Interfaces;
|
||||||
|
using xIdentityService.Providers;
|
||||||
|
using xIdentityService.Extensions;
|
||||||
using xIdentityService.Interfaces;
|
using xIdentityService.Interfaces;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
@@ -95,6 +96,10 @@ namespace xIds.DI
|
|||||||
source.AddScoped<IXScopeProvider, XScopeProvider>();
|
source.AddScoped<IXScopeProvider, XScopeProvider>();
|
||||||
source.AddScoped<IXPolicyProvider, XPolicyProvider>();
|
source.AddScoped<IXPolicyProvider, XPolicyProvider>();
|
||||||
|
|
||||||
|
//
|
||||||
|
source.AddXAuthorizationCache();
|
||||||
|
source.AddScoped<IXRoleHierarchyResolver, XRoleHierarchyResolver>();
|
||||||
|
|
||||||
//
|
//
|
||||||
source.AddXAuthorizationProvider();
|
source.AddXAuthorizationProvider();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
using xIds.Providers;
|
using xIds.Providers;
|
||||||
using xIds.Interfaces;
|
using xIds.Interfaces;
|
||||||
|
using xCommons.Extensions;
|
||||||
|
using xExceptions.Constants;
|
||||||
|
using xIdentityService.Interfaces;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
namespace xIds.Extensions
|
namespace xIds.Extensions
|
||||||
@@ -25,5 +28,37 @@ namespace xIds.Extensions
|
|||||||
{
|
{
|
||||||
source.AddScoped<IXIdentityMessageProvider, XIdentityMessageProvider>();
|
source.AddScoped<IXIdentityMessageProvider, XIdentityMessageProvider>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Register Authorization Provider ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
public static void AddXAuthorizationProvider(
|
||||||
|
this IServiceCollection source
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate Requirements ...
|
||||||
|
var roles = source.GetRegisteredService<IXRoleProvider>();
|
||||||
|
var scopes = source.GetRegisteredService<IXScopeProvider>();
|
||||||
|
var policies = source.GetRegisteredService<IXPolicyProvider>();
|
||||||
|
var cache = source.GetRegisteredService<IXAuthorizationCache>();
|
||||||
|
var resolver = source.GetRegisteredService<IXRoleHierarchyResolver>();
|
||||||
|
|
||||||
|
//
|
||||||
|
var isInvalid =
|
||||||
|
roles.IsNull() ||
|
||||||
|
cache.IsNull() ||
|
||||||
|
scopes.IsNull() ||
|
||||||
|
policies.IsNull() ||
|
||||||
|
resolver.IsNull();
|
||||||
|
if (isInvalid)
|
||||||
|
{
|
||||||
|
XException.InvalidConfiguration.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
source.AddScoped<IXAuthorizationProvider, XAuthorizationProvider>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -21,7 +21,7 @@ namespace xIds.Providers
|
|||||||
AllowMultiple = false,
|
AllowMultiple = false,
|
||||||
Inherited = true
|
Inherited = true
|
||||||
)]
|
)]
|
||||||
public class XApiKeyAccessAttribute : AuthorizeAttribute, IAuthorizationFilter
|
public class XApiKeyAccessAttribute : AuthorizeAttribute, IAsyncAuthorizationFilter
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Optional allowed scopes. If empty, no scope enforcement.
|
/// Optional allowed scopes. If empty, no scope enforcement.
|
||||||
@@ -46,21 +46,7 @@ namespace xIds.Providers
|
|||||||
/// Authorization Filtering ...
|
/// Authorization Filtering ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="context"></param>
|
/// <param name="context"></param>
|
||||||
public void OnAuthorization(AuthorizationFilterContext context)
|
public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
|
||||||
{
|
|
||||||
Task.FromResult(OnAuthorizationAsync(context));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Handle Authorization Filtering Using Tasks ...
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="context"></param>
|
|
||||||
/// <param name="cancellationToken"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
private async Task OnAuthorizationAsync(
|
|
||||||
AuthorizationFilterContext context,
|
|
||||||
CancellationToken cancellationToken = default
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
// Access HttpContext ...
|
// Access HttpContext ...
|
||||||
@@ -125,7 +111,7 @@ namespace xIds.Providers
|
|||||||
userInfo: null,
|
userInfo: null,
|
||||||
clientIP: clientIP,
|
clientIP: clientIP,
|
||||||
requiredScope: scopes,
|
requiredScope: scopes,
|
||||||
cancellationToken: cancellationToken
|
cancellationToken: default
|
||||||
);
|
);
|
||||||
if (validationResult.Errors.HasChild())
|
if (validationResult.Errors.HasChild())
|
||||||
{
|
{
|
||||||
@@ -155,5 +141,18 @@ namespace xIds.Providers
|
|||||||
context.Result = new UnauthorizedObjectResult(error);
|
context.Result = new UnauthorizedObjectResult(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Handle Authorization Filtering Using Tasks ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="context"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
private async Task OnAuthorizationAsync(
|
||||||
|
AuthorizationFilterContext context,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,11 +6,14 @@ using xCommons.Extensions;
|
|||||||
using xIdentityModels.Dtos;
|
using xIdentityModels.Dtos;
|
||||||
using xExceptions.Constants;
|
using xExceptions.Constants;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using xIdentityModels.Models;
|
||||||
using xIdentityService.Models;
|
using xIdentityService.Models;
|
||||||
|
using xIdentityModels.Constants;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using IdentityServer4.Extensions;
|
using IdentityServer4.Extensions;
|
||||||
using xIdentityService.Interfaces;
|
using xIdentityService.Interfaces;
|
||||||
using xIdentityService.Extensions;
|
using xIdentityService.Extensions;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
|
||||||
namespace xIds.Providers
|
namespace xIds.Providers
|
||||||
{
|
{
|
||||||
@@ -47,6 +50,8 @@ namespace xIds.Providers
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Clear Local Cache and Notify other Instances ...
|
/// Clear Local Cache and Notify other Instances ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="policyName"></param>
|
||||||
|
/// <returns></returns>
|
||||||
public async Task InvalidateAsync(string policyName = null)
|
public async Task InvalidateAsync(string policyName = null)
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
@@ -59,10 +64,193 @@ namespace xIds.Providers
|
|||||||
cache.RemovePolicy(policyName);
|
cache.RemovePolicy(policyName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Build Authorization Policy ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="policyName"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<AuthorizationPolicy> BuildAuthorizationPolicyAsync(
|
||||||
|
string policyName,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var effective = await GetEffectiveAuthorizationAsync(policyName, cancellationToken);
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = effective.ToAuthorizationPolicy();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get Effective Authorization ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="target"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<XEffectiveAuthorizationDto> GetEffectiveAuthorizationAsync(
|
||||||
|
XUserClaimsInfoDto target,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Define Result ...
|
||||||
|
var result = new XEffectiveAuthorizationDto
|
||||||
|
{
|
||||||
|
EffectiveClaims = [],
|
||||||
|
RequireEnabledUser = !target.IsNull(),
|
||||||
|
RequireAuthenticatedUser = !target.IsNull(),
|
||||||
|
EffectiveRoles = new HashSet<string>(StringComparer.OrdinalIgnoreCase),
|
||||||
|
EffectiveScopes = new HashSet<string>(StringComparer.OrdinalIgnoreCase),
|
||||||
|
};
|
||||||
|
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
if (target.IsNull())
|
||||||
|
{
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Effective roles = the user's actual roles (from RoleManager) ...
|
||||||
|
var userRoles = await GetUserRolesAsync(
|
||||||
|
cancellationToken: cancellationToken,
|
||||||
|
userId: target.GetUserSelectByParam(userSelectBy: XUserSelectBy.ID)
|
||||||
|
);
|
||||||
|
foreach (var role in userRoles)
|
||||||
|
{
|
||||||
|
result.EffectiveRoles.Add(role);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// NOTE: EffectiveScopes / EffectiveClaims aggregation is wired here from
|
||||||
|
// the user's scope / claim source on XUserClaimsInfoDto (xIdentityModels).
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get Effective Authorization ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="policyName"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<XEffectiveAuthorizationDto> GetEffectiveAuthorizationAsync(
|
||||||
|
string policyName,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (policyName.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var policy = await FindPolicyOrThrowAsync(policyName, cancellationToken);
|
||||||
|
var effective = await ToDtoWithEffectiveRolesAsync(policy, cancellationToken);
|
||||||
|
if (effective.IsNull())
|
||||||
|
{
|
||||||
|
XException.InvalidConfiguration.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return effective;
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
//
|
//
|
||||||
#region Private ...
|
#region Private ...
|
||||||
|
private async Task ValidateReferencesAsync(
|
||||||
|
XPolicyDefinitionDto item,
|
||||||
|
CancellationToken cancellationToken
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Defensive guard ...
|
||||||
|
if (item.IsNull())
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// The DTO stores RequiredRoles / RequiredScopes as CSV strings.
|
||||||
|
// Parse them into collections ONCE (ParseListString NREs on a null
|
||||||
|
// string, so guard first) ...
|
||||||
|
|
||||||
|
//
|
||||||
|
var requiredRoles = item.RequiredRoles.IsNullOrEmpty()
|
||||||
|
? []
|
||||||
|
: item.RequiredRoles.ParseListString<string>().ToList();
|
||||||
|
|
||||||
|
//
|
||||||
|
var requiredScopes = item.RequiredScopes.IsNullOrEmpty()
|
||||||
|
? []
|
||||||
|
: item.RequiredScopes.ParseListString<string>().ToList();
|
||||||
|
|
||||||
|
//
|
||||||
|
// Referenced Roles must exist ...
|
||||||
|
if (requiredRoles.HasChild())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var existingRoles = new HashSet<string>(
|
||||||
|
(await roles.GetAllAsync(cancellationToken: cancellationToken))
|
||||||
|
.Select(r => r.Name.ToNormalString()),
|
||||||
|
StringComparer.OrdinalIgnoreCase
|
||||||
|
);
|
||||||
|
if (requiredRoles.Any(r => !existingRoles.Contains(r.ToNormalString())))
|
||||||
|
{
|
||||||
|
XException.NotFound.Throw();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Referenced Scopes must exist ...
|
||||||
|
if (requiredScopes.HasChild())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var existingScopes = new HashSet<string>(
|
||||||
|
(await scopes.GetAllAsync(cancellationToken: cancellationToken))
|
||||||
|
.Select(s => s.Name.ToNormalString()),
|
||||||
|
StringComparer.OrdinalIgnoreCase
|
||||||
|
);
|
||||||
|
if (requiredScopes.Any(s => !existingScopes.Contains(s.ToNormalString())))
|
||||||
|
{
|
||||||
|
XException.NotFound.Throw();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IEnumerable<string>> GetUserRolesAsync(
|
||||||
|
string userId,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (userId.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
|
|
||||||
|
//
|
||||||
|
var user = await manager.UserManager.FindByIdAsync(userId);
|
||||||
|
if (user.IsNull())
|
||||||
|
{
|
||||||
|
XException.NotFound.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = await manager.UserManager.GetRolesAsync(user);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
private async Task<XRoleDefinitionDto> FindRoleOrThrowAsync(
|
private async Task<XRoleDefinitionDto> FindRoleOrThrowAsync(
|
||||||
string name,
|
string name,
|
||||||
CancellationToken cancellationToken
|
CancellationToken cancellationToken
|
||||||
@@ -141,66 +329,6 @@ namespace xIds.Providers
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task ValidateReferencesAsync(
|
|
||||||
XPolicyDefinitionDto item,
|
|
||||||
CancellationToken cancellationToken
|
|
||||||
)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// Defensive guard ...
|
|
||||||
if (item.IsNull())
|
|
||||||
{
|
|
||||||
XException.InvalidArgs.Throw();
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// The DTO stores RequiredRoles / RequiredScopes as CSV strings.
|
|
||||||
// Parse them into collections ONCE (ParseListString NREs on a null
|
|
||||||
// string, so guard first) ...
|
|
||||||
|
|
||||||
//
|
|
||||||
var requiredRoles = item.RequiredRoles.IsNullOrEmpty()
|
|
||||||
? []
|
|
||||||
: item.RequiredRoles.ParseListString<string>().ToList();
|
|
||||||
|
|
||||||
//
|
|
||||||
var requiredScopes = item.RequiredScopes.IsNullOrEmpty()
|
|
||||||
? []
|
|
||||||
: item.RequiredScopes.ParseListString<string>().ToList();
|
|
||||||
|
|
||||||
//
|
|
||||||
// Referenced Roles must exist ...
|
|
||||||
if (requiredRoles.HasChild())
|
|
||||||
{
|
|
||||||
//
|
|
||||||
var existingRoles = new HashSet<string>(
|
|
||||||
(await roles.GetAllAsync(cancellationToken: cancellationToken))
|
|
||||||
.Select(r => r.Name.ToNormalString()),
|
|
||||||
StringComparer.OrdinalIgnoreCase
|
|
||||||
);
|
|
||||||
if (requiredRoles.Any(r => !existingRoles.Contains(r.ToNormalString())))
|
|
||||||
{
|
|
||||||
XException.NotFound.Throw();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Referenced Scopes must exist ...
|
|
||||||
if (requiredScopes.HasChild())
|
|
||||||
{
|
|
||||||
//
|
|
||||||
var existingScopes = new HashSet<string>(
|
|
||||||
(await scopes.GetAllAsync(cancellationToken: cancellationToken))
|
|
||||||
.Select(s => s.Name.ToNormalString()),
|
|
||||||
StringComparer.OrdinalIgnoreCase
|
|
||||||
);
|
|
||||||
if (requiredScopes.Any(s => !existingScopes.Contains(s.ToNormalString())))
|
|
||||||
{
|
|
||||||
XException.NotFound.Throw();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task<XEffectiveAuthorizationDto> ToDtoWithEffectiveRolesAsync(
|
private async Task<XEffectiveAuthorizationDto> ToDtoWithEffectiveRolesAsync(
|
||||||
XPolicyDefinitionDto item,
|
XPolicyDefinitionDto item,
|
||||||
CancellationToken cancellationToken = default
|
CancellationToken cancellationToken = default
|
||||||
|
|||||||
@@ -0,0 +1,250 @@
|
|||||||
|
using xModels.Dtos;
|
||||||
|
using System.Threading;
|
||||||
|
using xCommons.Extensions;
|
||||||
|
using xIdentityModels.Dtos;
|
||||||
|
using xExceptions.Constants;
|
||||||
|
using xIdentityModels.Models;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using xIdentityService.Models;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace xIds.Providers
|
||||||
|
{
|
||||||
|
public partial class XAuthorizationProvider
|
||||||
|
{
|
||||||
|
//
|
||||||
|
#region Policy ...
|
||||||
|
public async Task<bool> RemovePolicyAsync(
|
||||||
|
string name,
|
||||||
|
XUserClaimsInfoDto userInfo,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var existing = await FindPolicyOrThrowAsync(name, cancellationToken);
|
||||||
|
if (existing.IsSystem)
|
||||||
|
{
|
||||||
|
XException.NotAllowed.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
await policies.RemoveAsync(
|
||||||
|
id: existing.Id,
|
||||||
|
userInfo: userInfo,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
await InvalidateAsync(existing.Name);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<XPolicyDefinitionDto> GetPolicyAsync(
|
||||||
|
string name,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = await FindPolicyOrThrowAsync(name, cancellationToken);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<XPolicyDefinitionDto> AddPolicyAsync(
|
||||||
|
XPolicyDefinitionDto item,
|
||||||
|
XUserClaimsInfoDto userInfo,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (item.IsNull() || item.Name.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var normalized = item.Name.ToNormalString();
|
||||||
|
var exists = await policies.FindOneAsync(
|
||||||
|
predicate: p => p.Name.ToLower() == normalized,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
if (!exists.IsNull())
|
||||||
|
{
|
||||||
|
XException.Duplicate.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// ⚠️ CRITICAL: مقصد همه Role/Scopeها وجود داشته باشند
|
||||||
|
await ValidateReferencesAsync(item, cancellationToken);
|
||||||
|
|
||||||
|
//
|
||||||
|
item.IsSystem = false;
|
||||||
|
var result = await policies.AddAsync(
|
||||||
|
item: item,
|
||||||
|
userInfo: userInfo,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
await InvalidateAsync();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<XPolicyDefinitionDto> UpdatePolicyAsync(
|
||||||
|
string name,
|
||||||
|
XPolicyDefinitionDto item,
|
||||||
|
XUserClaimsInfoDto userInfo,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (name.IsNullOrEmpty() || item.IsNull())
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var existing = await FindPolicyOrThrowAsync(name, cancellationToken);
|
||||||
|
if (existing.IsSystem)
|
||||||
|
{
|
||||||
|
XException.NotAllowed.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Reference validation BEFORE update — same as Add
|
||||||
|
await ValidateReferencesAsync(item, cancellationToken);
|
||||||
|
|
||||||
|
//
|
||||||
|
var updated = await policies.UpdateAsync(
|
||||||
|
id: existing.Id,
|
||||||
|
item: item,
|
||||||
|
userInfo: userInfo,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
await InvalidateAsync(existing.Name);
|
||||||
|
return updated;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<XPolicyEvaluationResultDto> EvaluatePolicyAsync(
|
||||||
|
string name,
|
||||||
|
XUserClaimsInfoDto target,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Load the policy (throws NotFound when missing) ...
|
||||||
|
var dto = await FindPolicyOrThrowAsync(name, cancellationToken);
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = new XPolicyEvaluationResultDto
|
||||||
|
{
|
||||||
|
Passed = true,
|
||||||
|
Policy = dto.Name,
|
||||||
|
FailedRequirements = []
|
||||||
|
};
|
||||||
|
|
||||||
|
//
|
||||||
|
// Build the policy's effective authorization via the shared helper
|
||||||
|
// (resolver + projection onto XEffectiveAuthorizationDto) ...
|
||||||
|
var auth = await ToDtoWithEffectiveRolesAsync(dto, cancellationToken);
|
||||||
|
if (auth.IsNull())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
result.Passed = false;
|
||||||
|
result.FailedRequirements.Add("PolicyNotBuilt");
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Anonymous user fails any policy that requires authentication ...
|
||||||
|
if (target.IsNull())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (auth.RequireAuthenticatedUser)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
result.Passed = false;
|
||||||
|
result.FailedRequirements.Add("RequireAuthenticatedUser");
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Effective Roles: the user must hold at least one ...
|
||||||
|
if (auth.EffectiveRoles.HasChild())
|
||||||
|
{
|
||||||
|
// //
|
||||||
|
// var userSelectByParam = target.GetUserSelectByParam();
|
||||||
|
|
||||||
|
// //
|
||||||
|
// var userRoles = await roles.FindManyAsync(
|
||||||
|
// predicate: x => x.
|
||||||
|
// cancellationToken: cancellationToken
|
||||||
|
// );
|
||||||
|
|
||||||
|
// //
|
||||||
|
// var matched = auth.EffectiveRoles.Any(er =>
|
||||||
|
// userRoles.Any(ur =>
|
||||||
|
// string.Equals(ur, er, StringComparison.OrdinalIgnoreCase)
|
||||||
|
// )
|
||||||
|
// );
|
||||||
|
|
||||||
|
//
|
||||||
|
// if (!matched)
|
||||||
|
// {
|
||||||
|
// //
|
||||||
|
// result.Passed = false;
|
||||||
|
// result.FailedRequirements.Add("RequiredRoles");
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// NOTE: RequireEnabledUser (IsEnabled / !IsBanned), EffectiveScopes and
|
||||||
|
// EffectiveClaims checks are layered on here once the matching flags /
|
||||||
|
// collections on XUserClaimsInfoDto (xIdentityModels) are wired.
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IEnumerable<XPolicyDefinitionDto>> GetPoliciesAsync(
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = await policies
|
||||||
|
.GetAllAsync(cancellationToken: cancellationToken);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<XQueryResult<XPolicyDefinitionDto>> QueryPoliciesAsync(
|
||||||
|
XQuery query,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
if (query.IsNull())
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = await policies.QueryAsync(
|
||||||
|
query: query,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,11 +1,13 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using xModels.Dtos;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using xCommons.Extensions;
|
using xCommons.Extensions;
|
||||||
using xIdentityModels.Dtos;
|
using xIdentityModels.Dtos;
|
||||||
using xExceptions.Constants;
|
using xExceptions.Constants;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using xIdentityModels.Models;
|
using xIdentityModels.Models;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace xIds.Providers
|
namespace xIds.Providers
|
||||||
{
|
{
|
||||||
@@ -13,6 +15,74 @@ namespace xIds.Providers
|
|||||||
{
|
{
|
||||||
//
|
//
|
||||||
#region Role ...
|
#region Role ...
|
||||||
|
public async Task<bool> RemoveRoleAsync(
|
||||||
|
string name,
|
||||||
|
XUserClaimsInfoDto userInfo,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var existing = await FindRoleOrThrowAsync(name, cancellationToken);
|
||||||
|
if (existing.IsSystem)
|
||||||
|
{
|
||||||
|
XException.NotAllowed.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// ⚠️ CRITICAL: capture affected users BEFORE delete.
|
||||||
|
// After DeleteRoleAsync, AspNetUserRoles rows are gone.
|
||||||
|
var affectedUsers = await manager.UserManager.GetUsersInRoleAsync(existing.Name);
|
||||||
|
var affectedUserIds = affectedUsers.Select(u => u.Id).ToList();
|
||||||
|
|
||||||
|
//
|
||||||
|
// Delete from local DB
|
||||||
|
await roles.RemoveAsync(
|
||||||
|
id: existing.Id,
|
||||||
|
userInfo: userInfo,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Delete from IdentityServer — this cascades AspNetUserRoles
|
||||||
|
await manager.DeleteRoleAsync(existing.Name, cancellationToken);
|
||||||
|
|
||||||
|
//
|
||||||
|
await InvalidateAsync();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<int> CountUsersInRoleAsync(
|
||||||
|
string name,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Fail fast if the role itself does not exist ...
|
||||||
|
await FindRoleOrThrowAsync(name, cancellationToken);
|
||||||
|
|
||||||
|
//
|
||||||
|
var usersInRole = await manager
|
||||||
|
.UserManager.GetUsersInRoleAsync(name);
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = usersInRole.Count();
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<XRoleDefinitionDto> GetRoleAsync(
|
||||||
|
string name,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = await FindRoleOrThrowAsync(name, cancellationToken);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
public async Task<XRoleDefinitionDto> AddRoleAsync(
|
public async Task<XRoleDefinitionDto> AddRoleAsync(
|
||||||
XRoleDefinitionDto item,
|
XRoleDefinitionDto item,
|
||||||
XUserClaimsInfoDto userInfo,
|
XUserClaimsInfoDto userInfo,
|
||||||
@@ -126,7 +196,7 @@ namespace xIds.Providers
|
|||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
foreach(var user in usersInRole)
|
foreach (var user in usersInRole)
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
await manager.AddUserToRoleAsync(
|
await manager.AddUserToRoleAsync(
|
||||||
@@ -141,40 +211,38 @@ namespace xIds.Providers
|
|||||||
return updated;
|
return updated;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<bool> RemoveRoleAsync(
|
public async Task<IEnumerable<XRoleDefinitionDto>> GetRolesAsync(
|
||||||
string name,
|
|
||||||
XUserClaimsInfoDto userInfo,
|
|
||||||
CancellationToken cancellationToken = default
|
CancellationToken cancellationToken = default
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
var existing = await FindRoleOrThrowAsync(name, cancellationToken);
|
var result = await roles
|
||||||
if (existing.IsSystem)
|
.GetAllAsync(cancellationToken: cancellationToken);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<XQueryResult<XRoleDefinitionDto>> QueryRolesAsync(
|
||||||
|
XQuery query,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
if (query.IsNull())
|
||||||
{
|
{
|
||||||
XException.NotAllowed.Throw();
|
XException.InvalidArgs.Throw();
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// ⚠️ CRITICAL: capture affected users BEFORE delete.
|
var result = await roles.QueryAsync(
|
||||||
// After DeleteRoleAsync, AspNetUserRoles rows are gone.
|
query: query,
|
||||||
var affectedUsers = await manager.UserManager.GetUsersInRoleAsync(existing.Name);
|
|
||||||
var affectedUserIds = affectedUsers.Select(u => u.Id).ToList();
|
|
||||||
|
|
||||||
//
|
|
||||||
// Delete from local DB
|
|
||||||
await roles.RemoveAsync(
|
|
||||||
id: existing.Id,
|
|
||||||
userInfo: userInfo,
|
|
||||||
cancellationToken: cancellationToken
|
cancellationToken: cancellationToken
|
||||||
);
|
);
|
||||||
|
|
||||||
//
|
//
|
||||||
// Delete from IdentityServer — this cascades AspNetUserRoles
|
return result;
|
||||||
await manager.DeleteRoleAsync(existing.Name, cancellationToken);
|
|
||||||
|
|
||||||
//
|
|
||||||
await InvalidateAsync();
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using xModels.Dtos;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using xCommons.Extensions;
|
using xCommons.Extensions;
|
||||||
using xIdentityModels.Dtos;
|
using xIdentityModels.Dtos;
|
||||||
@@ -14,12 +15,36 @@ namespace xIds.Providers
|
|||||||
{
|
{
|
||||||
//
|
//
|
||||||
#region Scope ...
|
#region Scope ...
|
||||||
public async Task<IEnumerable<XScopeDefinitionDto>> GetScopesAsync(
|
public async Task<bool> RemoveScopeAsync(
|
||||||
|
string name,
|
||||||
|
XUserClaimsInfoDto userInfo,
|
||||||
CancellationToken cancellationToken = default
|
CancellationToken cancellationToken = default
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
return await scopes
|
//
|
||||||
.GetAllAsync(cancellationToken: cancellationToken);
|
var existing = await FindScopeOrThrowAsync(name, cancellationToken);
|
||||||
|
if (existing.IsSystem) {
|
||||||
|
XException.NotAllowed.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var affectedPolicies = (await policies.GetAllAsync(cancellationToken: cancellationToken))
|
||||||
|
.Where(p => !p.RequiredScopes.IsNullOrEmpty()
|
||||||
|
&& p.RequiredScopes.ParseListString<string>()
|
||||||
|
.Any(s => string.Equals(s, existing.Name,
|
||||||
|
StringComparison.OrdinalIgnoreCase)))
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
//
|
||||||
|
await scopes.RemoveAsync(
|
||||||
|
id: existing.Id,
|
||||||
|
userInfo: userInfo,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
await InvalidateAsync();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<XScopeDefinitionDto> GetScopeAsync(
|
public async Task<XScopeDefinitionDto> GetScopeAsync(
|
||||||
@@ -98,36 +123,33 @@ namespace xIds.Providers
|
|||||||
return updated;
|
return updated;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<bool> RemoveScopeAsync(
|
public async Task<IEnumerable<XScopeDefinitionDto>> GetScopesAsync(
|
||||||
string name,
|
CancellationToken cancellationToken = default
|
||||||
XUserClaimsInfoDto userInfo,
|
)
|
||||||
|
{
|
||||||
|
return await scopes
|
||||||
|
.GetAllAsync(cancellationToken: cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<XQueryResult<XScopeDefinitionDto>> QueryScopesAsync(
|
||||||
|
XQuery query,
|
||||||
CancellationToken cancellationToken = default
|
CancellationToken cancellationToken = default
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
var existing = await FindScopeOrThrowAsync(name, cancellationToken);
|
if (query.IsNull())
|
||||||
if (existing.IsSystem) {
|
{
|
||||||
XException.NotAllowed.Throw();
|
XException.InvalidArgs.Throw();
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
var affectedPolicies = (await policies.GetAllAsync(cancellationToken: cancellationToken))
|
var result = await scopes.QueryAsync(
|
||||||
.Where(p => !p.RequiredScopes.IsNullOrEmpty()
|
query: query,
|
||||||
&& p.RequiredScopes.ParseListString<string>()
|
|
||||||
.Any(s => string.Equals(s, existing.Name,
|
|
||||||
StringComparison.OrdinalIgnoreCase)))
|
|
||||||
.ToList();
|
|
||||||
|
|
||||||
//
|
|
||||||
await scopes.RemoveAsync(
|
|
||||||
id: existing.Id,
|
|
||||||
userInfo: userInfo,
|
|
||||||
cancellationToken: cancellationToken
|
cancellationToken: cancellationToken
|
||||||
);
|
);
|
||||||
|
|
||||||
//
|
//
|
||||||
await InvalidateAsync();
|
return result;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-17
@@ -133,23 +133,7 @@ namespace xIds
|
|||||||
//
|
//
|
||||||
app.UseEndpoints(endpoints =>
|
app.UseEndpoints(endpoints =>
|
||||||
{
|
{
|
||||||
//
|
endpoints.MapDefaultControllerRoute();
|
||||||
// endpoints.MapDefaultControllerRoute();
|
|
||||||
|
|
||||||
//
|
|
||||||
try
|
|
||||||
{
|
|
||||||
endpoints.MapDefaultControllerRoute();
|
|
||||||
}
|
|
||||||
catch (ReflectionTypeLoadException ex)
|
|
||||||
{
|
|
||||||
foreach (var loaderException in ex.LoaderExceptions)
|
|
||||||
{
|
|
||||||
// Log or inspect the specific error, e.g., "Could not load file or assembly..."
|
|
||||||
Console.WriteLine(loaderException?.Message);
|
|
||||||
}
|
|
||||||
throw; // Re-throw to preserve the original behavior
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user