mirror of
http://192.168.1.205:9980/cf_devdept2/cf_imes_server.git
synced 2026-08-14 21:52:07 +08:00
优化角色菜单、角色用户、用户角色授权没有立即生效:
1、逐个删除key -> 批量删除; 2、操作人操作其他机构时才显示删除; 3、日志输出优化;
This commit is contained in:
+24
-15
@@ -3,6 +3,7 @@ package com.cf.imes.module.system.service.permission;
|
|||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.hutool.core.collection.CollectionUtil;
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
import cn.hutool.core.util.ArrayUtil;
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import cn.hutool.extra.spring.SpringUtil;
|
import cn.hutool.extra.spring.SpringUtil;
|
||||||
import com.cf.imes.framework.common.enums.CommonStatusEnum;
|
import com.cf.imes.framework.common.enums.CommonStatusEnum;
|
||||||
import com.cf.imes.framework.common.exception.ServiceException;
|
import com.cf.imes.framework.common.exception.ServiceException;
|
||||||
@@ -189,18 +190,22 @@ public class PermissionServiceImpl implements PermissionService {
|
|||||||
roleMenuMapper.deleteListByRoleIdAndMenuIds(roleId, deleteMenuIds);
|
roleMenuMapper.deleteListByRoleIdAndMenuIds(roleId, deleteMenuIds);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Long currentOrganId = SecurityFrameworkUtils.getUserOrganId();
|
||||||
// 角色菜单权限发生修改,通知刷新本地缓存
|
// 角色菜单权限发生修改,通知刷新本地缓存
|
||||||
if (CollectionUtil.isNotEmpty(createMenuIds) || CollectionUtil.isNotEmpty(deleteMenuIds)) {
|
if (CollectionUtil.isNotEmpty(createMenuIds) || CollectionUtil.isNotEmpty(deleteMenuIds)) {
|
||||||
CompletableFuture.runAsync(() -> {
|
CompletableFuture.runAsync(() -> {
|
||||||
// 获取新增和删除菜单id的并集
|
// 当前操作人和所操作的目标菜单机构不同时,清空MENU_ROLE_ID_LIST:目标机构id:菜单id的缓存
|
||||||
Collection<Long> delKeys = CollUtil.union(createMenuIds, deleteMenuIds);
|
if (ObjectUtil.notEqual(organId, currentOrganId)) {
|
||||||
delKeys.forEach(menuId -> {
|
// 获取新增和删除菜单id的并集
|
||||||
// 移除redis中的缓存
|
Collection<Long> delMenuIds = CollUtil.union(createMenuIds, deleteMenuIds);
|
||||||
redisTemplate.delete(String.format(String.valueOf(new StringBuffer(RedisKeyConstants.USER_ROLE_ID_LIST).append(":%s:%s")), organId, menuId));
|
// 构建批量删除的key集合
|
||||||
});
|
Set<String> delKeys = delMenuIds.stream().map(menuId -> String.format(String.valueOf(new StringBuffer(RedisKeyConstants.MENU_ROLE_ID_LIST).append(":%s:%s")), organId, menuId)).collect(Collectors.toSet());
|
||||||
|
Long batchDelNum = redisTemplate.delete(delKeys);
|
||||||
|
log.info("[assignRoleMenu] 批量删除redis[MENU_ROLE_ID_LIST]数量:{}", batchDelNum);
|
||||||
|
}
|
||||||
redisTemplate.convertAndSend(RedisRefreshChannelTopicConstants.PERMISSION_REFRESH, "");
|
redisTemplate.convertAndSend(RedisRefreshChannelTopicConstants.PERMISSION_REFRESH, "");
|
||||||
}).exceptionally(e -> {
|
}).exceptionally(e -> {
|
||||||
log.error("redis message发送失败, topic:{}, 异常:{}", RedisRefreshChannelTopicConstants.PERMISSION_REFRESH, e.getMessage());
|
log.error("[assignRoleMenu] redis message发送失败, topic:{}, 异常:{}", RedisRefreshChannelTopicConstants.PERMISSION_REFRESH, e);
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -326,18 +331,22 @@ public class PermissionServiceImpl implements PermissionService {
|
|||||||
userRoleMapper.deleteListByRoleIdAndUserIds(roleId, deleteRoleUserIds);
|
userRoleMapper.deleteListByRoleIdAndUserIds(roleId, deleteRoleUserIds);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Long currentOrganId = SecurityFrameworkUtils.getUserOrganId();
|
||||||
// 角色菜单权限发生修改,通知刷新本地缓存
|
// 角色菜单权限发生修改,通知刷新本地缓存
|
||||||
if (CollectionUtil.isNotEmpty(createRoleUserIds) || CollectionUtil.isNotEmpty(deleteRoleUserIds)) {
|
if (CollectionUtil.isNotEmpty(createRoleUserIds) || CollectionUtil.isNotEmpty(deleteRoleUserIds)) {
|
||||||
CompletableFuture.runAsync(() -> {
|
CompletableFuture.runAsync(() -> {
|
||||||
// 获取新增和删除用户id的并集
|
// 当前操作人和所操作的目标菜单机构不同时,清空USER_ROLE_ID_LIST:目标机构id:用户id的缓存
|
||||||
Collection<Long> delKeys = CollUtil.union(createRoleUserIds, deleteRoleUserIds);
|
if (ObjectUtil.notEqual(finalOrganId, currentOrganId)) {
|
||||||
delKeys.forEach(userId -> {
|
// 获取新增和删除用户id的并集
|
||||||
// 移除redis中的缓存
|
Collection<Long> delRoleUserIds = CollUtil.union(createRoleUserIds, deleteRoleUserIds);
|
||||||
redisTemplate.delete(String.format(String.valueOf(new StringBuffer(RedisKeyConstants.USER_ROLE_ID_LIST).append(":%s:%s")), finalOrganId, userId));
|
// 构建批量删除的key集合
|
||||||
});
|
Set<String> delKeys = delRoleUserIds.stream().map(userId -> String.format(String.valueOf(new StringBuffer(RedisKeyConstants.USER_ROLE_ID_LIST).append(":%s:%s")), finalOrganId, userId)).collect(Collectors.toSet());
|
||||||
|
Long batchDelNum = redisTemplate.delete(delKeys);
|
||||||
|
log.info("[batchAssignRoleUsers] 批量删除redis[USER_ROLE_ID_LIST]数量:{}", batchDelNum);
|
||||||
|
}
|
||||||
redisTemplate.convertAndSend(RedisRefreshChannelTopicConstants.PERMISSION_REFRESH, "");
|
redisTemplate.convertAndSend(RedisRefreshChannelTopicConstants.PERMISSION_REFRESH, "");
|
||||||
}).exceptionally(e -> {
|
}).exceptionally(e -> {
|
||||||
log.error("redis message发送失败, topic:{}, 异常:{}", RedisRefreshChannelTopicConstants.PERMISSION_REFRESH, e.getMessage());
|
log.error("[batchAssignRoleUsers] redis message发送失败, topic:{}, 异常:{}", RedisRefreshChannelTopicConstants.PERMISSION_REFRESH, e);
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -419,7 +428,7 @@ public class PermissionServiceImpl implements PermissionService {
|
|||||||
redisTemplate.delete(String.format(String.valueOf(new StringBuffer(RedisKeyConstants.USER_ROLE_ID_LIST).append(":%s:%s")), organId, userId));
|
redisTemplate.delete(String.format(String.valueOf(new StringBuffer(RedisKeyConstants.USER_ROLE_ID_LIST).append(":%s:%s")), organId, userId));
|
||||||
redisTemplate.convertAndSend(RedisRefreshChannelTopicConstants.PERMISSION_REFRESH, "");
|
redisTemplate.convertAndSend(RedisRefreshChannelTopicConstants.PERMISSION_REFRESH, "");
|
||||||
}).exceptionally(e -> {
|
}).exceptionally(e -> {
|
||||||
log.error("redis 清空user_role_ids或message发送失败, topic:{}, 异常:{}", RedisRefreshChannelTopicConstants.PERMISSION_REFRESH, e.getMessage());
|
log.error("[assignUserRole] redis 清空USER_ROLE_ID_LIST或message发送失败, topic:{}, 异常:{}", RedisRefreshChannelTopicConstants.PERMISSION_REFRESH, e);
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user