mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-20 02:08:15 +08:00
fix 修改保存角色时,只能修改当前用户的缓存,导致其它在线用户的缓存没有及时更新
This commit is contained in:
parent
cd33cd66ae
commit
eb2e2f61be
@ -1,22 +1,25 @@
|
||||
package com.ruoyi.web.controller.system;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckPermission;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.ruoyi.common.annotation.Log;
|
||||
import com.ruoyi.common.constant.UserConstants;
|
||||
import com.ruoyi.common.core.controller.BaseController;
|
||||
import com.ruoyi.common.core.domain.R;
|
||||
import com.ruoyi.common.core.domain.PageQuery;
|
||||
import com.ruoyi.common.core.domain.R;
|
||||
import com.ruoyi.common.core.domain.entity.SysRole;
|
||||
import com.ruoyi.common.core.domain.entity.SysUser;
|
||||
import com.ruoyi.common.core.domain.model.LoginUser;
|
||||
import com.ruoyi.common.core.page.TableDataInfo;
|
||||
import com.ruoyi.common.enums.BusinessType;
|
||||
import com.ruoyi.common.helper.LoginHelper;
|
||||
import com.ruoyi.common.utils.poi.ExcelUtil;
|
||||
import com.ruoyi.system.domain.SysUserRole;
|
||||
import com.ruoyi.system.mapper.SysUserRoleMapper;
|
||||
import com.ruoyi.system.service.ISysRoleService;
|
||||
import com.ruoyi.system.service.ISysUserService;
|
||||
import com.ruoyi.system.service.SysLoginService;
|
||||
import com.ruoyi.system.service.SysPermissionService;
|
||||
import io.swagger.annotations.*;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@ -25,6 +28,7 @@ import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 角色信息
|
||||
@ -41,6 +45,8 @@ public class SysRoleController extends BaseController {
|
||||
private final ISysRoleService roleService;
|
||||
private final ISysUserService userService;
|
||||
private final SysPermissionService permissionService;
|
||||
private final SysUserRoleMapper sysUserRoleMapper;
|
||||
private final SysLoginService loginService;
|
||||
|
||||
@ApiOperation("查询角色信息列表")
|
||||
@SaCheckPermission("system:role:list")
|
||||
@ -104,12 +110,19 @@ public class SysRoleController extends BaseController {
|
||||
|
||||
if (roleService.updateRole(role) > 0) {
|
||||
// 更新缓存用户权限
|
||||
LoginUser loginUser = getLoginUser();
|
||||
SysUser sysUser = userService.selectUserById(loginUser.getUserId());
|
||||
if (ObjectUtil.isNotNull(sysUser) && !sysUser.isAdmin()) {
|
||||
loginUser.setMenuPermission(permissionService.getMenuPermission(sysUser));
|
||||
LoginHelper.setLoginUser(loginUser);
|
||||
}
|
||||
List<Long> userIdList = sysUserRoleMapper.selectList(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getRoleId, role.getRoleId()))
|
||||
.stream().map(SysUserRole::getUserId).collect(Collectors.toList());
|
||||
userService.selectUserByUserIds(userIdList).forEach(sysUser -> {
|
||||
// 存储在saToken的用户key值
|
||||
String userId = sysUser.getUserType() + StrUtil.COLON + sysUser.getUserId();
|
||||
// 获取对应用户的token
|
||||
String token = StpUtil.getTokenValueByLoginId(userId);
|
||||
if (StrUtil.isBlank(token)) {
|
||||
return;
|
||||
}
|
||||
// 更新用户缓存
|
||||
LoginHelper.setLoginUserByToken(token, loginService.buildLoginUser(sysUser));
|
||||
});
|
||||
return R.ok();
|
||||
}
|
||||
return R.fail("修改角色'" + role.getRoleName() + "'失败,请联系管理员");
|
||||
|
||||
@ -13,7 +13,7 @@ import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* 登录鉴权助手
|
||||
*
|
||||
*
|
||||
* user_type 为 用户类型 同一个用户表 可以有多种用户类型 例如 pc,app
|
||||
* deivce 为 设备类型 同一个用户类型 可以有 多种设备类型 例如 web,ios
|
||||
* 可以组成 用户类型与设备类型多对多的 权限灵活控制
|
||||
@ -61,6 +61,13 @@ public class LoginHelper {
|
||||
StpUtil.getTokenSession().set(LOGIN_USER_KEY, loginUser);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据token修改不同用户的缓存
|
||||
*/
|
||||
public static void setLoginUserByToken(String token, LoginUser loginUser) {
|
||||
StpUtil.getTokenSessionByToken(token).set(LOGIN_USER_KEY, loginUser);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户(多级缓存)
|
||||
*/
|
||||
|
||||
@ -76,4 +76,12 @@ public interface SysUserMapper extends BaseMapperPlus<SysUserMapper, SysUser, Sy
|
||||
*/
|
||||
SysUser selectUserById(Long userId);
|
||||
|
||||
/**
|
||||
* 根据用户编号查询用户
|
||||
*
|
||||
* @param userIdList 用户编号
|
||||
* @return 用户列表
|
||||
*/
|
||||
List<SysUser> selectUserByUserIdList(@Param("userIdList") List<Long> userIdList);
|
||||
|
||||
}
|
||||
|
||||
@ -200,4 +200,12 @@ public interface ISysUserService {
|
||||
*/
|
||||
int deleteUserByIds(Long[] userIds);
|
||||
|
||||
/**
|
||||
* 根据用户编号查询用户
|
||||
*
|
||||
* @param userIdList 用户编号
|
||||
* @return 用户列表
|
||||
*/
|
||||
List<SysUser> selectUserByUserIds(List<Long> userIdList);
|
||||
|
||||
}
|
||||
|
||||
@ -139,7 +139,7 @@ public class SysLoginService {
|
||||
/**
|
||||
* 构建登录用户
|
||||
*/
|
||||
private LoginUser buildLoginUser(SysUser user) {
|
||||
public LoginUser buildLoginUser(SysUser user) {
|
||||
LoginUser loginUser = new LoginUser();
|
||||
loginUser.setUserId(user.getUserId());
|
||||
loginUser.setDeptId(user.getDeptId());
|
||||
|
||||
@ -488,4 +488,15 @@ public class SysUserServiceImpl implements ISysUserService {
|
||||
return baseMapper.deleteBatchIds(ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用户编号查询用户
|
||||
*
|
||||
* @param userIdList 用户编号
|
||||
* @return 用户列表
|
||||
*/
|
||||
@Override
|
||||
public List<SysUser> selectUserByUserIds(List<Long> userIdList) {
|
||||
return baseMapper.selectUserByUserIdList(userIdList);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -126,5 +126,16 @@
|
||||
where u.del_flag = '0' and u.user_id = #{userId}
|
||||
</select>
|
||||
|
||||
<select id="selectUserByUserIdList" parameterType="list" resultMap="SysUserResult">
|
||||
<include refid="selectUserVo"/>
|
||||
where u.del_flag = '0'
|
||||
<if test="userIdList != null and userIdList.size > 0">
|
||||
and u.user_id in
|
||||
<foreach collection="userIdList" item="item" open="(" close=")" separator=",">
|
||||
#{item}
|
||||
</foreach>
|
||||
</if>
|
||||
</select>
|
||||
|
||||
|
||||
</mapper>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user