From b6f20c9e797e914006c8b472d113e824ee699a1e Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Sun, 15 Nov 2026 21:25:33 +0330 Subject: [PATCH] last ... --- Constants/XIdentityServiceConstants.cs | 7 + Interfaces/IXAuthorizationCache.cs | 12 ++ Interfaces/IXPolicyProvider.cs | 247 +++++++++++++++++++++++++ Interfaces/IXRoleProvider.cs | 247 +++++++++++++++++++++++++ Interfaces/IXScopeProvider.cs | 247 +++++++++++++++++++++++++ Providers/XAuthorizationCache.cs | 96 ++++++++++ 6 files changed, 856 insertions(+) create mode 100644 Constants/XIdentityServiceConstants.cs create mode 100644 Interfaces/IXAuthorizationCache.cs create mode 100644 Interfaces/IXPolicyProvider.cs create mode 100644 Interfaces/IXRoleProvider.cs create mode 100644 Interfaces/IXScopeProvider.cs create mode 100644 Providers/XAuthorizationCache.cs diff --git a/Constants/XIdentityServiceConstants.cs b/Constants/XIdentityServiceConstants.cs new file mode 100644 index 0000000..4a4b04a --- /dev/null +++ b/Constants/XIdentityServiceConstants.cs @@ -0,0 +1,7 @@ +namespace xIdentityService.Constants +{ + public struct XIdentityServiceConstants + { + public const string PolicyCacheKeyPrefix = "XAuth:Cache"; + } +} \ No newline at end of file diff --git a/Interfaces/IXAuthorizationCache.cs b/Interfaces/IXAuthorizationCache.cs new file mode 100644 index 0000000..a98182e --- /dev/null +++ b/Interfaces/IXAuthorizationCache.cs @@ -0,0 +1,12 @@ +using Microsoft.AspNetCore.Authorization; + +namespace xIdentityService.Interfaces +{ + public interface IXAuthorizationCache + { + void Clear(); + void RemovePolicy(string name); + void SetPolicy(string name, AuthorizationPolicy policy); + bool TryGetPolicy(string name, out AuthorizationPolicy policy); + } +} \ No newline at end of file diff --git a/Interfaces/IXPolicyProvider.cs b/Interfaces/IXPolicyProvider.cs new file mode 100644 index 0000000..4364cae --- /dev/null +++ b/Interfaces/IXPolicyProvider.cs @@ -0,0 +1,247 @@ +using System; +using System.Linq; +using xModels.Dtos; +using System.Threading; +using xIdentityModels.Dtos; +using System.Threading.Tasks; +using xIdentityModels.Models; +using System.Linq.Expressions; +using System.Collections.Generic; + +namespace xIdentityService.Interfaces +{ + public interface IXPolicyProvider + { + // + #region Readers ... + /// + /// retrieve whole items as queryable ... + /// + /// a flag for Tracking behaviour + /// an Expression for Filter Items ... + /// an Order Builder expression for Ordering Query ... + /// + IQueryable AsQueryable( + bool asNoTracking = true, + Expression> predicate = null, + Func, IOrderedQueryable> orderBuilder = null + ); + + /// + /// retrieve an Entity by it's Id ... + /// + /// + /// + /// + /// + Task GetAsync( + int id, + bool ignoreSoftDeleteds = true, + CancellationToken cancellationToken = default + ); + + /// + /// retrieve all exists Entities ... + /// + /// + /// + /// + /// + Task> GetAllAsync( + bool ignoreSoftDeleteds = true, + Func, IOrderedQueryable> orderBuilder = null, + CancellationToken cancellationToken = default + ); + + /// + /// find an Entity by providing a Conditional Expression ... + /// + /// + /// + /// + /// + Task FindOneAsync( + Expression> predicate, + bool ignoreSoftDeleteds = true, + CancellationToken cancellationToken = default + ); + + /// + /// find a collection of Entities by proving a Conditional Expression ... + /// + /// + /// + /// + /// + /// + Task> FindManyAsync( + Expression> predicate, + bool ignoreSoftDeleteds = true, + Func, IOrderedQueryable> orderBuilder = null, + CancellationToken cancellationToken = default + ); + + /// + /// count all exists Entities ... + /// + /// + /// + /// + Task CountAsync( + bool ignoreSoftDeleteds = true, + CancellationToken cancellationToken = default + ); + + /// + /// count all exists Entities Pages by providing page size ... + /// + /// + /// + /// + /// + /// + Task PagesCountAsync( + int pageSize, + int? totalItems = null, + bool ignoreSoftDeleteds = true, + CancellationToken cancellationToken = default + ); + + /// + /// Check an Entity exists or not ... + /// + /// + /// + /// + /// + Task IsExistsAsync( + int id, + bool ignoreSoftDeleteds = true, + CancellationToken cancellationToken = default + ); + #endregion + + // + #region Query ... + /// + /// retrieve Entities based on XQuery Pagination structure ... + /// + /// + /// + /// + /// + /// + /// + Task> QueryAsync( + XQuery query, + bool ignoreSoftDeleteds = true, + Expression> predicate = null, + Func, IOrderedQueryable> orderBuilder = null, + CancellationToken cancellationToken = default + ); + #endregion + + // + #region Add/Update/Remove ... + /// + /// add a new Entity ... + /// + /// + /// + /// + /// + /// + Task AddAsync( + XPolicyDefinitionDto item, + bool saveChanges = true, + XUserClaimsInfoDto userInfo = null, + CancellationToken cancellationToken = default + ); + + /// + /// Update an Entity values ... + /// + /// + /// + /// + /// + /// + /// + Task UpdateAsync( + int id, + XPolicyDefinitionDto item, + bool saveChanges = true, + XUserClaimsInfoDto userInfo = null, + CancellationToken cancellationToken = default + ); + + /// + /// remove an Entity by it's Id ... + /// + /// + /// + /// + /// + /// + /// + Task RemoveAsync( + int id, + bool softDelete = true, + bool saveChanges = true, + XUserClaimsInfoDto userInfo = null, + CancellationToken cancellationToken = default + ); + #endregion + + // + #region Custom ... + #endregion + + // + #region Enrichers ... + /// + /// Enrich Item ... + /// + /// + /// + /// + Task EnrichAsync( + XPolicyDefinition source, + CancellationToken cancellationToken = default + ); + + /// + /// Enrich Item ... + /// + /// + /// + /// + Task EnrichAsync( + XPolicyDefinitionDto source, + CancellationToken cancellationToken = default + ); + + /// + /// Enrich a Collection of Items(s) ... + /// + /// + /// + /// + Task> EnrichRangeAsync( + IEnumerable sources, + CancellationToken cancellationToken = default + ); + + /// + /// Enrich a Collection of Items(s) ... + /// + /// + /// + /// + Task> EnrichRangeAsync( + IEnumerable sources, + CancellationToken cancellationToken = default + ); + #endregion + } +} \ No newline at end of file diff --git a/Interfaces/IXRoleProvider.cs b/Interfaces/IXRoleProvider.cs new file mode 100644 index 0000000..b023a81 --- /dev/null +++ b/Interfaces/IXRoleProvider.cs @@ -0,0 +1,247 @@ +using System; +using System.Linq; +using xModels.Dtos; +using System.Threading; +using xIdentityModels.Dtos; +using System.Threading.Tasks; +using xIdentityModels.Models; +using System.Linq.Expressions; +using System.Collections.Generic; + +namespace xIdentityService.Interfaces +{ + public interface IXRoleProvider + { + // + #region Readers ... + /// + /// retrieve whole items as queryable ... + /// + /// a flag for Tracking behaviour + /// an Expression for Filter Items ... + /// an Order Builder expression for Ordering Query ... + /// + IQueryable AsQueryable( + bool asNoTracking = true, + Expression> predicate = null, + Func, IOrderedQueryable> orderBuilder = null + ); + + /// + /// retrieve an Entity by it's Id ... + /// + /// + /// + /// + /// + Task GetAsync( + int id, + bool ignoreSoftDeleteds = true, + CancellationToken cancellationToken = default + ); + + /// + /// retrieve all exists Entities ... + /// + /// + /// + /// + /// + Task> GetAllAsync( + bool ignoreSoftDeleteds = true, + Func, IOrderedQueryable> orderBuilder = null, + CancellationToken cancellationToken = default + ); + + /// + /// find an Entity by providing a Conditional Expression ... + /// + /// + /// + /// + /// + Task FindOneAsync( + Expression> predicate, + bool ignoreSoftDeleteds = true, + CancellationToken cancellationToken = default + ); + + /// + /// find a collection of Entities by proving a Conditional Expression ... + /// + /// + /// + /// + /// + /// + Task> FindManyAsync( + Expression> predicate, + bool ignoreSoftDeleteds = true, + Func, IOrderedQueryable> orderBuilder = null, + CancellationToken cancellationToken = default + ); + + /// + /// count all exists Entities ... + /// + /// + /// + /// + Task CountAsync( + bool ignoreSoftDeleteds = true, + CancellationToken cancellationToken = default + ); + + /// + /// count all exists Entities Pages by providing page size ... + /// + /// + /// + /// + /// + /// + Task PagesCountAsync( + int pageSize, + int? totalItems = null, + bool ignoreSoftDeleteds = true, + CancellationToken cancellationToken = default + ); + + /// + /// Check an Entity exists or not ... + /// + /// + /// + /// + /// + Task IsExistsAsync( + int id, + bool ignoreSoftDeleteds = true, + CancellationToken cancellationToken = default + ); + #endregion + + // + #region Query ... + /// + /// retrieve Entities based on XQuery Pagination structure ... + /// + /// + /// + /// + /// + /// + /// + Task> QueryAsync( + XQuery query, + bool ignoreSoftDeleteds = true, + Expression> predicate = null, + Func, IOrderedQueryable> orderBuilder = null, + CancellationToken cancellationToken = default + ); + #endregion + + // + #region Add/Update/Remove ... + /// + /// add a new Entity ... + /// + /// + /// + /// + /// + /// + Task AddAsync( + XRoleDefinitionDto item, + bool saveChanges = true, + XUserClaimsInfoDto userInfo = null, + CancellationToken cancellationToken = default + ); + + /// + /// Update an Entity values ... + /// + /// + /// + /// + /// + /// + /// + Task UpdateAsync( + int id, + XRoleDefinitionDto item, + bool saveChanges = true, + XUserClaimsInfoDto userInfo = null, + CancellationToken cancellationToken = default + ); + + /// + /// remove an Entity by it's Id ... + /// + /// + /// + /// + /// + /// + /// + Task RemoveAsync( + int id, + bool softDelete = true, + bool saveChanges = true, + XUserClaimsInfoDto userInfo = null, + CancellationToken cancellationToken = default + ); + #endregion + + // + #region Custom ... + #endregion + + // + #region Enrichers ... + /// + /// Enrich Item ... + /// + /// + /// + /// + Task EnrichAsync( + XRoleDefinition source, + CancellationToken cancellationToken = default + ); + + /// + /// Enrich Item ... + /// + /// + /// + /// + Task EnrichAsync( + XRoleDefinitionDto source, + CancellationToken cancellationToken = default + ); + + /// + /// Enrich a Collection of Items(s) ... + /// + /// + /// + /// + Task> EnrichRangeAsync( + IEnumerable sources, + CancellationToken cancellationToken = default + ); + + /// + /// Enrich a Collection of Items(s) ... + /// + /// + /// + /// + Task> EnrichRangeAsync( + IEnumerable sources, + CancellationToken cancellationToken = default + ); + #endregion + } +} \ No newline at end of file diff --git a/Interfaces/IXScopeProvider.cs b/Interfaces/IXScopeProvider.cs new file mode 100644 index 0000000..f200067 --- /dev/null +++ b/Interfaces/IXScopeProvider.cs @@ -0,0 +1,247 @@ +using System; +using xModels.Dtos; +using System.Linq; +using System.Threading; +using xIdentityModels.Dtos; +using System.Threading.Tasks; +using xIdentityModels.Models; +using System.Linq.Expressions; +using System.Collections.Generic; + +namespace xIdentityService.Interfaces +{ + public interface IXScopeProvider + { + // + #region Readers ... + /// + /// retrieve whole items as queryable ... + /// + /// a flag for Tracking behaviour + /// an Expression for Filter Items ... + /// an Order Builder expression for Ordering Query ... + /// + IQueryable AsQueryable( + bool asNoTracking = true, + Expression> predicate = null, + Func, IOrderedQueryable> orderBuilder = null + ); + + /// + /// retrieve an Entity by it's Id ... + /// + /// + /// + /// + /// + Task GetAsync( + int id, + bool ignoreSoftDeleteds = true, + CancellationToken cancellationToken = default + ); + + /// + /// retrieve all exists Entities ... + /// + /// + /// + /// + /// + Task> GetAllAsync( + bool ignoreSoftDeleteds = true, + Func, IOrderedQueryable> orderBuilder = null, + CancellationToken cancellationToken = default + ); + + /// + /// find an Entity by providing a Conditional Expression ... + /// + /// + /// + /// + /// + Task FindOneAsync( + Expression> predicate, + bool ignoreSoftDeleteds = true, + CancellationToken cancellationToken = default + ); + + /// + /// find a collection of Entities by proving a Conditional Expression ... + /// + /// + /// + /// + /// + /// + Task> FindManyAsync( + Expression> predicate, + bool ignoreSoftDeleteds = true, + Func, IOrderedQueryable> orderBuilder = null, + CancellationToken cancellationToken = default + ); + + /// + /// count all exists Entities ... + /// + /// + /// + /// + Task CountAsync( + bool ignoreSoftDeleteds = true, + CancellationToken cancellationToken = default + ); + + /// + /// count all exists Entities Pages by providing page size ... + /// + /// + /// + /// + /// + /// + Task PagesCountAsync( + int pageSize, + int? totalItems = null, + bool ignoreSoftDeleteds = true, + CancellationToken cancellationToken = default + ); + + /// + /// Check an Entity exists or not ... + /// + /// + /// + /// + /// + Task IsExistsAsync( + int id, + bool ignoreSoftDeleteds = true, + CancellationToken cancellationToken = default + ); + #endregion + + // + #region Query ... + /// + /// retrieve Entities based on XQuery Pagination structure ... + /// + /// + /// + /// + /// + /// + /// + Task> QueryAsync( + XQuery query, + bool ignoreSoftDeleteds = true, + Expression> predicate = null, + Func, IOrderedQueryable> orderBuilder = null, + CancellationToken cancellationToken = default + ); + #endregion + + // + #region Add/Update/Remove ... + /// + /// add a new Entity ... + /// + /// + /// + /// + /// + /// + Task AddAsync( + XScopeDefinitionDto item, + bool saveChanges = true, + XUserClaimsInfoDto userInfo = null, + CancellationToken cancellationToken = default + ); + + /// + /// Update an Entity values ... + /// + /// + /// + /// + /// + /// + /// + Task UpdateAsync( + int id, + XScopeDefinitionDto item, + bool saveChanges = true, + XUserClaimsInfoDto userInfo = null, + CancellationToken cancellationToken = default + ); + + /// + /// remove an Entity by it's Id ... + /// + /// + /// + /// + /// + /// + /// + Task RemoveAsync( + int id, + bool softDelete = true, + bool saveChanges = true, + XUserClaimsInfoDto userInfo = null, + CancellationToken cancellationToken = default + ); + #endregion + + // + #region Custom ... + #endregion + + // + #region Enrichers ... + /// + /// Enrich Item ... + /// + /// + /// + /// + Task EnrichAsync( + XScopeDefinition source, + CancellationToken cancellationToken = default + ); + + /// + /// Enrich Item ... + /// + /// + /// + /// + Task EnrichAsync( + XScopeDefinitionDto source, + CancellationToken cancellationToken = default + ); + + /// + /// Enrich a Collection of Items(s) ... + /// + /// + /// + /// + Task> EnrichRangeAsync( + IEnumerable sources, + CancellationToken cancellationToken = default + ); + + /// + /// Enrich a Collection of Items(s) ... + /// + /// + /// + /// + Task> EnrichRangeAsync( + IEnumerable sources, + CancellationToken cancellationToken = default + ); + #endregion + } +} \ No newline at end of file diff --git a/Providers/XAuthorizationCache.cs b/Providers/XAuthorizationCache.cs new file mode 100644 index 0000000..2e3f2a6 --- /dev/null +++ b/Providers/XAuthorizationCache.cs @@ -0,0 +1,96 @@ +using System; +using xCommons.Extensions; +using xCommons.Configurations; +using xIdentityService.Constants; +using xIdentityService.Interfaces; +using System.Collections.Concurrent; +using Microsoft.AspNetCore.Authorization; +using Microsoft.Extensions.Caching.Memory; + +namespace xIdentityService.Providers +{ + /// + /// Thread-safe local cache; IMemoryCache gives TTL, + /// ConcurrentDictionary gives key tracking for Clear() ... + /// + public class XAuthorizationCache : IXAuthorizationCache + { + private readonly TimeSpan ttl; + private readonly IMemoryCache cache; + private readonly ConcurrentDictionary keys = new(); + private readonly XAppConfiguration appConfiguration; + + public XAuthorizationCache( + IMemoryCache cache, + XAppConfiguration appConfiguration + ) + { + // + this.cache = cache; + this.appConfiguration = appConfiguration; + ttl = TimeSpan.FromMinutes(appConfiguration.DefaultCacheTtlMinutes); + } + + public bool TryGetPolicy( + string name, + out AuthorizationPolicy policy + ) + { + // + policy = null; + + // + var result = + !name.IsNullOrEmpty() && + cache.TryGetValue(Key(name), out policy); + return result; + } + + public void SetPolicy( + string name, + AuthorizationPolicy policy + ) + { + // + if (name.IsNullOrEmpty() || policy.IsNull()) + { + return; + } + + // + cache.Set(Key(name), policy, ttl); + keys[Key(name)] = 0; + } + + public void RemovePolicy(string name) + { + // + if (name.IsNullOrEmpty()) + { + return; + } + + // + cache.Remove(Key(name)); + keys.TryRemove(Key(name), out _); + } + + public void Clear() + { + // + foreach (var key in keys.Keys) + { + cache.Remove(key); + } + + // + keys.Clear(); + } + + // + #region Private ... + private static string Key(string name) => + XIdentityServiceConstants.PolicyCacheKeyPrefix + name.ToNormalString(); + #endregion + } +} \ No newline at end of file