|
@@ -1,5 +1,7 @@
|
|
|
package com.ruoyi.system.service.impl;
|
|
|
|
|
|
+import cn.dev33.satoken.exception.NotLoginException;
|
|
|
+import cn.dev33.satoken.stp.StpUtil;
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
|
@@ -10,6 +12,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.ruoyi.common.constant.UserConstants;
|
|
|
import com.ruoyi.common.core.domain.PageQuery;
|
|
|
import com.ruoyi.common.core.domain.entity.SysRole;
|
|
|
+import com.ruoyi.common.core.domain.model.LoginUser;
|
|
|
import com.ruoyi.common.core.page.TableDataInfo;
|
|
|
import com.ruoyi.common.exception.ServiceException;
|
|
|
import com.ruoyi.common.helper.LoginHelper;
|
|
@@ -362,9 +365,13 @@ public class SysRoleServiceImpl implements ISysRoleService {
|
|
|
*/
|
|
|
@Override
|
|
|
public int deleteAuthUser(SysUserRole userRole) {
|
|
|
- return userRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>()
|
|
|
+ int rows = userRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>()
|
|
|
.eq(SysUserRole::getRoleId, userRole.getRoleId())
|
|
|
.eq(SysUserRole::getUserId, userRole.getUserId()));
|
|
|
+ if (rows > 0) {
|
|
|
+ cleanOnlineUserByRole(userRole.getRoleId());
|
|
|
+ }
|
|
|
+ return rows;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -376,9 +383,13 @@ public class SysRoleServiceImpl implements ISysRoleService {
|
|
|
*/
|
|
|
@Override
|
|
|
public int deleteAuthUsers(Long roleId, Long[] userIds) {
|
|
|
- return userRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>()
|
|
|
+ int rows = userRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>()
|
|
|
.eq(SysUserRole::getRoleId, roleId)
|
|
|
.in(SysUserRole::getUserId, Arrays.asList(userIds)));
|
|
|
+ if (rows > 0) {
|
|
|
+ cleanOnlineUserByRole(roleId);
|
|
|
+ }
|
|
|
+ return rows;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -401,6 +412,32 @@ public class SysRoleServiceImpl implements ISysRoleService {
|
|
|
if (CollUtil.isNotEmpty(list)) {
|
|
|
rows = userRoleMapper.insertBatch(list) ? list.size() : 0;
|
|
|
}
|
|
|
+ if (rows > 0) {
|
|
|
+ cleanOnlineUserByRole(roleId);
|
|
|
+ }
|
|
|
return rows;
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void cleanOnlineUserByRole(Long roleId) {
|
|
|
+ List<String> keys = StpUtil.searchTokenValue("", 0, -1, false);
|
|
|
+ if (CollUtil.isEmpty(keys)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 角色关联的在线用户量过大会导致redis阻塞卡顿 谨慎操作
|
|
|
+ keys.parallelStream().forEach(key -> {
|
|
|
+ String token = StringUtils.substringAfterLast(key, ":");
|
|
|
+ // 如果已经过期则跳过
|
|
|
+ if (StpUtil.stpLogic.getTokenActivityTimeoutByToken(token) < -1) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ LoginUser loginUser = LoginHelper.getLoginUser(token);
|
|
|
+ if (loginUser.getRoles().stream().anyMatch(r -> r.getRoleId().equals(roleId))) {
|
|
|
+ try {
|
|
|
+ StpUtil.logoutByTokenValue(token);
|
|
|
+ } catch (NotLoginException ignored) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|