From ad6bd8f39beefc9e51886dab4d7321feb07b93dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=99=E5=AD=90?= <454313500@qq.com> Date: Sun, 21 May 2023 21:43:11 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=B7=BB=E5=8A=A0=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=9D=83=E9=99=90=E6=90=AD=E5=BB=BA=EF=BC=8C=E5=87=86=E5=A4=87?= =?UTF-8?q?=E5=AE=8C=E6=88=90=E6=95=B0=E6=8D=AE=E6=9D=83=E9=99=90=E8=BF=87?= =?UTF-8?q?=E6=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Ddd/Repositories/IRepository.cs | 1 + .../Repositories/SqlsugarRepository.cs | 2 +- .../Sqlsugar/SqlSugarDbContext.cs | 124 ++++++++++-------- .../Sqlsugar/SqlsugarExtensions.cs | 35 ++++- .../Yi.Framework.Infrastructure/Startup.cs | 7 - .../Rbac/Services/IDeptService.cs | 2 +- .../Rbac/Services/Impl/DeptService.cs | 2 +- .../Rbac/Services/Impl/RoleService.cs | 31 ++++- .../Rbac/Services/Impl/UserService.cs | 2 +- .../Rbac/Dtos/Role/UpdateDataScpoceInput.cs | 18 +++ .../Yi.Furion.Sqlsugar.Core/Startup.cs | 7 +- .../Yi.Furion.Sqlsugar.Core/YiDbContext.cs | 42 ++++++ Yi.RuoYi.Vue3/src/api/system/dept.js | 2 +- Yi.RuoYi.Vue3/src/api/system/role.js | 2 +- Yi.RuoYi.Vue3/src/views/system/role/index.vue | 12 +- 15 files changed, 206 insertions(+), 83 deletions(-) create mode 100644 Yi.Furion.Net6/Yi.Furion.Core/Rbac/Dtos/Role/UpdateDataScpoceInput.cs create mode 100644 Yi.Furion.Net6/Yi.Furion.Sqlsugar.Core/YiDbContext.cs diff --git a/Yi.Furion.Net6/Yi.Framework.Infrastructure/Ddd/Repositories/IRepository.cs b/Yi.Furion.Net6/Yi.Framework.Infrastructure/Ddd/Repositories/IRepository.cs index 032b920a..80debb85 100644 --- a/Yi.Furion.Net6/Yi.Framework.Infrastructure/Ddd/Repositories/IRepository.cs +++ b/Yi.Furion.Net6/Yi.Framework.Infrastructure/Ddd/Repositories/IRepository.cs @@ -11,6 +11,7 @@ namespace Yi.Framework.Infrastructure.Ddd.Repositories /// 注释一下,严格意义这里应该protected,但是我认为 简易程度 与 耦合程度 中是需要进行衡量的 /// ISugarQueryable _DbQueryable { get; } + ISqlSugarClient _Db { get; } //单查 Task GetByIdAsync(dynamic id); Task GetSingleAsync(Expression> whereExpression); diff --git a/Yi.Furion.Net6/Yi.Framework.Infrastructure/Sqlsugar/Repositories/SqlsugarRepository.cs b/Yi.Furion.Net6/Yi.Framework.Infrastructure/Sqlsugar/Repositories/SqlsugarRepository.cs index 7e2c704f..c9a32b0c 100644 --- a/Yi.Furion.Net6/Yi.Framework.Infrastructure/Sqlsugar/Repositories/SqlsugarRepository.cs +++ b/Yi.Furion.Net6/Yi.Framework.Infrastructure/Sqlsugar/Repositories/SqlsugarRepository.cs @@ -19,7 +19,7 @@ namespace Yi.Framework.Infrastructure.Sqlsugar.Repositories /// public ISugarQueryable _DbQueryable => AsQueryable(); - protected ISqlSugarClient _Db { get { return Context; } set { } } + public ISqlSugarClient _Db { get { return Context; }} public async Task> GetPageListAsync(Expression> whereExpression, IPagedAndSortedResultRequestDto page) { diff --git a/Yi.Furion.Net6/Yi.Framework.Infrastructure/Sqlsugar/SqlSugarDbContext.cs b/Yi.Furion.Net6/Yi.Framework.Infrastructure/Sqlsugar/SqlSugarDbContext.cs index 47ad22a3..880ef42c 100644 --- a/Yi.Furion.Net6/Yi.Framework.Infrastructure/Sqlsugar/SqlSugarDbContext.cs +++ b/Yi.Furion.Net6/Yi.Framework.Infrastructure/Sqlsugar/SqlSugarDbContext.cs @@ -13,7 +13,7 @@ using Yi.Framework.Infrastructure.Data.Entities; namespace Yi.Framework.Infrastructure.Sqlsugar { - public class SqlSugarDbContext + public class SqlSugarDbContext { /// /// SqlSugar 客户端 @@ -81,69 +81,79 @@ namespace Yi.Framework.Infrastructure.Sqlsugar }, db => { - - db.Aop.DataExecuting = (oldValue, entityInfo) => - { - - switch (entityInfo.OperationType) - { - case DataFilterType.UpdateByObject: - - if (entityInfo.PropertyName.Equals(nameof(IAuditedObject.LastModificationTime))) - { - entityInfo.SetValue(DateTime.Now); - } - if (entityInfo.PropertyName.Equals(nameof(IAuditedObject.LastModifierId))) - { - if (_currentUser != null) - { - entityInfo.SetValue(_currentUser.Id); - } - } - break; - case DataFilterType.InsertByObject: - if (entityInfo.PropertyName.Equals(nameof(IAuditedObject.CreationTime))) - { - entityInfo.SetValue(DateTime.Now); - } - if (entityInfo.PropertyName.Equals(nameof(IAuditedObject.CreatorId))) - { - if (_currentUser != null) - { - entityInfo.SetValue(_currentUser.Id); - } - } - - //插入时,需要租户id,先预留 - if (entityInfo.PropertyName.Equals(nameof(IMultiTenant.TenantId))) - { - //if (this.CurrentTenant is not null) - //{ - // entityInfo.SetValue(this.CurrentTenant.Id); - //} - } - break; - } - }; - db.Aop.OnLogExecuting = (s, p) => - { - StringBuilder sb = new StringBuilder(); - //sb.Append("执行SQL:" + s.ToString()); - //foreach (var i in p) - //{ - // sb.Append($"\r\n参数:{i.ParameterName},参数值:{i.Value}"); - //} - sb.Append($"\r\n 完整SQL:{UtilMethods.GetSqlString(DbType.MySql, s, p)}"); - logger?.LogDebug(sb.ToString()); - }; + db.Aop.DataExecuting = DataExecuting; + db.Aop.OnLogExecuting = OnLogExecuting; //扩展 OnSqlSugarClientConfig(db); }); } - //上下文对象扩展 + /// + /// 上下文对象扩展 + /// + /// protected virtual void OnSqlSugarClientConfig(ISqlSugarClient sqlSugarClient) { } + + /// + /// 数据 + /// + /// + /// + protected virtual void DataExecuting(object oldValue, DataFilterModel entityInfo) + { + switch (entityInfo.OperationType) + { + case DataFilterType.UpdateByObject: + + if (entityInfo.PropertyName.Equals(nameof(IAuditedObject.LastModificationTime))) + { + entityInfo.SetValue(DateTime.Now); + } + if (entityInfo.PropertyName.Equals(nameof(IAuditedObject.LastModifierId))) + { + if (_currentUser != null) + { + entityInfo.SetValue(_currentUser.Id); + } + } + break; + case DataFilterType.InsertByObject: + if (entityInfo.PropertyName.Equals(nameof(IAuditedObject.CreationTime))) + { + entityInfo.SetValue(DateTime.Now); + } + if (entityInfo.PropertyName.Equals(nameof(IAuditedObject.CreatorId))) + { + if (_currentUser != null) + { + entityInfo.SetValue(_currentUser.Id); + } + } + + //插入时,需要租户id,先预留 + if (entityInfo.PropertyName.Equals(nameof(IMultiTenant.TenantId))) + { + //if (this.CurrentTenant is not null) + //{ + // entityInfo.SetValue(this.CurrentTenant.Id); + //} + } + break; + } + } + + /// + /// 日志 + /// + /// + /// + protected virtual void OnLogExecuting(string s, SugarParameter[] p) + { + StringBuilder sb = new StringBuilder(); + sb.Append($"\r\n 完整SQL:{UtilMethods.GetSqlString(DbType.MySql, s, p)}"); + _logger?.LogDebug(sb.ToString()); + } } } diff --git a/Yi.Furion.Net6/Yi.Framework.Infrastructure/Sqlsugar/SqlsugarExtensions.cs b/Yi.Furion.Net6/Yi.Framework.Infrastructure/Sqlsugar/SqlsugarExtensions.cs index 41742812..2abd7f19 100644 --- a/Yi.Furion.Net6/Yi.Framework.Infrastructure/Sqlsugar/SqlsugarExtensions.cs +++ b/Yi.Furion.Net6/Yi.Framework.Infrastructure/Sqlsugar/SqlsugarExtensions.cs @@ -1,4 +1,6 @@ -using Microsoft.Extensions.DependencyInjection; +using Furion; +using Microsoft.Extensions.DependencyInjection; +using Yi.Framework.Infrastructure.Sqlsugar.Uow; namespace Yi.Framework.Infrastructure.Sqlsugar { @@ -7,13 +9,42 @@ namespace Yi.Framework.Infrastructure.Sqlsugar /// public static class SqlsugarExtensions { - //使用上下文对象 + /// + /// 使用默认上下文 + /// + /// public static void AddDbSqlsugarContextServer(this IServiceCollection services) { + services.AddDbSqlsugarOption(); + + services.AddSingleton(x => x.GetRequiredService().SqlSugarClient); services.AddSingleton(); + + } + /// + /// 自定义上下文 + /// + /// + /// + public static void AddDbSqlsugarContextServer(this IServiceCollection services) where DbContext : SqlSugarDbContext + { + services.AddDbSqlsugarOption(); + + services.AddSingleton(x => x.GetRequiredService().SqlSugarClient); + services.AddSingleton(); + + } + + public static void AddDbSqlsugarOption(this IServiceCollection services) + { + services.Configure(App.Configuration.GetSection("DbConnOptions")); + + services.AddUnitOfWork(); + + } } } diff --git a/Yi.Furion.Net6/Yi.Framework.Infrastructure/Startup.cs b/Yi.Furion.Net6/Yi.Framework.Infrastructure/Startup.cs index 1e0a0076..4114c717 100644 --- a/Yi.Furion.Net6/Yi.Framework.Infrastructure/Startup.cs +++ b/Yi.Furion.Net6/Yi.Framework.Infrastructure/Startup.cs @@ -21,13 +21,6 @@ public class Startup : AppStartup public void ConfigureServices(IServiceCollection services) { services.AddCurrentUserServer(); - - services.Configure(App.Configuration.GetSection("DbConnOptions")); - - services.AddDbSqlsugarContextServer(); - - services.AddUnitOfWork(); - services.AddTransient(); diff --git a/Yi.Furion.Net6/Yi.Furion.Application/Rbac/Services/IDeptService.cs b/Yi.Furion.Net6/Yi.Furion.Application/Rbac/Services/IDeptService.cs index 8f864337..496848c8 100644 --- a/Yi.Furion.Net6/Yi.Furion.Application/Rbac/Services/IDeptService.cs +++ b/Yi.Furion.Net6/Yi.Furion.Application/Rbac/Services/IDeptService.cs @@ -8,6 +8,6 @@ namespace Yi.Furion.Application.Rbac.Services /// public interface IDeptService : ICrudAppService { - Task> GetChiIds(long deptId); + Task> GetChildListAsync(long deptId); } } diff --git a/Yi.Furion.Net6/Yi.Furion.Application/Rbac/Services/Impl/DeptService.cs b/Yi.Furion.Net6/Yi.Furion.Application/Rbac/Services/Impl/DeptService.cs index ece5b0c2..003785f8 100644 --- a/Yi.Furion.Net6/Yi.Furion.Application/Rbac/Services/Impl/DeptService.cs +++ b/Yi.Furion.Net6/Yi.Furion.Application/Rbac/Services/Impl/DeptService.cs @@ -13,7 +13,7 @@ namespace Yi.Furion.Application.Rbac.Services.Impl IDeptService, ITransient, IDynamicApiController { [NonAction] - public async Task> GetChiIds(long deptId) + public async Task> GetChildListAsync(long deptId) { var entities= await _DbQueryable.ToChildListAsync(x=>x.ParentId,deptId); return entities.Select(x => x.Id).ToList(); diff --git a/Yi.Furion.Net6/Yi.Furion.Application/Rbac/Services/Impl/RoleService.cs b/Yi.Furion.Net6/Yi.Furion.Application/Rbac/Services/Impl/RoleService.cs index 0b6353ba..76d1e6e4 100644 --- a/Yi.Furion.Net6/Yi.Furion.Application/Rbac/Services/Impl/RoleService.cs +++ b/Yi.Furion.Net6/Yi.Furion.Application/Rbac/Services/Impl/RoleService.cs @@ -1,9 +1,13 @@ +using Furion.DatabaseAccessor; using SqlSugar; using Yi.Framework.Infrastructure.Ddd.Dtos; +using Yi.Framework.Infrastructure.Ddd.Repositories; using Yi.Framework.Infrastructure.Ddd.Services; +using Yi.Framework.Infrastructure.Helper; using Yi.Furion.Application.Rbac.Domain; using Yi.Furion.Core.Rbac.Dtos.Role; using Yi.Furion.Core.Rbac.Entities; +using Yi.Furion.Core.Rbac.Enums; namespace Yi.Furion.Application.Rbac.Services.Impl { @@ -13,13 +17,34 @@ namespace Yi.Furion.Application.Rbac.Services.Impl public class RoleService : CrudAppService, IRoleService, ITransient, IDynamicApiController { - public RoleService(RoleManager roleManager) => - _roleManager = - roleManager; + public RoleService(RoleManager roleManager, IRepository roleDeptRepository) => + (_roleManager, _roleDeptRepository) = + (roleManager, roleDeptRepository); private RoleManager _roleManager { get; set; } + private IRepository _roleDeptRepository; + public async Task> GetDeptIdsAsync(long roleId) + { + var entities = await _roleDeptRepository.GetListAsync(x => x.RoleId == roleId); + return entities.Select(x => x.DeptId).ToList(); + + } + + [UnitOfWork] + public async Task UpdateDataScpoceAsync(UpdateDataScpoceInput input) + { + //只有自定义的需要特殊处理 + if (input.DataScope == DataScopeEnum.CUSTOM) + { + await _roleDeptRepository.DeleteAsync(x => x.RoleId == input.RoleId); + var insertEntities = input.DeptIds.Select(x => new RoleDeptEntity {Id=SnowflakeHelper.NextId, DeptId = x, RoleId = input.RoleId }).ToList(); + await _roleDeptRepository.InsertRangeAsync(insertEntities); + } + await _repository._Db.Updateable(new RoleEntity() { Id = input.RoleId, DataScope = input.DataScope }).UpdateColumns(x => x.DataScope).ExecuteCommandAsync(); + + } public override async Task> GetListAsync(RoleGetListInputVo input) { diff --git a/Yi.Furion.Net6/Yi.Furion.Application/Rbac/Services/Impl/UserService.cs b/Yi.Furion.Net6/Yi.Furion.Application/Rbac/Services/Impl/UserService.cs index d0701639..ca88a437 100644 --- a/Yi.Furion.Net6/Yi.Furion.Application/Rbac/Services/Impl/UserService.cs +++ b/Yi.Furion.Net6/Yi.Furion.Application/Rbac/Services/Impl/UserService.cs @@ -45,7 +45,7 @@ namespace Yi.Furion.Application.Rbac.Services.Impl List deptIds = null; if (input.DeptId is not null) { - deptIds= await _deptService.GetChiIds(input.DeptId ?? 0); + deptIds= await _deptService.GetChildListAsync(input.DeptId ?? 0); } diff --git a/Yi.Furion.Net6/Yi.Furion.Core/Rbac/Dtos/Role/UpdateDataScpoceInput.cs b/Yi.Furion.Net6/Yi.Furion.Core/Rbac/Dtos/Role/UpdateDataScpoceInput.cs new file mode 100644 index 00000000..595dcf0c --- /dev/null +++ b/Yi.Furion.Net6/Yi.Furion.Core/Rbac/Dtos/Role/UpdateDataScpoceInput.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Yi.Furion.Core.Rbac.Enums; + +namespace Yi.Furion.Core.Rbac.Dtos.Role +{ + public class UpdateDataScpoceInput + { + public long RoleId { get; set; } + + public List? DeptIds { get; set;} + + public DataScopeEnum DataScope { get; set; } + } +} diff --git a/Yi.Furion.Net6/Yi.Furion.Sqlsugar.Core/Startup.cs b/Yi.Furion.Net6/Yi.Furion.Sqlsugar.Core/Startup.cs index 88b00a31..d79869ff 100644 --- a/Yi.Furion.Net6/Yi.Furion.Sqlsugar.Core/Startup.cs +++ b/Yi.Furion.Net6/Yi.Furion.Sqlsugar.Core/Startup.cs @@ -1,5 +1,6 @@ using Furion; using Microsoft.Extensions.DependencyInjection; +using Yi.Framework.Infrastructure.Sqlsugar; namespace Yi.Furion.Sqlsugar.Core; @@ -7,10 +8,6 @@ public class Startup : AppStartup { public void ConfigureServices(IServiceCollection services) { - //services.AddDatabaseAccessor(options => - //{ - // options.AddDbPool(); - //}, "Yi.Furion.Rbac.Database.Migrations"); - System.Console.WriteLine(); + services.AddDbSqlsugarContextServer(); } } diff --git a/Yi.Furion.Net6/Yi.Furion.Sqlsugar.Core/YiDbContext.cs b/Yi.Furion.Net6/Yi.Furion.Sqlsugar.Core/YiDbContext.cs new file mode 100644 index 00000000..ee085d8a --- /dev/null +++ b/Yi.Furion.Net6/Yi.Furion.Sqlsugar.Core/YiDbContext.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using SqlSugar; +using Yi.Framework.Infrastructure.CurrentUsers; +using Yi.Framework.Infrastructure.Sqlsugar; + +namespace Yi.Furion.Sqlsugar.Core +{ + /// + /// 使用自定义上下文 + /// + public class YiDbContext : SqlSugarDbContext + { + public YiDbContext(IOptions options, ICurrentUser currentUser, ILogger logger) : base(options, currentUser, logger) + { + } + + + //进行Aop数据权限过滤 + protected override void OnSqlSugarClientConfig(ISqlSugarClient sqlSugarClient) + { + //这里Aop进行数据权限过滤 + var userId = _currentUser.Id; + if (userId == 0) return; + _logger.LogInformation($"用户【{userId}】访问Aop"); + + /* + * 这里数据权限,步骤: + * 1:获取用户id + * 2:通过用户id,获取该用户的全部角色 + * 3:便利每一个角色获取全部的数据权限 + * 4:会涉及部门表的筛选,所以还需要获取用户的所在部门,如果没有部门,那就是过滤到只看自己 + * 5:可直接使用DB进行查询部门即可 + */ + } + } +} diff --git a/Yi.RuoYi.Vue3/src/api/system/dept.js b/Yi.RuoYi.Vue3/src/api/system/dept.js index 7a2721cc..212f8ea5 100644 --- a/Yi.RuoYi.Vue3/src/api/system/dept.js +++ b/Yi.RuoYi.Vue3/src/api/system/dept.js @@ -55,7 +55,7 @@ export function delDept(deptId) { // 根据角色ID查询菜单下拉树结构 export function roleDeptTreeselect(roleId) { return request({ - url: '/dept/role-id/' + roleId, + url: '/role/dept-ids/' + roleId, method: 'get' }) } \ No newline at end of file diff --git a/Yi.RuoYi.Vue3/src/api/system/role.js b/Yi.RuoYi.Vue3/src/api/system/role.js index 88b9bbf2..3b90a13d 100644 --- a/Yi.RuoYi.Vue3/src/api/system/role.js +++ b/Yi.RuoYi.Vue3/src/api/system/role.js @@ -40,7 +40,7 @@ export function updateRole(data) { // 角色数据权限 export function dataScope(data) { return request({ - url: '/role/UpdateDataScpoce', + url: '/role/data-scpoce', method: 'put', data: data }) diff --git a/Yi.RuoYi.Vue3/src/views/system/role/index.vue b/Yi.RuoYi.Vue3/src/views/system/role/index.vue index 8033c59f..5eff5d7a 100644 --- a/Yi.RuoYi.Vue3/src/views/system/role/index.vue +++ b/Yi.RuoYi.Vue3/src/views/system/role/index.vue @@ -426,7 +426,7 @@ function handleAdd() { function getDeptTree(roleId) { return listDept().then((response) => { const selectList = []; - response.data.forEach((res) => { + response.data.items.forEach((res) => { selectList.push({ id: res.id, label: res.deptName, @@ -439,7 +439,7 @@ function getDeptTree(roleId) { let deptIds = []; roleDeptTreeselect(roleId).then((response) => { - deptIds = response.data.map((x) => x.id); + deptIds = response.data; // nextTick(() => { if (deptRef.value) { deptRef.value.setCheckedKeys(deptIds); @@ -534,7 +534,13 @@ function handleDataScope(row) { function submitDataScope() { if (form.value.id != undefined) { form.value.deptIds = getDeptAllCheckedKeys(); - dataScope(form.value).then((response) => { +const data={ + roleId:form.value.id, + deptIds:form.value.deptIds, + dataScope:form.value.dataScope +} +console.log(data) + dataScope(data).then((response) => { proxy.$modal.msgSuccess("修改成功"); openDataScope.value = false; getList();