fix: 租户授权菜单时没有清理缓存问题

This commit is contained in:
许俊杰 2025-03-23 22:17:28 +08:00
parent 16a49a8540
commit 09b905e44d
3 changed files with 60 additions and 48 deletions

View File

@ -51,7 +51,7 @@ public class SysRoleService : IDynamicApiController, ITransient
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[DisplayName("获取角色分页列表")]
[DisplayName("获取角色分页列表")]
public async Task<SqlSugarPagedList<PageRoleOutput>> Page(PageRoleInput input)
{
// 当前用户已拥有的角色集合
@ -76,7 +76,7 @@ public class SysRoleService : IDynamicApiController, ITransient
/// 获取角色列表 🔖
/// </summary>
/// <returns></returns>
[DisplayName("获取角色列表")]
[DisplayName("获取角色列表")]
public async Task<List<RoleOutput>> GetList()
{
// 当前用户已拥有的角色集合
@ -95,7 +95,7 @@ public class SysRoleService : IDynamicApiController, ITransient
/// <param name="input"></param>
/// <returns></returns>
[ApiDescriptionSettings(Name = "Add"), HttpPost]
[DisplayName("增加角色")]
[DisplayName("增加角色")]
public async Task AddRole(AddRoleInput input)
{
if (await _sysRoleRep.IsAnyAsync(u => u.Name == input.Name && u.Code == input.Code))
@ -104,8 +104,10 @@ public class SysRoleService : IDynamicApiController, ITransient
var roleId = await _sysRoleRep.AsInsertable(input.Adapt<SysRole>()).ExecuteReturnSnowflakeIdAsync();
// 授权角色基础菜单集合
var menuIdList = new List<long> { 1300000000111,1300000000121, // 工作台
1310000000161,1310000000162,1310000000163,1310000000164,1310000000165, // 个人中心
var menuIdList = new List<long>
{
1300000000111, 1300000000121, // 工作台
1310000000161, 1310000000162, 1310000000163, 1310000000164, 1310000000165, // 个人中心
};
await _sysRoleMenuService.GrantRoleMenu(new RoleMenuInput() { Id = roleId, MenuIdList = menuIdList });
}
@ -116,7 +118,7 @@ public class SysRoleService : IDynamicApiController, ITransient
/// <param name="input"></param>
/// <returns></returns>
[ApiDescriptionSettings(Name = "Update"), HttpPost]
[DisplayName("更新角色")]
[DisplayName("更新角色")]
public async Task UpdateRole(UpdateRoleInput input)
{
if (await _sysRoleRep.IsAnyAsync(u => u.Name == input.Name && u.Code == input.Code && u.Id != input.Id))
@ -133,7 +135,7 @@ public class SysRoleService : IDynamicApiController, ITransient
/// <returns></returns>
[UnitOfWork]
[ApiDescriptionSettings(Name = "Delete"), HttpPost]
[DisplayName("删除角色")]
[DisplayName("删除角色")]
public async Task DeleteRole(DeleteRoleInput input)
{
// 禁止删除系统管理员角色
@ -164,7 +166,7 @@ public class SysRoleService : IDynamicApiController, ITransient
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[DisplayName("授权角色菜单")]
[DisplayName("授权角色菜单")]
public async Task GrantMenu(RoleMenuInput input)
{
if (input.MenuIdList == null || input.MenuIdList.Count < 1) return;
@ -189,7 +191,7 @@ public class SysRoleService : IDynamicApiController, ITransient
/// <param name="input"></param>
/// <returns></returns>
[UnitOfWork]
[DisplayName("授权角色表格")]
[DisplayName("授权角色表格")]
public async Task GrantRoleTable(RoleTableInput input)
{
await _sysRoleTableService.GrantRoleTable(input);
@ -201,7 +203,7 @@ public class SysRoleService : IDynamicApiController, ITransient
/// <param name="input"></param>
/// <returns></returns>
[UnitOfWork]
[DisplayName("授权角色数据范围")]
[DisplayName("授权角色数据范围")]
public async Task GrantDataScope(RoleOrgInput input)
{
// 删除与该角色相关的用户机构缓存
@ -229,6 +231,7 @@ public class SysRoleService : IDynamicApiController, ITransient
if (orgIdList.Count < 1) throw Oops.Oh(ErrorCodeEnum.D1016);
if (!grantOrgIdList.All(u => orgIdList.Any(c => c == u))) throw Oops.Oh(ErrorCodeEnum.D1016);
}
break;
}
@ -236,6 +239,7 @@ public class SysRoleService : IDynamicApiController, ITransient
break;
}
}
role.DataScope = (DataScopeEnum)dataScope;
await _sysRoleRep.AsUpdateable(role).UpdateColumns(u => new { u.DataScope }).ExecuteCommandAsync();
await _sysRoleOrgService.GrantRoleOrg(input);
@ -247,7 +251,7 @@ public class SysRoleService : IDynamicApiController, ITransient
/// <param name="input"></param>
/// <returns></returns>
[UnitOfWork]
[DisplayName("授权角色接口")]
[DisplayName("授权角色接口")]
public async Task GrantApi(RoleApiInput input)
{
await ClearUserApiCache(input.Id);
@ -260,7 +264,7 @@ public class SysRoleService : IDynamicApiController, ITransient
/// <param name="input"></param>
/// <returns></returns>
[UnitOfWork]
[DisplayName("授权角色用户")]
[DisplayName("授权角色用户")]
public async Task GrantUser(RoleUserInput input)
{
await _sysUserRoleService.GrantRoleUser(input);
@ -271,7 +275,7 @@ public class SysRoleService : IDynamicApiController, ITransient
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[DisplayName("设置角色状态")]
[DisplayName("设置角色状态")]
public async Task<int> SetStatus(RoleInput input)
{
if (!Enum.IsDefined(typeof(StatusEnum), input.Status)) throw Oops.Oh(ErrorCodeEnum.D3005);
@ -286,7 +290,7 @@ public class SysRoleService : IDynamicApiController, ITransient
/// 获取所有表格字段 🔖
/// </summary>
/// <returns></returns>
[DisplayName("获取所有表格字段")]
[DisplayName("获取所有表格字段")]
public List<RoleTableOutput> GetAllTableColumnList()
{
return _sysRoleTableService.HandleTableColumn();
@ -297,7 +301,7 @@ public class SysRoleService : IDynamicApiController, ITransient
/// </summary>
/// <param name="roleId"></param>
/// <returns></returns>
[DisplayName("获取角色表格字段集合")]
[DisplayName("获取角色表格字段集合")]
public async Task<List<string>> GetRoleTable(long roleId)
{
return await _sysRoleTableService.GetRoleTable(roleId);
@ -307,7 +311,7 @@ public class SysRoleService : IDynamicApiController, ITransient
/// 获取当前用户表格字段集合 🔖
/// </summary>
/// <returns></returns>
[DisplayName("获取当前用户表格字段集合")]
[DisplayName("获取当前用户表格字段集合")]
public async Task<List<string>> GetUserRoleTableList()
{
return await _sysRoleTableService.GetUserRoleTableList(_userManager, _sysUserRoleService);
@ -318,7 +322,7 @@ public class SysRoleService : IDynamicApiController, ITransient
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[DisplayName("根据角色Id获取菜单Id集合")]
[DisplayName("根据角色Id获取菜单Id集合")]
public async Task<List<long>> GetOwnMenuList([FromQuery] RoleInput input)
{
return await _sysRoleMenuService.GetRoleMenuIdList(new List<long> { input.Id });
@ -329,7 +333,7 @@ public class SysRoleService : IDynamicApiController, ITransient
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[DisplayName("根据角色Id获取机构Id集合")]
[DisplayName("根据角色Id获取机构Id集合")]
public async Task<List<long>> GetOwnOrgList([FromQuery] RoleInput input)
{
return await _sysRoleOrgService.GetRoleOrgIdList(new List<long> { input.Id });
@ -340,7 +344,7 @@ public class SysRoleService : IDynamicApiController, ITransient
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[DisplayName("获取角色接口黑名单集合")]
[DisplayName("获取角色接口黑名单集合")]
public async Task<List<string>> GetRoleApiList([FromQuery] RoleInput input)
{
return await _sysRoleApiService.GetRoleApiList(new List<long> { input.Id });
@ -353,15 +357,15 @@ public class SysRoleService : IDynamicApiController, ITransient
/// 获取用户接口集合 🔖
/// </summary>
/// <returns></returns>
[DisplayName("获取用户接口集合")]
[DisplayName("获取用户接口集合")]
public async Task<List<List<string>>> GetUserApiList()
{
var userId = _userManager.UserId;
var apiList = _sysCacheService.Get<List<List<string>>>(CacheConst.KeyUserApi + userId);
if (apiList != null) return apiList;
if (apiList != null) return apiList;
apiList = [[], []];
// 所有按钮权限集合
apiList = [[], []];
// 所有按钮权限集合
var allButtonList = await GetButtonList();
// 超管账号获取所有接口
if (_userManager.SuperAdmin)
@ -380,20 +384,20 @@ public class SysRoleService : IDynamicApiController, ITransient
else
{
// 当前账号所有角色集合
var roleIdList = await _sysUserRoleService.GetUserRoleIdList(_userManager.UserId);
// 已有按钮权限集合
var menuIdList = await _sysRoleMenuService.GetRoleMenuIdList(roleIdList);
apiList[0] = await GetButtonList(menuIdList, false);
var roleIdList = await _sysUserRoleService.GetUserRoleIdList(_userManager.UserId);
// 已有按钮权限集合
var menuIdList = await _sysRoleMenuService.GetRoleMenuIdList(roleIdList);
apiList[0] = await GetButtonList(menuIdList, false);
// 未有按钮权限集合(放到接口黑名单里面)
apiList[1] = allButtonList.Except(apiList[0]).ToList(); // 差集
// 接口黑名单集合
var roleApiList = await _sysRoleApiService.GetRoleApiList(roleIdList);
apiList[1].AddRange(roleApiList);
}
// 排序接口名称
apiList[0].Sort();
}
// 排序接口名称
apiList[0].Sort();
apiList[1].Sort();
_sysCacheService.Set(CacheConst.KeyUserApi + userId, apiList, TimeSpan.FromDays(7)); // 缓存7天
return apiList;
@ -420,26 +424,26 @@ public class SysRoleService : IDynamicApiController, ITransient
///// </summary>
///// <param name="roleIds"></param>
///// <returns></returns>
//[NonAction]
//[NonAction]
//public async Task<List<string>> GetRoleButtonList(List<long> roleIds)
//{
// var menuIdList = await _sysRoleMenuService.GetRoleMenuIdList(roleIds);
// return await GetButtonList(menuIdList);
//}
/// <summary>
/// 根据菜单Id集合获取按钮集合
/// </summary>
/// <param name="menuIds"></param>
/// <param name="isAll"></param>
//}
/// <summary>
/// 根据菜单Id集合获取按钮集合
/// </summary>
/// <param name="menuIds"></param>
/// <param name="isAll"></param>
/// <returns></returns>
private async Task<List<string>> GetButtonList(List<long> menuIds = null, bool isAll = true)
{
return await _sysRoleRep.ChangeRepository<SqlSugarRepository<SysMenu>>().AsQueryable()
.WhereIF(menuIds != null && menuIds.Count > 0, u => menuIds.Contains(u.Id))
.WhereIF(!isAll, u => u.Status == StatusEnum.Enable)
.Where(u => u.Type == MenuTypeEnum.Btn)
.Select(u => u.Permission).ToListAsync();
return await _sysRoleRep.ChangeRepository<SqlSugarRepository<SysMenu>>().AsQueryable()
.WhereIF(menuIds != null && menuIds.Count > 0, u => menuIds.Contains(u.Id))
.WhereIF(!isAll, u => u.Status == StatusEnum.Enable)
.Where(u => u.Type == MenuTypeEnum.Btn)
.Select(u => u.Permission).ToListAsync();
}
/// <summary>
@ -447,7 +451,8 @@ public class SysRoleService : IDynamicApiController, ITransient
/// </summary>
/// <param name="roleId"></param>
/// <returns></returns>
private async Task ClearUserApiCache(long roleId)
[NonAction]
public async Task ClearUserApiCache(long roleId)
{
var userIdList = await _sysUserRoleService.GetUserIdList(roleId);
foreach (var userId in userIdList)

View File

@ -23,6 +23,7 @@ public class SysTenantService : IDynamicApiController, ITransient
private readonly SqlSugarRepository<SysUserRole> _userRoleRep;
private readonly SqlSugarRepository<SysFile> _fileRep;
private readonly SysUserRoleService _sysUserRoleService;
private readonly SysRoleService _sysRoleService;
private readonly SysRoleMenuService _sysRoleMenuService;
private readonly SysConfigService _sysConfigService;
private readonly SysCacheService _sysCacheService;
@ -40,6 +41,7 @@ public class SysTenantService : IDynamicApiController, ITransient
SqlSugarRepository<SysUserRole> userRoleRep,
SqlSugarRepository<SysFile> fileRep,
SysUserRoleService sysUserRoleService,
SysRoleService sysRoleService,
SysRoleMenuService sysRoleMenuService,
SysConfigService sysConfigService,
SysCacheService sysCacheService,
@ -57,6 +59,7 @@ public class SysTenantService : IDynamicApiController, ITransient
_userRoleRep = userRoleRep;
_fileRep = fileRep;
_sysUserRoleService = sysUserRoleService;
_sysRoleService = sysRoleService;
_sysRoleMenuService = sysRoleMenuService;
_sysConfigService = sysConfigService;
_sysCacheService = sysCacheService;
@ -345,6 +348,7 @@ public class SysTenantService : IDynamicApiController, ITransient
default:
throw Oops.Oh(ErrorCodeEnum.D3004);
}
// 从库配置判断
if (input.TenantType == TenantTypeEnum.Db && !string.IsNullOrWhiteSpace(input.SlaveConnections) && !JSON.IsValid(input.SlaveConnections, true))
throw Oops.Oh(ErrorCodeEnum.D1302);
@ -379,6 +383,8 @@ public class SysTenantService : IDynamicApiController, ITransient
input.Id = adminRole.Id; // 重置租户管理员角色Id
await _sysRoleMenuService.GrantRoleMenu(input);
await _sysRoleService.ClearUserApiCache(input.Id);
}
/// <summary>
@ -440,6 +446,7 @@ public class SysTenantService : IDynamicApiController, ITransient
if (!string.IsNullOrWhiteSpace(tenant.Connection))
tenant.Connection = CryptogramUtil.SM2Encrypt(tenant.Connection);
}
_sysCacheService.Set(CacheConst.KeyTenant, tenantList);
}

View File

@ -5,14 +5,14 @@
<template #toolbar_buttons>
<el-form :inlineMessage="true" label-width="auto" style="flex: 1 1 0%">
<el-row :gutter="10">
<el-col class="mb5" :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
<el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
<el-form-item label="库名" prop="configId">
<el-select v-model="state.configId" placeholder="库名" filterable>
<el-option v-for="item in state.dbData" :key="item.configId" :label="`${item.dbName}(${item.configId})`" :value="item.configId" />
</el-select>
</el-form-item>
</el-col>
<el-col class="mb5" :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
<el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
<el-button type="primary" icon="ele-Plus" @click="handleAdd" v-auth="'dbBackup/add'" :loading="state.loading"> 新增 </el-button>
</el-col>
</el-row>