update 优化 代码增加注释与深化权限判断

This commit is contained in:
疯狂的狮子Li
2025-07-04 17:02:16 +08:00
parent 753b456b4e
commit 348e8eb5fe
21 changed files with 311 additions and 75 deletions

View File

@@ -20,12 +20,30 @@ import java.util.List;
*/
public interface SysDeptMapper extends BaseMapperPlus<SysDept, SysDeptVo> {
/**
* 构建角色对应的部门 SQL 查询语句
*
* <p>该 SQL 用于查询某个角色关联的所有部门 ID常用于数据权限控制</p>
*
* @param roleId 角色ID
* @return 查询部门ID的 SQL 语句字符串
*/
default String buildDeptByRoleSql(Long roleId) {
return """
select dept_id from sys_role_dept where role_id = %d
""".formatted(roleId);
}
/**
* 构建 SQL 查询用于获取当前角色拥有的部门中所有的父部门ID
*
* <p>
* 该 SQL 用于 deptCheckStrictly 场景下,排除非叶子节点(父节点)用。
* </p>
*
* @param roleId 角色ID
* @return SQL 语句字符串查询角色下部门的所有父部门ID
*/
default String buildParentDeptByRoleSql(Long roleId) {
return """
select parent_id from sys_dept where dept_id in (

View File

@@ -15,6 +15,16 @@ import java.util.List;
*/
public interface SysMenuMapper extends BaseMapperPlus<SysMenu, SysMenuVo> {
/**
* 构建用户权限菜单 SQL
*
* <p>
* 查询用户所属角色所拥有的菜单权限,用于权限判断、菜单加载等场景
* </p>
*
* @param userId 用户ID
* @return SQL 字符串,用于 inSql 条件
*/
default String buildMenuByUserSql(Long userId) {
return """
select menu_id from sys_role_menu where role_id in (
@@ -23,12 +33,34 @@ public interface SysMenuMapper extends BaseMapperPlus<SysMenu, SysMenuVo> {
""".formatted(userId);
}
/**
* 构建角色对应的菜单ID SQL 子查询
*
* <p>
* 用于根据角色ID查询其所拥有的菜单权限用于权限标识、菜单显示等场景
* 通常配合 inSql 使用
* </p>
*
* @param roleId 角色ID
* @return 查询菜单ID的 SQL 子查询字符串
*/
default String buildMenuByRoleSql(Long roleId) {
return """
select menu_id from sys_role_menu where role_id = %d
""".formatted(roleId);
}
/**
* 构建角色所关联菜单的父菜单ID查询 SQL
*
* <p>
* 用于配合菜单勾选树结构的 {@code menuCheckStrictly} 模式,过滤掉非叶子节点(父菜单),
* 只返回角色实际勾选的末级菜单
* </p>
*
* @param roleId 角色ID
* @return SQL 语句字符串查询菜单的父菜单ID
*/
default String buildParentMenuByRoleSql(Long roleId) {
return """
select parent_id from sys_menu where menu_id in (

View File

@@ -18,6 +18,13 @@ import java.util.List;
*/
public interface SysPostMapper extends BaseMapperPlus<SysPost, SysPostVo> {
/**
* 分页查询岗位列表
*
* @param page 分页对象
* @param queryWrapper 查询条件
* @return 包含岗位信息的分页结果
*/
@DataPermission({
@DataColumn(key = "deptName", value = "dept_id"),
@DataColumn(key = "userName", value = "create_by")
@@ -27,10 +34,10 @@ public interface SysPostMapper extends BaseMapperPlus<SysPost, SysPostVo> {
}
/**
* 分页查询岗位列表
* 查询岗位列表
*
* @param queryWrapper 查询条件
* @return 包含岗位信息的分页结果
* @return 岗位信息列表
*/
@DataPermission({
@DataColumn(key = "deptName", value = "dept_id"),
@@ -41,10 +48,24 @@ public interface SysPostMapper extends BaseMapperPlus<SysPost, SysPostVo> {
}
/**
* 查询用户所属岗位组
* 根据岗位ID集合查询岗位数量
*
* @param postIds 岗位ID列表
* @return 匹配的岗位数量
*/
@DataPermission({
@DataColumn(key = "deptName", value = "dept_id"),
@DataColumn(key = "userName", value = "create_by")
})
default long selectPostCount(List<Long> postIds) {
return this.selectCount(new LambdaQueryWrapper<SysPost>().in(SysPost::getPostId, postIds));
}
/**
* 根据用户ID查询其关联的岗位列表
*
* @param userId 用户ID
* @return 结果
* @return 岗位信息列表
*/
default List<SysPostVo> selectPostsByUserId(Long userId) {
return this.selectVoList(new LambdaQueryWrapper<SysPost>()

View File

@@ -20,6 +20,12 @@ import java.util.List;
*/
public interface SysRoleMapper extends BaseMapperPlus<SysRole, SysRoleVo> {
/**
* 构建根据用户ID查询角色ID的SQL子查询
*
* @param userId 用户ID
* @return 查询用户对应角色ID的SQL语句字符串
*/
default String buildRoleByUserSql(Long userId) {
return """
select role_id from sys_user_role where user_id = %d
@@ -42,7 +48,7 @@ public interface SysRoleMapper extends BaseMapperPlus<SysRole, SysRoleVo> {
}
/**
* 根据条件分页查询角色数据
* 根据条件查询角色数据
*
* @param queryWrapper 查询条件
* @return 角色数据集合信息
@@ -55,6 +61,20 @@ public interface SysRoleMapper extends BaseMapperPlus<SysRole, SysRoleVo> {
return this.selectVoList(queryWrapper);
}
/**
* 根据角色ID集合查询角色数量
*
* @param roleIds 角色ID列表
* @return 匹配的角色数量
*/
@DataPermission({
@DataColumn(key = "deptName", value = "create_dept"),
@DataColumn(key = "userName", value = "create_by")
})
default long selectRoleCount(List<Long> roleIds) {
return this.selectCount(new LambdaQueryWrapper<SysRole>().in(SysRole::getRoleId, roleIds));
}
/**
* 根据角色ID查询角色信息
*
@@ -62,8 +82,8 @@ public interface SysRoleMapper extends BaseMapperPlus<SysRole, SysRoleVo> {
* @return 对应的角色信息
*/
@DataPermission({
@DataColumn(key = "deptName", value = "r.create_dept"),
@DataColumn(key = "userName", value = "r.create_by")
@DataColumn(key = "deptName", value = "create_dept"),
@DataColumn(key = "userName", value = "create_by")
})
default SysRoleVo selectRoleById(Long roleId) {
return this.selectVoById(roleId);

View File

@@ -14,7 +14,13 @@ import java.util.List;
*/
public interface ISysConfigService {
/**
* 分页查询参数配置列表
*
* @param config 查询条件
* @param pageQuery 分页参数
* @return 参数配置分页列表
*/
TableDataInfo<SysConfigVo> selectPageConfigList(SysConfigBo config, PageQuery pageQuery);
/**

View File

@@ -14,7 +14,13 @@ import java.util.List;
*/
public interface ISysDictDataService {
/**
* 分页查询字典数据列表
*
* @param dictData 查询条件
* @param pageQuery 分页参数
* @return 字典数据分页列表
*/
TableDataInfo<SysDictDataVo> selectPageDictDataList(SysDictDataBo dictData, PageQuery pageQuery);
/**

View File

@@ -15,7 +15,13 @@ import java.util.List;
*/
public interface ISysDictTypeService {
/**
* 分页查询字典类型列表
*
* @param dictType 查询条件
* @param pageQuery 分页参数
* @return 字典类型分页列表
*/
TableDataInfo<SysDictTypeVo> selectPageDictTypeList(SysDictTypeBo dictType, PageQuery pageQuery);
/**

View File

@@ -14,7 +14,13 @@ import java.util.List;
*/
public interface ISysLogininforService {
/**
* 分页查询登录日志列表
*
* @param logininfor 查询条件
* @param pageQuery 分页参数
* @return 登录日志分页列表
*/
TableDataInfo<SysLogininforVo> selectPageLogininforList(SysLogininforBo logininfor, PageQuery pageQuery);
/**

View File

@@ -14,7 +14,13 @@ import java.util.List;
*/
public interface ISysNoticeService {
/**
* 分页查询通知公告列表
*
* @param notice 查询条件
* @param pageQuery 分页参数
* @return 通知公告分页列表
*/
TableDataInfo<SysNoticeVo> selectPageNoticeList(SysNoticeBo notice, PageQuery pageQuery);
/**

View File

@@ -14,6 +14,13 @@ import java.util.List;
*/
public interface ISysOperLogService {
/**
* 分页查询操作日志列表
*
* @param operLog 查询条件
* @param pageQuery 分页参数
* @return 操作日志分页列表
*/
TableDataInfo<SysOperLogVo> selectPageOperLogList(SysOperLogBo operLog, PageQuery pageQuery);
/**

View File

@@ -14,7 +14,13 @@ import java.util.List;
*/
public interface ISysPostService {
/**
* 分页查询岗位列表
*
* @param post 查询条件
* @param pageQuery 分页参数
* @return 岗位分页列表
*/
TableDataInfo<SysPostVo> selectPagePostList(SysPostBo post, PageQuery pageQuery);
/**

View File

@@ -16,7 +16,13 @@ import java.util.Set;
*/
public interface ISysRoleService {
/**
* 分页查询角色列表
*
* @param role 查询条件
* @param pageQuery 分页参数
* @return 角色分页列表
*/
TableDataInfo<SysRoleVo> selectPageRoleList(SysRoleBo role, PageQuery pageQuery);
/**

View File

@@ -39,6 +39,13 @@ public class SysConfigServiceImpl implements ISysConfigService {
private final SysConfigMapper baseMapper;
/**
* 分页查询参数配置列表
*
* @param config 查询条件
* @param pageQuery 分页参数
* @return 参数配置分页列表
*/
@Override
public TableDataInfo<SysConfigVo> selectPageConfigList(SysConfigBo config, PageQuery pageQuery) {
LambdaQueryWrapper<SysConfig> lqw = buildQueryWrapper(config);

View File

@@ -33,6 +33,13 @@ public class SysDictDataServiceImpl implements ISysDictDataService {
private final SysDictDataMapper baseMapper;
/**
* 分页查询字典数据列表
*
* @param dictData 查询条件
* @param pageQuery 分页参数
* @return 字典数据分页列表
*/
@Override
public TableDataInfo<SysDictDataVo> selectPageDictDataList(SysDictDataBo dictData, PageQuery pageQuery) {
LambdaQueryWrapper<SysDictData> lqw = buildQueryWrapper(dictData);

View File

@@ -43,6 +43,13 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService {
private final SysDictTypeMapper baseMapper;
private final SysDictDataMapper dictDataMapper;
/**
* 分页查询字典类型列表
*
* @param dictType 查询条件
* @param pageQuery 分页参数
* @return 字典类型分页列表
*/
@Override
public TableDataInfo<SysDictTypeVo> selectPageDictTypeList(SysDictTypeBo dictType, PageQuery pageQuery) {
LambdaQueryWrapper<SysDictType> lqw = buildQueryWrapper(dictType);

View File

@@ -32,6 +32,13 @@ public class SysLogininforServiceImpl implements ISysLogininforService {
private final SysLogininforMapper baseMapper;
/**
* 分页查询登录日志列表
*
* @param logininfor 查询条件
* @param pageQuery 分页参数
* @return 登录日志分页列表
*/
@Override
public TableDataInfo<SysLogininforVo> selectPageLogininforList(SysLogininforBo logininfor, PageQuery pageQuery) {
Map<String, Object> params = logininfor.getParams();

View File

@@ -34,6 +34,13 @@ public class SysNoticeServiceImpl implements ISysNoticeService {
private final SysNoticeMapper baseMapper;
private final SysUserMapper userMapper;
/**
* 分页查询通知公告列表
*
* @param notice 查询条件
* @param pageQuery 分页参数
* @return 通知公告分页列表
*/
@Override
public TableDataInfo<SysNoticeVo> selectPageNoticeList(SysNoticeBo notice, PageQuery pageQuery) {
LambdaQueryWrapper<SysNotice> lqw = buildQueryWrapper(notice);

View File

@@ -31,7 +31,13 @@ public class SysOperLogServiceImpl implements ISysOperLogService {
private final SysOperLogMapper baseMapper;
/**
* 分页查询操作日志列表
*
* @param operLog 查询条件
* @param pageQuery 分页参数
* @return 操作日志分页列表
*/
@Override
public TableDataInfo<SysOperLogVo> selectPageOperLogList(SysOperLogBo operLog, PageQuery pageQuery) {
LambdaQueryWrapper<SysOperLog> lqw = buildQueryWrapper(operLog);

View File

@@ -39,6 +39,13 @@ public class SysPostServiceImpl implements ISysPostService {
private final SysDeptMapper deptMapper;
private final SysUserPostMapper userPostMapper;
/**
* 分页查询岗位列表
*
* @param post 查询条件
* @param pageQuery 分页参数
* @return 岗位分页列表
*/
@Override
public TableDataInfo<SysPostVo> selectPagePostList(SysPostBo post, PageQuery pageQuery) {
Page<SysPostVo> page = baseMapper.selectPagePostList(pageQuery.build(), buildQueryWrapper(post));

View File

@@ -4,6 +4,7 @@ import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
@@ -38,6 +39,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import java.util.stream.Collectors;
/**
* 角色 业务层处理
@@ -53,6 +55,13 @@ public class SysRoleServiceImpl implements ISysRoleService {
private final SysUserRoleMapper userRoleMapper;
private final SysRoleDeptMapper roleDeptMapper;
/**
* 分页查询角色列表
*
* @param role 查询条件
* @param pageQuery 分页参数
* @return 角色分页列表
*/
@Override
public TableDataInfo<SysRoleVo> selectPageRoleList(SysRoleBo role, PageQuery pageQuery) {
Page<SysRoleVo> page = baseMapper.selectPageRoleList(pageQuery.build(), this.buildQueryWrapper(role));
@@ -60,7 +69,7 @@ public class SysRoleServiceImpl implements ISysRoleService {
}
/**
* 根据条件分页查询角色数据
* 根据条件查询角色数据
*
* @param role 角色信息
* @return 角色数据集合信息
@@ -347,19 +356,18 @@ public class SysRoleServiceImpl implements ISysRoleService {
* @param role 角色对象
*/
private int insertRoleMenu(SysRoleBo role) {
int rows = 1;
// 新增用户与角色管理
List<SysRoleMenu> list = new ArrayList<>();
for (Long menuId : role.getMenuIds()) {
SysRoleMenu rm = new SysRoleMenu();
rm.setRoleId(role.getRoleId());
rm.setMenuId(menuId);
list.add(rm);
Long[] menuIds = role.getMenuIds();
if (ArrayUtil.isEmpty(menuIds)) {
return 0;
}
if (CollUtil.isEmpty(list)) {
rows = roleMenuMapper.insertBatch(list) ? list.size() : 0;
}
return rows;
List<SysRoleMenu> roleMenuList = Arrays.stream(menuIds)
.map(menuId -> {
SysRoleMenu roleMenu = new SysRoleMenu();
roleMenu.setRoleId(role.getRoleId());
roleMenu.setMenuId(menuId);
return roleMenu;
}).collect(Collectors.toList());
return roleMenuMapper.insertBatch(roleMenuList) ? roleMenuList.size() : 0;
}
/**
@@ -368,19 +376,18 @@ public class SysRoleServiceImpl implements ISysRoleService {
* @param role 角色对象
*/
private int insertRoleDept(SysRoleBo role) {
int rows = 1;
// 新增角色与部门(数据权限)管理
List<SysRoleDept> list = new ArrayList<>();
for (Long deptId : role.getDeptIds()) {
SysRoleDept rd = new SysRoleDept();
rd.setRoleId(role.getRoleId());
rd.setDeptId(deptId);
list.add(rd);
Long[] deptIds = role.getDeptIds();
if (ArrayUtil.isEmpty(deptIds)) {
return 0;
}
if (CollUtil.isEmpty(list)) {
rows = roleDeptMapper.insertBatch(list) ? list.size() : 0;
}
return rows;
List<SysRoleDept> roleDeptList = Arrays.stream(deptIds)
.map(deptId -> {
SysRoleDept roleDept = new SysRoleDept();
roleDept.setRoleId(role.getRoleId());
roleDept.setDeptId(deptId);
return roleDept;
}).collect(Collectors.toList());
return roleDeptMapper.insertBatch(roleDeptList) ? roleDeptList.size() : 0;
}
/**
@@ -433,6 +440,9 @@ public class SysRoleServiceImpl implements ISysRoleService {
*/
@Override
public int deleteAuthUser(SysUserRole userRole) {
if (LoginHelper.getUserId().equals(userRole.getUserId())) {
throw new ServiceException("不允许修改当前用户角色!");
}
int rows = userRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>()
.eq(SysUserRole::getRoleId, userRole.getRoleId())
.eq(SysUserRole::getUserId, userRole.getUserId()));
@@ -452,6 +462,9 @@ public class SysRoleServiceImpl implements ISysRoleService {
@Override
public int deleteAuthUsers(Long roleId, Long[] userIds) {
List<Long> ids = List.of(userIds);
if (ids.contains(LoginHelper.getUserId())) {
throw new ServiceException("不允许修改当前用户角色!");
}
int rows = userRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>()
.eq(SysUserRole::getRoleId, roleId)
.in(SysUserRole::getUserId, ids));
@@ -473,6 +486,9 @@ public class SysRoleServiceImpl implements ISysRoleService {
// 新增用户与角色管理
int rows = 1;
List<Long> ids = List.of(userIds);
if (ids.contains(LoginHelper.getUserId())) {
throw new ServiceException("不允许修改当前用户角色!");
}
List<SysUserRole> list = StreamUtils.toList(ids, userId -> {
SysUserRole ur = new SysUserRole();
ur.setUserId(userId);
@@ -488,6 +504,17 @@ public class SysRoleServiceImpl implements ISysRoleService {
return rows;
}
/**
* 根据角色ID清除该角色关联的所有在线用户的登录状态踢出在线用户
*
* <p>
* 先判断角色是否绑定用户,若无绑定则直接返回
* 然后遍历当前所有在线Token查找拥有该角色的用户并强制登出
* 注意:在线用户量过大时,操作可能导致 Redis 阻塞,需谨慎调用
* </p>
*
* @param roleId 角色ID
*/
@Override
public void cleanOnlineUserByRole(Long roleId) {
// 如果角色未绑定用户 直接返回
@@ -519,6 +546,16 @@ public class SysRoleServiceImpl implements ISysRoleService {
});
}
/**
* 根据用户ID列表清除对应在线用户的登录状态踢出指定用户
*
* <p>
* 遍历当前所有在线Token匹配用户ID列表中的用户强制登出
* 注意:在线用户量过大时,操作可能导致 Redis 阻塞,需谨慎调用
* </p>
*
* @param userIds 需要清除的用户ID列表
*/
@Override
public void cleanOnlineUser(List<Long> userIds) {
List<String> keys = StpUtil.searchTokenValue("", 0, -1, false);

View File

@@ -33,6 +33,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@@ -447,27 +448,31 @@ public class SysUserServiceImpl implements ISysUserService {
* @param clear 清除已存在的关联数据
*/
private void insertUserPost(SysUserBo user, boolean clear) {
List<Long> postIds = List.of(user.getPostIds());
if (ArrayUtil.isNotEmpty(postIds)) {
// 判断是否具有此角色的操作权限
List<SysPostVo> posts = postMapper.selectPostList(
new LambdaQueryWrapper<SysPost>().in(SysPost::getPostId, postIds));
if (CollUtil.isEmpty(posts) || posts.size() != postIds.size()) {
throw new ServiceException("没有权限访问岗位的数据");
}
if (clear) {
// 删除用户与岗位关联
userPostMapper.delete(new LambdaQueryWrapper<SysUserPost>().eq(SysUserPost::getUserId, user.getUserId()));
}
// 新增用户与岗位管理
List<SysUserPost> list = StreamUtils.toList(postIds, postId -> {
Long[] postIdArr = user.getPostIds();
if (ArrayUtil.isEmpty(postIdArr)) {
return;
}
List<Long> postIds = Arrays.asList(postIdArr);
// 校验是否有权限操作这些岗位(含数据权限控制)
if (postMapper.selectPostCount(postIds) != postIds.size()) {
throw new ServiceException("没有权限访问岗位的数据");
}
// 是否清除旧的用户岗位绑定
if (clear) {
userPostMapper.delete(new LambdaQueryWrapper<SysUserPost>().eq(SysUserPost::getUserId, user.getUserId()));
}
// 构建用户岗位关联列表并批量插入
List<SysUserPost> list = StreamUtils.toList(postIds,
postId -> {
SysUserPost up = new SysUserPost();
up.setUserId(user.getUserId());
up.setPostId(postId);
return up;
});
userPostMapper.insertBatch(list);
}
userPostMapper.insertBatch(list);
}
/**
@@ -478,30 +483,36 @@ public class SysUserServiceImpl implements ISysUserService {
* @param clear 清除已存在的关联数据
*/
private void insertUserRole(Long userId, Long[] roleIds, boolean clear) {
if (ArrayUtil.isNotEmpty(roleIds)) {
List<Long> roleList = new ArrayList<>(List.of(roleIds));
if (!LoginHelper.isSuperAdmin(userId)) {
roleList.remove(SystemConstants.SUPER_ADMIN_ID);
}
// 判断是否具有此角色的操作权限
List<SysRoleVo> roles = roleMapper.selectRoleList(
new LambdaQueryWrapper<SysRole>().in(SysRole::getRoleId, roleList));
if (CollUtil.isEmpty(roles) || roles.size() != roleList.size()) {
throw new ServiceException("没有权限访问角色的数据");
}
if (clear) {
// 删除用户与角色关联
userRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getUserId, userId));
}
// 新增用户与角色管理
List<SysUserRole> list = StreamUtils.toList(roleList, roleId -> {
if (ArrayUtil.isEmpty(roleIds)) {
return;
}
List<Long> roleList = new ArrayList<>(Arrays.asList(roleIds));
// 非超级管理员,禁止包含超级管理员角色
if (!LoginHelper.isSuperAdmin(userId)) {
roleList.remove(SystemConstants.SUPER_ADMIN_ID);
}
// 校验是否有权限访问这些角色(含数据权限控制)
if (roleMapper.selectRoleCount(roleList) != roleList.size()) {
throw new ServiceException("没有权限访问角色的数据");
}
// 是否清除原有绑定
if (clear) {
userRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getUserId, userId));
}
// 批量插入用户-角色关联
List<SysUserRole> list = StreamUtils.toList(roleList,
roleId -> {
SysUserRole ur = new SysUserRole();
ur.setUserId(userId);
ur.setRoleId(roleId);
return ur;
});
userRoleMapper.insertBatch(list);
}
userRoleMapper.insertBatch(list);
}
/**