diff --git a/Admin.NET/Admin.NET.Core/Service/Auth/SysAuthService.cs b/Admin.NET/Admin.NET.Core/Service/Auth/SysAuthService.cs index 72bccdb5..5162be65 100644 --- a/Admin.NET/Admin.NET.Core/Service/Auth/SysAuthService.cs +++ b/Admin.NET/Admin.NET.Core/Service/Auth/SysAuthService.cs @@ -621,7 +621,7 @@ public class SysAuthService : IDynamicApiController, ITransient if (dataScopes.All(u => u != DataScopeEnum.All) && dataScopes.Any(u => u == DataScopeEnum.DeptChild)) { var childOrg = db.Queryable().IgnoreTenant().ToTree(u => u.Children, u => u.Pid, user.OrgId); - if (childOrg == null || childOrg.Count > 0) return orgIds; + if (childOrg is not { Count: > 0 }) return orgIds; var queue = new Queue(childOrg); while (queue.Count > 0) { diff --git a/Admin.NET/Admin.NET.Core/Service/Menu/SysMenuService.cs b/Admin.NET/Admin.NET.Core/Service/Menu/SysMenuService.cs index 2c30b58f..86dbbadb 100644 --- a/Admin.NET/Admin.NET.Core/Service/Menu/SysMenuService.cs +++ b/Admin.NET/Admin.NET.Core/Service/Menu/SysMenuService.cs @@ -139,6 +139,9 @@ public class SysMenuService : IDynamicApiController, ITransient var newMenuId = await _sysMenuRep.InsertReturnSnowflakeIdAsync(sysMenu); // 将新增的菜单Id增加到当前账号角色菜单集合 await AddRoleMenuId(newMenuId); + + // 菜单按钮增加了权限标识,全部用户状态都需要更新 + if (input.Type == MenuTypeEnum.Btn) LazyHelper.GetService().Value.RemoveAll(); } /// @@ -150,31 +153,33 @@ public class SysMenuService : IDynamicApiController, ITransient [DisplayName("更新菜单")] public async Task UpdateMenu(UpdateMenuInput input) { - if (input.Id == input.Pid) - throw Oops.Oh(ErrorCodeEnum.D4008); + if (input.Id == input.Pid) throw Oops.Oh(ErrorCodeEnum.D4008); var isExist = input.Type != MenuTypeEnum.Btn ? await _sysMenuRep.IsAnyAsync(u => u.Title == input.Title && u.Type == input.Type && u.Pid == input.Pid && u.Id != input.Id) : await _sysMenuRep.IsAnyAsync(u => u.Pid == input.Pid && u.Permission == input.Permission && u.Id != input.Id); - if (isExist) - throw Oops.Oh(ErrorCodeEnum.D4000); + if (isExist) throw Oops.Oh(ErrorCodeEnum.D4000); if (!string.IsNullOrWhiteSpace(input.Name)) { - if (await _sysMenuRep.IsAnyAsync(u => u.Id != input.Id && u.Name == input.Name)) - throw Oops.Oh(ErrorCodeEnum.D4009); + if (await _sysMenuRep.IsAnyAsync(u => u.Id != input.Id && u.Name == input.Name)) throw Oops.Oh(ErrorCodeEnum.D4009); } if (input.Pid != 0) { - if (await _sysMenuRep.IsAnyAsync(u => u.Id == input.Pid && u.Type == MenuTypeEnum.Btn)) - throw Oops.Oh(ErrorCodeEnum.D4010); + if (await _sysMenuRep.IsAnyAsync(u => u.Id == input.Pid && u.Type == MenuTypeEnum.Btn)) throw Oops.Oh(ErrorCodeEnum.D4010); } // 校验菜单参数 var sysMenu = input.Adapt(); CheckMenuParam(sysMenu); + // 菜单按钮增加了权限标识,全部用户状态都需要更新 + if (input.Type == MenuTypeEnum.Btn || await _sysMenuRep.IsAnyAsync(u => u.Id == input.Id && u.Type == MenuTypeEnum.Btn)) + { + LazyHelper.GetService().Value.RemoveAll(); + } + await _sysMenuRep.AsUpdateable(sysMenu).ExecuteCommandAsync(); } @@ -198,6 +203,9 @@ public class SysMenuService : IDynamicApiController, ITransient // 级联删除用户收藏菜单 await _sysUserMenuService.DeleteMenuList(menuIdList); + + // 删除菜单全部数据都需要更新 + if (menuTreeList.Any(u => u.Type == MenuTypeEnum.Btn)) LazyHelper.GetService().Value.RemoveAll(); } /// @@ -206,11 +214,11 @@ public class SysMenuService : IDynamicApiController, ITransient /// /// [DisplayName("设置菜单状态")] - public async Task SetStatus(BaseStatusInput input) + public async Task SetStatus(BaseStatusInput input) { var menu = await _sysMenuRep.GetByIdAsync(input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002); - menu.Status = input.Status; - return await _sysMenuRep.AsUpdateable(menu).UpdateColumns(u => new { u.Status }).ExecuteCommandAsync(); + if (menu.Type == MenuTypeEnum.Btn) LazyHelper.GetService().Value.RemoveAll(); + return await _sysMenuRep.UpdateAsync(u => new() { Status = input.Status }, u => u.Id == input.Id); } /// @@ -261,8 +269,7 @@ public class SysMenuService : IDynamicApiController, ITransient private async Task AddRoleMenuId(long menuId) { if (_userManager.SuperAdmin) return; - - var roleIdList = await _sysUserRoleService.GetUserRoleIdList(_userManager.UserId); - await _sysRoleMenuService.GrantRoleMenu(new RoleMenuInput() { Id = roleIdList[0], MenuIdList = [menuId] }, false); + await _sysRoleMenuService.GrantRoleMenu(new RoleMenuInput { Id = _userManager.RoleIds[0], MenuIdList = [menuId] }, false); + LazyHelper.GetService().Value.RemoveByRoleId([_userManager.RoleIds[0]]); } } \ No newline at end of file diff --git a/Admin.NET/Admin.NET.Core/Service/Org/SysOrgService.cs b/Admin.NET/Admin.NET.Core/Service/Org/SysOrgService.cs index 31d0f051..ce3caebe 100644 --- a/Admin.NET/Admin.NET.Core/Service/Org/SysOrgService.cs +++ b/Admin.NET/Admin.NET.Core/Service/Org/SysOrgService.cs @@ -132,15 +132,15 @@ public class SysOrgService : IDynamicApiController, ITransient } // 删除与此父机构有关的用户机构缓存 + var userManager = LazyHelper.GetService().Value; if (input.Pid == 0) { - DeleteAllUserOrgCache(0, 0); + userManager.RemoveAll(); } else { var pOrg = await _sysOrgRep.GetByIdAsync(input.Pid); - if (pOrg != null) - DeleteAllUserOrgCache(pOrg.Id, pOrg.Pid); + if (pOrg != null) userManager.RemoveByOrgId([pOrg.Id, input.Pid]); } var newOrg = await _sysOrgRep.AsInsertable(input.Adapt()).ExecuteReturnEntityAsync(); @@ -158,6 +158,7 @@ public class SysOrgService : IDynamicApiController, ITransient DeleteAllUserOrgCache(0, 0); await _sysOrgRep.AsDeleteable().ExecuteCommandAsync(); await _sysOrgRep.AsInsertable(orgs).ExecuteCommandAsync(); + LazyHelper.GetService().Value.RemoveAll(); } /// @@ -170,42 +171,30 @@ public class SysOrgService : IDynamicApiController, ITransient [DisplayName("更新机构")] public async Task UpdateOrg(UpdateOrgInput input) { - if (!_userManager.SuperAdmin && input.Pid == 0) - throw Oops.Oh(ErrorCodeEnum.D2010); + if (!_userManager.SuperAdmin && input.Pid == 0) throw Oops.Oh(ErrorCodeEnum.D2010); + if (input.Id == input.Pid) throw Oops.Oh(ErrorCodeEnum.D2001); - if (input.Pid != 0) - { - //var pOrg = await _sysOrgRep.GetByIdAsync(input.Pid); - //_ = pOrg ?? throw Oops.Oh(ErrorCodeEnum.D2000); - - // 若父机构发生变化则清空用户机构缓存 - var sysOrg = await _sysOrgRep.GetByIdAsync(input.Id); - if (sysOrg != null && sysOrg.Pid != input.Pid) - { - // 删除与此机构、新父机构有关的用户机构缓存 - DeleteAllUserOrgCache(sysOrg.Id, input.Pid); - } - } - if (input.Id == input.Pid) - throw Oops.Oh(ErrorCodeEnum.D2001); - - if (await _sysOrgRep.IsAnyAsync(u => u.Name == input.Name && u.Code == input.Code && u.Id != input.Id)) - throw Oops.Oh(ErrorCodeEnum.D2002); + if (await _sysOrgRep.IsAnyAsync(u => u.Name == input.Name && u.Code == input.Code && u.Id != input.Id)) throw Oops.Oh(ErrorCodeEnum.D2002); // 父Id不能为自己的子节点 var childIdList = await GetChildIdListWithSelfById(input.Id); - if (childIdList.Contains(input.Pid)) - throw Oops.Oh(ErrorCodeEnum.D2001); + if (childIdList.Contains(input.Pid)) throw Oops.Oh(ErrorCodeEnum.D2001); // 是否有权限操作此机构 if (!_userManager.SuperAdmin) { - var orgIdList = await GetUserOrgIdList(); - if (orgIdList.Count < 1 || !orgIdList.Contains(input.Id)) - throw Oops.Oh(ErrorCodeEnum.D2003); + var orgIdList = _userManager.OrgIds; + if (orgIdList.Count < 1 || !orgIdList.Contains(input.Id)) throw Oops.Oh(ErrorCodeEnum.D2003); } await _sysOrgRep.AsUpdateable(input.Adapt()).IgnoreColumns(true).ExecuteCommandAsync(); + + if (input.Pid != 0) + { + // 若父机构发生变化则清空用户机构缓存 + var sysOrg = await _sysOrgRep.GetByIdAsync(input.Id); + if (sysOrg != null && sysOrg.Pid != input.Pid) LazyHelper.GetService().Value.RemoveByOrgId([input.Pid, input.Id]); + } } /// @@ -224,26 +213,22 @@ public class SysOrgService : IDynamicApiController, ITransient if (!_userManager.SuperAdmin) { var orgIdList = await GetUserOrgIdList(); - if (orgIdList.Count < 1 || !orgIdList.Contains(sysOrg.Id)) - throw Oops.Oh(ErrorCodeEnum.D2003); + if (orgIdList.Count < 1 || !orgIdList.Contains(sysOrg.Id)) throw Oops.Oh(ErrorCodeEnum.D2003); } // 若机构为租户默认机构禁止删除 var isTenantOrg = await _sysOrgRep.ChangeRepository>() .IsAnyAsync(u => u.OrgId == input.Id); - if (isTenantOrg) - throw Oops.Oh(ErrorCodeEnum.D2008); + if (isTenantOrg) throw Oops.Oh(ErrorCodeEnum.D2008); // 若机构有用户则禁止删除 var orgHasEmp = await _sysOrgRep.ChangeRepository>() .IsAnyAsync(u => u.OrgId == input.Id); - if (orgHasEmp) - throw Oops.Oh(ErrorCodeEnum.D2004); + if (orgHasEmp) throw Oops.Oh(ErrorCodeEnum.D2004); // 若扩展机构有用户则禁止删除 var hasExtOrgEmp = await _sysUserExtOrgService.HasUserOrg(sysOrg.Id); - if (hasExtOrgEmp) - throw Oops.Oh(ErrorCodeEnum.D2005); + if (hasExtOrgEmp) throw Oops.Oh(ErrorCodeEnum.D2005); // 若子机构有用户则禁止删除 var childOrgTreeList = await _sysOrgRep.AsQueryable().ToChildListAsync(u => u.Pid, input.Id, true); @@ -252,8 +237,7 @@ public class SysOrgService : IDynamicApiController, ITransient // 若子机构有用户则禁止删除 var cOrgHasEmp = await _sysOrgRep.ChangeRepository>() .IsAnyAsync(u => childOrgIdList.Contains(u.OrgId)); - if (cOrgHasEmp) - throw Oops.Oh(ErrorCodeEnum.D2007); + if (cOrgHasEmp) throw Oops.Oh(ErrorCodeEnum.D2007); // 删除与此机构、父机构有关的用户机构缓存 DeleteAllUserOrgCache(sysOrg.Id, sysOrg.Pid); @@ -266,6 +250,8 @@ public class SysOrgService : IDynamicApiController, ITransient // 级联删除用户机构数据 await _sysUserExtOrgService.DeleteUserExtOrgByOrgIdList(childOrgIdList); + + LazyHelper.GetService().Value.RemoveByOrgId([sysOrg.Pid, sysOrg.Id]); } /// @@ -343,26 +329,6 @@ public class SysOrgService : IDynamicApiController, ITransient return await GetUserOrgIdList(roleList, userId, userOrgId); } - /// - /// 判定用户是否有某角色权限 - /// - /// - /// 角色代码 - /// - [NonAction] - public async Task GetUserHasRole(long userId, SysRole role) - { - if (_userManager.SuperAdmin) return true; - - var userOrgId = _userManager.OrgId; - var roleList = await _sysUserRoleService.GetUserRoleList(userId); - if (roleList != null && roleList.Exists(r => r.Code == role.Code)) return true; - - roleList = [role]; - var orgIds = await GetUserOrgIdList(roleList, userId, userOrgId); - return orgIds.Contains(userOrgId); - } - /// /// 根据角色Id集合获取机构Id集合 /// @@ -400,8 +366,8 @@ public class SysOrgService : IDynamicApiController, ITransient }); } - // 缓存当前用户最大角色数据范围 - _sysCacheService.Set(CacheConst.KeyRoleMaxDataScope + userId, strongerDataScopeType, TimeSpan.FromDays(7)); + // // 缓存当前用户最大角色数据范围 + // _sysCacheService.Set(CacheConst.KeyRoleMaxDataScope + userId, strongerDataScopeType, TimeSpan.FromDays(7)); // 根据角色集合获取机构集合 var roleOrgIdList = await _sysRoleOrgService.GetRoleOrgIdList(customDataScopeRoleIdList); diff --git a/Admin.NET/Admin.NET.Core/Service/Pos/SysPosService.cs b/Admin.NET/Admin.NET.Core/Service/Pos/SysPosService.cs index 4c3e36bf..06883225 100644 --- a/Admin.NET/Admin.NET.Core/Service/Pos/SysPosService.cs +++ b/Admin.NET/Admin.NET.Core/Service/Pos/SysPosService.cs @@ -78,7 +78,6 @@ public class SysPosService : IDynamicApiController, ITransient public async Task AddPos(AddPosInput input) { if (await _sysPosRep.IsAnyAsync(u => u.Name == input.Name && u.Code == input.Code)) throw Oops.Oh(ErrorCodeEnum.D6000); - await _sysPosRep.InsertAsync(input.Adapt()); } @@ -97,6 +96,7 @@ public class SysPosService : IDynamicApiController, ITransient if (!_userManager.SuperAdmin && sysPos.CreateUserId != _userManager.UserId) throw Oops.Oh(ErrorCodeEnum.D6002); await _sysPosRep.AsUpdateable(input.Adapt()).IgnoreColumns(true).ExecuteCommandAsync(); + LazyHelper.GetService().Value.RemoveByPosId([input.Id]); } /// @@ -121,6 +121,7 @@ public class SysPosService : IDynamicApiController, ITransient if (hasExtPosEmp) throw Oops.Oh(ErrorCodeEnum.D6001); await _sysPosRep.DeleteByIdAsync(input.Id); + LazyHelper.GetService().Value.RemoveByPosId([input.Id]); } /// diff --git a/Admin.NET/Admin.NET.Core/Service/Role/Dto/RoleInput.cs b/Admin.NET/Admin.NET.Core/Service/Role/Dto/RoleInput.cs index fa091a6e..c38892c4 100644 --- a/Admin.NET/Admin.NET.Core/Service/Role/Dto/RoleInput.cs +++ b/Admin.NET/Admin.NET.Core/Service/Role/Dto/RoleInput.cs @@ -11,6 +11,7 @@ public class RoleInput : BaseIdInput /// /// 状态 /// + [Enum] public virtual StatusEnum Status { get; set; } } diff --git a/Admin.NET/Admin.NET.Core/Service/Role/SysRoleApiService.cs b/Admin.NET/Admin.NET.Core/Service/Role/SysRoleApiService.cs index f33af6a6..363edea6 100644 --- a/Admin.NET/Admin.NET.Core/Service/Role/SysRoleApiService.cs +++ b/Admin.NET/Admin.NET.Core/Service/Role/SysRoleApiService.cs @@ -36,6 +36,7 @@ public class SysRoleApiService : ITransient Route = u }).ToList(); await _sysRoleApiRep.InsertRangeAsync(roleApis); + LazyHelper.GetService().Value.RemoveByRoleId([input.Id]); } /// @@ -58,6 +59,7 @@ public class SysRoleApiService : ITransient public async Task DeleteRoleApiByRoleId(long roleId) { await _sysRoleApiRep.DeleteAsync(u => u.RoleId == roleId); + LazyHelper.GetService().Value.RemoveByRoleId([roleId]); } /// @@ -75,5 +77,6 @@ public class SysRoleApiService : ITransient u.RoleId = newRoleId; }); await _sysRoleApiRep.InsertRangeAsync(roleApiList); + LazyHelper.GetService().Value.RemoveByRoleId([newRoleId]); } } \ No newline at end of file diff --git a/Admin.NET/Admin.NET.Core/Service/Role/SysRoleMenuService.cs b/Admin.NET/Admin.NET.Core/Service/Role/SysRoleMenuService.cs index 76c2d54b..3fa278b3 100644 --- a/Admin.NET/Admin.NET.Core/Service/Role/SysRoleMenuService.cs +++ b/Admin.NET/Admin.NET.Core/Service/Role/SysRoleMenuService.cs @@ -47,6 +47,7 @@ public class SysRoleMenuService : ITransient MenuId = u }).ToList(); await _sysRoleMenuRep.InsertRangeAsync(menus); + LazyHelper.GetService().Value.RemoveByRoleId([input.Id]); } /// @@ -56,7 +57,9 @@ public class SysRoleMenuService : ITransient /// public async Task DeleteRoleMenuByMenuIdList(List menuIdList) { + var roleIds = await _sysRoleMenuRep.AsQueryable().Where(u => menuIdList.Contains(u.MenuId)).Select(u => u.RoleId).Distinct().ToListAsync(); await _sysRoleMenuRep.DeleteAsync(u => menuIdList.Contains(u.MenuId)); + LazyHelper.GetService().Value.RemoveByRoleId(roleIds); } /// @@ -67,6 +70,7 @@ public class SysRoleMenuService : ITransient public async Task DeleteRoleMenuByRoleId(long roleId) { await _sysRoleMenuRep.DeleteAsync(u => u.RoleId == roleId); + LazyHelper.GetService().Value.RemoveByRoleId([roleId]); } /// @@ -84,5 +88,6 @@ public class SysRoleMenuService : ITransient u.RoleId = newRoleId; }); await _sysRoleMenuRep.InsertRangeAsync(roleMenuList); + LazyHelper.GetService().Value.RemoveByRoleId([newRoleId]); } } \ No newline at end of file diff --git a/Admin.NET/Admin.NET.Core/Service/Role/SysRoleOrgService.cs b/Admin.NET/Admin.NET.Core/Service/Role/SysRoleOrgService.cs index 6da7b30e..f8ab0254 100644 --- a/Admin.NET/Admin.NET.Core/Service/Role/SysRoleOrgService.cs +++ b/Admin.NET/Admin.NET.Core/Service/Role/SysRoleOrgService.cs @@ -35,6 +35,7 @@ public class SysRoleOrgService : ITransient }).ToList(); await _sysRoleOrgRep.InsertRangeAsync(roleOrgList); } + LazyHelper.GetService().Value.RemoveByRoleId([input.Id]); } /// @@ -50,7 +51,7 @@ public class SysRoleOrgService : ITransient .Where(u => roleIdList.Contains(u.RoleId)) .Select(u => u.OrgId).ToListAsync(); } - else return new List(); + return new List(); } /// @@ -60,7 +61,9 @@ public class SysRoleOrgService : ITransient /// public async Task DeleteRoleOrgByOrgIdList(List orgIdList) { + var roleIdList = await _sysRoleOrgRep.AsQueryable().Where(u => orgIdList.Contains(u.OrgId)).Select(u => u.RoleId).Distinct().ToListAsync(); await _sysRoleOrgRep.DeleteAsync(u => orgIdList.Contains(u.OrgId)); + LazyHelper.GetService().Value.RemoveByRoleId(roleIdList); } /// @@ -71,6 +74,7 @@ public class SysRoleOrgService : ITransient public async Task DeleteRoleOrgByRoleId(long roleId) { await _sysRoleOrgRep.DeleteAsync(u => u.RoleId == roleId); + LazyHelper.GetService().Value.RemoveByRoleId([roleId]); } /// @@ -88,5 +92,8 @@ public class SysRoleOrgService : ITransient u.RoleId = newRoleId; }); await _sysRoleOrgRep.InsertRangeAsync(roleOrgList); + + // 清除关联用户状态 + LazyHelper.GetService().Value.RemoveByRoleId([newRoleId]); } } \ No newline at end of file diff --git a/Admin.NET/Admin.NET.Core/Service/Role/SysRoleService.cs b/Admin.NET/Admin.NET.Core/Service/Role/SysRoleService.cs index 726c8728..a255b791 100644 --- a/Admin.NET/Admin.NET.Core/Service/Role/SysRoleService.cs +++ b/Admin.NET/Admin.NET.Core/Service/Role/SysRoleService.cs @@ -126,6 +126,8 @@ public class SysRoleService : IDynamicApiController, ITransient await _sysRoleRep.AsUpdateable(input.Adapt()).IgnoreColumns(true) .IgnoreColumns(u => new { u.DataScope }).ExecuteCommandAsync(); + + LazyHelper.GetService().Value.RemoveByRoleId([input.Id]); } /// @@ -162,6 +164,9 @@ public class SysRoleService : IDynamicApiController, ITransient // 级联删除角色表格数据 await _sysRoleTableService.DeleteRolTableByRoleId(sysRole.Id); + + // 删除关联的用户状态 + LazyHelper.GetService().Value.RemoveByRoleId([input.Id]); } /// @@ -201,6 +206,9 @@ public class SysRoleService : IDynamicApiController, ITransient // 复制角色表格数据 await _sysRoleTableService.CopyRolTableByRoleId(input.Id, newRoleId); + + // 清除关联用户状态 + LazyHelper.GetService().Value.RemoveByRoleId([newRoleId]); } /// @@ -213,18 +221,8 @@ public class SysRoleService : IDynamicApiController, ITransient { if (input.MenuIdList == null || input.MenuIdList.Count < 1) return; - //// 将父节点为0的菜单排除,防止前端全选异常 - //var pMenuIds = await _sysRoleRep.ChangeRepository>().AsQueryable().Where(u => input.MenuIdList.Contains(u.Id) && u.Pid == 0).ToListAsync(u => u.Id); - //var menuIds = input.MenuIdList.Except(pMenuIds); // 差集 - //await _sysRoleMenuService.GrantRoleMenu(new RoleMenuInput() - //{ - // Id = input.Id, - // MenuIdList = menuIds.ToList() - //}); - await _sysRoleMenuService.GrantRoleMenu(input); - - await ClearUserApiCache(input.Id); + LazyHelper.GetService().Value.RemoveByRoleId([input.Id]); } /// @@ -269,7 +267,7 @@ public class SysRoleService : IDynamicApiController, ITransient var grantOrgIdList = input.OrgIdList; if (grantOrgIdList.Count > 0) { - var orgIdList = await _sysOrgService.GetUserOrgIdList(); + var orgIdList = _userManager.OrgIds; if (orgIdList.Count < 1) throw Oops.Oh(ErrorCodeEnum.D1016); if (!grantOrgIdList.All(u => orgIdList.Any(c => c == u))) throw Oops.Oh(ErrorCodeEnum.D1016); } @@ -285,6 +283,8 @@ public class SysRoleService : IDynamicApiController, ITransient role.DataScope = (DataScopeEnum)dataScope; await _sysRoleRep.AsUpdateable(role).UpdateColumns(u => new { u.DataScope }).ExecuteCommandAsync(); await _sysRoleOrgService.GrantRoleOrg(input); + + LazyHelper.GetService().Value.RemoveByRoleId([input.Id]); } /// @@ -296,8 +296,8 @@ public class SysRoleService : IDynamicApiController, ITransient [DisplayName("授权角色接口")] public async Task GrantApi(RoleApiInput input) { - await ClearUserApiCache(input.Id); await _sysRoleApiService.GrantRoleApi(input); + LazyHelper.GetService().Value.RemoveByRoleId([input.Id]); } /// @@ -310,6 +310,10 @@ public class SysRoleService : IDynamicApiController, ITransient public async Task GrantUser(RoleUserInput input) { await _sysUserRoleService.GrantRoleUser(input); + + // 清除关联用户状态 + var userManager = LazyHelper.GetService().Value; + foreach (var userId in input.UserIdList) userManager.Remove(userId); } /// @@ -322,10 +326,15 @@ public class SysRoleService : IDynamicApiController, ITransient { if (!Enum.IsDefined(input.Status)) throw Oops.Oh(ErrorCodeEnum.D3005); - return await _sysRoleRep.AsUpdateable() - .SetColumns(u => u.Status == input.Status) + var result = await _sysRoleRep.AsUpdateable() + .SetColumns(u => u.Status, input.Status) .Where(u => u.Id == input.Id) .ExecuteCommandAsync(); + + // 清除关联用户状态 + LazyHelper.GetService().Value.RemoveByRoleId([input.Id]); + + return result; } /// @@ -390,101 +399,8 @@ public class SysRoleService : IDynamicApiController, ITransient public async Task> GetRoleApiList([FromQuery] RoleInput input) { return await _sysRoleApiService.GetRoleApiList(new List { input.Id }); - - //var roleButtons = await GetRoleButtonList(new List { input.Id }); - //return roleApis.Union(roleButtons).ToList(); } - /// - /// 获取用户接口集合 - /// - /// - /// - [NonAction] - public async Task> GetUserApiList(long userId) - { - List apiList = []; - // 超管账号获取所有接口 - if (await _sysRoleRep.Context.Queryable().IgnoreTenant().AnyAsync(u => u.Id == userId && u.AccountType == AccountTypeEnum.SuperAdmin)) - { - // 获取所有接口 - var queue = new Queue(_sysCommonService.GetApiList()); - var item = queue.Dequeue(); - while (item != null) - { - if (item.Children is { Count: > 0 }) queue.EnqueueRange(item.Children); - else apiList.Add(item.Route); - item = queue.Count > 0 ? queue.Dequeue() : null; - } - var allButtonList = await GetButtonList(); - apiList.AddRange(allButtonList); - } - else - { - // 获取账号所有权限集 - var menuIdList = await _sysRoleMenuService.GetRoleMenuIdList(await _sysUserRoleService.GetUserRoleIdList(userId)); - apiList = await GetButtonList(menuIdList, false); - } - apiList = apiList.Distinct().ToList(); - apiList.Sort(); - return apiList; - } - - /// - /// 获取无权访问接口集合 🔖 - /// - [NonAction] - public async Task> GetUnAuthApiList(long userId) - { - if (await _sysRoleRep.Context.Queryable().IgnoreTenant().AnyAsync(u => u.Id == userId && u.AccountType == AccountTypeEnum.SuperAdmin)) return []; - - // 所有按钮权限集合 - var allButtonList = await GetButtonList(); - - // 当前账号所有角色集合 - var roleIds = await _sysUserRoleService.GetUserRoleIdList(userId); - - // 菜单中已有的权限集与当前用户的权限集差集,就是无权访问的权限集 - var menuIdList = await _sysRoleMenuService.GetRoleMenuIdList(roleIds); - var apiList = await GetButtonList(menuIdList, false); - var blackList = allButtonList.Except(apiList).ToList(); // 差集 - - // 角色接口黑名单集合 - blackList.AddRange(await _sysRoleApiService.GetRoleApiList(roleIds)); - blackList = blackList.Distinct().ToList(); - - blackList.Sort(); - return blackList; - } - - ///// - ///// 获取用户按钮权限集合 - ///// - ///// - //[NonAction] - //public async Task> GetUserButtonList() - //{ - // var menuIdList = new List(); - // if (!_userManager.SuperAdmin) - // { - // var roleIdList = await _sysUserRoleService.GetUserRoleIdList(_userManager.UserId); - // menuIdList = await _sysRoleMenuService.GetRoleMenuIdList(roleIdList); - // } - // return await GetButtonList(menuIdList); - //} - - ///// - ///// 获取角色按钮权限集合 - ///// - ///// - ///// - //[NonAction] - //public async Task> GetRoleButtonList(List roleIds) - //{ - // var menuIdList = await _sysRoleMenuService.GetRoleMenuIdList(roleIds); - // return await GetButtonList(menuIdList); - //} - /// /// 根据菜单Id集合获取按钮集合 /// @@ -500,16 +416,4 @@ public class SysRoleService : IDynamicApiController, ITransient .Select(u => u.Permission) .ToListAsync(); } - - /// - /// 删除与该角色相关的用户接口缓存 - /// - /// - /// - [NonAction] - public async Task ClearUserApiCache(long roleId) - { - var userIdList = await _sysUserRoleService.GetUserIdList(roleId); - foreach (var userId in userIdList) _sysCacheService.Remove(CacheConst.KeyUserManager + userId); - } } \ No newline at end of file diff --git a/Admin.NET/Admin.NET.Core/Service/Role/SysRoleTableService.cs b/Admin.NET/Admin.NET.Core/Service/Role/SysRoleTableService.cs index c663e5fb..f9a7d362 100644 --- a/Admin.NET/Admin.NET.Core/Service/Role/SysRoleTableService.cs +++ b/Admin.NET/Admin.NET.Core/Service/Role/SysRoleTableService.cs @@ -61,6 +61,9 @@ public class SysRoleTableService : ITransient } await _sysRoleTableRep.InsertRangeAsync(sysRoleTableList); + + // 清除关联用户状态 + LazyHelper.GetService().Value.RemoveByRoleId([input.Id]); } /// @@ -167,6 +170,9 @@ public class SysRoleTableService : ITransient public async Task DeleteRolTableByRoleId(long roleId) { await _sysRoleTableRep.DeleteAsync(u => u.RoleId == roleId); + + // 清除关联用户状态 + LazyHelper.GetService().Value.RemoveByRoleId([roleId]); } /// @@ -184,5 +190,8 @@ public class SysRoleTableService : ITransient u.RoleId = newRoleId; }); await _sysRoleTableRep.InsertRangeAsync(roleTableList); + + // 清除关联用户状态 + LazyHelper.GetService().Value.RemoveByRoleId([newRoleId]); } } \ No newline at end of file diff --git a/Admin.NET/Admin.NET.Core/Service/Tenant/SysTenantService.cs b/Admin.NET/Admin.NET.Core/Service/Tenant/SysTenantService.cs index fdc49d18..b09ab9ee 100644 --- a/Admin.NET/Admin.NET.Core/Service/Tenant/SysTenantService.cs +++ b/Admin.NET/Admin.NET.Core/Service/Tenant/SysTenantService.cs @@ -347,7 +347,8 @@ public class SysTenantService : IDynamicApiController, ITransient input.Id = adminRole.Id; // 重置租户管理员角色Id await _sysRoleMenuService.GrantRoleMenu(input); - await _sysRoleService.ClearUserApiCache(input.Id); + // 清除关联用户状态 + LazyHelper.GetService().Value.RemoveByRoleId([input.Id]); } /// diff --git a/Admin.NET/Admin.NET.Core/Service/User/SysUserRoleService.cs b/Admin.NET/Admin.NET.Core/Service/User/SysUserRoleService.cs index 943481b4..393ecd3c 100644 --- a/Admin.NET/Admin.NET.Core/Service/User/SysUserRoleService.cs +++ b/Admin.NET/Admin.NET.Core/Service/User/SysUserRoleService.cs @@ -57,12 +57,6 @@ public class SysUserRoleService : ITransient RoleId = input.Id }).ToList(); await _sysUserRoleRep.InsertRangeAsync(userRoles); - - // 清除缓存 - foreach (var userId in input.UserIdList) - { - _sysCacheService.Remove(CacheConst.KeyUserManager + userId); - } } /// @@ -72,18 +66,10 @@ public class SysUserRoleService : ITransient /// public async Task DeleteUserRoleByRoleId(long roleId) { - var userIdList = await _sysUserRoleRep.AsQueryable() - .Where(u => u.RoleId == roleId) - .Select(u => u.UserId) - .ToListAsync(); - - // 清除缓存 - foreach (var userId in userIdList) - { - _sysCacheService.Remove(CacheConst.KeyUserManager + userId); - } - await _sysUserRoleRep.DeleteAsync(u => u.RoleId == roleId); + + // 清除关联用户状态 + LazyHelper.GetService().Value.RemoveByRoleId([roleId]); } /// @@ -95,8 +81,8 @@ public class SysUserRoleService : ITransient { await _sysUserRoleRep.DeleteAsync(u => u.UserId == userId); - // 清除缓存 - _sysCacheService.Remove(CacheConst.KeyUserManager + userId); + // 清除关联用户状态 + LazyHelper.GetService().Value.Remove(userId); } /// diff --git a/Admin.NET/Admin.NET.Core/Service/User/SysUserService.cs b/Admin.NET/Admin.NET.Core/Service/User/SysUserService.cs index 9ac2d2db..91981149 100644 --- a/Admin.NET/Admin.NET.Core/Service/User/SysUserService.cs +++ b/Admin.NET/Admin.NET.Core/Service/User/SysUserService.cs @@ -61,7 +61,7 @@ public class SysUserService : IDynamicApiController, ITransient public virtual async Task> Page(PageUserInput input) { // 获取用户拥有的机构集合 - var userOrgIdList = await _sysOrgService.GetUserOrgIdList(); + var userOrgIdList = _userManager.OrgIds; List orgList = null; List extOrgUserIdList = null; if (input.OrgId > 0) // 指定机构查询时 @@ -149,12 +149,12 @@ public class SysUserService : IDynamicApiController, ITransient // 若账号的角色和组织架构发生变化,则强制下线账号进行权限更新 var user = await _sysUserRep.GetByIdAsync(input.Id); - var roleIds = await _sysUserRoleService.GetUserRoleIdList(input.Id); - if (input.OrgId != user.OrgId || !input.RoleIdList.OrderBy(u => u).SequenceEqual(roleIds.OrderBy(u => u))) - { - // 强制下线账号和失效Token - await OfflineAndExpireToken(user); - } + // var roleIds = await _sysUserRoleService.GetUserRoleIdList(input.Id); + // if (input.OrgId != user.OrgId || !input.RoleIdList.OrderBy(u => u).SequenceEqual(roleIds.OrderBy(u => u))) + // { + // // 强制下线账号和失效Token + // await OfflineAndExpireToken(user); + // } // 更新用户 await _sysUserRep.AsUpdateable(input.Adapt()).IgnoreColumns(true).IgnoreColumns(u => new { u.Password, u.Status, u.TenantId }).ExecuteCommandAsync(); @@ -183,7 +183,6 @@ public class SysUserService : IDynamicApiController, ITransient private async Task UpdateRoleAndExtOrg(AddUserInput input) { await GrantRole(new UserRoleInput { UserId = input.Id, RoleIdList = input.RoleIdList }); - await _sysUserExtOrgService.UpdateUserExtOrg(input.Id, input.ExtOrgIdList); } @@ -229,6 +228,8 @@ public class SysUserService : IDynamicApiController, ITransient // 发布删除用户事件 await _eventPublisher.PublishAsync(UserEventTypeEnum.Delete, input); + + LazyHelper.GetService().Value.Remove(user.Id); } /// @@ -249,8 +250,7 @@ public class SysUserService : IDynamicApiController, ITransient [DisplayName("更新用户基本信息")] public virtual async Task UpdateBaseInfo(SysUser user) { - var count = await _sysUserRep.AsUpdateable(user) - .IgnoreColumns(u => new { u.CreateTime, u.Account, u.Password, u.AccountType, u.OrgId, u.PosId }).ExecuteCommandAsync(); + var count = await _sysUserRep.AsUpdateable(user).IgnoreColumns(u => new { u.CreateTime, u.Account, u.Password, u.AccountType, u.OrgId, u.PosId }).ExecuteCommandAsync(); // 清除用户状态 _userManager.Remove(user.Id); return count; @@ -265,14 +265,12 @@ public class SysUserService : IDynamicApiController, ITransient [DisplayName("设置用户状态")] public virtual async Task SetStatus(BaseStatusInput input) { - if (_userManager.UserId == input.Id) - throw Oops.Oh(ErrorCodeEnum.D1026); + if (_userManager.UserId == input.Id) throw Oops.Oh(ErrorCodeEnum.D1026); var user = await _sysUserRep.GetByIdAsync(input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D0009); user.ValidateIsSuperAdminAccountType(ErrorCodeEnum.D1015); - if (!Enum.IsDefined(input.Status)) - throw Oops.Oh(ErrorCodeEnum.D3005); + if (!Enum.IsDefined(input.Status)) throw Oops.Oh(ErrorCodeEnum.D3005); if (input.Status != StatusEnum.Enable) { @@ -286,6 +284,8 @@ public class SysUserService : IDynamicApiController, ITransient // 发布设置用户状态事件 await _eventPublisher.PublishAsync(UserEventTypeEnum.SetStatus, input); + _userManager.Remove(user.Id); + return rows; } @@ -298,16 +298,14 @@ public class SysUserService : IDynamicApiController, ITransient [DisplayName("授权用户角色")] public async Task GrantRole(UserRoleInput input) { - var user = await _sysUserRep.GetByIdAsync(input.UserId) ?? throw Oops.Oh(ErrorCodeEnum.D0009); - // 若角色发生改变则进行更新 var roles = await _sysUserRoleService.GetUserRoleIdList(input.UserId); if (!roles.SequenceEqual(input.RoleIdList)) { // 更新用户角色 await _sysUserRoleService.GrantUserRole(input); - // 强制下线账号和失效Token - await OfflineAndExpireToken(user); + // // 强制下线账号和失效Token + // await OfflineAndExpireToken(user); // 发布更新用户角色事件 await _eventPublisher.PublishAsync(UserEventTypeEnum.UpdateRole, input); } @@ -418,10 +416,10 @@ public class SysUserService : IDynamicApiController, ITransient if (int.TryParse(sysConfig.Value, out int expirationTime) && expirationTime > 0) { var user = await _sysUserRep.GetByIdAsync(_userManager.UserId); - if (user.LastChangePasswordTime == null) - return false; - if ((DateTime.Now - user.LastChangePasswordTime.Value).Days > expirationTime) - return false; + + if (user.LastChangePasswordTime == null) return false; + + if ((DateTime.Now - user.LastChangePasswordTime.Value).Days > expirationTime) return false; } return true; } diff --git a/Admin.NET/Admin.NET.Core/Service/User/UserManager.cs b/Admin.NET/Admin.NET.Core/Service/User/UserManager.cs index dfaf6193..169c52de 100644 --- a/Admin.NET/Admin.NET.Core/Service/User/UserManager.cs +++ b/Admin.NET/Admin.NET.Core/Service/User/UserManager.cs @@ -189,6 +189,44 @@ public class UserManager( sysCacheService.Remove(CacheConst.KeyUserManager + userId); } + /// + /// 清除所有用户状态 + /// + public void RemoveAll() + { + sysCacheService.RemoveByPrefixKey(CacheConst.KeyUserManager); + } + + /// + /// 根据角色Id集删除用户状态 + /// + public void RemoveByRoleId(List roleIds) + { + if (roleIds == null || roleIds.Count == 0) return; + var list = GetUserManagerList(); + foreach (var id in roleIds) list?.Where(x => x.RoleIds.Contains(id)).ToList()?.ForEach(x => sysCacheService.Remove(CacheConst.KeyUserManager + x.UserId)); + } + + /// + /// 根据机构Id集删除用户状态 + /// + public void RemoveByOrgId(List orgIds) + { + if (orgIds == null || orgIds.Count == 0) return; + var list = GetUserManagerList(); + foreach (var id in orgIds) list?.Where(x => x.OrgIds.Contains(id)).ToList()?.ForEach(x => sysCacheService.Remove(CacheConst.KeyUserManager + x.UserId)); + } + + /// + /// 根据职位Id集删除用户状态 + /// + public void RemoveByPosId(List posIds) + { + if (posIds == null || posIds.Count == 0) return; + var list = GetUserManagerList(); + foreach (var id in posIds) list?.Where(x => x.PosIds.Contains(id)).ToList()?.ForEach(x => sysCacheService.Remove(CacheConst.KeyUserManager + x.UserId)); + } + /// /// 获取指定用户状态 /// @@ -197,6 +235,15 @@ public class UserManager( return sysCacheService.Get(CacheConst.KeyUserManager + userId); } + /// + /// 获取用户状态列表 + /// + public List GetUserManagerList() + { + var keys = sysCacheService.GetKeysByPrefixKey(CacheConst.KeyUserManager); + return keys?.Select(x => sysCacheService.Get(x)).ToList(); + } + /// /// 获取指定用户状态,如果不存在则刷新 ///