1. 新增业务数据查询,新增 部门 数据权限规则支持

2. 补全子角色排除父角色管理菜单测试用例
This commit is contained in:
chenbowen
2025-07-15 10:01:46 +08:00
parent 7f0957d9c4
commit eaea76e955
11 changed files with 360 additions and 26 deletions

View File

@@ -157,29 +157,50 @@ public class PermissionServiceImpl implements PermissionService {
allEntries = true) // allEntries 清空所有缓存,主要一次更新涉及到的 menuIds 较多,反倒批量会更快
})
public void assignRoleMenu(Long roleId, Set<Long> menuIds) {
RoleDO role = roleService.getRole(roleId);
Set<Long> userRoleIdListByUserId = permissionService.getUserRoleIdListByUserId(getLoginUserId());
// 如果为标准角色,只允许管理员修改菜单权限
if (RoleTypeEnum.NORMAL.getType().equals(role.getType()) && !roleService.hasAnySuperAdmin(userRoleIdListByUserId)) {
throw exception(ROLE_CAN_NOT_UPDATE_NORMAL_TYPE_ROLE);
}
// 获得角色拥有菜单编号
Set<Long> dbMenuIds = convertSet(roleMenuMapper.selectListByRoleId(roleId), RoleMenuDO::getMenuId);
Set<Long> dbMenuIds = convertSet(getRoleMenuListByRoleId(roleId));
// 获取父级角色拥有的菜单编号
Set<Long> parentRoleIds = roleService.getAllParentAndSelfRoleIds(singleton(roleId));
// 移除自身角色编号
parentRoleIds.remove(roleId);
Set<Long> dbInheritedMenuIds = convertSet(roleMenuMapper.selectListByRoleId(parentRoleIds), RoleMenuDO::getMenuId);
// 计算新增和删除的菜单编号
Set<Long> menuIdList = CollUtil.emptyIfNull(menuIds);
Collection<Long> createMenuIds = CollUtil.subtract(menuIdList, dbMenuIds);
Collection<Long> deleteMenuIds = CollUtil.subtract(dbMenuIds, menuIdList);
// 执行新增和删除。对于已经授权的菜单,不用做任何处理
// 执行新增和删除。对于已经授权的菜单,不用进行新增和删除,处理排除关系即可
if (CollUtil.isNotEmpty(createMenuIds)) {
roleMenuMapper.insertBatch(CollectionUtils.convertList(createMenuIds, menuId -> {
RoleMenuDO entity = new RoleMenuDO();
entity.setRoleId(roleId);
entity.setMenuId(menuId);
return entity;
}));
Set<Long> inheritedCreateMenuIds = new HashSet<>(dbInheritedMenuIds);
inheritedCreateMenuIds.retainAll(createMenuIds);
if (CollUtil.isNotEmpty(inheritedCreateMenuIds)) {
// 不需要新增,只需要检查是否存在排除关系,如果存在,则标记排除关系失效
roleMenuExclusionMapper.deleteListByRoleIdAndMenuIds(roleId, inheritedCreateMenuIds);
createMenuIds.removeAll(inheritedCreateMenuIds);
}
if (CollUtil.isNotEmpty(createMenuIds)) {
roleMenuMapper.insertBatch(CollectionUtils.convertList(createMenuIds, menuId -> {
RoleMenuDO entity = new RoleMenuDO();
entity.setRoleId(roleId);
entity.setMenuId(menuId);
return entity;
}));
}
}
if (CollUtil.isNotEmpty(deleteMenuIds)) {
roleMenuMapper.deleteListByRoleIdAndMenuIds(roleId, deleteMenuIds);
Set<Long> inheritedDeleteMenuIds = new HashSet<>(dbInheritedMenuIds);
inheritedDeleteMenuIds.retainAll(deleteMenuIds);
if (CollUtil.isNotEmpty(inheritedDeleteMenuIds)) {
// 标记排除
roleMenuExclusionMapper.insertBatch(CollectionUtils.convertList(inheritedDeleteMenuIds, menuId -> {
RoleMenuExclusionDO entity = new RoleMenuExclusionDO();
entity.setRoleId(roleId);
entity.setMenuId(menuId);
return entity;
}));
}
if (CollUtil.isNotEmpty(deleteMenuIds)) {
roleMenuMapper.deleteListByRoleIdAndMenuIds(roleId, deleteMenuIds);
}
}
}
@@ -303,7 +324,7 @@ public class PermissionServiceImpl implements PermissionService {
Set<Long> userRoleIdListByUserId = permissionService.getUserRoleIdListByUserId(getLoginUserId());
// 如果为标准角色,只允许管理员修改数据权限
if (RoleTypeEnum.NORMAL.getType().equals(role.getType()) && !roleService.hasAnySuperAdmin(userRoleIdListByUserId)) {
throw exception(ROLE_CAN_NOT_UPDATE_SYSTEM_TYPE_ROLE);
throw exception(ROLE_CAN_NOT_UPDATE_NORMAL_TYPE_ROLE);
}
roleService.updateRoleDataScope(roleId, dataScope, dataScopeDeptIds);
}