comment uncomplete authorization services ...
This commit is contained in:
@@ -1,115 +1,115 @@
|
|||||||
using xModels.Dtos;
|
// using xModels.Dtos;
|
||||||
using System.Threading;
|
// using System.Threading;
|
||||||
using xIdentityModels.Dtos;
|
// using xIdentityModels.Dtos;
|
||||||
using System.Threading.Tasks;
|
// using System.Threading.Tasks;
|
||||||
using xIdentityModels.Models;
|
// using xIdentityModels.Models;
|
||||||
using System.Collections.Generic;
|
// using System.Collections.Generic;
|
||||||
using xIdentityService.Models;
|
// using xIdentityService.Models;
|
||||||
|
|
||||||
namespace xIdentityService.Interfaces
|
// namespace xIdentityService.Interfaces
|
||||||
{
|
// {
|
||||||
public interface IXAuthorizationProvider
|
// public interface IXAuthorizationProvider
|
||||||
{
|
// {
|
||||||
//
|
// //
|
||||||
#region Scope ...
|
// #region Scope ...
|
||||||
Task<XQueryResult<XScopeDefinitionDto>> QueryScopesAsync(
|
// Task<XQueryResult<XScopeDefinitionDto>> QueryScopesAsync(
|
||||||
XQuery query,
|
// XQuery query,
|
||||||
CancellationToken cancellationToken = default
|
// CancellationToken cancellationToken = default
|
||||||
);
|
// );
|
||||||
Task<XScopeDefinitionDto> GetScopeAsync(
|
// Task<XScopeDefinitionDto> GetScopeAsync(
|
||||||
string name,
|
// string name,
|
||||||
CancellationToken cancellationToken = default
|
// CancellationToken cancellationToken = default
|
||||||
);
|
// );
|
||||||
Task<XScopeDefinitionDto> AddScopeAsync(
|
// Task<XScopeDefinitionDto> AddScopeAsync(
|
||||||
XScopeDefinitionDto item,
|
// XScopeDefinitionDto item,
|
||||||
XUserClaimsInfoDto userInfo,
|
// XUserClaimsInfoDto userInfo,
|
||||||
CancellationToken cancellationToken = default
|
// CancellationToken cancellationToken = default
|
||||||
);
|
// );
|
||||||
Task<XScopeDefinitionDto> UpdateScopeAsync(
|
// Task<XScopeDefinitionDto> UpdateScopeAsync(
|
||||||
string name,
|
// string name,
|
||||||
XScopeDefinitionDto item,
|
// XScopeDefinitionDto item,
|
||||||
XUserClaimsInfoDto userInfo,
|
// XUserClaimsInfoDto userInfo,
|
||||||
CancellationToken cancellationToken = default
|
// CancellationToken cancellationToken = default
|
||||||
);
|
// );
|
||||||
Task<bool> RemoveScopeAsync(
|
// Task<bool> RemoveScopeAsync(
|
||||||
string name,
|
// string name,
|
||||||
XUserClaimsInfoDto userInfo,
|
// XUserClaimsInfoDto userInfo,
|
||||||
CancellationToken cancellationToken = default
|
// CancellationToken cancellationToken = default
|
||||||
);
|
// );
|
||||||
#endregion
|
// #endregion
|
||||||
|
|
||||||
//
|
// //
|
||||||
#region Role ...
|
// #region Role ...
|
||||||
Task<IEnumerable<XRoleDefinitionDto>> GetRolesAsync(
|
// Task<IEnumerable<XRoleDefinitionDto>> GetRolesAsync(
|
||||||
CancellationToken cancellationToken = default
|
// CancellationToken cancellationToken = default
|
||||||
);
|
// );
|
||||||
Task<XRoleDefinitionDto> GetRoleAsync(
|
// Task<XRoleDefinitionDto> GetRoleAsync(
|
||||||
string name,
|
// string name,
|
||||||
CancellationToken cancellationToken = default
|
// CancellationToken cancellationToken = default
|
||||||
);
|
// );
|
||||||
Task<XRoleDefinitionDto> AddRoleAsync(
|
// Task<XRoleDefinitionDto> AddRoleAsync(
|
||||||
XRoleDefinitionDto item,
|
// XRoleDefinitionDto item,
|
||||||
XUserClaimsInfoDto userInfo,
|
// XUserClaimsInfoDto userInfo,
|
||||||
CancellationToken cancellationToken = default
|
// CancellationToken cancellationToken = default
|
||||||
);
|
// );
|
||||||
Task<XRoleDefinitionDto> UpdateRoleAsync(
|
// Task<XRoleDefinitionDto> UpdateRoleAsync(
|
||||||
string name,
|
// string name,
|
||||||
XRoleDefinitionDto item,
|
// XRoleDefinitionDto item,
|
||||||
XUserClaimsInfoDto userInfo,
|
// XUserClaimsInfoDto userInfo,
|
||||||
CancellationToken cancellationToken = default
|
// CancellationToken cancellationToken = default
|
||||||
);
|
// );
|
||||||
Task<bool> RemoveRoleAsync(
|
// Task<bool> RemoveRoleAsync(
|
||||||
string name,
|
// string name,
|
||||||
XUserClaimsInfoDto userInfo,
|
// XUserClaimsInfoDto userInfo,
|
||||||
CancellationToken cancellationToken = default
|
// CancellationToken cancellationToken = default
|
||||||
);
|
// );
|
||||||
Task<int> CountUsersInRoleAsync(
|
// Task<int> CountUsersInRoleAsync(
|
||||||
string name,
|
// string name,
|
||||||
CancellationToken cancellationToken = default
|
// CancellationToken cancellationToken = default
|
||||||
);
|
// );
|
||||||
#endregion
|
// #endregion
|
||||||
|
|
||||||
//
|
// //
|
||||||
#region Policy ...
|
// #region Policy ...
|
||||||
Task<XQueryResult<XPolicyDefinitionDto>> QueryPoliciesAsync(
|
// Task<XQueryResult<XPolicyDefinitionDto>> QueryPoliciesAsync(
|
||||||
XQuery query,
|
// XQuery query,
|
||||||
CancellationToken cancellationToken = default
|
// CancellationToken cancellationToken = default
|
||||||
);
|
// );
|
||||||
Task<XPolicyDefinitionDto> GetPolicyAsync(
|
// Task<XPolicyDefinitionDto> GetPolicyAsync(
|
||||||
string name,
|
// string name,
|
||||||
CancellationToken cancellationToken = default
|
// CancellationToken cancellationToken = default
|
||||||
);
|
// );
|
||||||
Task<XPolicyDefinitionDto> AddPolicyAsync(
|
// Task<XPolicyDefinitionDto> AddPolicyAsync(
|
||||||
XPolicyDefinitionDto item,
|
// XPolicyDefinitionDto item,
|
||||||
XUserClaimsInfoDto userInfo,
|
// XUserClaimsInfoDto userInfo,
|
||||||
CancellationToken cancellationToken = default
|
// CancellationToken cancellationToken = default
|
||||||
);
|
// );
|
||||||
Task<XPolicyDefinitionDto> UpdatePolicyAsync(
|
// Task<XPolicyDefinitionDto> UpdatePolicyAsync(
|
||||||
string name,
|
// string name,
|
||||||
XPolicyDefinitionDto item,
|
// XPolicyDefinitionDto item,
|
||||||
XUserClaimsInfoDto userInfo,
|
// XUserClaimsInfoDto userInfo,
|
||||||
CancellationToken cancellationToken = default
|
// CancellationToken cancellationToken = default
|
||||||
);
|
// );
|
||||||
Task<bool> RemovePolicyAsync(
|
// Task<bool> RemovePolicyAsync(
|
||||||
string name,
|
// string name,
|
||||||
XUserClaimsInfoDto userInfo,
|
// XUserClaimsInfoDto userInfo,
|
||||||
CancellationToken cancellationToken = default
|
// CancellationToken cancellationToken = default
|
||||||
);
|
// );
|
||||||
Task<XPolicyEvaluationResultDto> EvaluatePolicyAsync(
|
// Task<XPolicyEvaluationResultDto> EvaluatePolicyAsync(
|
||||||
string name,
|
// string name,
|
||||||
XUserClaimsInfoDto target,
|
// XUserClaimsInfoDto target,
|
||||||
CancellationToken cancellationToken = default
|
// CancellationToken cancellationToken = default
|
||||||
);
|
// );
|
||||||
Task<XEffectiveAuthorizationDto> GetEffectiveAuthorizationAsync(
|
// Task<XEffectiveAuthorizationDto> GetEffectiveAuthorizationAsync(
|
||||||
XUserClaimsInfoDto target,
|
// XUserClaimsInfoDto target,
|
||||||
CancellationToken cancellationToken = default
|
// CancellationToken cancellationToken = default
|
||||||
);
|
// );
|
||||||
#endregion
|
// #endregion
|
||||||
|
|
||||||
//
|
// //
|
||||||
#region Utilities ...
|
// #region Utilities ...
|
||||||
Task SyncAllAsync(CancellationToken cancellationToken = default);
|
// Task SyncAllAsync(CancellationToken cancellationToken = default);
|
||||||
Task InvalidateAsync(string policyName = null);
|
// Task InvalidateAsync(string policyName = null);
|
||||||
#endregion
|
// #endregion
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
@@ -1,86 +1,86 @@
|
|||||||
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;
|
||||||
|
|
||||||
namespace xIdentityService.Providers
|
// namespace xIdentityService.Providers
|
||||||
{
|
// {
|
||||||
public partial class XAuthorizationProvider
|
// public partial class XAuthorizationProvider
|
||||||
{
|
// {
|
||||||
//
|
// //
|
||||||
#region Policy ...
|
// #region Policy ...
|
||||||
public async Task<XPolicyDefinitionDto> AddPolicyAsync(
|
// public async Task<XPolicyDefinitionDto> AddPolicyAsync(
|
||||||
XPolicyDefinitionDto item,
|
// XPolicyDefinitionDto item,
|
||||||
XUserClaimsInfoDto userInfo,
|
// XUserClaimsInfoDto userInfo,
|
||||||
CancellationToken cancellationToken = default
|
// CancellationToken cancellationToken = default
|
||||||
)
|
// )
|
||||||
{
|
// {
|
||||||
//
|
// //
|
||||||
// Validate ...
|
// // Validate ...
|
||||||
if (item.IsNull() || item.Name.IsNullOrEmpty())
|
// if (item.IsNull() || item.Name.IsNullOrEmpty())
|
||||||
{
|
// {
|
||||||
XException.InvalidArgs.Throw();
|
// XException.InvalidArgs.Throw();
|
||||||
}
|
// }
|
||||||
|
|
||||||
//
|
// //
|
||||||
var normalized = item.Name.ToNormalString();
|
// var normalized = item.Name.ToNormalString();
|
||||||
var exists = await policies.FindOneAsync(
|
// var exists = await policies.FindOneAsync(
|
||||||
predicate: p => p.Name.ToLower() == normalized,
|
// predicate: p => p.Name.ToLower() == normalized,
|
||||||
cancellationToken: cancellationToken
|
// cancellationToken: cancellationToken
|
||||||
);
|
// );
|
||||||
if (!exists.IsNull())
|
// if (!exists.IsNull())
|
||||||
{
|
// {
|
||||||
XException.Duplicate.Throw();
|
// XException.Duplicate.Throw();
|
||||||
}
|
// }
|
||||||
|
|
||||||
//
|
// //
|
||||||
await ValidateReferencesAsync(item, cancellationToken);
|
// await ValidateReferencesAsync(item, cancellationToken);
|
||||||
|
|
||||||
//
|
// //
|
||||||
item.IsSystem = false;
|
// item.IsSystem = false;
|
||||||
|
|
||||||
//
|
// //
|
||||||
var result = await policies.AddAsync(
|
// var result = await policies.AddAsync(
|
||||||
item: item,
|
// item: item,
|
||||||
cancellationToken: cancellationToken
|
// 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(
|
// public async Task<bool> RemovePolicyAsync(
|
||||||
string name,
|
// string name,
|
||||||
XUserClaimsInfoDto userInfo,
|
// XUserClaimsInfoDto userInfo,
|
||||||
CancellationToken cancellationToken = default
|
// CancellationToken cancellationToken = default
|
||||||
)
|
// )
|
||||||
{
|
// {
|
||||||
//
|
// //
|
||||||
var entity = await FindPolicyOrThrowAsync(name, cancellationToken);
|
// var entity = await FindPolicyOrThrowAsync(name, cancellationToken);
|
||||||
if (entity.IsSystem)
|
// if (entity.IsSystem)
|
||||||
{
|
// {
|
||||||
XException.NotAllowed.Throw();
|
// XException.NotAllowed.Throw();
|
||||||
}
|
// }
|
||||||
|
|
||||||
//
|
// //
|
||||||
await policies.RemoveAsync(
|
// await policies.RemoveAsync(
|
||||||
id: entity.Id,
|
// id: entity.Id,
|
||||||
cancellationToken: cancellationToken
|
// cancellationToken: cancellationToken
|
||||||
);
|
// );
|
||||||
|
|
||||||
//
|
// //
|
||||||
await InvalidateAsync(entity.Name);
|
// await InvalidateAsync(entity.Name);
|
||||||
return true;
|
// return true;
|
||||||
}
|
// }
|
||||||
#endregion
|
// #endregion
|
||||||
|
|
||||||
//
|
// //
|
||||||
#region Private ...
|
// #region Private ...
|
||||||
#endregion
|
// #endregion
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
+139
-139
@@ -1,156 +1,156 @@
|
|||||||
using System.Linq;
|
// using System.Linq;
|
||||||
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 xIdentityService.Interfaces;
|
// using xIdentityService.Interfaces;
|
||||||
|
|
||||||
//
|
// //
|
||||||
// TODO: Remove this to xIds ...
|
// // TODO: Remove this to xIds ...
|
||||||
namespace xIdentityService.Providers
|
// namespace xIdentityService.Providers
|
||||||
{
|
// {
|
||||||
public partial class XAuthorizationProvider : IXAuthorizationProvider
|
// public partial class XAuthorizationProvider : IXAuthorizationProvider
|
||||||
{
|
// {
|
||||||
//
|
// //
|
||||||
private readonly IXRoleProvider roles;
|
// private readonly IXRoleProvider roles;
|
||||||
private readonly IXScopeProvider scopes;
|
// private readonly IXScopeProvider scopes;
|
||||||
private readonly IXPolicyProvider policies;
|
// private readonly IXPolicyProvider policies;
|
||||||
private readonly IXRoleSyncService roleSync;
|
// private readonly IXRoleSyncService roleSync;
|
||||||
private readonly IXAuthorizationCache cache;
|
// private readonly IXAuthorizationCache cache;
|
||||||
private readonly IXScopeSyncService scopeSync;
|
// private readonly IXScopeSyncService scopeSync;
|
||||||
private readonly IXRoleHierarchyResolver resolver;
|
// private readonly IXRoleHierarchyResolver resolver;
|
||||||
// private readonly IXEventServiceProvider eventService;
|
// // private readonly IXEventServiceProvider eventService;
|
||||||
|
|
||||||
public XAuthorizationProvider(
|
// public XAuthorizationProvider(
|
||||||
IXRoleProvider roles,
|
// IXRoleProvider roles,
|
||||||
IXScopeProvider scopes,
|
// IXScopeProvider scopes,
|
||||||
IXPolicyProvider policies,
|
// IXPolicyProvider policies,
|
||||||
IXRoleSyncService roleSync,
|
// IXRoleSyncService roleSync,
|
||||||
IXAuthorizationCache cache,
|
// IXAuthorizationCache cache,
|
||||||
IXScopeSyncService scopeSync,
|
// IXScopeSyncService scopeSync,
|
||||||
IXRoleHierarchyResolver resolver
|
// IXRoleHierarchyResolver resolver
|
||||||
)
|
// )
|
||||||
{
|
// {
|
||||||
//
|
// //
|
||||||
this.roles = roles;
|
// this.roles = roles;
|
||||||
this.cache = cache;
|
// this.cache = cache;
|
||||||
this.scopes = scopes;
|
// this.scopes = scopes;
|
||||||
this.policies = policies;
|
// this.policies = policies;
|
||||||
this.roleSync = roleSync;
|
// this.roleSync = roleSync;
|
||||||
this.resolver = resolver;
|
// this.resolver = resolver;
|
||||||
this.scopeSync = scopeSync;
|
// this.scopeSync = scopeSync;
|
||||||
}
|
// }
|
||||||
|
|
||||||
//
|
// //
|
||||||
#region Commons ...
|
// #region Commons ...
|
||||||
/// <summary>
|
// /// <summary>
|
||||||
/// Clear Local Cache and Notify other Instances ...
|
// /// Clear Local Cache and Notify other Instances ...
|
||||||
/// </summary>
|
// /// </summary>
|
||||||
public async Task InvalidateAsync(string policyName = null)
|
// public async Task InvalidateAsync(string policyName = null)
|
||||||
{
|
// {
|
||||||
//
|
// //
|
||||||
if (policyName.IsNullOrEmpty())
|
// if (policyName.IsNullOrEmpty())
|
||||||
{
|
// {
|
||||||
cache.Clear();
|
// cache.Clear();
|
||||||
}
|
// }
|
||||||
else
|
// else
|
||||||
{
|
// {
|
||||||
cache.RemovePolicy(policyName);
|
// cache.RemovePolicy(policyName);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
#endregion
|
// #endregion
|
||||||
|
|
||||||
//
|
// //
|
||||||
#region Private ...
|
// #region Private ...
|
||||||
private async Task<XPolicyDefinitionDto> FindPolicyOrThrowAsync(
|
// private async Task<XPolicyDefinitionDto> FindPolicyOrThrowAsync(
|
||||||
string name,
|
// 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();
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
// private async Task ValidateReferencesAsync(
|
|
||||||
// XPolicyDefinitionDto item,
|
|
||||||
// CancellationToken cancellationToken
|
// CancellationToken cancellationToken
|
||||||
// )
|
// )
|
||||||
// {
|
// {
|
||||||
// //
|
// //
|
||||||
// // Referenced Roles must exist ...
|
// if (name.IsNullOrEmpty())
|
||||||
// if (item.RequiredRoles.HasChild())
|
|
||||||
// {
|
// {
|
||||||
// var all = (await roles.GetAllAsync(cancellationToken: cancellationToken))
|
// XException.InvalidArgs.Throw();
|
||||||
// .Select(r => r.Name.ToNormalString()).ToList();
|
|
||||||
// if (item.RequiredRoles.Any(r => !all.Contains(r.ToNormalString())))
|
|
||||||
// {
|
|
||||||
// XException.NotFound.Throw();
|
|
||||||
// }
|
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// //
|
// //
|
||||||
// // Referenced Scopes must exist ...
|
// var normalized = name.ToNormalString();
|
||||||
// if (item.RequiredScopes.HasChild())
|
// var result = await policies.FindOneAsync(
|
||||||
// {
|
// predicate: p => p.Name.ToLower() == normalized,
|
||||||
// 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
|
// cancellationToken: cancellationToken
|
||||||
// )).ToList();
|
// );
|
||||||
|
// if (result.IsNull())
|
||||||
// //
|
|
||||||
// return dto;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// private async Task<XPolicyDefinitionDto> ToDtoWithEffectiveRolesAsync(
|
|
||||||
// XPolicyDefinitionDto item,
|
|
||||||
// CancellationToken cancellationToken = default
|
|
||||||
// )
|
|
||||||
// {
|
// {
|
||||||
// //
|
// XException.NotFound.Throw();
|
||||||
// item.EffectiveRoles = (await resolver.ResolveEffectiveRolesAsync(
|
// }
|
||||||
// minRoleWeight: item.MinRoleWeight,
|
|
||||||
// cancellationToken: cancellationToken,
|
|
||||||
// explicitRoles: item.RequiredRoles.ParseListString<string>()
|
|
||||||
// )).ToList();
|
|
||||||
|
|
||||||
// //
|
// //
|
||||||
// return item;
|
// 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();
|
||||||
|
// // }
|
||||||
|
// // }
|
||||||
|
|
||||||
|
// // //
|
||||||
|
// // // 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();
|
||||||
|
|
||||||
|
// // //
|
||||||
|
// // 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();
|
||||||
|
|
||||||
|
// // //
|
||||||
|
// // return item;
|
||||||
|
// // }
|
||||||
|
// #endregion
|
||||||
|
// }
|
||||||
// }
|
// }
|
||||||
#endregion
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,94 +1,94 @@
|
|||||||
using xCommons.Extensions;
|
// using xCommons.Extensions;
|
||||||
using System.Threading.Tasks;
|
// using System.Threading.Tasks;
|
||||||
using xIdentityService.Interfaces;
|
// using xIdentityService.Interfaces;
|
||||||
using xIdentityService.Extensions;
|
// using xIdentityService.Extensions;
|
||||||
using Microsoft.Extensions.Options;
|
// using Microsoft.Extensions.Options;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
// using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
// using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
namespace xIdentityService.Providers
|
// namespace xIdentityService.Providers
|
||||||
{
|
// {
|
||||||
/// <summary>
|
// /// <summary>
|
||||||
/// Resolve Policies from Cache → Repository → Default (static) Provider ...
|
// /// Resolve Policies from Cache → Repository → Default (static) Provider ...
|
||||||
/// </summary>
|
// /// </summary>
|
||||||
public class XDynamicPolicyProvider : IAuthorizationPolicyProvider
|
// public class XDynamicPolicyProvider : IAuthorizationPolicyProvider
|
||||||
{
|
// {
|
||||||
private readonly IXAuthorizationCache cache;
|
// private readonly IXAuthorizationCache cache;
|
||||||
private readonly IServiceScopeFactory scopeFactory;
|
// private readonly IServiceScopeFactory scopeFactory;
|
||||||
private readonly DefaultAuthorizationPolicyProvider fallback;
|
// private readonly DefaultAuthorizationPolicyProvider fallback;
|
||||||
|
|
||||||
public XDynamicPolicyProvider(
|
// public XDynamicPolicyProvider(
|
||||||
IXAuthorizationCache cache,
|
// IXAuthorizationCache cache,
|
||||||
IServiceScopeFactory scopeFactory,
|
// IServiceScopeFactory scopeFactory,
|
||||||
IOptions<AuthorizationOptions> options
|
// IOptions<AuthorizationOptions> options
|
||||||
)
|
// )
|
||||||
{
|
// {
|
||||||
//
|
// //
|
||||||
this.cache = cache;
|
// this.cache = cache;
|
||||||
this.scopeFactory = scopeFactory;
|
// this.scopeFactory = scopeFactory;
|
||||||
fallback = new DefaultAuthorizationPolicyProvider(options);
|
// fallback = new DefaultAuthorizationPolicyProvider(options);
|
||||||
}
|
// }
|
||||||
|
|
||||||
public Task<AuthorizationPolicy> GetDefaultPolicyAsync() =>
|
// public Task<AuthorizationPolicy> GetDefaultPolicyAsync() =>
|
||||||
fallback.GetDefaultPolicyAsync();
|
// fallback.GetDefaultPolicyAsync();
|
||||||
|
|
||||||
public async Task<AuthorizationPolicy> GetPolicyAsync(string policyName)
|
// public async Task<AuthorizationPolicy> GetPolicyAsync(string policyName)
|
||||||
{
|
// {
|
||||||
//
|
// //
|
||||||
if (policyName.IsNullOrEmpty())
|
// if (policyName.IsNullOrEmpty())
|
||||||
{
|
// {
|
||||||
return await fallback.GetPolicyAsync(policyName);
|
// return await fallback.GetPolicyAsync(policyName);
|
||||||
}
|
// }
|
||||||
|
|
||||||
//
|
// //
|
||||||
// 1. Cache ...
|
// // 1. Cache ...
|
||||||
if (cache.TryGetPolicy(policyName, out var cached))
|
// if (cache.TryGetPolicy(policyName, out var cached))
|
||||||
{
|
// {
|
||||||
return cached;
|
// return cached;
|
||||||
}
|
// }
|
||||||
|
|
||||||
//
|
// //
|
||||||
// 2. Repository (needs a Scope because Provider is Singleton) ...
|
// // 2. Repository (needs a Scope because Provider is Singleton) ...
|
||||||
using (var scope = scopeFactory.CreateScope())
|
// using (var scope = scopeFactory.CreateScope())
|
||||||
{
|
// {
|
||||||
//
|
// //
|
||||||
var repository = scope.ServiceProvider.GetService<IXPolicyProvider>();
|
// var repository = scope.ServiceProvider.GetService<IXPolicyProvider>();
|
||||||
var resolver = scope.ServiceProvider.GetService<IXRoleHierarchyResolver>();
|
// var resolver = scope.ServiceProvider.GetService<IXRoleHierarchyResolver>();
|
||||||
|
|
||||||
//
|
// //
|
||||||
if (!repository.IsNull() && !resolver.IsNull())
|
// if (!repository.IsNull() && !resolver.IsNull())
|
||||||
{
|
// {
|
||||||
//
|
// //
|
||||||
var normalized = policyName.ToNormalString();
|
// var normalized = policyName.ToNormalString();
|
||||||
var definition = await repository.FindOneAsync(
|
// var definition = await repository.FindOneAsync(
|
||||||
predicate: p => p.IsEnabled && p.Name.ToLower() == normalized
|
// predicate: p => p.IsEnabled && p.Name.ToLower() == normalized
|
||||||
);
|
// );
|
||||||
|
|
||||||
//
|
// //
|
||||||
var requiredRoles = definition.RequiredRoles
|
// var requiredRoles = definition.RequiredRoles
|
||||||
.ParseListString<string>();
|
// .ParseListString<string>();
|
||||||
if (!definition.IsNull())
|
// if (!definition.IsNull())
|
||||||
{
|
// {
|
||||||
//
|
// //
|
||||||
var roles = await resolver.ResolveEffectiveRolesAsync(
|
// var roles = await resolver.ResolveEffectiveRolesAsync(
|
||||||
explicitRoles: requiredRoles,
|
// explicitRoles: requiredRoles,
|
||||||
minRoleWeight: definition.MinRoleWeight
|
// minRoleWeight: definition.MinRoleWeight
|
||||||
);
|
// );
|
||||||
|
|
||||||
//
|
// //
|
||||||
var policy = definition.ToAuthorizationPolicy(roles);
|
// var policy = definition.ToAuthorizationPolicy(roles);
|
||||||
if (!policy.IsNull())
|
// if (!policy.IsNull())
|
||||||
{
|
// {
|
||||||
cache.SetPolicy(policyName, policy);
|
// cache.SetPolicy(policyName, policy);
|
||||||
return policy;
|
// return policy;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
//
|
// //
|
||||||
// 3. Fallback to Static Policies (LocalApi.PolicyName, XPolicies.*) ...
|
// // 3. Fallback to Static Policies (LocalApi.PolicyName, XPolicies.*) ...
|
||||||
return await fallback.GetPolicyAsync(policyName);
|
// return await fallback.GetPolicyAsync(policyName);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
Reference in New Issue
Block a user