mirror of
				https://github.com/continew-org/continew-admin.git
				synced 2025-10-31 00:57:13 +08:00 
			
		
		
		
	新增:新增角色数据权限功能(基于 MyBatis Plus DataPermissionInterceptor 插件实现)
1.基于 MyBatis Plus DataPermissionInterceptor 插件实现的数据权限功能 2.通过在指定 Mapper 接口层方法添加 @DataPermission 注解实现数据权限
This commit is contained in:
		| @@ -33,6 +33,7 @@ import top.charles7c.cnadmin.common.util.helper.LoginHelper; | ||||
| import top.charles7c.cnadmin.common.util.validate.CheckUtils; | ||||
| import top.charles7c.cnadmin.system.model.entity.UserDO; | ||||
| import top.charles7c.cnadmin.system.service.DeptService; | ||||
| import top.charles7c.cnadmin.system.service.RoleService; | ||||
| import top.charles7c.cnadmin.system.service.UserService; | ||||
|  | ||||
| /** | ||||
| @@ -47,6 +48,7 @@ public class LoginServiceImpl implements LoginService { | ||||
|  | ||||
|     private final UserService userService; | ||||
|     private final DeptService deptService; | ||||
|     private final RoleService roleService; | ||||
|     private final PermissionService permissionService; | ||||
|  | ||||
|     @Override | ||||
| @@ -62,6 +64,7 @@ public class LoginServiceImpl implements LoginService { | ||||
|         loginUser.setDeptName(ExceptionUtils.exToNull(() -> deptService.get(loginUser.getDeptId()).getName())); | ||||
|         loginUser.setPermissions(permissionService.listPermissionByUserId(userId)); | ||||
|         loginUser.setRoles(permissionService.listRoleCodeByUserId(userId)); | ||||
|         loginUser.setRoleSet(roleService.listByUserId(userId)); | ||||
|         LoginHelper.login(loginUser); | ||||
|  | ||||
|         // 返回令牌 | ||||
|   | ||||
| @@ -46,7 +46,7 @@ public class PermissionServiceImpl implements PermissionService { | ||||
|     public Set<String> listPermissionByUserId(Long userId) { | ||||
|         Set<String> roleCodeSet = this.listRoleCodeByUserId(userId); | ||||
|         // 超级管理员赋予全部权限 | ||||
|         if (roleCodeSet.contains(SysConsts.SUPER_ADMIN)) { | ||||
|         if (roleCodeSet.contains(SysConsts.ADMIN_ROLE_CODE)) { | ||||
|             return CollUtil.newHashSet(SysConsts.ALL_PERMISSION); | ||||
|         } | ||||
|         return menuService.listPermissionByUserId(userId); | ||||
|   | ||||
| @@ -45,6 +45,11 @@ public class DeptDO extends BaseDO { | ||||
|      */ | ||||
|     private Long parentId; | ||||
|  | ||||
|     /** | ||||
|      * 祖级列表 | ||||
|      */ | ||||
|     private String ancestors; | ||||
|  | ||||
|     /** | ||||
|      * 描述 | ||||
|      */ | ||||
|   | ||||
| @@ -72,4 +72,10 @@ public class DeptRequest extends BaseRequest { | ||||
|      */ | ||||
|     @Schema(description = "状态(1启用 2禁用)", type = "Integer", allowableValues = {"1", "2"}) | ||||
|     private DisEnableStatusEnum status; | ||||
|  | ||||
|     /** | ||||
|      * 祖级列表 | ||||
|      */ | ||||
|     @Schema(description = "祖级列表") | ||||
|     private String ancestors; | ||||
| } | ||||
|   | ||||
| @@ -84,7 +84,7 @@ public class RoleVO extends BaseVO { | ||||
|     private Boolean disabled; | ||||
|  | ||||
|     public Boolean getDisabled() { | ||||
|         if (SysConsts.SUPER_ADMIN.equals(code)) { | ||||
|         if (SysConsts.ADMIN_ROLE_CODE.equals(code)) { | ||||
|             return true; | ||||
|         } | ||||
|         return disabled; | ||||
|   | ||||
| @@ -20,6 +20,7 @@ import java.util.List; | ||||
| import java.util.Set; | ||||
|  | ||||
| import top.charles7c.cnadmin.common.base.BaseService; | ||||
| import top.charles7c.cnadmin.common.model.dto.RoleDTO; | ||||
| import top.charles7c.cnadmin.common.model.vo.LabelValueVO; | ||||
| import top.charles7c.cnadmin.system.model.query.RoleQuery; | ||||
| import top.charles7c.cnadmin.system.model.request.RoleRequest; | ||||
| @@ -60,4 +61,13 @@ public interface RoleService extends BaseService<RoleVO, RoleDetailVO, RoleQuery | ||||
|      * @return 角色编码集合 | ||||
|      */ | ||||
|     Set<String> listCodeByUserId(Long userId); | ||||
|  | ||||
|     /** | ||||
|      * 根据用户 ID 查询角色 | ||||
|      * | ||||
|      * @param userId | ||||
|      *            用户 ID | ||||
|      * @return 角色集合 | ||||
|      */ | ||||
|     Set<RoleDTO> listByUserId(Long userId); | ||||
| } | ||||
|   | ||||
| @@ -16,6 +16,7 @@ | ||||
|  | ||||
| package top.charles7c.cnadmin.system.service.impl; | ||||
|  | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
| import java.util.Objects; | ||||
|  | ||||
| @@ -26,6 +27,8 @@ import lombok.RequiredArgsConstructor; | ||||
| import org.springframework.stereotype.Service; | ||||
| import org.springframework.transaction.annotation.Transactional; | ||||
|  | ||||
| import cn.hutool.core.collection.CollUtil; | ||||
|  | ||||
| import top.charles7c.cnadmin.common.base.BaseServiceImpl; | ||||
| import top.charles7c.cnadmin.common.constant.SysConsts; | ||||
| import top.charles7c.cnadmin.common.enums.DisEnableStatusEnum; | ||||
| @@ -62,6 +65,9 @@ public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, DeptDO, DeptVO, | ||||
|         CheckUtils.throwIf(() -> isExists, String.format("新增失败,'%s'已存在", name)); | ||||
|  | ||||
|         request.setStatus(DisEnableStatusEnum.ENABLE); | ||||
|         DeptDO parentDept = baseMapper.selectById(request.getParentId()); | ||||
|         CheckUtils.throwIfNull(parentDept, "上级部门不存在"); | ||||
|         request.setAncestors(String.format("%s,%s", parentDept.getAncestors(), request.getParentId())); | ||||
|         return super.add(request); | ||||
|     } | ||||
|  | ||||
| @@ -72,6 +78,14 @@ public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, DeptDO, DeptVO, | ||||
|         boolean isExists = this.checkNameExists(name, request.getParentId(), request.getId()); | ||||
|         CheckUtils.throwIf(() -> isExists, String.format("修改失败,'%s'已存在", name)); | ||||
|  | ||||
|         DeptDO oldDept = baseMapper.selectById(request.getId()); | ||||
|         // 更新祖级列表 | ||||
|         if (!Objects.equals(oldDept.getParentId(), request.getParentId())) { | ||||
|             DeptDO newParentDept = baseMapper.selectById(request.getParentId()); | ||||
|             CheckUtils.throwIfNull(newParentDept, "上级部门不存在"); | ||||
|             request.setAncestors(String.format("%s,%s", newParentDept.getAncestors(), request.getParentId())); | ||||
|             this.updateChildrenAncestors(request.getId(), request.getAncestors(), oldDept.getAncestors()); | ||||
|         } | ||||
|         super.update(request); | ||||
|     } | ||||
|  | ||||
| @@ -110,4 +124,30 @@ public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, DeptDO, DeptVO, | ||||
|         return baseMapper.lambdaQuery().eq(DeptDO::getName, name).eq(DeptDO::getParentId, parentId) | ||||
|             .ne(id != null, DeptDO::getId, id).exists(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 更新子部门祖级列表 | ||||
|      * | ||||
|      * @param id | ||||
|      *            ID | ||||
|      * @param newAncestors | ||||
|      *            新祖级列表 | ||||
|      * @param oldAncestors | ||||
|      *            原祖级列表 | ||||
|      */ | ||||
|     private void updateChildrenAncestors(Long id, String newAncestors, String oldAncestors) { | ||||
|         List<DeptDO> children = | ||||
|             baseMapper.lambdaQuery().apply(String.format("find_in_set(%s, `ancestors`)", id)).list(); | ||||
|         if (CollUtil.isEmpty(children)) { | ||||
|             return; | ||||
|         } | ||||
|         List<DeptDO> list = new ArrayList<>(children.size()); | ||||
|         for (DeptDO child : children) { | ||||
|             DeptDO dept = new DeptDO(); | ||||
|             dept.setId(child.getId()); | ||||
|             dept.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors)); | ||||
|             list.add(dept); | ||||
|         } | ||||
|         baseMapper.updateBatchById(list); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -17,6 +17,7 @@ | ||||
| package top.charles7c.cnadmin.system.service.impl; | ||||
|  | ||||
| import java.util.ArrayList; | ||||
| import java.util.HashSet; | ||||
| import java.util.List; | ||||
| import java.util.Set; | ||||
| import java.util.stream.Collectors; | ||||
| @@ -26,11 +27,13 @@ import lombok.RequiredArgsConstructor; | ||||
| import org.springframework.stereotype.Service; | ||||
| import org.springframework.transaction.annotation.Transactional; | ||||
|  | ||||
| import cn.hutool.core.bean.BeanUtil; | ||||
| import cn.hutool.core.collection.CollUtil; | ||||
|  | ||||
| import top.charles7c.cnadmin.common.base.BaseServiceImpl; | ||||
| import top.charles7c.cnadmin.common.constant.SysConsts; | ||||
| import top.charles7c.cnadmin.common.enums.DisEnableStatusEnum; | ||||
| import top.charles7c.cnadmin.common.model.dto.RoleDTO; | ||||
| import top.charles7c.cnadmin.common.model.vo.LabelValueVO; | ||||
| import top.charles7c.cnadmin.common.util.validate.CheckUtils; | ||||
| import top.charles7c.cnadmin.system.mapper.RoleMapper; | ||||
| @@ -106,7 +109,7 @@ public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, RoleDO, RoleVO, | ||||
|         if (detailObj instanceof RoleDetailVO) { | ||||
|             RoleDetailVO detailVO = (RoleDetailVO)detailObj; | ||||
|             Long roleId = detailVO.getId(); | ||||
|             if (SysConsts.SUPER_ADMIN.equals(detailVO.getCode())) { | ||||
|             if (SysConsts.ADMIN_ROLE_CODE.equals(detailVO.getCode())) { | ||||
|                 List<MenuVO> list = menuService.list(null, null); | ||||
|                 List<Long> menuIds = list.stream().map(MenuVO::getId).collect(Collectors.toList()); | ||||
|                 detailVO.setMenuIds(menuIds); | ||||
| @@ -138,6 +141,13 @@ public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, RoleDO, RoleVO, | ||||
|         return roleList.stream().map(RoleDO::getCode).collect(Collectors.toSet()); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public Set<RoleDTO> listByUserId(Long userId) { | ||||
|         List<Long> roleIds = userRoleService.listRoleIdByUserId(userId); | ||||
|         List<RoleDO> roleList = baseMapper.lambdaQuery().in(RoleDO::getId, roleIds).list(); | ||||
|         return new HashSet<>(BeanUtil.copyToList(roleList, RoleDTO.class)); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 检查名称是否存在 | ||||
|      * | ||||
|   | ||||
		Reference in New Issue
	
	Block a user