feat(新增): 用户分配角色,路由分配角色
This commit is contained in:
parent
4c9db97991
commit
eb1399cea8
|
@ -0,0 +1,24 @@
|
|||
package cn.bunny.dao.dto.system.router;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
@Schema(name = "AssignRolesToRoutersDto对象", title = "路由分配角色", description = "路由分配角色")
|
||||
public class AssignRolesToRoutersDto {
|
||||
|
||||
@Schema(name = "routerId", title = "路由id")
|
||||
private Long routerId;
|
||||
|
||||
@Schema(name = "roleIds", title = "角色id列表")
|
||||
private List<Long> roleIds;
|
||||
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package cn.bunny.dao.dto.system.user;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
@Schema(name = "AssignRolesToUsersDto对象", title = "用户分配角色", description = "用户分配角色")
|
||||
public class AssignRolesToUsersDto {
|
||||
|
||||
@Schema(name = "userId", title = "用户id")
|
||||
private Long userId;
|
||||
|
||||
@Schema(name = "roleIds", title = "角色id列表")
|
||||
private List<Long> roleIds;
|
||||
|
||||
}
|
||||
|
|
@ -15,9 +15,9 @@ import lombok.experimental.Accessors;
|
|||
public class UserRole extends BaseEntity {
|
||||
|
||||
@Schema(name = "userId", title = "用户id")
|
||||
private String userId;
|
||||
private Long userId;
|
||||
|
||||
@Schema(name = "roleId", title = "角色id")
|
||||
private String roleId;
|
||||
private Long roleId;
|
||||
|
||||
}
|
|
@ -4,6 +4,8 @@ import cn.bunny.dao.vo.BaseVo;
|
|||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
|
@ -24,4 +26,7 @@ public class PowerVo extends BaseVo {
|
|||
@Schema(name = "requestUrl", title = "请求路径")
|
||||
private String requestUrl;
|
||||
|
||||
@Schema(name = "children", title = "子级")
|
||||
private List<PowerVo> children;
|
||||
|
||||
}
|
|
@ -69,4 +69,11 @@ public class PowerController {
|
|||
powerService.deletePower(ids);
|
||||
return Mono.just(Result.success(ResultCodeEnum.DELETE_SUCCESS));
|
||||
}
|
||||
|
||||
@Operation(summary = "获取所有权限", description = "获取所有权限")
|
||||
@GetMapping("getAllPowers")
|
||||
public Mono<Result<List<PowerVo>>> getAllPowers() {
|
||||
List<PowerVo> voList = powerService.getAllPowers();
|
||||
return Mono.just(Result.success(voList));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -69,4 +69,11 @@ public class RoleController {
|
|||
roleService.deleteRole(ids);
|
||||
return Mono.just(Result.success(ResultCodeEnum.DELETE_SUCCESS));
|
||||
}
|
||||
|
||||
@Operation(summary = "获取所有角色", description = "获取所有角色")
|
||||
@GetMapping("getAllRoles")
|
||||
public Mono<Result<List<RoleVo>>> getAllRoles() {
|
||||
List<RoleVo> roleVoList = roleService.getAllRoles();
|
||||
return Mono.just(Result.success(roleVoList));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,15 @@
|
|||
package cn.bunny.services.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import cn.bunny.dao.dto.system.router.AssignRolesToRoutersDto;
|
||||
import cn.bunny.dao.pojo.result.Result;
|
||||
import cn.bunny.services.service.RouterRoleService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
|
@ -11,8 +19,25 @@ import org.springframework.web.bind.annotation.RestController;
|
|||
* @author Bunny
|
||||
* @since 2024-09-26
|
||||
*/
|
||||
@Tag(name = "路由和角色", description = "路由和角色相关接口")
|
||||
@RestController
|
||||
@RequestMapping("/routerRole")
|
||||
@RequestMapping("admin/routerRole")
|
||||
public class RouterRoleController {
|
||||
|
||||
@Autowired
|
||||
private RouterRoleService routerRoleService;
|
||||
|
||||
@Operation(summary = "根据路由id获取所有角色", description = "根据路由id获取所有角色")
|
||||
@GetMapping("getRoleListByRouterId")
|
||||
public Mono<Result<List<String>>> getRoleListByRouterId(Long routerId) {
|
||||
List<String> roleListByRouterId = routerRoleService.getRoleListByRouterId(routerId);
|
||||
return Mono.just(Result.success(roleListByRouterId));
|
||||
}
|
||||
|
||||
@Operation(summary = "为菜单分配角色", description = "为菜单分配角色")
|
||||
@PostMapping("assignRolesToRouter")
|
||||
public Mono<Result<String>> assignRolesToRouter(@RequestBody AssignRolesToRoutersDto dto) {
|
||||
routerRoleService.assignRolesToRouter(dto);
|
||||
return Mono.just(Result.success());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,15 @@
|
|||
package cn.bunny.services.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import cn.bunny.dao.dto.system.user.AssignRolesToUsersDto;
|
||||
import cn.bunny.dao.pojo.result.Result;
|
||||
import cn.bunny.services.service.UserRoleService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
|
@ -11,8 +19,25 @@ import org.springframework.web.bind.annotation.RestController;
|
|||
* @author Bunny
|
||||
* @since 2024-09-26
|
||||
*/
|
||||
@Tag(name = "用户和角色", description = "用户和角色相关接口")
|
||||
@RestController
|
||||
@RequestMapping("/userRole")
|
||||
@RequestMapping("admin/userRole")
|
||||
public class UserRoleController {
|
||||
|
||||
@Autowired
|
||||
private UserRoleService userRoleService;
|
||||
|
||||
@Operation(summary = "根据用户id获取角色列表", description = "根据用户id获取角色列表")
|
||||
@GetMapping("getRoleListByUserId")
|
||||
public Mono<Result<List<String>>> getRoleListByUserId(Long userId) {
|
||||
List<String> roleVoList = userRoleService.getRoleListByUserId(userId);
|
||||
return Mono.just(Result.success(roleVoList));
|
||||
}
|
||||
|
||||
@Operation(summary = "为用户分配角色", description = "为用户分配角色")
|
||||
@PostMapping("assignRolesToUsers")
|
||||
public Mono<Result<String>> assignRolesToUsers(@RequestBody AssignRolesToUsersDto dto) {
|
||||
userRoleService.assignRolesToUsers(dto);
|
||||
return Mono.just(Result.success());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
package cn.bunny.services.factory;
|
||||
|
||||
import cn.bunny.dao.vo.system.rolePower.PowerVo;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class PowerFactory {
|
||||
|
||||
|
||||
/**
|
||||
* * 构建权限树形结构
|
||||
*
|
||||
* @param id 节点ID
|
||||
* @param powerVoList 节点列表
|
||||
* @return 树形列表
|
||||
*/
|
||||
public List<PowerVo> handlePowerVoChildren(Long id, List<PowerVo> powerVoList) {
|
||||
return powerVoList.stream()
|
||||
.filter(powerVo -> powerVo.getParentId().equals(id))
|
||||
.peek(powerVo -> powerVo.setChildren(handlePowerVoChildren(powerVo.getId(), powerVoList)))
|
||||
.toList();
|
||||
}
|
||||
}
|
|
@ -15,4 +15,10 @@ import org.apache.ibatis.annotations.Mapper;
|
|||
@Mapper
|
||||
public interface RouterRoleMapper extends BaseMapper<RouterRole> {
|
||||
|
||||
/**
|
||||
* 根据路由id删除所有角色和路由信息
|
||||
*
|
||||
* @param routerId 路由id
|
||||
*/
|
||||
void deleteBatchIdsWithPhysicsByRouterId(Long routerId);
|
||||
}
|
||||
|
|
|
@ -15,4 +15,10 @@ import org.apache.ibatis.annotations.Mapper;
|
|||
@Mapper
|
||||
public interface UserRoleMapper extends BaseMapper<UserRole> {
|
||||
|
||||
/**
|
||||
* * 删除这个用户id下所有的角色信息
|
||||
*
|
||||
* @param userId 用户id
|
||||
*/
|
||||
void deleteBatchIdsWithPhysicsByUserId(Long userId);
|
||||
}
|
||||
|
|
|
@ -49,4 +49,12 @@ public interface PowerService extends IService<Power> {
|
|||
* @param ids 删除id列表
|
||||
*/
|
||||
void deletePower(List<Long> ids);
|
||||
|
||||
/**
|
||||
* * 获取所有权限
|
||||
*
|
||||
* @return 所有权限列表
|
||||
*/
|
||||
List<PowerVo> getAllPowers();
|
||||
|
||||
}
|
||||
|
|
|
@ -49,4 +49,11 @@ public interface RoleService extends IService<Role> {
|
|||
* @param ids 删除id列表
|
||||
*/
|
||||
void deleteRole(List<Long> ids);
|
||||
|
||||
/**
|
||||
* * 获取所有角色
|
||||
*
|
||||
* @return 所有角色列表
|
||||
*/
|
||||
List<RoleVo> getAllRoles();
|
||||
}
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
package cn.bunny.services.service;
|
||||
|
||||
import cn.bunny.dao.dto.system.router.AssignRolesToRoutersDto;
|
||||
import cn.bunny.dao.entity.system.RouterRole;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 服务类
|
||||
|
@ -13,4 +16,18 @@ import com.baomidou.mybatisplus.extension.service.IService;
|
|||
*/
|
||||
public interface RouterRoleService extends IService<RouterRole> {
|
||||
|
||||
/**
|
||||
* * 根据路由id获取所有角色
|
||||
*
|
||||
* @param routerId 路由id
|
||||
* @return 角色列表
|
||||
*/
|
||||
List<String> getRoleListByRouterId(Long routerId);
|
||||
|
||||
/**
|
||||
* * 为菜单分配角色
|
||||
*
|
||||
* @param dto 路由分配角色
|
||||
*/
|
||||
void assignRolesToRouter(AssignRolesToRoutersDto dto);
|
||||
}
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
package cn.bunny.services.service;
|
||||
|
||||
import cn.bunny.dao.dto.system.user.AssignRolesToUsersDto;
|
||||
import cn.bunny.dao.entity.system.UserRole;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 服务类
|
||||
|
@ -13,4 +16,18 @@ import com.baomidou.mybatisplus.extension.service.IService;
|
|||
*/
|
||||
public interface UserRoleService extends IService<UserRole> {
|
||||
|
||||
/**
|
||||
* * 为用户分配角色
|
||||
*
|
||||
* @param dto 用户分配角色
|
||||
*/
|
||||
void assignRolesToUsers(AssignRolesToUsersDto dto);
|
||||
|
||||
/**
|
||||
* * 根据用户id获取角色列表
|
||||
*
|
||||
* @param userId 用户id
|
||||
* @return 角色列表
|
||||
*/
|
||||
List<String> getRoleListByUserId(Long userId);
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ import cn.bunny.dao.dto.system.rolePower.PowerUpdateDto;
|
|||
import cn.bunny.dao.entity.system.Power;
|
||||
import cn.bunny.dao.pojo.result.PageResult;
|
||||
import cn.bunny.dao.vo.system.rolePower.PowerVo;
|
||||
import cn.bunny.services.factory.PowerFactory;
|
||||
import cn.bunny.services.mapper.PowerMapper;
|
||||
import cn.bunny.services.service.PowerService;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
|
@ -15,6 +16,7 @@ import jakarta.validation.Valid;
|
|||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
@ -28,6 +30,12 @@ import java.util.List;
|
|||
@Service
|
||||
public class PowerServiceImpl extends ServiceImpl<PowerMapper, Power> implements PowerService {
|
||||
|
||||
private final PowerFactory powerFactory;
|
||||
|
||||
public PowerServiceImpl(PowerFactory powerFactory) {
|
||||
this.powerFactory = powerFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* * 权限 服务实现类
|
||||
*
|
||||
|
@ -89,4 +97,30 @@ public class PowerServiceImpl extends ServiceImpl<PowerMapper, Power> implements
|
|||
public void deletePower(List<Long> ids) {
|
||||
baseMapper.deleteBatchIdsWithPhysics(ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* * 获取所有权限
|
||||
*
|
||||
* @return 所有权限列表
|
||||
*/
|
||||
@Override
|
||||
public List<PowerVo> getAllPowers() {
|
||||
List<Power> powerList = list();
|
||||
ArrayList<PowerVo> powerVos = new ArrayList<>();
|
||||
|
||||
// 构建返回波对象
|
||||
List<PowerVo> powerVoList = powerList.stream().map(power -> {
|
||||
PowerVo powerVo = new PowerVo();
|
||||
BeanUtils.copyProperties(power, powerVo);
|
||||
return powerVo;
|
||||
}).toList();
|
||||
|
||||
powerVoList.stream()
|
||||
.filter(power -> power.getParentId() == 0)
|
||||
.forEach(powerVo -> {
|
||||
powerVo.setChildren(powerFactory.handlePowerVoChildren(powerVo.getId(), powerVoList));
|
||||
powerVos.add(powerVo);
|
||||
});
|
||||
return powerVos;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -89,4 +89,18 @@ public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements Ro
|
|||
public void deleteRole(List<Long> ids) {
|
||||
baseMapper.deleteBatchIdsWithPhysics(ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* * 获取所有角色
|
||||
*
|
||||
* @return 所有角色列表
|
||||
*/
|
||||
@Override
|
||||
public List<RoleVo> getAllRoles() {
|
||||
return list().stream().map(role -> {
|
||||
RoleVo roleVo = new RoleVo();
|
||||
BeanUtils.copyProperties(role, roleVo);
|
||||
return roleVo;
|
||||
}).toList();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,15 @@
|
|||
package cn.bunny.services.service.impl;
|
||||
|
||||
import cn.bunny.dao.dto.system.router.AssignRolesToRoutersDto;
|
||||
import cn.bunny.dao.entity.system.RouterRole;
|
||||
import cn.bunny.services.mapper.RouterRoleMapper;
|
||||
import cn.bunny.services.service.RouterRoleService;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
|
@ -15,6 +20,41 @@ import org.springframework.stereotype.Service;
|
|||
* @since 2024-09-26
|
||||
*/
|
||||
@Service
|
||||
@Transactional
|
||||
public class RouterRoleServiceImpl extends ServiceImpl<RouterRoleMapper, RouterRole> implements RouterRoleService {
|
||||
|
||||
/**
|
||||
* * 根据路由id获取所有角色
|
||||
*
|
||||
* @param routerId 路由id
|
||||
* @return 角色列表
|
||||
*/
|
||||
@Override
|
||||
public List<String> getRoleListByRouterId(Long routerId) {
|
||||
return list(Wrappers.<RouterRole>lambdaQuery().eq(RouterRole::getRouterId, routerId)).stream()
|
||||
.map(routerRole -> routerRole.getRoleId().toString()).toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* * 为菜单分配角色
|
||||
*
|
||||
* @param dto 路由分配角色
|
||||
*/
|
||||
@Override
|
||||
public void assignRolesToRouter(AssignRolesToRoutersDto dto) {
|
||||
Long routerId = dto.getRouterId();
|
||||
List<Long> roleIds = dto.getRoleIds();
|
||||
|
||||
// 删除这个用户下所有已经分配好的角色内容
|
||||
baseMapper.deleteBatchIdsWithPhysicsByRouterId(routerId);
|
||||
|
||||
// 保存分配好的角色信息
|
||||
List<RouterRole> roleList = roleIds.stream().map(roleId -> {
|
||||
RouterRole routerRole = new RouterRole();
|
||||
routerRole.setRouterId(routerId);
|
||||
routerRole.setRoleId(roleId);
|
||||
return routerRole;
|
||||
}).toList();
|
||||
saveBatch(roleList);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,18 @@
|
|||
package cn.bunny.services.service.impl;
|
||||
|
||||
import cn.bunny.common.service.exception.BunnyException;
|
||||
import cn.bunny.dao.dto.system.user.AssignRolesToUsersDto;
|
||||
import cn.bunny.dao.entity.system.UserRole;
|
||||
import cn.bunny.dao.pojo.result.ResultCodeEnum;
|
||||
import cn.bunny.services.mapper.UserRoleMapper;
|
||||
import cn.bunny.services.service.UserRoleService;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
|
@ -15,6 +23,46 @@ import org.springframework.stereotype.Service;
|
|||
* @since 2024-09-26
|
||||
*/
|
||||
@Service
|
||||
@Transactional
|
||||
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {
|
||||
|
||||
@Autowired
|
||||
private UserRoleMapper userRoleMapper;
|
||||
|
||||
/**
|
||||
* * 根据用户id获取角色列表
|
||||
*
|
||||
* @param userId 用户id
|
||||
* @return 角色列表
|
||||
*/
|
||||
@Override
|
||||
public List<String> getRoleListByUserId(Long userId) {
|
||||
if (userId == null) throw new BunnyException(ResultCodeEnum.REQUEST_IS_EMPTY);
|
||||
|
||||
List<UserRole> userRoles = userRoleMapper.selectList(Wrappers.<UserRole>lambdaQuery().eq(UserRole::getUserId, userId));
|
||||
return userRoles.stream().map(userRole -> userRole.getRoleId().toString()).toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* * 为用户分配角色
|
||||
*
|
||||
* @param dto 用户分配角色
|
||||
*/
|
||||
@Override
|
||||
public void assignRolesToUsers(AssignRolesToUsersDto dto) {
|
||||
Long userId = dto.getUserId();
|
||||
List<Long> roleIds = dto.getRoleIds();
|
||||
|
||||
// 删除这个用户下所有已经分配好的角色内容
|
||||
baseMapper.deleteBatchIdsWithPhysicsByUserId(userId);
|
||||
|
||||
// 保存分配好的角色信息
|
||||
List<UserRole> roleList = roleIds.stream().map(roleId -> {
|
||||
UserRole userRole = new UserRole();
|
||||
userRole.setUserId(userId);
|
||||
userRole.setRoleId(roleId);
|
||||
return userRole;
|
||||
}).toList();
|
||||
saveBatch(roleList);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,4 +19,11 @@
|
|||
id, router_id, role_id, create_user, update_time, update_user, create_time, is_deleted
|
||||
</sql>
|
||||
|
||||
<!-- 根据路由id删除所有角色和路由信息 -->
|
||||
<delete id="deleteBatchIdsWithPhysicsByRouterId">
|
||||
delete
|
||||
from sys_router_role
|
||||
where router_id = #{routerId}
|
||||
</delete>
|
||||
|
||||
</mapper>
|
||||
|
|
|
@ -19,4 +19,11 @@
|
|||
id, user_id, role_id, create_time, update_time, create_user, update_user, is_deleted
|
||||
</sql>
|
||||
|
||||
<!-- 删除这个用户id下所有的角色信息 -->
|
||||
<delete id="deleteBatchIdsWithPhysicsByUserId">
|
||||
delete
|
||||
from sys_user_role
|
||||
where user_id = #{userId}
|
||||
</delete>
|
||||
|
||||
</mapper>
|
||||
|
|
Loading…
Reference in New Issue