🐛 fix: 用户根据角色获取菜单问题

This commit is contained in:
Bunny 2024-10-26 15:13:25 +08:00
parent bf62e081c0
commit d1a2f5a59a
6 changed files with 66 additions and 27 deletions

View File

@ -19,7 +19,7 @@ public class AssignRolesToRoutersDto {
@Schema(name = "routerId", title = "路由id") @Schema(name = "routerId", title = "路由id")
@NotNull(message = "路由id不能为空") @NotNull(message = "路由id不能为空")
private Long routerId; private List<Long> routerIds;
@Schema(name = "roleIds", title = "角色id列表") @Schema(name = "roleIds", title = "角色id列表")
@NotNull(message = "角色id列表不能为空") @NotNull(message = "角色id列表不能为空")

View File

@ -40,4 +40,11 @@ public class RouterRoleController {
routerRoleService.assignRolesToRouter(dto); routerRoleService.assignRolesToRouter(dto);
return Mono.just(Result.success()); return Mono.just(Result.success());
} }
@Operation(summary = "清除选中菜单所有角色", description = "清除选中菜单所有角色")
@DeleteMapping("clearAllRolesSelect")
public Mono<Result<String>> clearAllRolesSelect(@RequestBody List<Long> routerIds) {
routerRoleService.clearAllRolesSelect(routerIds);
return Mono.just(Result.success());
}
} }

View File

@ -30,4 +30,11 @@ public interface RouterRoleService extends IService<RouterRole> {
* @param dto 路由分配角色 * @param dto 路由分配角色
*/ */
void assignRolesToRouter(AssignRolesToRoutersDto dto); void assignRolesToRouter(AssignRolesToRoutersDto dto);
/**
* 清除选中菜单所有角色
*
* @param routerIds 路由id
*/
void clearAllRolesSelect(List<Long> routerIds);
} }

View File

@ -1,7 +1,9 @@
package cn.bunny.services.service.impl; package cn.bunny.services.service.impl;
import cn.bunny.common.service.exception.BunnyException;
import cn.bunny.dao.dto.system.router.AssignRolesToRoutersDto; import cn.bunny.dao.dto.system.router.AssignRolesToRoutersDto;
import cn.bunny.dao.entity.system.RouterRole; import cn.bunny.dao.entity.system.RouterRole;
import cn.bunny.dao.pojo.result.ResultCodeEnum;
import cn.bunny.services.mapper.RouterRoleMapper; import cn.bunny.services.mapper.RouterRoleMapper;
import cn.bunny.services.service.RouterRoleService; import cn.bunny.services.service.RouterRoleService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
@ -9,6 +11,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
@ -42,19 +45,38 @@ public class RouterRoleServiceImpl extends ServiceImpl<RouterRoleMapper, RouterR
*/ */
@Override @Override
public void assignRolesToRouter(AssignRolesToRoutersDto dto) { public void assignRolesToRouter(AssignRolesToRoutersDto dto) {
Long routerId = dto.getRouterId(); List<Long> routerIds = dto.getRouterIds();
List<Long> roleIds = dto.getRoleIds(); List<Long> roleIds = dto.getRoleIds();
// 删除这个用户下所有已经分配好的角色内容 // 删除这个路由下所有已经分配好的角色内容
baseMapper.deleteBatchIdsByRouterIdsWithPhysics(List.of(routerId)); baseMapper.deleteBatchIdsByRouterIdsWithPhysics(routerIds);
// 保存分配好的角色信息 // 保存分配好的角色信息
List<RouterRole> roleList = roleIds.stream().map(roleId -> { List<RouterRole> roleList = new ArrayList<>();
RouterRole routerRole = new RouterRole(); for (Long roleId : roleIds) {
routerRole.setRouterId(routerId); List<RouterRole> list = routerIds.stream().map(routerId -> {
routerRole.setRoleId(roleId); RouterRole routerRole = new RouterRole();
return routerRole; routerRole.setRouterId(routerId);
}).toList(); routerRole.setRoleId(roleId);
return routerRole;
}).toList();
roleList.addAll(list);
}
saveBatch(roleList); saveBatch(roleList);
} }
/**
* 清除选中菜单所有角色
*
* @param routerIds 路由id
*/
@Override
public void clearAllRolesSelect(List<Long> routerIds) {
if (routerIds.isEmpty()) {
throw new BunnyException(ResultCodeEnum.REQUEST_IS_EMPTY);
}
baseMapper.deleteBatchIdsByRouterIdsWithPhysics(routerIds);
}
} }

View File

