From 703892a253bf8509f2456d0961c79995dac81ba4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=AD=E6=98=8E=E9=94=8B?= Date: Tue, 27 Feb 2024 15:43:48 +0800 Subject: [PATCH 01/11] =?UTF-8?q?wip:=20=E5=B0=86=E8=A7=92=E8=89=B2?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=9D=83=E9=99=90=E7=9A=84=E4=BF=9D=E5=AD=98?= =?UTF-8?q?=E8=BF=87=E6=BB=A4=E6=9D=A1=E4=BB=B6=E6=93=8D=E4=BD=9C=E4=BB=8E?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=93=8D=E4=BD=9C=E4=B8=AD=E5=89=A5=E7=A6=BB?= =?UTF-8?q?=E4=B8=BA=E5=8D=95=E7=8B=AC=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataAuthorizationManagerBase.cs | 86 +++++++++++++++---- .../Dtos/EntityRoleInputDtoBase.cs | 14 --- .../IEntityRoleStore.cs | 8 ++ src/OSharp.EntityFrameworkCore/Repository.cs | 4 +- .../Controllers/Auth/RoleEntityController.cs | 17 ++++ .../Dtos/AutoMapperConfiguration.cs | 3 - 6 files changed, 97 insertions(+), 35 deletions(-) diff --git a/src/OSharp.Authorization.Datas/DataAuthorizationManagerBase.cs b/src/OSharp.Authorization.Datas/DataAuthorizationManagerBase.cs index f62db05b..7b95b4d7 100644 --- a/src/OSharp.Authorization.Datas/DataAuthorizationManagerBase.cs +++ b/src/OSharp.Authorization.Datas/DataAuthorizationManagerBase.cs @@ -7,6 +7,8 @@ // 2020-02-26 23:15 // ----------------------------------------------------------------------- +using OSharp.Json; + namespace OSharp.Authorization.DataAuthorization; @@ -141,22 +143,23 @@ public virtual async Task CreateEntityRoles(params TEntityRoleI throw new OsharpException($"角色“{role.Name}”和实体“{entityInfo.Name}”和操作“{dto.Operation}”的数据权限规则已存在,不能重复添加"); } - OperationResult checkResult = CheckFilterGroup(dto.FilterGroup, entityInfo); - if (!checkResult.Succeeded) - { - throw new OsharpException($"数据规则验证失败:{checkResult.Message}"); - } - + }, + async (dto, entity) => + { if (!dto.IsLocked) { + TRole role = await RoleRepository.GetAsync(dto.RoleId); + TEntityInfo entityInfo = await EntityInfoRepository.GetAsync(dto.EntityId); eventData.SetItems.Add(new DataAuthCacheItem() { RoleName = role.Name, EntityTypeFullName = entityInfo.TypeName, - Operation = dto.Operation, - FilterGroup = dto.FilterGroup + Operation = entity.Operation, + FilterGroup = entity.FilterGroup }); } + + return entity; }); if (result.Succeeded && eventData.HasData()) { @@ -197,18 +200,12 @@ public virtual async Task UpdateEntityRoles(params TEntityRoleI throw new OsharpException($"角色“{role.Name}”和实体“{entityInfo.Name}”和操作“{dto.Operation}”的数据权限规则已存在,不能重复添加"); } - OperationResult checkResult = CheckFilterGroup(dto.FilterGroup, entityInfo); - if (!checkResult.Succeeded) - { - throw new OsharpException($"数据规则验证失败:{checkResult.Message}"); - } - DataAuthCacheItem cacheItem = new DataAuthCacheItem() { RoleName = role.Name, EntityTypeFullName = entityInfo.TypeName, Operation = dto.Operation, - FilterGroup = dto.FilterGroup + FilterGroup = entity.FilterGroup }; if (dto.IsLocked) { @@ -228,6 +225,63 @@ public virtual async Task UpdateEntityRoles(params TEntityRoleI return result; } + /// + /// 设置角色数据权限的过滤条件组 + /// + /// 权限记录identity.api + /// 过滤条件组 + /// 业务操作结果 + public async Task SetFilterGroup(Guid id, FilterGroup group) + { + TEntityRole entityRole = await EntityRoleRepository.GetAsync(id); + if (entityRole == null) + { + return new OperationResult(OperationResultType.QueryNull, $"编号为“{id}”的数据权限信息不存在"); + } + + TRole role = await RoleRepository.GetAsync(entityRole.RoleId); + if (role == null) + { + return new OperationResult(OperationResultType.QueryNull, $"编号为“{entityRole.RoleId}”的角色信息不存在"); + } + + TEntityInfo entityInfo = await EntityInfoRepository.GetAsync(entityRole.EntityId); + if (entityInfo == null) + { + return new OperationResult(OperationResultType.QueryNull, $"编号为“{entityRole.EntityId}”的数据实体信息不存在"); + } + + OperationResult checkResult = CheckFilterGroup(group, entityInfo); + if (!checkResult.Succeeded) + { + return new OperationResult(OperationResultType.Error, $"数据规则验证失败:{checkResult.Message}"); + } + + IUnitOfWork unitOfWork = _provider.GetUnitOfWork(true); + entityRole.FilterGroupJson = group.ToJsonString(); + int count = await EntityRoleRepository.UpdateAsync(entityRole); + await unitOfWork.CommitAsync(); + if (count > 0) + { + DataAuthCacheRefreshEventData eventData = new DataAuthCacheRefreshEventData(); + eventData.SetItems.Add(new DataAuthCacheItem() + { + RoleName = role.Name, + EntityTypeFullName = entityInfo.TypeName, + Operation = entityRole.Operation, + FilterGroup = entityRole.FilterGroup + }); + if (eventData.HasData()) + { + await EventBus.PublishAsync(eventData); + } + + return new OperationResult(OperationResultType.Success, $"{role.Name} - {entityInfo.Name} - {entityRole.Operation} 的过滤条件设置成功"); + } + + return OperationResult.NoChanged; + } + /// /// 删除实体角色信息 /// @@ -244,7 +298,7 @@ public virtual async Task DeleteEntityRoles(params Guid[] ids) if (role != null && entityInfo != null) { eventData.RemoveItems.Add(new DataAuthCacheItem() - { RoleName = role.Name, EntityTypeFullName = entityInfo.TypeName, Operation = entity.Operation }); + { RoleName = role.Name, EntityTypeFullName = entityInfo.TypeName, Operation = entity.Operation }); } }); if (result.Succeeded && eventData.HasData()) diff --git a/src/OSharp.Authorization.Datas/Dtos/EntityRoleInputDtoBase.cs b/src/OSharp.Authorization.Datas/Dtos/EntityRoleInputDtoBase.cs index 4b719b7c..a2b8eeed 100644 --- a/src/OSharp.Authorization.Datas/Dtos/EntityRoleInputDtoBase.cs +++ b/src/OSharp.Authorization.Datas/Dtos/EntityRoleInputDtoBase.cs @@ -16,14 +16,6 @@ namespace OSharp.Authorization.Dtos; /// 角色编号类型 public abstract class EntityRoleInputDtoBase : IInputDto { - /// - /// 初始化一个类型的新实例 - /// - protected EntityRoleInputDtoBase() - { - FilterGroup = new FilterGroup(); - } - /// /// 获取或设置 主键,唯一标识 /// @@ -48,12 +40,6 @@ protected EntityRoleInputDtoBase() [DisplayName("数据权限操作")] public DataAuthOperation Operation { get; set; } - /// - /// 获取或设置 过滤条件组 - /// - [DisplayName("数据筛选条件组")] - public FilterGroup FilterGroup { get; set; } - /// /// 获取或设置 是否锁定 /// diff --git a/src/OSharp.Authorization.Datas/IEntityRoleStore.cs b/src/OSharp.Authorization.Datas/IEntityRoleStore.cs index aaac6aee..86f6d627 100644 --- a/src/OSharp.Authorization.Datas/IEntityRoleStore.cs +++ b/src/OSharp.Authorization.Datas/IEntityRoleStore.cs @@ -53,6 +53,14 @@ public interface IEntityRoleStore业务操作结果 Task UpdateEntityRoles(params TEntityRoleInputDto[] dtos); + /// + /// 设置角色数据权限的过滤条件组 + /// + /// 权限记录identity.api + /// 过滤条件组 + /// 业务操作结果 + Task SetFilterGroup(Guid id, FilterGroup group); + /// /// 删除实体角色信息 /// diff --git a/src/OSharp.EntityFrameworkCore/Repository.cs b/src/OSharp.EntityFrameworkCore/Repository.cs index eb15922f..718bc140 100644 --- a/src/OSharp.EntityFrameworkCore/Repository.cs +++ b/src/OSharp.EntityFrameworkCore/Repository.cs @@ -942,7 +942,7 @@ private void CheckDataAuth(DataAuthOperation operation, params TEntity[] entitie bool flag = _dataAuthService.CheckDataAuth(operation, entities); if (!flag) { - throw new OsharpException($"实体 {typeof(TEntity)} 的数据 {entities.ExpandAndToString(m => m.Id.ToString())} 进行 {operation.ToDescription()} 操作时权限不足"); + throw new OsharpException($"实体 {typeof(TEntity).Name} 的数据 {entities.ExpandAndToString(m => m.Id.ToString())} 进行 {operation.ToDescription()} 操作时权限不足"); } } @@ -1026,4 +1026,4 @@ private void DeleteInternal(params TEntity[] entities) } #endregion -} +} diff --git a/src/OSharp.Hosting.Apis/Areas/Admin/Controllers/Auth/RoleEntityController.cs b/src/OSharp.Hosting.Apis/Areas/Admin/Controllers/Auth/RoleEntityController.cs index 10c87263..b379ca59 100644 --- a/src/OSharp.Hosting.Apis/Areas/Admin/Controllers/Auth/RoleEntityController.cs +++ b/src/OSharp.Hosting.Apis/Areas/Admin/Controllers/Auth/RoleEntityController.cs @@ -105,6 +105,23 @@ public async Task Update(params EntityRoleInputDto[] dtos) return result.ToAjaxResult(); } + /// + /// 设置角色数据权限的过滤条件组 + /// + /// 权限记录identity.api + /// 过滤条件组 + /// JSON操作结果 + [HttpPost] + [ModuleInfo] + [DependOnFunction(nameof(Read))] + [UnitOfWork] + [Description("设置过滤条件")] + public async Task SetFilterGroup(Guid id, [FromBody]FilterGroup group) + { + OperationResult result = await _dataAuthManager.SetFilterGroup(id, group); + return result.ToAjaxResult(); + } + /// /// 删除角色数据权限信息 /// diff --git a/src/OSharp.Hosting.Core/Authorization/Dtos/AutoMapperConfiguration.cs b/src/OSharp.Hosting.Core/Authorization/Dtos/AutoMapperConfiguration.cs index 8d66ee5b..c8ff8504 100644 --- a/src/OSharp.Hosting.Core/Authorization/Dtos/AutoMapperConfiguration.cs +++ b/src/OSharp.Hosting.Core/Authorization/Dtos/AutoMapperConfiguration.cs @@ -22,9 +22,6 @@ public class AutoMapperConfiguration : AutoMapperTupleBase /// public override void CreateMap() { - CreateMap() - .ForMember(mr => mr.FilterGroupJson, opt => opt.MapFrom(dto => dto.FilterGroup.ToJsonString(false, false))); - //mapper.CreateMap() // .ForMember(dto => dto.FilterGroup, opt => opt.ResolveUsing(mr => mr.FilterGroupJson?.FromJsonString())); } From 5bfeba991b242a078b418d2152cde93c572d9121 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=AD=E6=98=8E=E9=94=8B?= Date: Tue, 27 Feb 2024 15:54:51 +0800 Subject: [PATCH 02/11] =?UTF-8?q?wip:=20=E6=9B=B4=E6=96=B0=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=9D=83=E9=99=90=E4=B8=8D=E8=B6=B3=E6=97=B6=E7=9A=84?= =?UTF-8?q?=E6=8F=90=E7=A4=BA=E8=AF=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Dtos/AutoMapperConfiguration.cs | 4 ++-- src/OSharp.EntityFrameworkCore/Repository.cs | 19 ++++++++++--------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/samples/web/Liuliu.Demo.Core/Authorization/Dtos/AutoMapperConfiguration.cs b/samples/web/Liuliu.Demo.Core/Authorization/Dtos/AutoMapperConfiguration.cs index 2a26737e..cb4a13f1 100644 --- a/samples/web/Liuliu.Demo.Core/Authorization/Dtos/AutoMapperConfiguration.cs +++ b/samples/web/Liuliu.Demo.Core/Authorization/Dtos/AutoMapperConfiguration.cs @@ -27,8 +27,8 @@ public class AutoMapperConfiguration : AutoMapperTupleBase /// public override void CreateMap() { - CreateMap() - .ForMember(mr => mr.FilterGroupJson, opt => opt.MapFrom(dto => dto.FilterGroup.ToJsonString(false, false))); + //CreateMap() + // .ForMember(mr => mr.FilterGroupJson, opt => opt.MapFrom(dto => dto.FilterGroup.ToJsonString(false, false))); //mapper.CreateMap() // .ForMember(dto => dto.FilterGroup, opt => opt.ResolveUsing(mr => mr.FilterGroupJson?.FromJsonString())); diff --git a/src/OSharp.EntityFrameworkCore/Repository.cs b/src/OSharp.EntityFrameworkCore/Repository.cs index 718bc140..cfba552f 100644 --- a/src/OSharp.EntityFrameworkCore/Repository.cs +++ b/src/OSharp.EntityFrameworkCore/Repository.cs @@ -734,8 +734,8 @@ public virtual async Task DeleteBatchAsync(Expression> { // 物理删除 count = await _dbSet.Where(predicate).DeleteAsync(_cancellationTokenProvider.Token); - } - + } + await unitOfWork.CommitAsync(_cancellationTokenProvider.Token); return count; } @@ -828,8 +828,8 @@ public virtual async Task UpdateBatchAsync(Expression> //走EF.Plus的时候,是不调用SaveChanges的,需要手动开启事务 await ((DbContextBase)_dbContext).BeginOrUseTransactionAsync(_cancellationTokenProvider.Token); - int count = await _dbSet.Where(predicate).UpdateAsync(updateExpression, _cancellationTokenProvider.Token); - + int count = await _dbSet.Where(predicate).UpdateAsync(updateExpression, _cancellationTokenProvider.Token); + await unitOfWork.CommitAsync(_cancellationTokenProvider.Token); return count; } @@ -937,15 +937,16 @@ private void CheckDataAuth(DataAuthOperation operation, params TEntity[] entitie if (entities.Length == 0 || _dataAuthService == null) { return; - } - + } + bool flag = _dataAuthService.CheckDataAuth(operation, entities); if (!flag) { - throw new OsharpException($"实体 {typeof(TEntity).Name} 的数据 {entities.ExpandAndToString(m => m.Id.ToString())} 进行 {operation.ToDescription()} 操作时权限不足"); + throw new OsharpException( + $"{operation.ToDescription()}编号为 {entities.ExpandAndToString(m => m.Id.ToString())} 的 {typeof(TEntity).GetDescription()} 时操作权限不足(403)"); } - } - + } + private TEntity[] CheckInsert(params TEntity[] entities) { for (int i = 0; i < entities.Length; i++) From 3b211eeff28167629d489c66aa9835754111c7cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=AD=E6=98=8E=E9=94=8B?= Date: Tue, 27 Feb 2024 17:28:59 +0800 Subject: [PATCH 03/11] =?UTF-8?q?wip:=20ScopedDictionary=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?IsIgnoreDataAuth=E7=94=A8=E4=BA=8E=E5=BF=BD=E7=95=A5=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=9D=83=E9=99=90=E6=A3=80=E6=9F=A5=E7=9A=84=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Authentication/JwtBearer/JwtBearerService.cs | 5 ++++- src/OSharp/Authorization/DataAuthService.cs | 8 +++++++- src/OSharp/Dependency/ScopedDictionary.cs | 7 ++++++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/OSharp.Identity/Authentication/JwtBearer/JwtBearerService.cs b/src/OSharp.Identity/Authentication/JwtBearer/JwtBearerService.cs index 158217af..b3fb08ef 100644 --- a/src/OSharp.Identity/Authentication/JwtBearer/JwtBearerService.cs +++ b/src/OSharp.Identity/Authentication/JwtBearer/JwtBearerService.cs @@ -116,6 +116,9 @@ private async Task CreateToken(string userId, string userName, Req }; var (token, expires) = CreateToken(claims, _jwtOptions, JwtTokenType.RefreshToken, refreshToken); string refreshTokenStr = token; + //关闭数据权限检查 + ScopedDictionary scopedDict = _provider.GetService(); + scopedDict.IsIgnoreDataAuth = true; IUnitOfWork unitOfWork = _provider.GetUnitOfWork(true); UserManager userManager = _provider.GetService>(); refreshToken = new RefreshToken() { ClientId = clientId, Value = refreshTokenStr, EndUtcTime = expires }; @@ -200,4 +203,4 @@ private enum JwtTokenType RefreshToken } -} \ No newline at end of file +} diff --git a/src/OSharp/Authorization/DataAuthService.cs b/src/OSharp/Authorization/DataAuthService.cs index e7cb4d3c..b99502a3 100644 --- a/src/OSharp/Authorization/DataAuthService.cs +++ b/src/OSharp/Authorization/DataAuthService.cs @@ -116,9 +116,15 @@ public bool CheckDataAuth(DataAuthOperation operation, params TEntity[] return true; } + ScopedDictionary dict = _provider.GetService(); + if (dict.IsIgnoreDataAuth) + { + return true; + } + Expression> exp = GetDataFilter(operation); Func func = exp.Compile(); bool has = entities.All(func); return has; } -} \ No newline at end of file +} diff --git a/src/OSharp/Dependency/ScopedDictionary.cs b/src/OSharp/Dependency/ScopedDictionary.cs index c5ab220b..0e803161 100644 --- a/src/OSharp/Dependency/ScopedDictionary.cs +++ b/src/OSharp/Dependency/ScopedDictionary.cs @@ -34,6 +34,11 @@ public sealed class ScopedDictionary : ConcurrentDictionary, IDi /// public ClaimsIdentity Identity { get; set; } + /// + /// 获取或设置 是否忽略数据权限检查 + /// + public bool IsIgnoreDataAuth { get; set; } = false; + /// 释放资源. public void Dispose() { @@ -42,4 +47,4 @@ public void Dispose() Identity = null; this.Clear(); } -} \ No newline at end of file +} From 8e8206232a6a93687ab54a8817d80df655c2371a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=AD=E6=98=8E=E9=94=8B?= Date: Tue, 27 Feb 2024 17:29:27 +0800 Subject: [PATCH 04/11] =?UTF-8?q?bug:=20=E4=BF=AE=E5=A4=8DEntityRole?= =?UTF-8?q?=E7=9A=84FilterGroup=E4=B8=BAnull=E6=97=B6=E7=9A=84=E5=BC=82?= =?UTF-8?q?=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Authorization/Dtos/EntityRoleOutputDto.cs | 4 ++-- .../DataAuthCacheBase.cs | 6 ++++- .../DataAuthorizationManagerBase.cs | 24 +------------------ .../Authorization/Dtos/EntityRoleOutputDto.cs | 2 +- 4 files changed, 9 insertions(+), 27 deletions(-) diff --git a/samples/web/Liuliu.Demo.Core/Authorization/Dtos/EntityRoleOutputDto.cs b/samples/web/Liuliu.Demo.Core/Authorization/Dtos/EntityRoleOutputDto.cs index ab7ae694..135bd6fe 100644 --- a/samples/web/Liuliu.Demo.Core/Authorization/Dtos/EntityRoleOutputDto.cs +++ b/samples/web/Liuliu.Demo.Core/Authorization/Dtos/EntityRoleOutputDto.cs @@ -1,4 +1,4 @@ -// ----------------------------------------------------------------------- +// ----------------------------------------------------------------------- // // Copyright (c) 2014-2018 OSharp. All rights reserved. // @@ -43,7 +43,7 @@ public EntityRoleOutputDto(EntityRole entityRole) IsLocked = entityRole.IsLocked; Operation = entityRole.Operation; CreatedTime = entityRole.CreatedTime; - FilterGroup = entityRole.FilterGroupJson.FromJsonString(); + FilterGroup = entityRole.FilterGroup; } /// diff --git a/src/OSharp.Authorization.Datas/DataAuthCacheBase.cs b/src/OSharp.Authorization.Datas/DataAuthCacheBase.cs index 6400682c..9a7f6388 100644 --- a/src/OSharp.Authorization.Datas/DataAuthCacheBase.cs +++ b/src/OSharp.Authorization.Datas/DataAuthCacheBase.cs @@ -54,7 +54,11 @@ public virtual void BuildCaches() foreach (var entityRole in entityRoles) { - FilterGroup filterGroup = entityRole.FilterGroupJson.FromJsonString(); + FilterGroup filterGroup = entityRole.FilterGroupJson?.FromJsonString(); + if (filterGroup == null) + { + continue; + } string key = GetKey(entityRole.RoleName, entityRole.EntityTypeFullName, entityRole.Operation); string name = GetName(entityRole.RoleName, entityRole.EntityTypeFullName, entityRole.Operation); diff --git a/src/OSharp.Authorization.Datas/DataAuthorizationManagerBase.cs b/src/OSharp.Authorization.Datas/DataAuthorizationManagerBase.cs index 7b95b4d7..2eea3864 100644 --- a/src/OSharp.Authorization.Datas/DataAuthorizationManagerBase.cs +++ b/src/OSharp.Authorization.Datas/DataAuthorizationManagerBase.cs @@ -122,7 +122,6 @@ public virtual async Task CreateEntityRoles(params TEntityRoleI { Check2.Validate(dtos, nameof(dtos)); - DataAuthCacheRefreshEventData eventData = new DataAuthCacheRefreshEventData(); OperationResult result = await EntityRoleRepository.InsertAsync(dtos, async dto => { @@ -143,28 +142,7 @@ public virtual async Task CreateEntityRoles(params TEntityRoleI throw new OsharpException($"角色“{role.Name}”和实体“{entityInfo.Name}”和操作“{dto.Operation}”的数据权限规则已存在,不能重复添加"); } - }, - async (dto, entity) => - { - if (!dto.IsLocked) - { - TRole role = await RoleRepository.GetAsync(dto.RoleId); - TEntityInfo entityInfo = await EntityInfoRepository.GetAsync(dto.EntityId); - eventData.SetItems.Add(new DataAuthCacheItem() - { - RoleName = role.Name, - EntityTypeFullName = entityInfo.TypeName, - Operation = entity.Operation, - FilterGroup = entity.FilterGroup - }); - } - - return entity; }); - if (result.Succeeded && eventData.HasData()) - { - await EventBus.PublishAsync(eventData); - } return result; } @@ -211,7 +189,7 @@ public virtual async Task UpdateEntityRoles(params TEntityRoleI { eventData.RemoveItems.Add(cacheItem); } - else + else if(entity.FilterGroup != null) { eventData.SetItems.Add(cacheItem); } diff --git a/src/OSharp.Hosting.Core/Authorization/Dtos/EntityRoleOutputDto.cs b/src/OSharp.Hosting.Core/Authorization/Dtos/EntityRoleOutputDto.cs index 0da2b66e..0668a4fd 100644 --- a/src/OSharp.Hosting.Core/Authorization/Dtos/EntityRoleOutputDto.cs +++ b/src/OSharp.Hosting.Core/Authorization/Dtos/EntityRoleOutputDto.cs @@ -35,7 +35,7 @@ public EntityRoleOutputDto(EntityRole entityRole) IsLocked = entityRole.IsLocked; Operation = entityRole.Operation; CreatedTime = entityRole.CreatedTime; - FilterGroup = entityRole.FilterGroupJson.FromJsonString(); + FilterGroup = entityRole.FilterGroup; } /// From f2b1e71a62dcfe2c0079d216f1936ed6ca86a268 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=AD=E6=98=8E=E9=94=8B?= Date: Tue, 27 Feb 2024 18:54:39 +0800 Subject: [PATCH 05/11] =?UTF-8?q?wip:=20=E5=9C=A8DataAuthService=E4=B8=AD?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E6=8C=87=E5=AE=9A=E5=AE=9E=E4=BD=93=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E7=9A=84=E5=BF=BD=E7=95=A5=E6=95=B0=E6=8D=AE=E6=9D=83?= =?UTF-8?q?=E9=99=90=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Logout_RemoveRefreshTokenEventHandler.cs | 19 +++++++++++-------- .../JwtBearer/JwtBearerService.cs | 8 ++++++-- src/OSharp/Authorization/DataAuthService.cs | 19 ++++++++++++------- src/OSharp/Authorization/IDataAuthService.cs | 8 +++++++- src/OSharp/Dependency/ScopedDictionary.cs | 5 ----- 5 files changed, 36 insertions(+), 23 deletions(-) diff --git a/src/OSharp.Hosting.Core/Identity/Events/Logout_RemoveRefreshTokenEventHandler.cs b/src/OSharp.Hosting.Core/Identity/Events/Logout_RemoveRefreshTokenEventHandler.cs index 51c1d153..175022de 100644 --- a/src/OSharp.Hosting.Core/Identity/Events/Logout_RemoveRefreshTokenEventHandler.cs +++ b/src/OSharp.Hosting.Core/Identity/Events/Logout_RemoveRefreshTokenEventHandler.cs @@ -1,4 +1,4 @@ -// ----------------------------------------------------------------------- +// ----------------------------------------------------------------------- // // Copyright (c) 2014-2022 OSharp. All rights reserved. // @@ -16,16 +16,14 @@ namespace OSharp.Hosting.Identity.Events; public class Logout_RemoveRefreshTokenEventHandler : EventHandlerBase { - private readonly IPrincipal _principal; - private readonly UserManager _userManager; + private readonly IServiceProvider _provider; /// /// 初始化一个类型的新实例 /// - public Logout_RemoveRefreshTokenEventHandler(UserManager userManager, IPrincipal principal) + public Logout_RemoveRefreshTokenEventHandler(IServiceProvider provider) { - _userManager = userManager; - _principal = principal; + _provider = provider; } /// @@ -45,7 +43,8 @@ public override void Handle(LogoutEventData eventData) /// 是否成功 public override async Task HandleAsync(LogoutEventData eventData, CancellationToken cancelToken = default(CancellationToken)) { - ClaimsIdentity identity = _principal.Identity as ClaimsIdentity; + IPrincipal principal = _provider.GetCurrentUser(); + ClaimsIdentity identity = principal.Identity as ClaimsIdentity; if (identity?.IsAuthenticated != true) { return; @@ -57,6 +56,10 @@ public override void Handle(LogoutEventData eventData) return; } - await _userManager.RemoveRefreshToken(eventData.UserId.ToString(), clientId); + var dataAuthService = _provider.GetService(); + dataAuthService.SetIgnoreDataAuth(typeof(User)); + + var userManager = _provider.GetService>(); + await userManager.RemoveRefreshToken(eventData.UserId.ToString(), clientId); } } diff --git a/src/OSharp.Identity/Authentication/JwtBearer/JwtBearerService.cs b/src/OSharp.Identity/Authentication/JwtBearer/JwtBearerService.cs index b3fb08ef..4f8d8297 100644 --- a/src/OSharp.Identity/Authentication/JwtBearer/JwtBearerService.cs +++ b/src/OSharp.Identity/Authentication/JwtBearer/JwtBearerService.cs @@ -7,6 +7,9 @@ // 2019-06-15 13:26 // ----------------------------------------------------------------------- +using OSharp.Authorization; + + namespace OSharp.Authentication.JwtBearer; /// @@ -117,8 +120,9 @@ private async Task CreateToken(string userId, string userName, Req var (token, expires) = CreateToken(claims, _jwtOptions, JwtTokenType.RefreshToken, refreshToken); string refreshTokenStr = token; //关闭数据权限检查 - ScopedDictionary scopedDict = _provider.GetService(); - scopedDict.IsIgnoreDataAuth = true; + var dataAuthService = _provider.GetService(); + dataAuthService.SetIgnoreDataAuth(typeof(TUser)); + IUnitOfWork unitOfWork = _provider.GetUnitOfWork(true); UserManager userManager = _provider.GetService>(); refreshToken = new RefreshToken() { ClientId = clientId, Value = refreshTokenStr, EndUtcTime = expires }; diff --git a/src/OSharp/Authorization/DataAuthService.cs b/src/OSharp/Authorization/DataAuthService.cs index b99502a3..7f32d315 100644 --- a/src/OSharp/Authorization/DataAuthService.cs +++ b/src/OSharp/Authorization/DataAuthService.cs @@ -45,6 +45,8 @@ public DataAuthService(IServiceProvider provider) /// protected ScopedDictionary ScopedDictionary => _provider.GetService(); + protected IList IgnoreDataAuthTypes { get; } = new List(); + /// /// 获取指定实体的数据权限过滤表达式 /// @@ -111,13 +113,7 @@ public Expression> GetDataFilter(DataAuthOperation operation, F /// 是否有权限 public bool CheckDataAuth(DataAuthOperation operation, params TEntity[] entities) { - if (entities.Length == 0) - { - return true; - } - - ScopedDictionary dict = _provider.GetService(); - if (dict.IsIgnoreDataAuth) + if (entities.Length == 0 || IgnoreDataAuthTypes.Contains(typeof(TEntity))) { return true; } @@ -127,4 +123,13 @@ public bool CheckDataAuth(DataAuthOperation operation, params TEntity[] bool has = entities.All(func); return has; } + + /// + /// 设置当前请求中忽略数据权限验证的实体类型 + /// + /// 实体类型 + public void SetIgnoreDataAuth(Type entityType) + { + IgnoreDataAuthTypes.AddIfNotExist(entityType); + } } diff --git a/src/OSharp/Authorization/IDataAuthService.cs b/src/OSharp/Authorization/IDataAuthService.cs index d1a3d2ce..5445b233 100644 --- a/src/OSharp/Authorization/IDataAuthService.cs +++ b/src/OSharp/Authorization/IDataAuthService.cs @@ -31,4 +31,10 @@ public interface IDataAuthService /// 待检测的实体数据 /// 是否有权限 bool CheckDataAuth(DataAuthOperation operation, params TEntity[] entities); -} \ No newline at end of file + + /// + /// 设置当前请求中忽略数据权限验证的实体类型 + /// + /// 实体类型 + void SetIgnoreDataAuth(Type entityType); +} diff --git a/src/OSharp/Dependency/ScopedDictionary.cs b/src/OSharp/Dependency/ScopedDictionary.cs index 0e803161..6c0081f5 100644 --- a/src/OSharp/Dependency/ScopedDictionary.cs +++ b/src/OSharp/Dependency/ScopedDictionary.cs @@ -34,11 +34,6 @@ public sealed class ScopedDictionary : ConcurrentDictionary, IDi /// public ClaimsIdentity Identity { get; set; } - /// - /// 获取或设置 是否忽略数据权限检查 - /// - public bool IsIgnoreDataAuth { get; set; } = false; - /// 释放资源. public void Dispose() { From 59fc28935f9f54e0ecc4c7a1283eb94f45a8d49e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=AD=E6=98=8E=E9=94=8B?= Date: Wed, 28 Feb 2024 09:06:50 +0800 Subject: [PATCH 06/11] =?UTF-8?q?sdk:=20=E6=9B=B4=E6=96=B0.net=20sdk?= =?UTF-8?q?=E5=88=B0=208.0.2,=207.0.16,=206.0.27?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/Liuliu.Demo.Web/Liuliu.Demo.Web.csproj | 4 ++-- .../Liuliu.Demo.WebApi.csproj | 18 +++++++++--------- src/OSharp.AspNetCore/OSharp.AspNetCore.csproj | 18 +++++++++--------- .../OSharp.EntityFrameworkCore.MySql.csproj | 2 +- ...Sharp.EntityFrameworkCore.PostgreSql.csproj | 2 +- ...OSharp.EntityFrameworkCore.SqlServer.csproj | 6 +++--- .../OSharp.EntityFrameworkCore.Sqlite.csproj | 6 +++--- .../OSharp.EntityFrameworkCore.csproj | 18 +++++++++--------- src/OSharp.Hangfire/OSharp.Hangfire.csproj | 2 +- .../OSharp.Hosting.Apis.csproj | 4 ++-- src/OSharp.Identity/OSharp.Identity.csproj | 12 ++++++------ src/OSharp.Redis/OSharp.Redis.csproj | 6 +++--- src/OSharp.Utils/OSharp.Utils.csproj | 2 +- src/OSharp.Wpf/OSharp.Wpf.csproj | 6 +++--- .../Liuliu.Demo.Core.Tests.csproj | 4 ++-- .../OSharp.AspNetCore.Tests.csproj | 2 +- .../OSharp.AutoMapper.Tests.csproj | 2 +- tests/OSharp.Tests/OSharp.Tests.csproj | 2 +- .../OSharp.UnitTest.Infrastructure.csproj | 12 ++++++------ 19 files changed, 64 insertions(+), 64 deletions(-) diff --git a/samples/web/Liuliu.Demo.Web/Liuliu.Demo.Web.csproj b/samples/web/Liuliu.Demo.Web/Liuliu.Demo.Web.csproj index 44186201..a336aa8d 100644 --- a/samples/web/Liuliu.Demo.Web/Liuliu.Demo.Web.csproj +++ b/samples/web/Liuliu.Demo.Web/Liuliu.Demo.Web.csproj @@ -17,8 +17,8 @@ - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/samples/web/Liuliu.Demo.WebApi/Liuliu.Demo.WebApi.csproj b/samples/web/Liuliu.Demo.WebApi/Liuliu.Demo.WebApi.csproj index 74f67d01..0f22b184 100644 --- a/samples/web/Liuliu.Demo.WebApi/Liuliu.Demo.WebApi.csproj +++ b/samples/web/Liuliu.Demo.WebApi/Liuliu.Demo.WebApi.csproj @@ -21,30 +21,30 @@ - + - + - + all runtime; build; native; contentfiles; analyzers; buildtransitive - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/OSharp.AspNetCore/OSharp.AspNetCore.csproj b/src/OSharp.AspNetCore/OSharp.AspNetCore.csproj index 57a31bd7..80ba8515 100644 --- a/src/OSharp.AspNetCore/OSharp.AspNetCore.csproj +++ b/src/OSharp.AspNetCore/OSharp.AspNetCore.csproj @@ -16,19 +16,19 @@ - - - + + + - - - + + + - - - + + + diff --git a/src/OSharp.EntityFrameworkCore.MySql/OSharp.EntityFrameworkCore.MySql.csproj b/src/OSharp.EntityFrameworkCore.MySql/OSharp.EntityFrameworkCore.MySql.csproj index e0823f1b..667e8845 100644 --- a/src/OSharp.EntityFrameworkCore.MySql/OSharp.EntityFrameworkCore.MySql.csproj +++ b/src/OSharp.EntityFrameworkCore.MySql/OSharp.EntityFrameworkCore.MySql.csproj @@ -18,7 +18,7 @@ - + diff --git a/src/OSharp.EntityFrameworkCore.PostgreSql/OSharp.EntityFrameworkCore.PostgreSql.csproj b/src/OSharp.EntityFrameworkCore.PostgreSql/OSharp.EntityFrameworkCore.PostgreSql.csproj index 6c6bcd04..43d0d345 100644 --- a/src/OSharp.EntityFrameworkCore.PostgreSql/OSharp.EntityFrameworkCore.PostgreSql.csproj +++ b/src/OSharp.EntityFrameworkCore.PostgreSql/OSharp.EntityFrameworkCore.PostgreSql.csproj @@ -18,7 +18,7 @@ - + diff --git a/src/OSharp.EntityFrameworkCore.SqlServer/OSharp.EntityFrameworkCore.SqlServer.csproj b/src/OSharp.EntityFrameworkCore.SqlServer/OSharp.EntityFrameworkCore.SqlServer.csproj index f72c7609..bd226cb1 100644 --- a/src/OSharp.EntityFrameworkCore.SqlServer/OSharp.EntityFrameworkCore.SqlServer.csproj +++ b/src/OSharp.EntityFrameworkCore.SqlServer/OSharp.EntityFrameworkCore.SqlServer.csproj @@ -12,13 +12,13 @@ - + - + - + diff --git a/src/OSharp.EntityFrameworkCore.Sqlite/OSharp.EntityFrameworkCore.Sqlite.csproj b/src/OSharp.EntityFrameworkCore.Sqlite/OSharp.EntityFrameworkCore.Sqlite.csproj index 6e8826b1..8f6a232a 100644 --- a/src/OSharp.EntityFrameworkCore.Sqlite/OSharp.EntityFrameworkCore.Sqlite.csproj +++ b/src/OSharp.EntityFrameworkCore.Sqlite/OSharp.EntityFrameworkCore.Sqlite.csproj @@ -12,13 +12,13 @@ - + - + - + diff --git a/src/OSharp.EntityFrameworkCore/OSharp.EntityFrameworkCore.csproj b/src/OSharp.EntityFrameworkCore/OSharp.EntityFrameworkCore.csproj index 07ce5442..bd65ae5c 100644 --- a/src/OSharp.EntityFrameworkCore/OSharp.EntityFrameworkCore.csproj +++ b/src/OSharp.EntityFrameworkCore/OSharp.EntityFrameworkCore.csproj @@ -16,19 +16,19 @@ - - - + + + - - - + + + - - - + + + diff --git a/src/OSharp.Hangfire/OSharp.Hangfire.csproj b/src/OSharp.Hangfire/OSharp.Hangfire.csproj index f72e6ba0..969c1756 100644 --- a/src/OSharp.Hangfire/OSharp.Hangfire.csproj +++ b/src/OSharp.Hangfire/OSharp.Hangfire.csproj @@ -11,7 +11,7 @@ - + diff --git a/src/OSharp.Hosting.Apis/OSharp.Hosting.Apis.csproj b/src/OSharp.Hosting.Apis/OSharp.Hosting.Apis.csproj index f4ae4fda..32db2b19 100644 --- a/src/OSharp.Hosting.Apis/OSharp.Hosting.Apis.csproj +++ b/src/OSharp.Hosting.Apis/OSharp.Hosting.Apis.csproj @@ -23,10 +23,10 @@ - + - + diff --git a/src/OSharp.Identity/OSharp.Identity.csproj b/src/OSharp.Identity/OSharp.Identity.csproj index 0791fcfa..d1a8ac58 100644 --- a/src/OSharp.Identity/OSharp.Identity.csproj +++ b/src/OSharp.Identity/OSharp.Identity.csproj @@ -21,16 +21,16 @@ - - + + - - + + - - + + diff --git a/src/OSharp.Redis/OSharp.Redis.csproj b/src/OSharp.Redis/OSharp.Redis.csproj index 57ee2f8b..d7b2d9a2 100644 --- a/src/OSharp.Redis/OSharp.Redis.csproj +++ b/src/OSharp.Redis/OSharp.Redis.csproj @@ -11,13 +11,13 @@ - + - + - + diff --git a/src/OSharp.Utils/OSharp.Utils.csproj b/src/OSharp.Utils/OSharp.Utils.csproj index 845481ae..9f81d7d5 100644 --- a/src/OSharp.Utils/OSharp.Utils.csproj +++ b/src/OSharp.Utils/OSharp.Utils.csproj @@ -23,7 +23,7 @@ - + diff --git a/src/OSharp.Wpf/OSharp.Wpf.csproj b/src/OSharp.Wpf/OSharp.Wpf.csproj index 6ebe38d0..77ac944b 100644 --- a/src/OSharp.Wpf/OSharp.Wpf.csproj +++ b/src/OSharp.Wpf/OSharp.Wpf.csproj @@ -22,13 +22,13 @@ - + - + - + diff --git a/tests/Liuliu.Demo.Core.Tests/Liuliu.Demo.Core.Tests.csproj b/tests/Liuliu.Demo.Core.Tests/Liuliu.Demo.Core.Tests.csproj index d22da2aa..5aeb3bf2 100644 --- a/tests/Liuliu.Demo.Core.Tests/Liuliu.Demo.Core.Tests.csproj +++ b/tests/Liuliu.Demo.Core.Tests/Liuliu.Demo.Core.Tests.csproj @@ -7,8 +7,8 @@ - - + + diff --git a/tests/OSharp.AspNetCore.Tests/OSharp.AspNetCore.Tests.csproj b/tests/OSharp.AspNetCore.Tests/OSharp.AspNetCore.Tests.csproj index cd28058f..53ec0f43 100644 --- a/tests/OSharp.AspNetCore.Tests/OSharp.AspNetCore.Tests.csproj +++ b/tests/OSharp.AspNetCore.Tests/OSharp.AspNetCore.Tests.csproj @@ -12,7 +12,7 @@ - + diff --git a/tests/OSharp.AutoMapper.Tests/OSharp.AutoMapper.Tests.csproj b/tests/OSharp.AutoMapper.Tests/OSharp.AutoMapper.Tests.csproj index 6c6f4dc2..4bd9912c 100644 --- a/tests/OSharp.AutoMapper.Tests/OSharp.AutoMapper.Tests.csproj +++ b/tests/OSharp.AutoMapper.Tests/OSharp.AutoMapper.Tests.csproj @@ -7,7 +7,7 @@ - + diff --git a/tests/OSharp.Tests/OSharp.Tests.csproj b/tests/OSharp.Tests/OSharp.Tests.csproj index 17215158..be199819 100644 --- a/tests/OSharp.Tests/OSharp.Tests.csproj +++ b/tests/OSharp.Tests/OSharp.Tests.csproj @@ -6,7 +6,7 @@ - + diff --git a/tests/OSharp.UnitTest.Infrastructure/OSharp.UnitTest.Infrastructure.csproj b/tests/OSharp.UnitTest.Infrastructure/OSharp.UnitTest.Infrastructure.csproj index 99f34341..9ee991ee 100644 --- a/tests/OSharp.UnitTest.Infrastructure/OSharp.UnitTest.Infrastructure.csproj +++ b/tests/OSharp.UnitTest.Infrastructure/OSharp.UnitTest.Infrastructure.csproj @@ -6,14 +6,14 @@ - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -21,7 +21,7 @@ - + From c8df3fa675452feb82fb820d303355f8d4c0b506 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=AD=E6=98=8E=E9=94=8B?= Date: Fri, 6 Sep 2024 08:53:18 +0800 Subject: [PATCH 07/11] =?UTF-8?q?wip:=20=E5=88=86=E5=B8=83=E5=BC=8F?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E6=89=A9=E5=B1=95=E6=B7=BB=E5=8A=A0Cancellat?= =?UTF-8?q?ionToken=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Caching/DistributedCacheExtensions.cs | 82 +++++++++++++++---- 1 file changed, 65 insertions(+), 17 deletions(-) diff --git a/src/OSharp.Utils/Caching/DistributedCacheExtensions.cs b/src/OSharp.Utils/Caching/DistributedCacheExtensions.cs index 8a95e313..2504c55f 100644 --- a/src/OSharp.Utils/Caching/DistributedCacheExtensions.cs +++ b/src/OSharp.Utils/Caching/DistributedCacheExtensions.cs @@ -8,6 +8,7 @@ // ----------------------------------------------------------------------- using System; +using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Caching.Distributed; @@ -46,7 +47,7 @@ public static void Set(this IDistributedCache cache, string key, object value, D /// /// 异步将对象存入缓存中 /// - public static async Task SetAsync(this IDistributedCache cache, string key, object value, DistributedCacheEntryOptions options = null) + public static async Task SetAsync(this IDistributedCache cache, string key, object value, DistributedCacheEntryOptions options = null, CancellationToken token = default) { Check.NotNullOrEmpty(key, nameof(key)); Check.NotNull(value, nameof(value)); @@ -54,11 +55,11 @@ public static async Task SetAsync(this IDistributedCache cache, string key, obje string json = value.ToJsonString(); if (options == null) { - await cache.SetStringAsync(key, json); + await cache.SetStringAsync(key, json, token); } else { - await cache.SetStringAsync(key, json, options); + await cache.SetStringAsync(key, json, options, token); } } @@ -79,7 +80,7 @@ public static void Set(this IDistributedCache cache, string key, object value, i /// /// 异步将对象存入缓存中,使用指定时长 /// - public static Task SetAsync(this IDistributedCache cache, string key, object value, int cacheSeconds) + public static Task SetAsync(this IDistributedCache cache, string key, object value, int cacheSeconds, CancellationToken token = default) { Check.NotNullOrEmpty(key, nameof(key)); Check.NotNull(value, nameof(value)); @@ -87,9 +88,9 @@ public static Task SetAsync(this IDistributedCache cache, string key, object val DistributedCacheEntryOptions options = new DistributedCacheEntryOptions(); options.SetAbsoluteExpiration(TimeSpan.FromSeconds(cacheSeconds)); - return cache.SetAsync(key, value, options); + return cache.SetAsync(key, value, options, token); } - + /// /// 获取指定键的缓存项 /// @@ -106,9 +107,9 @@ public static TResult Get(this IDistributedCache cache, string key) /// /// 异步获取指定键的缓存项 /// - public static async Task GetAsync(this IDistributedCache cache, string key) + public static async Task GetAsync(this IDistributedCache cache, string key, CancellationToken token = default) { - string json = await cache.GetStringAsync(key); + string json = await cache.GetStringAsync(key, token); if (json == null) { return default(TResult); @@ -138,9 +139,13 @@ public static TResult Get(this IDistributedCache cache, string key, Fun /// /// 异步获取指定键的缓存项,不存在则从指定委托获取,并回存到缓存中再返回 /// - public static async Task GetAsync(this IDistributedCache cache, string key, Func> getAsyncFunc, DistributedCacheEntryOptions options = null) + public static async Task GetAsync(this IDistributedCache cache, + string key, + Func> getAsyncFunc, + DistributedCacheEntryOptions options = null, + CancellationToken token = default) { - TResult result = await cache.GetAsync(key); + TResult result = await cache.GetAsync(key, token); if (!Equals(result, default(TResult))) { return result; @@ -150,7 +155,30 @@ public static async Task GetAsync(this IDistributedCache cache { return default(TResult); } - await cache.SetAsync(key, result, options); + await cache.SetAsync(key, result, options, token); + return result; + } + + /// + /// 异步获取指定键的缓存项,不存在则从指定委托获取,并回存到缓存中再返回 + /// + public static async Task GetAsync(this IDistributedCache cache, + string key, + Func> getAsyncFunc, + DistributedCacheEntryOptions options = null, + CancellationToken token = default) + { + TResult result = await cache.GetAsync(key, token); + if (!Equals(result, default(TResult))) + { + return result; + } + result = await getAsyncFunc(token); + if (Equals(result, default(TResult))) + { + return default(TResult); + } + await cache.SetAsync(key, result, options, token); return result; } @@ -169,15 +197,35 @@ public static TResult Get(this IDistributedCache cache, string key, Fun /// /// 异步获取指定键的缓存项,不存在则从指定委托获取,并回存到缓存中再返回 /// - public static Task GetAsync(this IDistributedCache cache, string key, Func> getAsyncFunc, int cacheSeconds) + public static Task GetAsync(this IDistributedCache cache, + string key, + Func> getAsyncFunc, + int cacheSeconds, + CancellationToken token = default) + { + Check.GreaterThan(cacheSeconds, nameof(cacheSeconds), 0); + + DistributedCacheEntryOptions options = new DistributedCacheEntryOptions(); + options.SetAbsoluteExpiration(TimeSpan.FromSeconds(cacheSeconds)); + return cache.GetAsync(key, getAsyncFunc, options, token); + } + + /// + /// 异步获取指定键的缓存项,不存在则从指定委托获取,并回存到缓存中再返回 + /// + public static Task GetAsync(this IDistributedCache cache, + string key, + Func> getAsyncFunc, + int cacheSeconds, + CancellationToken token = default) { Check.GreaterThan(cacheSeconds, nameof(cacheSeconds), 0); DistributedCacheEntryOptions options = new DistributedCacheEntryOptions(); options.SetAbsoluteExpiration(TimeSpan.FromSeconds(cacheSeconds)); - return cache.GetAsync(key, getAsyncFunc, options); + return cache.GetAsync(key, getAsyncFunc, options, token); } - + /// /// 移除指定键的缓存项 /// @@ -193,14 +241,14 @@ public static void Remove(this IDistributedCache cache, params string[] keys) /// /// 移除指定键的缓存项 /// - public static async Task RemoveAsync(this IDistributedCache cache, params string[] keys) + public static async Task RemoveAsync(this IDistributedCache cache, CancellationToken token = default, params string[] keys) { Check.NotNull(keys, nameof(keys)); foreach (string key in keys) { - await cache.RemoveAsync(key); + await cache.RemoveAsync(key, token); } } - + } } From d31bd5a0ccb1e11548fe641508682944b219fd91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=AD=E6=98=8E=E9=94=8B?= Date: Fri, 6 Sep 2024 08:55:05 +0800 Subject: [PATCH 08/11] =?UTF-8?q?wip:=20=E6=89=8B=E6=9C=BA=E5=8F=B7?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E6=94=AF=E6=8C=8119x?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/OSharp.Utils/Extensions/StringExtensions.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/OSharp.Utils/Extensions/StringExtensions.cs b/src/OSharp.Utils/Extensions/StringExtensions.cs index 733b5e1b..45ad3261 100644 --- a/src/OSharp.Utils/Extensions/StringExtensions.cs +++ b/src/OSharp.Utils/Extensions/StringExtensions.cs @@ -1,4 +1,4 @@ -// ----------------------------------------------------------------------- +// ----------------------------------------------------------------------- // // Copyright (c) 2014 OSharp. All rights reserved. // @@ -327,7 +327,7 @@ public static bool IsIdentityCardId(this string value) /// 是否按严格格式验证 public static bool IsMobileNumber(this string value, bool isRestrict = false) { - string pattern = isRestrict ? @"^[1][3-8]\d{9}$" : @"^[1]\d{10}$"; + string pattern = isRestrict ? @"^1[3-9]\d{9}$" : @"^1\d{10}$"; return value.IsMatch(pattern); } @@ -956,4 +956,4 @@ public static double GetSimilarityWith(this string source, string target, bool i #endregion } -} \ No newline at end of file +} From 87a70370d1feafe2a16b30d1f8e805f074936339 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=AD=E6=98=8E=E9=94=8B?= Date: Fri, 6 Sep 2024 08:55:36 +0800 Subject: [PATCH 09/11] =?UTF-8?q?wip:=20=E4=B8=80=E4=BA=9B=E6=9B=B4?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/OSharpNS.nuspec | 22 +++++++++---------- build/version.props | 2 +- osharp.sln.DotSettings | 11 ++++++---- .../HttpRequestExtensions.cs | 3 +-- .../Mvc/Filters/AuditOperationAttribute.cs | 2 +- .../Identity/Entities/Organization.cs | 20 ++++++++++++++++- .../Authentication/UserClaimsProvider.cs | 2 +- .../Identity/IdentityPackBase.cs | 2 +- 8 files changed, 42 insertions(+), 22 deletions(-) diff --git a/build/OSharpNS.nuspec b/build/OSharpNS.nuspec index 00c4dacf..67b470b8 100644 --- a/build/OSharpNS.nuspec +++ b/build/OSharpNS.nuspec @@ -2,7 +2,7 @@ OSharpNS - 6.0.10-preview.1021 + 8.0.1-preview.501 OSharpFramework(.NET6.0/.NETCoreApp3.1) 柳柳软件(66soft.net) LiuliuSoft nnc @@ -16,18 +16,18 @@ osharp - - - - - + + + + + - - - - - + + + + + diff --git a/build/version.props b/build/version.props index 0f42ca8a..7a30763e 100644 --- a/build/version.props +++ b/build/version.props @@ -3,7 +3,7 @@ 8.0 1 -preview. - 113 + 529 $(VersionMain).$(VersionPrefix)$(VersionSuffix)$(VersionSuffixVersion) $(VersionMain).$(VersionPrefix).$(VersionSuffixVersion) + + $(VersionMain).$(VersionPrefix) + $(VersionMain).$(VersionPrefix) diff --git a/samples/web/Liuliu.Demo.Web/Liuliu.Demo.Web.csproj b/samples/web/Liuliu.Demo.Web/Liuliu.Demo.Web.csproj index a336aa8d..06379256 100644 --- a/samples/web/Liuliu.Demo.Web/Liuliu.Demo.Web.csproj +++ b/samples/web/Liuliu.Demo.Web/Liuliu.Demo.Web.csproj @@ -14,15 +14,15 @@ - + - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/samples/web/Liuliu.Demo.WebApi/Liuliu.Demo.WebApi.csproj b/samples/web/Liuliu.Demo.WebApi/Liuliu.Demo.WebApi.csproj index 0f22b184..db3a74dc 100644 --- a/samples/web/Liuliu.Demo.WebApi/Liuliu.Demo.WebApi.csproj +++ b/samples/web/Liuliu.Demo.WebApi/Liuliu.Demo.WebApi.csproj @@ -21,30 +21,30 @@ - + - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/samples/wpf/OSharp.CodeGeneration/OSharp.CodeGeneration.csproj b/samples/wpf/OSharp.CodeGeneration/OSharp.CodeGeneration.csproj index be835b7c..0c8d05ed 100644 --- a/samples/wpf/OSharp.CodeGeneration/OSharp.CodeGeneration.csproj +++ b/samples/wpf/OSharp.CodeGeneration/OSharp.CodeGeneration.csproj @@ -9,8 +9,8 @@ - - + + diff --git a/samples/wpf/OSharp.CodeGenerator/OSharp.CodeGenerator.csproj b/samples/wpf/OSharp.CodeGenerator/OSharp.CodeGenerator.csproj index c4f677ef..330356ee 100644 --- a/samples/wpf/OSharp.CodeGenerator/OSharp.CodeGenerator.csproj +++ b/samples/wpf/OSharp.CodeGenerator/OSharp.CodeGenerator.csproj @@ -14,16 +14,16 @@ - - - - - + + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - + + diff --git a/src/OSharp.AspNetCore/OSharp.AspNetCore.csproj b/src/OSharp.AspNetCore/OSharp.AspNetCore.csproj index 80ba8515..a4261d78 100644 --- a/src/OSharp.AspNetCore/OSharp.AspNetCore.csproj +++ b/src/OSharp.AspNetCore/OSharp.AspNetCore.csproj @@ -16,19 +16,19 @@ - - - + + + - - - + + + - - - + + + diff --git a/src/OSharp.AutoMapper/OSharp.AutoMapper.csproj b/src/OSharp.AutoMapper/OSharp.AutoMapper.csproj index e9fd8129..9c93a4aa 100644 --- a/src/OSharp.AutoMapper/OSharp.AutoMapper.csproj +++ b/src/OSharp.AutoMapper/OSharp.AutoMapper.csproj @@ -12,7 +12,7 @@ - + diff --git a/src/OSharp.EntityFrameworkCore.MySql/OSharp.EntityFrameworkCore.MySql.csproj b/src/OSharp.EntityFrameworkCore.MySql/OSharp.EntityFrameworkCore.MySql.csproj index 667e8845..d4bad59b 100644 --- a/src/OSharp.EntityFrameworkCore.MySql/OSharp.EntityFrameworkCore.MySql.csproj +++ b/src/OSharp.EntityFrameworkCore.MySql/OSharp.EntityFrameworkCore.MySql.csproj @@ -12,13 +12,13 @@ - + - + - + diff --git a/src/OSharp.EntityFrameworkCore.Oracle/OSharp.EntityFrameworkCore.Oracle.csproj b/src/OSharp.EntityFrameworkCore.Oracle/OSharp.EntityFrameworkCore.Oracle.csproj index d30781ad..e2cca3b9 100644 --- a/src/OSharp.EntityFrameworkCore.Oracle/OSharp.EntityFrameworkCore.Oracle.csproj +++ b/src/OSharp.EntityFrameworkCore.Oracle/OSharp.EntityFrameworkCore.Oracle.csproj @@ -12,13 +12,13 @@ - + - + - + diff --git a/src/OSharp.EntityFrameworkCore.PostgreSql/OSharp.EntityFrameworkCore.PostgreSql.csproj b/src/OSharp.EntityFrameworkCore.PostgreSql/OSharp.EntityFrameworkCore.PostgreSql.csproj index 43d0d345..d6607d2e 100644 --- a/src/OSharp.EntityFrameworkCore.PostgreSql/OSharp.EntityFrameworkCore.PostgreSql.csproj +++ b/src/OSharp.EntityFrameworkCore.PostgreSql/OSharp.EntityFrameworkCore.PostgreSql.csproj @@ -12,13 +12,13 @@ - + - + - + diff --git a/src/OSharp.EntityFrameworkCore.SqlServer/OSharp.EntityFrameworkCore.SqlServer.csproj b/src/OSharp.EntityFrameworkCore.SqlServer/OSharp.EntityFrameworkCore.SqlServer.csproj index bd226cb1..2b483974 100644 --- a/src/OSharp.EntityFrameworkCore.SqlServer/OSharp.EntityFrameworkCore.SqlServer.csproj +++ b/src/OSharp.EntityFrameworkCore.SqlServer/OSharp.EntityFrameworkCore.SqlServer.csproj @@ -12,13 +12,13 @@ - + - + - + diff --git a/src/OSharp.EntityFrameworkCore.Sqlite/OSharp.EntityFrameworkCore.Sqlite.csproj b/src/OSharp.EntityFrameworkCore.Sqlite/OSharp.EntityFrameworkCore.Sqlite.csproj index 8f6a232a..5f82fb11 100644 --- a/src/OSharp.EntityFrameworkCore.Sqlite/OSharp.EntityFrameworkCore.Sqlite.csproj +++ b/src/OSharp.EntityFrameworkCore.Sqlite/OSharp.EntityFrameworkCore.Sqlite.csproj @@ -12,13 +12,13 @@ - + - + - + diff --git a/src/OSharp.EntityFrameworkCore/OSharp.EntityFrameworkCore.csproj b/src/OSharp.EntityFrameworkCore/OSharp.EntityFrameworkCore.csproj index bd65ae5c..d52fd582 100644 --- a/src/OSharp.EntityFrameworkCore/OSharp.EntityFrameworkCore.csproj +++ b/src/OSharp.EntityFrameworkCore/OSharp.EntityFrameworkCore.csproj @@ -13,22 +13,22 @@ - + - - - + + + - - - + + + - - - + + + diff --git a/src/OSharp.Exceptionless/OSharp.Exceptionless.csproj b/src/OSharp.Exceptionless/OSharp.Exceptionless.csproj index 7b1c1960..f82c139b 100644 --- a/src/OSharp.Exceptionless/OSharp.Exceptionless.csproj +++ b/src/OSharp.Exceptionless/OSharp.Exceptionless.csproj @@ -12,7 +12,7 @@ - + diff --git a/src/OSharp.Hangfire/OSharp.Hangfire.csproj b/src/OSharp.Hangfire/OSharp.Hangfire.csproj index 969c1756..7e473b73 100644 --- a/src/OSharp.Hangfire/OSharp.Hangfire.csproj +++ b/src/OSharp.Hangfire/OSharp.Hangfire.csproj @@ -11,8 +11,8 @@ - - + + diff --git a/src/OSharp.Hosting.Apis/OSharp.Hosting.Apis.csproj b/src/OSharp.Hosting.Apis/OSharp.Hosting.Apis.csproj index 32db2b19..293e8a43 100644 --- a/src/OSharp.Hosting.Apis/OSharp.Hosting.Apis.csproj +++ b/src/OSharp.Hosting.Apis/OSharp.Hosting.Apis.csproj @@ -17,16 +17,16 @@ - + - + - + - + diff --git a/src/OSharp.Hosting.Core/OSharp.Hosting.Core.csproj b/src/OSharp.Hosting.Core/OSharp.Hosting.Core.csproj index a902e2ef..e32422f7 100644 --- a/src/OSharp.Hosting.Core/OSharp.Hosting.Core.csproj +++ b/src/OSharp.Hosting.Core/OSharp.Hosting.Core.csproj @@ -12,8 +12,8 @@ - - + + diff --git a/src/OSharp.Identity/Authentication/AuthenticationPackBase.cs b/src/OSharp.Identity/Authentication/AuthenticationPackBase.cs index b12a5800..1cf2a7a4 100644 --- a/src/OSharp.Identity/Authentication/AuthenticationPackBase.cs +++ b/src/OSharp.Identity/Authentication/AuthenticationPackBase.cs @@ -187,13 +187,13 @@ protected virtual AuthenticationBuilder AddOAuth2(IServiceCollection services, A opts.ClientSecret = options.ClientSecret; }); break; - case "GitHub": - builder.AddGitHub(opts => - { - opts.ClientId = options.ClientId; - opts.ClientSecret = options.ClientSecret; - }); - break; + //case "GitHub": + // builder.AddGitHub(opts => + // { + // opts.ClientId = options.ClientId; + // opts.ClientSecret = options.ClientSecret; + // }); + // break; } } diff --git a/src/OSharp.Identity/OSharp.Identity.csproj b/src/OSharp.Identity/OSharp.Identity.csproj index d1a8ac58..3cec36c0 100644 --- a/src/OSharp.Identity/OSharp.Identity.csproj +++ b/src/OSharp.Identity/OSharp.Identity.csproj @@ -17,20 +17,17 @@ - - - - - + + - - + + - - + + diff --git a/src/OSharp.Log4Net/OSharp.Log4Net.csproj b/src/OSharp.Log4Net/OSharp.Log4Net.csproj index 8d84f50f..f113a07f 100644 --- a/src/OSharp.Log4Net/OSharp.Log4Net.csproj +++ b/src/OSharp.Log4Net/OSharp.Log4Net.csproj @@ -11,7 +11,7 @@ - + diff --git a/src/OSharp.MiniProfiler/OSharp.MiniProfiler.csproj b/src/OSharp.MiniProfiler/OSharp.MiniProfiler.csproj index f696a267..bc1eda93 100644 --- a/src/OSharp.MiniProfiler/OSharp.MiniProfiler.csproj +++ b/src/OSharp.MiniProfiler/OSharp.MiniProfiler.csproj @@ -11,8 +11,8 @@ - - + + diff --git a/src/OSharp.NLog/OSharp.NLog.csproj b/src/OSharp.NLog/OSharp.NLog.csproj index c9d3f7b5..e637e706 100644 --- a/src/OSharp.NLog/OSharp.NLog.csproj +++ b/src/OSharp.NLog/OSharp.NLog.csproj @@ -12,7 +12,7 @@ - + diff --git a/src/OSharp.Redis/OSharp.Redis.csproj b/src/OSharp.Redis/OSharp.Redis.csproj index d7b2d9a2..77d51c56 100644 --- a/src/OSharp.Redis/OSharp.Redis.csproj +++ b/src/OSharp.Redis/OSharp.Redis.csproj @@ -11,13 +11,13 @@ - + - + - + diff --git a/src/OSharp.Swagger/OSharp.Swagger.csproj b/src/OSharp.Swagger/OSharp.Swagger.csproj index 910342da..7ad52d3e 100644 --- a/src/OSharp.Swagger/OSharp.Swagger.csproj +++ b/src/OSharp.Swagger/OSharp.Swagger.csproj @@ -19,7 +19,7 @@ - + diff --git a/src/OSharp.Utils/OSharp.Utils.csproj b/src/OSharp.Utils/OSharp.Utils.csproj index 9f81d7d5..7dd268a4 100644 --- a/src/OSharp.Utils/OSharp.Utils.csproj +++ b/src/OSharp.Utils/OSharp.Utils.csproj @@ -15,27 +15,27 @@ - - + + - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + diff --git a/src/OSharp.Wpf/OSharp.Wpf.csproj b/src/OSharp.Wpf/OSharp.Wpf.csproj index 77ac944b..f0cde2a8 100644 --- a/src/OSharp.Wpf/OSharp.Wpf.csproj +++ b/src/OSharp.Wpf/OSharp.Wpf.csproj @@ -17,18 +17,18 @@ - - - + + + - + - + - + diff --git a/src/OSharp/Core/Systems/KeyValueStore.cs b/src/OSharp/Core/Systems/KeyValueStore.cs index 2c1de32f..2ab9b16e 100644 --- a/src/OSharp/Core/Systems/KeyValueStore.cs +++ b/src/OSharp/Core/Systems/KeyValueStore.cs @@ -46,7 +46,7 @@ public KeyValueStore(IServiceProvider provider) Type type = typeof(TSetting); foreach (PropertyInfo property in type.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(m => m.PropertyType == typeof(IKeyValue))) { - string key = ((KeyValue)property.GetValue(setting)).Key; + string key = ((KeyValue)property.GetValue(setting))?.Key; IKeyValue keyValue = GetByKey(key); if (keyValue != null) { @@ -78,7 +78,7 @@ public async Task SaveSetting(ISetting setting) public IKeyValue[] GetByRootKey(string rootKey) { string[] keys = GetKeys(rootKey); - return keys.Select(key => GetByKey(key)).Where(value => value != null).ToArray(); + return keys.Select(GetByKey).Where(value => value != null).ToArray(); } /// @@ -103,7 +103,7 @@ public IKeyValue GetByKey(string key) /// 检查谓语表达式 /// 更新的键值对信息编号 /// 键值对信息是否存在 - public Task CheckExists(Expression> predicate, Guid id = default(Guid)) + public Task CheckExists(Expression> predicate, Guid id = default) { return KeyValueRepository.CheckExistsAsync(predicate, id); } @@ -158,10 +158,10 @@ public async Task CreateOrUpdate(params IKeyValue[] dtos) } } - unitOfWork.Commit(); + await unitOfWork.CommitAsync(); - string[] cacheKeys = removeKeys.Select(m => GetCacheKey(m)).ToArray(); - await Cache.RemoveAsync(cacheKeys); + string[] cacheKeys = removeKeys.Select(GetCacheKey).ToArray(); + await Cache.RemoveAsync(default, cacheKeys); return OperationResult.Success; } @@ -194,10 +194,10 @@ public async Task Delete(params Guid[] ids) removeKeys.AddIf(pair.Key, count > 0); } - unitOfWork.Commit(); + await unitOfWork.CommitAsync(); - string[] cacheKeys = removeKeys.Select(m => GetCacheKey(m)).ToArray(); - await Cache.RemoveAsync(cacheKeys); + string[] cacheKeys = removeKeys.Select(GetCacheKey).ToArray(); + await Cache.RemoveAsync(default, cacheKeys); return OperationResult.Success; } @@ -224,4 +224,4 @@ private static string GetCacheKey(string key) { return $"Systems:KeyValues:{key}"; } -} \ No newline at end of file +} diff --git a/src/OSharp/OSharp.csproj b/src/OSharp/OSharp.csproj index 96064968..885342d7 100644 --- a/src/OSharp/OSharp.csproj +++ b/src/OSharp/OSharp.csproj @@ -13,7 +13,7 @@ - + From 24551ae37d054b785bb59f8c8cc82f0678a32e36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=AD=E6=98=8E=E9=94=8B?= Date: Fri, 6 Sep 2024 10:32:41 +0800 Subject: [PATCH 11/11] =?UTF-8?q?wip:=20=E6=B7=BB=E5=8A=A0=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=E5=8C=96=E6=8F=8F=E8=BF=B0=E7=89=B9=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/OSharpNS.nuspec | 22 +++++----- build/version.props | 2 +- .../LocalizedDescriptionAttribute.cs | 44 +++++++++++++++++++ src/OSharp.Utils/Reflection/TypeExtensions.cs | 10 ++++- 4 files changed, 64 insertions(+), 14 deletions(-) create mode 100644 src/OSharp.Utils/Localized/LocalizedDescriptionAttribute.cs diff --git a/build/OSharpNS.nuspec b/build/OSharpNS.nuspec index 1c70d539..6603c96f 100644 --- a/build/OSharpNS.nuspec +++ b/build/OSharpNS.nuspec @@ -2,7 +2,7 @@ OSharpNS - 8.0.8 + 8.0.9 OSharpFramework(.NET6.0/.NETCoreApp3.1) 柳柳软件(66soft.net) LiuliuSoft nnc @@ -16,18 +16,18 @@ osharp - - - - - + + + + + - - - - - + + + + + diff --git a/build/version.props b/build/version.props index 089b8fd0..258a15d4 100644 --- a/build/version.props +++ b/build/version.props @@ -1,7 +1,7 @@ 8.0 - 8 + 9 -preview. 529