comment uncomplete authorization services ...
This commit is contained in:
@@ -1,115 +1,115 @@
|
||||
using xModels.Dtos;
|
||||
using System.Threading;
|
||||
using xIdentityModels.Dtos;
|
||||
using System.Threading.Tasks;
|
||||
using xIdentityModels.Models;
|
||||
using System.Collections.Generic;
|
||||
using xIdentityService.Models;
|
||||
// using xModels.Dtos;
|
||||
// using System.Threading;
|
||||
// using xIdentityModels.Dtos;
|
||||
// using System.Threading.Tasks;
|
||||
// using xIdentityModels.Models;
|
||||
// using System.Collections.Generic;
|
||||
// using xIdentityService.Models;
|
||||
|
||||
namespace xIdentityService.Interfaces
|
||||
{
|
||||
public interface IXAuthorizationProvider
|
||||
{
|
||||
//
|
||||
#region Scope ...
|
||||
Task<XQueryResult<XScopeDefinitionDto>> QueryScopesAsync(
|
||||
XQuery query,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
Task<XScopeDefinitionDto> GetScopeAsync(
|
||||
string name,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
Task<XScopeDefinitionDto> AddScopeAsync(
|
||||
XScopeDefinitionDto item,
|
||||
XUserClaimsInfoDto userInfo,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
Task<XScopeDefinitionDto> UpdateScopeAsync(
|
||||
string name,
|
||||
XScopeDefinitionDto item,
|
||||
XUserClaimsInfoDto userInfo,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
Task<bool> RemoveScopeAsync(
|
||||
string name,
|
||||
XUserClaimsInfoDto userInfo,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
#endregion
|
||||
// namespace xIdentityService.Interfaces
|
||||
// {
|
||||
// public interface IXAuthorizationProvider
|
||||
// {
|
||||
// //
|
||||
// #region Scope ...
|
||||
// Task<XQueryResult<XScopeDefinitionDto>> QueryScopesAsync(
|
||||
// XQuery query,
|
||||
// CancellationToken cancellationToken = default
|
||||
// );
|
||||
// Task<XScopeDefinitionDto> GetScopeAsync(
|
||||
// string name,
|
||||
// CancellationToken cancellationToken = default
|
||||
// );
|
||||
// Task<XScopeDefinitionDto> AddScopeAsync(
|
||||
// XScopeDefinitionDto item,
|
||||
// XUserClaimsInfoDto userInfo,
|
||||
// CancellationToken cancellationToken = default
|
||||
// );
|
||||
// Task<XScopeDefinitionDto> UpdateScopeAsync(
|
||||
// string name,
|
||||
// XScopeDefinitionDto item,
|
||||
// XUserClaimsInfoDto userInfo,
|
||||
// CancellationToken cancellationToken = default
|
||||
// );
|
||||
// Task<bool> RemoveScopeAsync(
|
||||
// string name,
|
||||
// XUserClaimsInfoDto userInfo,
|
||||
// CancellationToken cancellationToken = default
|
||||
// );
|
||||
// #endregion
|
||||
|
||||
//
|
||||
#region Role ...
|
||||
Task<IEnumerable<XRoleDefinitionDto>> GetRolesAsync(
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
Task<XRoleDefinitionDto> GetRoleAsync(
|
||||
string name,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
Task<XRoleDefinitionDto> AddRoleAsync(
|
||||
XRoleDefinitionDto item,
|
||||
XUserClaimsInfoDto userInfo,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
Task<XRoleDefinitionDto> UpdateRoleAsync(
|
||||
string name,
|
||||
XRoleDefinitionDto item,
|
||||
XUserClaimsInfoDto userInfo,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
Task<bool> RemoveRoleAsync(
|
||||
string name,
|
||||
XUserClaimsInfoDto userInfo,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
Task<int> CountUsersInRoleAsync(
|
||||
string name,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
#endregion
|
||||
// //
|
||||
// #region Role ...
|
||||
// Task<IEnumerable<XRoleDefinitionDto>> GetRolesAsync(
|
||||
// CancellationToken cancellationToken = default
|
||||
// );
|
||||
// Task<XRoleDefinitionDto> GetRoleAsync(
|
||||
// string name,
|
||||
// CancellationToken cancellationToken = default
|
||||
// );
|
||||
// Task<XRoleDefinitionDto> AddRoleAsync(
|
||||
// XRoleDefinitionDto item,
|
||||
// XUserClaimsInfoDto userInfo,
|
||||
// CancellationToken cancellationToken = default
|
||||
// );
|
||||
// Task<XRoleDefinitionDto> UpdateRoleAsync(
|
||||
// string name,
|
||||
// XRoleDefinitionDto item,
|
||||
// XUserClaimsInfoDto userInfo,
|
||||
// CancellationToken cancellationToken = default
|
||||
// );
|
||||
// Task<bool> RemoveRoleAsync(
|
||||
// string name,
|
||||
// XUserClaimsInfoDto userInfo,
|
||||
// CancellationToken cancellationToken = default
|
||||
// );
|
||||
// Task<int> CountUsersInRoleAsync(
|
||||
// string name,
|
||||
// CancellationToken cancellationToken = default
|
||||
// );
|
||||
// #endregion
|
||||
|
||||
//
|
||||
#region Policy ...
|
||||
Task<XQueryResult<XPolicyDefinitionDto>> QueryPoliciesAsync(
|
||||
XQuery query,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
Task<XPolicyDefinitionDto> GetPolicyAsync(
|
||||
string name,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
Task<XPolicyDefinitionDto> AddPolicyAsync(
|
||||
XPolicyDefinitionDto item,
|
||||
XUserClaimsInfoDto userInfo,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
Task<XPolicyDefinitionDto> UpdatePolicyAsync(
|
||||
string name,
|
||||
XPolicyDefinitionDto item,
|
||||
XUserClaimsInfoDto userInfo,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
Task<bool> RemovePolicyAsync(
|
||||
string name,
|
||||
XUserClaimsInfoDto userInfo,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
Task<XPolicyEvaluationResultDto> EvaluatePolicyAsync(
|
||||
string name,
|
||||
XUserClaimsInfoDto target,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
Task<XEffectiveAuthorizationDto> GetEffectiveAuthorizationAsync(
|
||||
XUserClaimsInfoDto target,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
#endregion
|
||||
// //
|
||||
// #region Policy ...
|
||||
// Task<XQueryResult<XPolicyDefinitionDto>> QueryPoliciesAsync(
|
||||
// XQuery query,
|
||||
// CancellationToken cancellationToken = default
|
||||
// );
|
||||
// Task<XPolicyDefinitionDto> GetPolicyAsync(
|
||||
// string name,
|
||||
// CancellationToken cancellationToken = default
|
||||
// );
|
||||
// Task<XPolicyDefinitionDto> AddPolicyAsync(
|
||||
// XPolicyDefinitionDto item,
|
||||
// XUserClaimsInfoDto userInfo,
|
||||
// CancellationToken cancellationToken = default
|
||||
// );
|
||||
// Task<XPolicyDefinitionDto> UpdatePolicyAsync(
|
||||
// string name,
|
||||
// XPolicyDefinitionDto item,
|
||||
// XUserClaimsInfoDto userInfo,
|
||||
// CancellationToken cancellationToken = default
|
||||
// );
|
||||
// Task<bool> RemovePolicyAsync(
|
||||
// string name,
|
||||
// XUserClaimsInfoDto userInfo,
|
||||
// CancellationToken cancellationToken = default
|
||||
// );
|
||||
// Task<XPolicyEvaluationResultDto> EvaluatePolicyAsync(
|
||||
// string name,
|
||||
// XUserClaimsInfoDto target,
|
||||
// CancellationToken cancellationToken = default
|
||||
// );
|
||||
// Task<XEffectiveAuthorizationDto> GetEffectiveAuthorizationAsync(
|
||||
// XUserClaimsInfoDto target,
|
||||
// CancellationToken cancellationToken = default
|
||||
// );
|
||||
// #endregion
|
||||
|
||||
//
|
||||
#region Utilities ...
|
||||
Task SyncAllAsync(CancellationToken cancellationToken = default);
|
||||
Task InvalidateAsync(string policyName = null);
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
// //
|
||||
// #region Utilities ...
|
||||
// Task SyncAllAsync(CancellationToken cancellationToken = default);
|
||||
// Task InvalidateAsync(string policyName = null);
|
||||
// #endregion
|
||||
// }
|
||||
// }
|
||||
@@ -1,86 +1,86 @@
|
||||
using System.Threading;
|
||||
using xCommons.Extensions;
|
||||
using xIdentityModels.Dtos;
|
||||
using xExceptions.Constants;
|
||||
using System.Threading.Tasks;
|
||||
using xIdentityModels.Models;
|
||||
// using System.Threading;
|
||||
// using xCommons.Extensions;
|
||||
// using xIdentityModels.Dtos;
|
||||
// using xExceptions.Constants;
|
||||
// using System.Threading.Tasks;
|
||||
// using xIdentityModels.Models;
|
||||
|
||||
namespace xIdentityService.Providers
|
||||
{
|
||||
public partial class XAuthorizationProvider
|
||||
{
|
||||
//
|
||||
#region Policy ...
|
||||
public async Task<XPolicyDefinitionDto> AddPolicyAsync(
|
||||
XPolicyDefinitionDto item,
|
||||
XUserClaimsInfoDto userInfo,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (item.IsNull() || item.Name.IsNullOrEmpty())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
// namespace xIdentityService.Providers
|
||||
// {
|
||||
// public partial class XAuthorizationProvider
|
||||
// {
|
||||
// //
|
||||
// #region Policy ...
|
||||
// public async Task<XPolicyDefinitionDto> AddPolicyAsync(
|
||||
// XPolicyDefinitionDto item,
|
||||
// XUserClaimsInfoDto userInfo,
|
||||
// CancellationToken cancellationToken = default
|
||||
// )
|
||||
// {
|
||||
// //
|
||||
// // Validate ...
|
||||
// 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();
|
||||
}
|
||||
// //
|
||||
// var normalized = item.Name.ToNormalString();
|
||||
// var exists = await policies.FindOneAsync(
|
||||
// predicate: p => p.Name.ToLower() == normalized,
|
||||
// cancellationToken: cancellationToken
|
||||
// );
|
||||
// if (!exists.IsNull())
|
||||
// {
|
||||
// XException.Duplicate.Throw();
|
||||
// }
|
||||
|
||||
//
|
||||
await ValidateReferencesAsync(item, cancellationToken);
|
||||
// //
|
||||
// await ValidateReferencesAsync(item, cancellationToken);
|
||||
|
||||
//
|
||||
item.IsSystem = false;
|
||||
// //
|
||||
// item.IsSystem = false;
|
||||
|
||||
//
|
||||
var result = await policies.AddAsync(
|
||||
item: item,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
// //
|
||||
// var result = await policies.AddAsync(
|
||||
// item: item,
|
||||
// cancellationToken: cancellationToken
|
||||
// );
|
||||
|
||||
//
|
||||
await InvalidateAsync(result.Name);
|
||||
// //
|
||||
// await InvalidateAsync(result.Name);
|
||||
|
||||
//
|
||||
return await ToDtoWithEffectiveRolesAsync(result, cancellationToken);
|
||||
}
|
||||
// //
|
||||
// return await ToDtoWithEffectiveRolesAsync(result, cancellationToken);
|
||||
// }
|
||||
|
||||
public async Task<bool> RemovePolicyAsync(
|
||||
string name,
|
||||
XUserClaimsInfoDto userInfo,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var entity = await FindPolicyOrThrowAsync(name, cancellationToken);
|
||||
if (entity.IsSystem)
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
// public async Task<bool> RemovePolicyAsync(
|
||||
// string name,
|
||||
// XUserClaimsInfoDto userInfo,
|
||||
// CancellationToken cancellationToken = default
|
||||
// )
|
||||
// {
|
||||
// //
|
||||
// var entity = await FindPolicyOrThrowAsync(name, cancellationToken);
|
||||
// if (entity.IsSystem)
|
||||
// {
|
||||
// XException.NotAllowed.Throw();
|
||||
// }
|
||||
|
||||
//
|
||||
await policies.RemoveAsync(
|
||||
id: entity.Id,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
// //
|
||||
// await policies.RemoveAsync(
|
||||
// id: entity.Id,
|
||||
// cancellationToken: cancellationToken
|
||||
// );
|
||||
|
||||
//
|
||||
await InvalidateAsync(entity.Name);
|
||||
return true;
|
||||
}
|
||||
#endregion
|
||||
// //
|
||||
// await InvalidateAsync(entity.Name);
|
||||
// return true;
|
||||
// }
|
||||
// #endregion
|
||||
|
||||
//
|
||||
#region Private ...
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
// //
|
||||
// #region Private ...
|
||||
// #endregion
|
||||
// }
|
||||
// }
|
||||
+144
-144
@@ -1,156 +1,156 @@
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using xCommons.Extensions;
|
||||
using xIdentityModels.Dtos;
|
||||
using xExceptions.Constants;
|
||||
using System.Threading.Tasks;
|
||||
using xIdentityService.Interfaces;
|
||||
// using System.Linq;
|
||||
// using System.Threading;
|
||||
// using xCommons.Extensions;
|
||||
// using xIdentityModels.Dtos;
|
||||
// using xExceptions.Constants;
|
||||
// using System.Threading.Tasks;
|
||||
// using xIdentityService.Interfaces;
|
||||
|
||||
//
|
||||
// TODO: Remove this to xIds ...
|
||||
namespace xIdentityService.Providers
|
||||
{
|
||||
public partial class XAuthorizationProvider : IXAuthorizationProvider
|
||||
{
|
||||
//
|
||||
private readonly IXRoleProvider roles;
|
||||
private readonly IXScopeProvider scopes;
|
||||
private readonly IXPolicyProvider policies;
|
||||
private readonly IXRoleSyncService roleSync;
|
||||
private readonly IXAuthorizationCache cache;
|
||||
private readonly IXScopeSyncService scopeSync;
|
||||
private readonly IXRoleHierarchyResolver resolver;
|
||||
// private readonly IXEventServiceProvider eventService;
|
||||
// //
|
||||
// // TODO: Remove this to xIds ...
|
||||
// namespace xIdentityService.Providers
|
||||
// {
|
||||
// public partial class XAuthorizationProvider : IXAuthorizationProvider
|
||||
// {
|
||||
// //
|
||||
// private readonly IXRoleProvider roles;
|
||||
// private readonly IXScopeProvider scopes;
|
||||
// private readonly IXPolicyProvider policies;
|
||||
// private readonly IXRoleSyncService roleSync;
|
||||
// private readonly IXAuthorizationCache cache;
|
||||
// private readonly IXScopeSyncService scopeSync;
|
||||
// private readonly IXRoleHierarchyResolver resolver;
|
||||
// // private readonly IXEventServiceProvider eventService;
|
||||
|
||||
public XAuthorizationProvider(
|
||||
IXRoleProvider roles,
|
||||
IXScopeProvider scopes,
|
||||
IXPolicyProvider policies,
|
||||
IXRoleSyncService roleSync,
|
||||
IXAuthorizationCache cache,
|
||||
IXScopeSyncService scopeSync,
|
||||
IXRoleHierarchyResolver resolver
|
||||
)
|
||||
{
|
||||
//
|
||||
this.roles = roles;
|
||||
this.cache = cache;
|
||||
this.scopes = scopes;
|
||||
this.policies = policies;
|
||||
this.roleSync = roleSync;
|
||||
this.resolver = resolver;
|
||||
this.scopeSync = scopeSync;
|
||||
}
|
||||
// public XAuthorizationProvider(
|
||||
// IXRoleProvider roles,
|
||||
// IXScopeProvider scopes,
|
||||
// IXPolicyProvider policies,
|
||||
// IXRoleSyncService roleSync,
|
||||
// IXAuthorizationCache cache,
|
||||
// IXScopeSyncService scopeSync,
|
||||
// IXRoleHierarchyResolver resolver
|
||||
// )
|
||||
// {
|
||||
// //
|
||||
// this.roles = roles;
|
||||
// this.cache = cache;
|
||||
// this.scopes = scopes;
|
||||
// this.policies = policies;
|
||||
// this.roleSync = roleSync;
|
||||
// this.resolver = resolver;
|
||||
// this.scopeSync = scopeSync;
|
||||
// }
|
||||
|
||||
//
|
||||
#region Commons ...
|
||||
/// <summary>
|
||||
/// Clear Local Cache and Notify other Instances ...
|
||||
/// </summary>
|
||||
public async Task InvalidateAsync(string policyName = null)
|
||||
{
|
||||
//
|
||||
if (policyName.IsNullOrEmpty())
|
||||
{
|
||||
cache.Clear();
|
||||
}
|
||||
else
|
||||
{
|
||||
cache.RemovePolicy(policyName);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
// //
|
||||
// #region Commons ...
|
||||
// /// <summary>
|
||||
// /// Clear Local Cache and Notify other Instances ...
|
||||
// /// </summary>
|
||||
// public async Task InvalidateAsync(string policyName = null)
|
||||
// {
|
||||
// //
|
||||
// if (policyName.IsNullOrEmpty())
|
||||
// {
|
||||
// cache.Clear();
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// cache.RemovePolicy(policyName);
|
||||
// }
|
||||
// }
|
||||
// #endregion
|
||||
|
||||
//
|
||||
#region Private ...
|
||||
private async Task<XPolicyDefinitionDto> FindPolicyOrThrowAsync(
|
||||
string name,
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
//
|
||||
if (name.IsNullOrEmpty())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
// //
|
||||
// #region Private ...
|
||||
// private async Task<XPolicyDefinitionDto> FindPolicyOrThrowAsync(
|
||||
// string name,
|
||||
// CancellationToken cancellationToken
|
||||
// )
|
||||
// {
|
||||
// //
|
||||
// if (name.IsNullOrEmpty())
|
||||
// {
|
||||
// XException.InvalidArgs.Throw();
|
||||
// }
|
||||
|
||||
//
|
||||
var normalized = name.ToNormalString();
|
||||
var result = await policies.FindOneAsync(
|
||||
predicate: p => p.Name.ToLower() == normalized,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
if (result.IsNull())
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
// //
|
||||
// var normalized = name.ToNormalString();
|
||||
// var result = await policies.FindOneAsync(
|
||||
// predicate: p => p.Name.ToLower() == normalized,
|
||||
// cancellationToken: cancellationToken
|
||||
// );
|
||||
// if (result.IsNull())
|
||||
// {
|
||||
// XException.NotFound.Throw();
|
||||
// }
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
// //
|
||||
// return result;
|
||||
// }
|
||||
|
||||
// private async Task ValidateReferencesAsync(
|
||||
// XPolicyDefinitionDto item,
|
||||
// CancellationToken cancellationToken
|
||||
// )
|
||||
// {
|
||||
// //
|
||||
// // Referenced Roles must exist ...
|
||||
// if (item.RequiredRoles.HasChild())
|
||||
// {
|
||||
// var all = (await roles.GetAllAsync(cancellationToken: cancellationToken))
|
||||
// .Select(r => r.Name.ToNormalString()).ToList();
|
||||
// if (item.RequiredRoles.Any(r => !all.Contains(r.ToNormalString())))
|
||||
// {
|
||||
// XException.NotFound.Throw();
|
||||
// }
|
||||
// }
|
||||
// // private async Task ValidateReferencesAsync(
|
||||
// // XPolicyDefinitionDto item,
|
||||
// // CancellationToken cancellationToken
|
||||
// // )
|
||||
// // {
|
||||
// // //
|
||||
// // // Referenced Roles must exist ...
|
||||
// // if (item.RequiredRoles.HasChild())
|
||||
// // {
|
||||
// // var all = (await roles.GetAllAsync(cancellationToken: cancellationToken))
|
||||
// // .Select(r => r.Name.ToNormalString()).ToList();
|
||||
// // if (item.RequiredRoles.Any(r => !all.Contains(r.ToNormalString())))
|
||||
// // {
|
||||
// // XException.NotFound.Throw();
|
||||
// // }
|
||||
// // }
|
||||
|
||||
// //
|
||||
// // Referenced Scopes must exist ...
|
||||
// if (item.RequiredScopes.HasChild())
|
||||
// {
|
||||
// var all = (await scopes.GetAllAsync(cancellationToken: cancellationToken))
|
||||
// .Select(s => s.Name.ToNormalString()).ToList();
|
||||
// if (item.RequiredScopes.Any(s => !all.Contains(s.ToNormalString())))
|
||||
// {
|
||||
// XException.NotFound.Throw();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// // //
|
||||
// // // Referenced Scopes must exist ...
|
||||
// // if (item.RequiredScopes.HasChild())
|
||||
// // {
|
||||
// // var all = (await scopes.GetAllAsync(cancellationToken: cancellationToken))
|
||||
// // .Select(s => s.Name.ToNormalString()).ToList();
|
||||
// // if (item.RequiredScopes.Any(s => !all.Contains(s.ToNormalString())))
|
||||
// // {
|
||||
// // XException.NotFound.Throw();
|
||||
// // }
|
||||
// // }
|
||||
// // }
|
||||
|
||||
// private async Task<XPolicyDefinitionDto> ToDtoWithEffectiveRolesAsync(
|
||||
// XPolicyDefinition entity,
|
||||
// CancellationToken cancellationToken
|
||||
// )
|
||||
// {
|
||||
// //
|
||||
// var dto = entity.ToDto();
|
||||
// dto.EffectiveRoles = (await resolver.ResolveEffectiveRolesAsync(
|
||||
// explicitRoles: entity.RequiredRoles.ToCsvList(),
|
||||
// minRoleWeight: entity.MinRoleWeight,
|
||||
// cancellationToken: cancellationToken
|
||||
// )).ToList();
|
||||
// // private async Task<XPolicyDefinitionDto> ToDtoWithEffectiveRolesAsync(
|
||||
// // XPolicyDefinition entity,
|
||||
// // CancellationToken cancellationToken
|
||||
// // )
|
||||
// // {
|
||||
// // //
|
||||
// // var dto = entity.ToDto();
|
||||
// // dto.EffectiveRoles = (await resolver.ResolveEffectiveRolesAsync(
|
||||
// // explicitRoles: entity.RequiredRoles.ToCsvList(),
|
||||
// // minRoleWeight: entity.MinRoleWeight,
|
||||
// // cancellationToken: cancellationToken
|
||||
// // )).ToList();
|
||||
|
||||
// //
|
||||
// return dto;
|
||||
// }
|
||||
// // //
|
||||
// // return dto;
|
||||
// // }
|
||||
|
||||
// private async Task<XPolicyDefinitionDto> ToDtoWithEffectiveRolesAsync(
|
||||
// XPolicyDefinitionDto item,
|
||||
// CancellationToken cancellationToken = default
|
||||
// )
|
||||
// {
|
||||
// //
|
||||
// item.EffectiveRoles = (await resolver.ResolveEffectiveRolesAsync(
|
||||
// minRoleWeight: item.MinRoleWeight,
|
||||
// cancellationToken: cancellationToken,
|
||||
// explicitRoles: item.RequiredRoles.ParseListString<string>()
|
||||
// )).ToList();
|
||||
// // private async Task<XPolicyDefinitionDto> ToDtoWithEffectiveRolesAsync(
|
||||
// // XPolicyDefinitionDto item,
|
||||
// // CancellationToken cancellationToken = default
|
||||
// // )
|
||||
// // {
|
||||
// // //
|
||||
// // item.EffectiveRoles = (await resolver.ResolveEffectiveRolesAsync(
|
||||
// // minRoleWeight: item.MinRoleWeight,
|
||||
// // cancellationToken: cancellationToken,
|
||||
// // explicitRoles: item.RequiredRoles.ParseListString<string>()
|
||||
// // )).ToList();
|
||||
|
||||
// //
|
||||
// return item;
|
||||
// }
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
// // //
|
||||
// // return item;
|
||||
// // }
|
||||
// #endregion
|
||||
// }
|
||||
// }
|
||||
@@ -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