@ -6,15 +6,17 @@ import cn.bunny.dao.dto.system.router.RouterAddDto;
import cn.bunny.dao.dto.system.router.RouterManageDto; import cn.bunny.dao.dto.system.router.RouterManageDto;
import cn.bunny.dao.dto.system.router.RouterUpdateByIdWithRankDto; import cn.bunny.dao.dto.system.router.RouterUpdateByIdWithRankDto;
import cn.bunny.dao.dto.system.router.RouterUpdateDto; import cn.bunny.dao.dto.system.router.RouterUpdateDto;
import cn.bunny.dao.entity.system.Power;
import cn.bunny.dao.entity.system.Role;
import cn.bunny.dao.entity.system.Router; import cn.bunny.dao.entity.system.Router;
import cn.bunny.dao.pojo.constant.RedisUserConstant;
import cn.bunny.dao.pojo.result.PageResult; import cn.bunny.dao.pojo.result.PageResult;
import cn.bunny.dao.pojo.result.ResultCodeEnum; import cn.bunny.dao.pojo.result.ResultCodeEnum;
import cn.bunny.dao.vo.system.router.RouterManageVo; import cn.bunny.dao.vo.system.router.RouterManageVo;
import cn.bunny.dao.vo.system.router.RouterMeta; import cn.bunny.dao.vo.system.router.RouterMeta;
import cn.bunny.dao.vo.system.router.UserRouterVo; import cn.bunny.dao.vo.system.router.UserRouterVo;
import cn.bunny.dao.vo.system.user.LoginVo;
import cn.bunny.services.factory.RouterServiceFactory; import cn.bunny.services.factory.RouterServiceFactory;
import cn.bunny.services.mapper.PowerMapper;
import cn.bunny.services.mapper.RoleMapper;
import cn.bunny.services.mapper.RouterMapper; import cn.bunny.services.mapper.RouterMapper;
import cn.bunny.services.security.custom.CustomCheckIsAdmin; import cn.bunny.services.security.custom.CustomCheckIsAdmin;
import cn.bunny.services.service.RouterService; import cn.bunny.services.service.RouterService;
@ -25,7 +27,6 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -49,7 +50,10 @@ public class RouterServiceImpl extends ServiceImpl<RouterMapper, Router> impleme
private RouterServiceFactory routerServiceFactory; private RouterServiceFactory routerServiceFactory;
@Autowired @Autowired
private RedisTemplate<String, Object> redisTemplate; private RoleMapper roleMapper;
@Autowired
private PowerMapper powerMapper;
/** /**
* * 获取路由内容 * * 获取路由内容
@ -58,17 +62,16 @@ public class RouterServiceImpl extends ServiceImpl<RouterMapper, Router> impleme
*/ */
@Override @Override
public List<UserRouterVo> getRouterAsync() { public List<UserRouterVo> getRouterAsync() {
// 当前用户id // 根据用户ID查询角色数据
String username = BaseContext.getUsername(); Long userId = BaseContext.getUserId();
LoginVo loginVo = (LoginVo) redisTemplate.opsForValue().get(RedisUserConstant.getAdminLoginInfoPrefix(username)); // 查询角色信息
if (loginVo == null) throw new BunnyException(ResultCodeEnum.FAIL); List<Role> roleList = roleMapper.selectListByUserId(userId);
List<String> roleCodeList = roleList.stream().map(Role::getRoleCode).toList();
// 角色列表 // 根据角色列表查询权限信息
List<String> roleList = loginVo.getRoles(); List<Power> powerList = powerMapper.selectListByUserId(userId);
List<String> powerCodeList = powerList.stream().map(Power::getPowerCode).toList();
// 权限列表
List<String> powerCodeList = loginVo.getPermissions();
// 路由列表根据用户角色判断 // 路由列表根据用户角色判断
List<Router> routerList; List<Router> routerList;
@ -77,10 +80,10 @@ public class RouterServiceImpl extends ServiceImpl<RouterMapper, Router> impleme
List<UserRouterVo> list = new ArrayList<>(); List<UserRouterVo> list = new ArrayList<>();
// 查询用户角色判断是否是管理员角色 // 查询用户角色判断是否是管理员角色
boolean isAdmin = CustomCheckIsAdmin.checkAdmin(roleList); boolean isAdmin = CustomCheckIsAdmin.checkAdmin(roleCodeList);
if (isAdmin) routerList = list(); if (isAdmin) routerList = list();
else { else {
List<Long> routerIds = baseMapper.selectListByUserId(loginVo.getId()); List<Long> routerIds = baseMapper.selectListByUserId(userId);
routerList = baseMapper.selectParentListByRouterId(routerIds); routerList = baseMapper.selectParentListByRouterId(routerIds);
} }
@ -99,7 +102,7 @@ public class RouterServiceImpl extends ServiceImpl<RouterMapper, Router> impleme
.rank(router.getRouterRank()) .rank(router.getRouterRank())
.icon(router.getIcon()) .icon(router.getIcon())
.title(router.getTitle()) .title(router.getTitle())
.roles(roleList) .roles(roleCodeList)
.auths(powerCodeList) .auths(powerCodeList)
.build(); .build();

View File

@ -24,7 +24,7 @@
<!-- 通用查询结果列 --> <!-- 通用查询结果列 -->
<sql id="Base_Column_List"> <sql id="Base_Column_List">
id, parent_id, path, component, frame_src, route_name, title, menuType, icon, router_rank, visible, create_user, update_user, update_time, create_time, is_deleted id, parent_id, path, component, frame_src, route_name, title, menu_type, icon, router_rank, visible, create_user, update_user, update_time, create_time, is_deleted
</sql> </sql>
<!-- 物理删除路由菜单 --> <!-- 物理删除路由菜单 -->