fix: 统一账号角色校验与默认首页

- 创建与导入账号时强制校验启用角色

- 按启用角色返回默认首页并过滤禁用角色
This commit is contained in:
2026-08-03 14:48:39 +08:00
parent 219e4f7eff
commit 866688b92f
9 changed files with 697 additions and 42 deletions

View File

@@ -16,6 +16,12 @@ public interface SysRoleService extends IService<SysRole> {
void saveRoleMenu(BigInteger roleId, List<String> keys);
/**
* 查询账号当前关联的启用角色。
*
* @param accountId 账号 ID
* @return 启用角色列表
*/
List<SysRole> getRolesByAccountId(BigInteger accountId);
void saveRole(SysRole sysRole);

View File

@@ -80,7 +80,9 @@ public class SysAccountServiceImpl extends ServiceImpl<SysAccountMapper, SysAcco
private static final String IMPORT_HEAD_MOBILE = "手机号";
private static final String IMPORT_HEAD_EMAIL = "邮箱";
private static final String IMPORT_HEAD_STATUS = "状态";
private static final String IMPORT_HEAD_ROLE_NAMES = "角色名称";
private static final String IMPORT_HEAD_ROLE_NAMES = "角色名称*";
/** 兼容已下载的旧版导入模板表头。 */
private static final String IMPORT_HEAD_ROLE_NAMES_LEGACY = "角色名称";
private static final String IMPORT_HEAD_POSITION_NAMES = "岗位名称";
private static final String IMPORT_HEAD_REMARK = "备注";
private static final String IMPORT_GUIDE_HEAD_ITEM = "说明项";
@@ -328,7 +330,9 @@ public class SysAccountServiceImpl extends ServiceImpl<SysAccountMapper, SysAcco
SysDept::getDeptName
);
ImportNameLookup<SysRole> roleLookup = buildImportNameLookup(
sysRoleMapper.selectListByQuery(QueryWrapper.create()),
sysRoleMapper.selectListByQuery(
QueryWrapper.create().eq(SysRole::getStatus, EnumDataStatus.AVAILABLE.getCode())
),
SysRole::getRoleName
);
ImportNameLookup<SysPosition> positionLookup = buildImportNameLookup(
@@ -388,6 +392,7 @@ public class SysAccountServiceImpl extends ServiceImpl<SysAccountMapper, SysAcco
String nickname = trimToNull(row.getNickname());
String mobile = trimToNull(row.getMobile());
String email = trimToNull(row.getEmail());
List<String> roleNames = splitCodes(row.getRoleNames());
if (deptName == null) {
addImportDetail(details, IMPORT_FIELD_DEPT_NAME, row.getDeptName(), "部门名称不能为空");
@@ -400,6 +405,9 @@ public class SysAccountServiceImpl extends ServiceImpl<SysAccountMapper, SysAcco
if (nickname == null) {
addImportDetail(details, IMPORT_FIELD_NICKNAME, row.getNickname(), "昵称不能为空");
}
if (roleNames.isEmpty()) {
addImportDetail(details, IMPORT_FIELD_ROLE_NAME, row.getRoleNames(), "角色名称不能为空");
}
if (mobile != null && !StringUtil.isMobileNumber(mobile)) {
addImportDetail(details, IMPORT_FIELD_MOBILE, row.getMobile(), "手机号格式不正确");
}
@@ -410,14 +418,14 @@ public class SysAccountServiceImpl extends ServiceImpl<SysAccountMapper, SysAcco
SysDept dept = resolveSingleResource(deptName, deptLookup, IMPORT_FIELD_DEPT_NAME, details);
Integer status = parseStatus(row.getStatus(), details);
List<BigInteger> roleIds = resolveResourceIds(
row.getRoleNames(),
roleNames,
roleLookup,
SysRole::getId,
IMPORT_FIELD_ROLE_NAME,
details
);
List<BigInteger> positionIds = resolveResourceIds(
row.getPositionNames(),
splitCodes(row.getPositionNames()),
positionLookup,
SysPosition::getId,
IMPORT_FIELD_POSITION_NAME,
@@ -487,7 +495,7 @@ public class SysAccountServiceImpl extends ServiceImpl<SysAccountMapper, SysAcco
/**
* 解析名称列表并映射为主键集合。
*
* @param rawNames 原始名称文本
* @param names 标准化后的名称列表
* @param lookup 名称查找表
* @param fieldName 字段名称
* @param details 错误明细收集器
@@ -495,12 +503,11 @@ public class SysAccountServiceImpl extends ServiceImpl<SysAccountMapper, SysAcco
* @return 匹配到的主键集合
*/
private <T> List<BigInteger> resolveResourceIds(
String rawNames,
List<String> names,
ImportNameLookup<T> lookup,
Function<T, BigInteger> idExtractor,
String fieldName,
List<SysAccountImportErrorDetailVo> details) {
List<String> names = splitCodes(rawNames);
if (names.isEmpty()) {
return Collections.emptyList();
}
@@ -693,8 +700,8 @@ public class SysAccountServiceImpl extends ServiceImpl<SysAccountMapper, SysAcco
private List<List<String>> buildImportGuideRows() {
List<List<String>> rows = new ArrayList<>();
rows.add(List.of("填写规则", "请按名称填写部门、角色、岗位。"));
rows.add(List.of("必填字段", "部门名称*、登录账号*、昵称*"));
rows.add(List.of("可选字段", "手机号、邮箱、状态、角色名称、岗位名称、备注"));
rows.add(List.of("必填字段", "部门名称*、登录账号*、昵称*、角色名称*"));
rows.add(List.of("可选字段", "手机号、邮箱、状态、岗位名称、备注"));
rows.add(List.of("状态可选值", "可留空,或填写 1/0/已启用/启用/未启用/停用/禁用"));
rows.add(List.of("多值分隔", "角色名称、岗位名称支持使用英文逗号,或中文逗号,分隔多个名称"));
rows.add(List.of(
@@ -853,7 +860,7 @@ public class SysAccountServiceImpl extends ServiceImpl<SysAccountMapper, SysAcco
String mobile = getCellValue(data, IMPORT_HEAD_MOBILE);
String email = getCellValue(data, IMPORT_HEAD_EMAIL);
String status = getCellValue(data, IMPORT_HEAD_STATUS);
String roleNames = getCellValue(data, IMPORT_HEAD_ROLE_NAMES);
String roleNames = getCellValue(data, IMPORT_HEAD_ROLE_NAMES, IMPORT_HEAD_ROLE_NAMES_LEGACY);
String positionNames = getCellValue(data, IMPORT_HEAD_POSITION_NAMES);
String remark = getCellValue(data, IMPORT_HEAD_REMARK);
if (!StringUtil.hasText(deptName)
@@ -896,9 +903,14 @@ public class SysAccountServiceImpl extends ServiceImpl<SysAccountMapper, SysAcco
List<String> requiredHeads = List.of(
IMPORT_HEAD_DEPT_NAME,
IMPORT_HEAD_LOGIN_NAME,
IMPORT_HEAD_NICKNAME
IMPORT_HEAD_NICKNAME,
IMPORT_HEAD_ROLE_NAMES
);
for (String requiredHead : requiredHeads) {
if (IMPORT_HEAD_ROLE_NAMES.equals(requiredHead)
&& headIndex.containsKey(IMPORT_HEAD_ROLE_NAMES_LEGACY)) {
continue;
}
if (!headIndex.containsKey(requiredHead)) {
throw new BusinessException("导入模板表头不正确,必须包含:" + String.join("", requiredHeads));
}
@@ -914,13 +926,23 @@ public class SysAccountServiceImpl extends ServiceImpl<SysAccountMapper, SysAcco
return rows;
}
private String getCellValue(Map<Integer, Object> row, String headName) {
Integer index = headIndex.get(headName);
if (index == null) {
return null;
/**
* 按候选表头顺序读取单元格文本。
*
* @param row 当前数据行
* @param headNames 候选表头
* @return 单元格文本,无匹配表头时返回 {@code null}
*/
private String getCellValue(Map<Integer, Object> row, String... headNames) {
for (String headName : headNames) {
Integer index = headIndex.get(headName);
if (index == null) {
continue;
}
Object value = row.get(index);
return value == null ? null : String.valueOf(value).trim();
}
Object value = row.get(index);
return value == null ? null : String.valueOf(value).trim();
return null;
}
}

View File

@@ -7,6 +7,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tech.easyflow.common.cache.RedisLockExecutor;
import tech.easyflow.common.constant.enums.EnumDataScope;
import tech.easyflow.common.constant.enums.EnumDataStatus;
import tech.easyflow.system.entity.SysAccountRole;
import tech.easyflow.system.entity.SysRole;
import tech.easyflow.system.entity.SysRoleDept;
@@ -80,6 +81,9 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
});
}
/**
* {@inheritDoc}
*/
@Override
public List<SysRole> getRolesByAccountId(BigInteger accountId) {
// 查询用户对应角色id集合
@@ -89,7 +93,10 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
if (CollectionUtil.isEmpty(roleIds)) {
return new ArrayList<>();
}
return listByIds(roleIds);
return listByIds(roleIds).stream()
.filter(role -> role != null
&& EnumDataStatus.AVAILABLE.getCode().equals(role.getStatus()))
.collect(Collectors.toList());
}
@Override