fix: 已知问题修复
This commit is contained in:
@@ -15,7 +15,7 @@ public interface UserRoleService extends IService<UserRole> {
|
||||
* @param roleIds
|
||||
* @return
|
||||
*/
|
||||
boolean saveUserRoles(Long userId, List<Long> roleIds);
|
||||
void saveUserRoles(Long userId, List<Long> roleIds);
|
||||
|
||||
/**
|
||||
* 判断角色是否存在绑定的用户
|
||||
|
||||
@@ -3,80 +3,92 @@ package com.youlai.boot.system.service.impl;
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.youlai.boot.core.security.token.TokenManager;
|
||||
import com.youlai.boot.core.security.util.SecurityUtils;
|
||||
import com.youlai.boot.system.mapper.UserRoleMapper;
|
||||
import com.youlai.boot.system.model.entity.UserRole;
|
||||
import com.youlai.boot.system.service.UserRoleService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {
|
||||
|
||||
/**
|
||||
* 保存用户角色
|
||||
*
|
||||
* @param userId
|
||||
* @param roleIds
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean saveUserRoles(Long userId, List<Long> roleIds) {
|
||||
|
||||
if (userId == null || CollectionUtil.isEmpty(roleIds)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 用户原角色ID集合
|
||||
List<Long> userRoleIds = this.list(new LambdaQueryWrapper<UserRole>()
|
||||
.eq(UserRole::getUserId, userId))
|
||||
.stream()
|
||||
.map(UserRole::getRoleId)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 新增用户角色
|
||||
List<Long> saveRoleIds;
|
||||
if (CollectionUtil.isEmpty(userRoleIds)) {
|
||||
saveRoleIds = roleIds;
|
||||
} else {
|
||||
saveRoleIds = roleIds.stream()
|
||||
.filter(roleId -> !userRoleIds.contains(roleId))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
List<UserRole> saveUserRoles = saveRoleIds
|
||||
.stream()
|
||||
.map(roleId -> new UserRole(userId, roleId))
|
||||
.collect(Collectors.toList());
|
||||
this.saveBatch(saveUserRoles);
|
||||
|
||||
// 删除用户角色
|
||||
if (CollectionUtil.isNotEmpty(userRoleIds)) {
|
||||
List<Long> removeRoleIds = userRoleIds.stream()
|
||||
.filter(roleId -> !roleIds.contains(roleId))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
if (CollectionUtil.isNotEmpty(removeRoleIds)) {
|
||||
this.remove(new LambdaQueryWrapper<UserRole>()
|
||||
.eq(UserRole::getUserId, userId)
|
||||
.in(UserRole::getRoleId, removeRoleIds)
|
||||
);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
private final TokenManager tokenManager;
|
||||
|
||||
/**
|
||||
* 保存用户角色
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param roleIds 选择的角色ID集合
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public void saveUserRoles(Long userId, List<Long> roleIds) {
|
||||
if (userId == null || CollectionUtil.isEmpty(roleIds)) {
|
||||
return ;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断角色是否存在绑定的用户
|
||||
*
|
||||
* @param roleId 角色ID
|
||||
* @return true:已分配 false:未分配
|
||||
*/
|
||||
@Override
|
||||
public boolean hasAssignedUsers(Long roleId) {
|
||||
int count = this.baseMapper.countUsersForRole(roleId);
|
||||
return count > 0;
|
||||
// 获取现有角色
|
||||
List<Long> userRoleIds = this.list(new LambdaQueryWrapper<UserRole>()
|
||||
.select(UserRole::getRoleId)
|
||||
.eq(UserRole::getUserId, userId))
|
||||
.parallelStream()
|
||||
.map(UserRole::getRoleId)
|
||||
.toList();
|
||||
|
||||
// 使用Set提升对比效率
|
||||
Set<Long> oldRoles = new HashSet<>(userRoleIds);
|
||||
Set<Long> newRoles = new HashSet<>(roleIds);
|
||||
|
||||
// 计算变更集
|
||||
Set<Long> addedRoles = new HashSet<>(newRoles);
|
||||
addedRoles.removeAll(oldRoles);
|
||||
|
||||
Set<Long> removedRoles = new HashSet<>(oldRoles);
|
||||
removedRoles.removeAll(newRoles);
|
||||
|
||||
boolean rolesChanged = !addedRoles.isEmpty() || !removedRoles.isEmpty();
|
||||
|
||||
// 批量保存新增角色
|
||||
if (!addedRoles.isEmpty()) {
|
||||
this.saveBatch(addedRoles.stream()
|
||||
.map(roleId -> new UserRole(userId, roleId))
|
||||
.collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
// 删除废弃角色
|
||||
if (!removedRoles.isEmpty()) {
|
||||
this.remove(new LambdaQueryWrapper<UserRole>()
|
||||
.eq(UserRole::getUserId, userId)
|
||||
.in(UserRole::getRoleId, removedRoles));
|
||||
}
|
||||
|
||||
// 当权限变更时清除登录态
|
||||
if (rolesChanged) {
|
||||
// 获取用户所有有效token(根据实际token存储实现)
|
||||
String accessToken = SecurityUtils.getTokenFromRequest();
|
||||
tokenManager.invalidateToken(accessToken);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断角色是否存在绑定的用户
|
||||
*
|
||||
* @param roleId 角色ID
|
||||
* @return true:已分配 false:未分配
|
||||
*/
|
||||
@Override
|
||||
public boolean hasAssignedUsers(Long roleId) {
|
||||
int count = this.baseMapper.countUsersForRole(roleId);
|
||||
return count > 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,6 +131,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
|
||||
// 设置默认加密密码
|
||||
String defaultEncryptPwd = passwordEncoder.encode(SystemConstants.DEFAULT_PASSWORD);
|
||||
entity.setPassword(defaultEncryptPwd);
|
||||
entity.setCreateBy(SecurityUtils.getUserId());
|
||||
|
||||
// 新增用户
|
||||
boolean result = this.save(entity);
|
||||
@@ -163,6 +164,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
|
||||
|
||||
// form -> entity
|
||||
User entity = userConverter.toEntity(userForm);
|
||||
entity.setUpdateBy(SecurityUtils.getUserId());
|
||||
|
||||
// 修改用户
|
||||
boolean result = this.updateById(entity);
|
||||
|
||||
Reference in New Issue
Block a user