fix: sync admin flag with role assignments
This commit is contained in:
parent
e7554c746d
commit
cca9a4c2eb
@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|||||||
import com.designhub.dto.LoginDTO;
|
import com.designhub.dto.LoginDTO;
|
||||||
import com.designhub.dto.RegisterDTO;
|
import com.designhub.dto.RegisterDTO;
|
||||||
import com.designhub.entity.Department;
|
import com.designhub.entity.Department;
|
||||||
|
import com.designhub.entity.Role;
|
||||||
import com.designhub.entity.User;
|
import com.designhub.entity.User;
|
||||||
import com.designhub.entity.UserDepartment;
|
import com.designhub.entity.UserDepartment;
|
||||||
import com.designhub.exception.BusinessException;
|
import com.designhub.exception.BusinessException;
|
||||||
@ -27,10 +28,13 @@ import org.springframework.util.StringUtils;
|
|||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.LinkedHashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -343,18 +347,17 @@ public class UserService {
|
|||||||
throw new BusinessException("用户不存在");
|
throw new BusinessException("用户不存在");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 将角色ID列表转换为逗号分隔的字符串
|
Set<Long> targetRoleIds = roleIds == null ? Collections.emptySet() : new LinkedHashSet<>(roleIds);
|
||||||
if (roleIds != null && !roleIds.isEmpty()) {
|
String roleIdsStr = targetRoleIds.isEmpty()
|
||||||
String roleIdsStr = roleIds.stream()
|
? null
|
||||||
.map(String::valueOf)
|
: targetRoleIds.stream().map(String::valueOf).collect(Collectors.joining(","));
|
||||||
.reduce((a, b) -> a + "," + b)
|
Integer targetIsAdmin = hasAdminRole(targetRoleIds) ? 1 : 0;
|
||||||
.orElse("");
|
|
||||||
user.setRoleIds(roleIdsStr);
|
LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
|
||||||
userMapper.updateById(user);
|
updateWrapper.eq(User::getId, userId)
|
||||||
} else {
|
.set(User::getRoleIds, roleIdsStr)
|
||||||
user.setRoleIds(null);
|
.set(User::getIsAdmin, targetIsAdmin);
|
||||||
userMapper.updateById(user);
|
userMapper.update(null, updateWrapper);
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -1768,13 +1771,34 @@ public class UserService {
|
|||||||
throw new BusinessException(ResultCode.NOT_FOUND, "用户不存在");
|
throw new BusinessException(ResultCode.NOT_FOUND, "用户不存在");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新管理员标识
|
Set<Long> roleIdSet = parseRoleIds(user.getRoleIds());
|
||||||
|
boolean enableAdmin = isAdmin != null && isAdmin == 1;
|
||||||
|
if (enableAdmin) {
|
||||||
|
Long adminRoleId = findRoleIdByCode("ADMIN");
|
||||||
|
if (adminRoleId == null) {
|
||||||
|
throw new BusinessException(ResultCode.ERROR, "未找到管理员角色,请先检查角色配置");
|
||||||
|
}
|
||||||
|
roleIdSet.add(adminRoleId);
|
||||||
|
} else {
|
||||||
|
Long adminRoleId = findRoleIdByCode("ADMIN");
|
||||||
|
if (adminRoleId != null) {
|
||||||
|
roleIdSet.remove(adminRoleId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String targetRoleIds = roleIdSet.isEmpty()
|
||||||
|
? null
|
||||||
|
: roleIdSet.stream().map(String::valueOf).collect(Collectors.joining(","));
|
||||||
|
Integer targetIsAdmin = hasAdminRole(roleIdSet) ? 1 : 0;
|
||||||
|
|
||||||
LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
|
LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
|
||||||
updateWrapper.eq(User::getId, userId);
|
updateWrapper.eq(User::getId, userId)
|
||||||
updateWrapper.set(User::getIsAdmin, isAdmin != null ? isAdmin : 0);
|
.set(User::getRoleIds, targetRoleIds)
|
||||||
|
.set(User::getIsAdmin, targetIsAdmin);
|
||||||
|
|
||||||
boolean result = userMapper.update(null, updateWrapper) > 0;
|
boolean result = userMapper.update(null, updateWrapper) > 0;
|
||||||
log.info("设置管理员身份结果: userId={}, isAdmin={}, result={}", userId, isAdmin, result);
|
log.info("设置管理员身份结果: userId={}, isAdmin={}, roleIds={}, result={}",
|
||||||
|
userId, targetIsAdmin, targetRoleIds, result);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -1786,4 +1810,48 @@ public class UserService {
|
|||||||
User user = userMapper.selectById(userId);
|
User user = userMapper.selectById(userId);
|
||||||
return user != null && user.getIsAdmin() != null && user.getIsAdmin() == 1;
|
return user != null && user.getIsAdmin() != null && user.getIsAdmin() == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Set<Long> parseRoleIds(String roleIdsStr) {
|
||||||
|
if (!StringUtils.hasText(roleIdsStr)) {
|
||||||
|
return new LinkedHashSet<>();
|
||||||
|
}
|
||||||
|
Set<Long> roleIds = new LinkedHashSet<>();
|
||||||
|
for (String part : roleIdsStr.split(",")) {
|
||||||
|
String trimmed = part == null ? "" : part.trim();
|
||||||
|
if (trimmed.isEmpty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
roleIds.add(Long.parseLong(trimmed));
|
||||||
|
} catch (NumberFormatException ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return roleIds;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Long findRoleIdByCode(String roleCode) {
|
||||||
|
if (!StringUtils.hasText(roleCode)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return roleService.getAllRoles().stream()
|
||||||
|
.filter(role -> roleCode.equalsIgnoreCase(role.getRoleCode()))
|
||||||
|
.map(Role::getId)
|
||||||
|
.findFirst()
|
||||||
|
.orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean hasAdminRole(Set<Long> roleIds) {
|
||||||
|
if (roleIds == null || roleIds.isEmpty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Set<Long> adminRoleIds = roleService.getAllRoles().stream()
|
||||||
|
.filter(role -> "ADMIN".equalsIgnoreCase(role.getRoleCode())
|
||||||
|
|| "SUPER_ADMIN".equalsIgnoreCase(role.getRoleCode()))
|
||||||
|
.map(Role::getId)
|
||||||
|
.collect(Collectors.toSet());
|
||||||
|
if (adminRoleIds.isEmpty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return roleIds.stream().anyMatch(adminRoleIds::contains);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user