1、角色管理后端接口
package com.cizhu.vo.query;
import com.cizhu.entity.Permission;
import lombok.Data;
@Data
public class RoleQueryVo extends Permission {
private Long pageNo = 1L;//当前页码
private Long pageSize = 10L;//每页显示数量
private Long userId;//用户ID
}
package com.cizhu.mapper;
import com.cizhu.entity.Role;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* <p>
* Mapper 接口
* </p>
*
* @author cizhu
* @since 2023-12-14
*/
public interface RoleMapper extends BaseMapper<Role> {
@Select("select count(1) from sys_user_role where role_id = #{roleId}")
int getRoleCountByRoleId(Long roleId);
@Delete("delete from sys_role_permission where role_id = #{roleId}")
void deleteRolePermissionByRoleId(Long roleId);
/**
* 保存角色权限关系
* @param roleId
* @param permissionIds
* @return
*/
int saveRolePermission(Long roleId, List<Long> permissionIds);
/**
* 根据用户ID查询该用户拥有的角色ID
* @param userId
* @return
*/
@Select("select role_id from `sys_user_role` where user_id = #{userId}")
List<Long> findRoleIdByUserId(Long userId);
}
package com.cizhu.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cizhu.entity.Role;
import com.baomidou.mybatisplus.extension.service.IService;
import com.cizhu.vo.query.RoleQueryVo;
import java.util.List;
/**
* <p>
* 服务类
* </p>
*
* @author cizhu
* @since 2023-12-14
*/
public interface IRoleService extends IService<Role> {
/**
* 根据用户查询角色列表
* @param page
* @param roleQueryVo
* @return
*/
IPage<Role> findRoleListByUserId(IPage<Role> page, RoleQueryVo roleQueryVo);
/***
* 检查该角色是否已被使用
* @param id:
* @return boolean
*/
boolean hasRoleCount(Long id);
/***
* 删除角色
* @param id:
* @return boolean
*/
boolean deleteRoleById(Long id);
/**
* 保存角色权限关系
* @param roleId
* @param permissionIds
* @return
*/
boolean saveRolePermission(Long roleId, List<Long> permissionIds);
/**
* 根据用户ID查询该用户拥有的角色ID
* @param userId
* @return
*/
List<Long> findRoleIdByUserId(Long userId);
}
package com.cizhu.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cizhu.entity.Role;
import com.cizhu.entity.User;
import com.cizhu.mapper.RoleMapper;
import com.cizhu.mapper.UserMapper;
import com.cizhu.service.IPermissionService;
import com.cizhu.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cizhu.utils.Result;
import com.cizhu.vo.query.RoleQueryVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
/**
* <p>
* 服务实现类
* </p>
*
* @author cizhu
* @since 2023-12-14
*/
@Service
@Transactional
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
@Resource
private UserMapper userMapper;
/**
* 根据用户查询角色列表
*
* @param page
* @param roleQueryVo
* @return
*/
@Override
public IPage<Role> findRoleListByUserId(IPage<Role> page, RoleQueryVo roleQueryVo) {
//创建条件构造器
QueryWrapper<Role> queryWrapper = new QueryWrapper<Role>();
//角色名称
queryWrapper.like(!ObjectUtils.isEmpty(roleQueryVo.getRoleName()),"role_name",roleQueryVo.getRoleName());
//排序
queryWrapper.orderByAsc("id");
//根据用户ID查询用户信息
User user = userMapper.selectById(roleQueryVo.getUserId());
//如果用户不为空、且不是管理员,则只能查询自己创建的角色
if(user!=null && !ObjectUtils.isEmpty(user.getIsAdmin()) && user.getIsAdmin() !=1){
queryWrapper.eq("create_user",roleQueryVo.getUserId());
}
return baseMapper.selectPage(page,queryWrapper);
}
/***
* 检查该角色是否已被使用
* @param id :
* @return boolean
*/
@Override
public boolean hasRoleCount(Long id) {
return baseMapper.getRoleCountByRoleId(id) > 0 ;
}
/***
* 删除角色
* @param id :
* @return boolean
*/
@Override
public boolean deleteRoleById(Long id) {
// 删除角色权限关系
baseMapper.deleteRolePermissionByRoleId(id);
// 删除角色
return baseMapper.deleteById(id) > 0 ;
}
/**
* 保存角色权限关系
*
* @param roleId
* @param permissionIds
* @return
*/
@Override
public boolean saveRolePermission(Long roleId, List<Long> permissionIds) {
//删除该角色对应的权限信息
baseMapper.deleteRolePermissionByRoleId(roleId);
//保存角色权限
return baseMapper.saveRolePermission(roleId,permissionIds)>0;
}
/**
* 根据用户ID查询该用户拥有的角色ID
*
* @param userId
* @return
*/
@Override
public List<Long> findRoleIdByUserId(Long userId) {
return baseMapper.findRoleIdByUserId(userId);
}
}
package com.cizhu.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cizhu.dto.RolePermissionDTO;
import com.cizhu.entity.Role;
import com.cizhu.service.IPermissionService;
import com.cizhu.service.IRoleService;
import com.cizhu.utils.Result;
import com.cizhu.vo.query.RolePermissionVo;
import com.cizhu.vo.query.RoleQueryVo;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
* <p>
* 前端控制器
* </p>
*
* @author cizhu
* @since 2023-12-14
*/
@RestController
@RequestMapping("/api/role")
public class RoleController {
@Resource
private IRoleService roleService;
@Resource
private IPermissionService permissionService;
/**
* 分页查询角色列表
* @param roleQueryVo
* @return
*/
@GetMapping("/list")
public Result list(RoleQueryVo roleQueryVo){
//创建分页对象
IPage<Role> page = new Page<Role>(roleQueryVo.getPageNo(),roleQueryVo.getPageSize());
//调用分页查询方法
roleService.findRoleListByUserId(page,roleQueryVo);
//返回数据
return Result.ok(page);
}
/**
* 添加角色
* @param role
* @return
*/
@PostMapping("/add")
public Result add(@RequestBody Role role){
if(roleService.save(role)){
return Result.ok().message("角色添加成功");
}
return Result.error().message("角色添加失败");
}
/**
* 修改角色
* @param role
* @return
*/
@PutMapping("/update")
public Result update(@RequestBody Role role){
if(roleService.updateById(role)){
return Result.ok().message("角色修改成功");
}
return Result.error().message("角色修改失败");
}
/**
* 删除角色
* @param id
* @return
*/
@DeleteMapping("/delete/{id}")
public Result delete(@PathVariable Long id){
if(roleService.deleteRoleById(id)){
return Result.ok().message("角色删除成功");
}
return Result.error().message("角色删除失败");
}
/**
* 检查该角色是否已被使用
* @param id
* @return
*/
@GetMapping("/check/{id}")
public Result check(@PathVariable Long id){
//检查该角色是否已被使用
if(roleService.hasRoleCount(id)){
return Result.exist().message("该角色已分配给其他用户使用,无法删除");
}
return Result.ok();
}
/**
* 分配权限-查询权限树数据
* @param userId
* @param roleId
* @return
*/
@GetMapping("/getAssignPermissionTree")
public Result getAssignPermissionTree(Long userId, Long roleId) {
//调用查询权限树数据的方法
RolePermissionVo permissionTree = permissionService.findPermissionTree(userId, roleId);
//返回数据
return Result.ok(permissionTree);
}
/**
* 分配权限-保存权限数据
*
* @param rolePermissionDTO
* @return
*/
@PostMapping("/saveRoleAssign")
public Result saveRoleAssign(@RequestBody RolePermissionDTO rolePermissionDTO) {
if (roleService.saveRolePermission(rolePermissionDTO.getRoleId(),
rolePermissionDTO.getList())) {
return Result.ok().message("权限分配成功");
} else {
return Result.error().message("权限分配失败");
}
}
}