fix: 已知问题修复

This commit is contained in:
Ray.Hao
2025-06-01 18:20:20 +08:00
4 changed files with 76 additions and 62 deletions

View File

@@ -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);
/**
* 判断角色是否存在绑定的用户

View File

@@ -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;
}
}

View File

@@ -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);

View File

@@ -1,6 +1,6 @@
<template>
<div class="app-container">
<div class="search-bar">
<div class="search-container">
<el-form ref="queryFormRef" :model="queryParams" :inline="true">
#foreach($fieldConfig in $fieldConfigs)
#if($fieldConfig.isShowInQuery == 1)