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

dev
疯狂的狮子Li 7 months ago
parent 753b456b4e
commit 348e8eb5fe

@ -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 (

@ -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 (

@ -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>()

@ -20,6 +20,12 @@ import java.util.List;
*/
public interface SysRoleMapper extends BaseMapperPlus<SysRole, SysRoleVo> {
/**
* IDIDSQL
*
* @param userId ID
* @return IDSQL
*/
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);

@ -14,7 +14,13 @@ import java.util.List;
*/
public interface ISysConfigService {
/**
*
*
* @param config
* @param pageQuery
* @return
*/
TableDataInfo<SysConfigVo> selectPageConfigList(SysConfigBo config, PageQuery pageQuery);
/**

@ -14,7 +14,13 @@ import java.util.List;
*/
public interface ISysDictDataService {
/**
*
*
* @param dictData
* @param pageQuery
* @return
*/
TableDataInfo<SysDictDataVo> selectPageDictDataList(SysDictDataBo dictData, PageQuery pageQuery);
/**

@ -15,7 +15,13 @@ import java.util.List;
*/
public interface ISysDictTypeService {
/**
*
*
* @param dictType
* @param pageQuery
* @return
*/
TableDataInfo<SysDictTypeVo> selectPageDictTypeList(SysDictTypeBo dictType, PageQuery pageQuery);
/**

@ -14,7 +14,13 @@ import java.util.List;
*/
public interface ISysLogininforService {
/**
*
*
* @param logininfor
* @param pageQuery
* @return
*/
TableDataInfo<SysLogininforVo> selectPageLogininforList(SysLogininforBo logininfor, PageQuery pageQuery);
/**

@ -14,7 +14,13 @@ import java.util.List;
*/
public interface ISysNoticeService {
/**
*
*
* @param notice
* @param pageQuery
* @return
*/
TableDataInfo<SysNoticeVo> selectPageNoticeList(SysNoticeBo notice, PageQuery pageQuery);
/**

@ -14,6 +14,13 @@ import java.util.List;
*/
public interface ISysOperLogService {
/**
*
*
* @param operLog
* @param pageQuery
* @return
*/
TableDataInfo<SysOperLogVo> selectPageOperLogList(SysOperLogBo operLog, PageQuery pageQuery);
/**

@ -14,7 +14,13 @@ import java.util.List;
*/
public interface ISysPostService {
/**
*
*
* @param post
* @param pageQuery
* @return
*/
TableDataInfo<SysPostVo> selectPagePostList(SysPostBo post, PageQuery pageQuery);
/**

@ -16,7 +16,13 @@ import java.util.Set;
*/
public interface ISysRoleService {
/**
*
*
* @param role
* @param pageQuery
* @return
*/
TableDataInfo<SysRoleVo> selectPageRoleList(SysRoleBo role, PageQuery pageQuery);
/**

@ -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);

@ -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);

@ -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);

@ -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();

@ -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);

@ -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);

@ -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));

@ -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>
* 线TokenID
* 线 Redis
* </p>
*
* @param userIds ID
*/
@Override
public void cleanOnlineUser(List<Long> userIds) {
List<String> keys = StpUtil.searchTokenValue("", 0, -1, false);

@ -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);
}
/**

Loading…
Cancel
Save