fix:用户角色部门编辑问题修复

V0.5.x
xiwa 2023-06-13 08:28:19 +08:00
parent 343d1f00ee
commit b2295bdd4c
11 changed files with 92 additions and 68 deletions

View File

@ -1,19 +1,16 @@
package cc.iotkit.data.config.id;
import cc.iotkit.common.utils.SnowflakeIdGeneratorUtil;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Objects;
import com.github.yitter.contract.IdGeneratorOptions;
import com.github.yitter.idgen.YitIdHelper;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Objects;
/**
* @author: Longjun.Tu

View File

@ -62,14 +62,14 @@ public class SysDeptDataImpl implements ISysDeptData, IJPACommData<SysDept, Long
.and(ObjectUtil.isNotNull(dept.getParentId()), () -> tbSysDept.parentId.eq(dept.getParentId()))
.and(StringUtils.isNotEmpty(dept.getDeptName()), () -> tbSysDept.deptName.like(dept.getDeptName()))
.and(StringUtils.isNotEmpty(dept.getStatus()), () -> tbSysDept.status.eq(dept.getStatus()));
return MapstructUtils.convert(StreamSupport.stream(deptRepository.findAll(predicateBuilder.build()).spliterator(),false).collect(Collectors.toList()),SysDept.class);
return MapstructUtils.convert(StreamSupport.stream(deptRepository.findAll(predicateBuilder.build()).spliterator(), false).collect(Collectors.toList()), SysDept.class);
}
@Override
public List<SysDept> findByRoleId(Long roleId) {
List<TbSysDept> list = jpaQueryFactory.select(tbSysDept).from(tbSysDept).leftJoin(tbSysRoleDept).on(tbSysDept.id.eq(tbSysRoleDept.deptId))
.where(tbSysRoleDept.roleId.eq(roleId)).orderBy(tbSysDept.parentId.desc(), tbSysDept.orderNum.desc()).fetch();
return MapstructUtils.convert(list,SysDept.class);
return MapstructUtils.convert(list, SysDept.class);
}
@ -80,8 +80,8 @@ public class SysDeptDataImpl implements ISysDeptData, IJPACommData<SysDept, Long
@Override
public List<SysDept> findByDeptId(Long deptId) {
return MapstructUtils.convert(deptRepository.findAll().stream().filter(o->o.getAncestors().indexOf(deptId.toString())!=-1)
.collect(Collectors.toList()),SysDept.class);
return MapstructUtils.convert(deptRepository.findAll().stream().filter(o -> o.getAncestors().indexOf(deptId.toString()) != -1)
.collect(Collectors.toList()), SysDept.class);
}
@Override
@ -91,18 +91,18 @@ public class SysDeptDataImpl implements ISysDeptData, IJPACommData<SysDept, Long
if (ObjectUtil.isNotNull(deptId)) {
predicateBuilder.and(tbSysDept.id.ne(deptId));
}
Long aLong = jpaQueryFactory.select(tbSysDept.id.count()).where(predicateBuilder.build()).fetchOne();
if (aLong > 0) {
return true;
}
return false;
Long count = jpaQueryFactory.select(tbSysDept.id.count())
.from(tbSysDept)
.where(predicateBuilder.build())
.fetchOne();
return count==0;
}
@Override
public long selectNormalChildrenDeptById(Long deptId) {
PredicateBuilder predicateBuilder = PredicateBuilder.instance().and(tbSysDept.status.eq(UserConstants.DEPT_NORMAL));
return jpaQueryFactory.select(tbSysDept.ancestors).where(predicateBuilder.build()).fetch().stream().filter(o->o.indexOf(deptId.toString())!=-1).count();
return jpaQueryFactory.select(tbSysDept.ancestors).where(predicateBuilder.build()).fetch().stream().filter(o -> o.indexOf(deptId.toString()) != -1).count();
}

View File

@ -106,12 +106,17 @@ public class SysUserDataImpl implements ISysUserData, IJPACommData<SysUser, Long
SysUser convert = MapstructUtils.convert(sysUser, SysUser.class);
List<SysRole> sysRoles = sysRoleData.findByUserId(id);
convert.setRoles(sysRoles);
SysDept dept = sysDeptData.findById(convert.getDeptId());
Long deptId = convert.getDeptId();
if(deptId==null){
return convert;
}
SysDept dept = sysDeptData.findById(deptId);
if (ObjectUtil.isNotNull(dept)) {
convert.setDept(dept);
}
return convert;
}
@Override

View File

@ -9,16 +9,12 @@ import cc.iotkit.data.util.PredicateBuilder;
import cc.iotkit.model.system.SysUserRole;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;
import static cc.iotkit.data.model.QTbSysRole.tbSysRole;
import static cc.iotkit.data.model.QTbSysRoleMenu.tbSysRoleMenu;
import static cc.iotkit.data.model.QTbSysUserRole.tbSysUserRole;
/**

View File

@ -44,7 +44,6 @@ public class SysDeptController extends BaseController {
/**
*
*
*/
@ApiOperation("查询部门列表(排除节点)")
@SaCheckPermission("system:dept:list")
@ -59,12 +58,11 @@ public class SysDeptController extends BaseController {
/**
*
*
*/
@SaCheckPermission("system:dept:query")
@ApiOperation("根据部门编号获取详细信息")
@PostMapping(value = "/getInfo")
public SysDeptVo getInfo(@Validated @RequestBody Request<Long> bo) {
public SysDeptVo getInfo(@Validated @RequestBody Request<Long> bo) {
Long deptId = bo.getData();
deptService.checkDeptDataScope(deptId);
return deptService.selectDeptById(deptId);
@ -94,11 +92,9 @@ public class SysDeptController extends BaseController {
@PostMapping("/edit")
public void edit(@Validated @RequestBody Request<SysDeptBo> bo) {
SysDeptBo dept = bo.getData();
Long deptId = dept.getDeptId();
Long deptId = dept.getId();
deptService.checkDeptDataScope(deptId);
if (!deptService.checkDeptNameUnique(dept)) {
fail("修改部门'" + dept.getDeptName() + "'失败,部门名称已存在");
} else if (dept.getParentId().equals(deptId)) {
if (dept.getParentId().equals(deptId)) {
fail("修改部门'" + dept.getDeptName() + "'失败,上级部门不能是自己");
} else if (StringUtils.equals(UserConstants.DEPT_DISABLE, dept.getStatus())
&& deptService.selectNormalChildrenDeptById(deptId) > 0) {
@ -109,7 +105,6 @@ public class SysDeptController extends BaseController {
/**
*
*
*/
@SaCheckPermission("system:dept:remove")
@ApiOperation("删除部门")

View File

@ -95,11 +95,6 @@ public class SysPostController extends BaseController {
@Log(title = "岗位管理", businessType = BusinessType.UPDATE)
@PostMapping("/edit")
public void edit(@RequestBody @Validated(EditGroup.class) Request<SysPostBo> post) {
if (!postService.checkPostNameUnique(post.getData())) {
fail("修改岗位'" + post.getData().getPostName() + "'失败,岗位名称已存在");
} else if (!postService.checkPostCodeUnique(post.getData())) {
fail("修改岗位'" + post.getData().getPostName() + "'失败,岗位编码已存在");
}
postService.updatePost(post.getData());
}

View File

@ -18,6 +18,7 @@ import cc.iotkit.common.validate.QueryGroup;
import cc.iotkit.common.web.core.BaseController;
import cc.iotkit.system.dto.bo.SysDeptBo;
import cc.iotkit.system.dto.bo.SysUserBo;
import cc.iotkit.system.dto.bo.SysUserRolesBo;
import cc.iotkit.system.dto.vo.*;
import cc.iotkit.system.listener.SysUserImportListener;
import cc.iotkit.system.service.*;
@ -236,7 +237,6 @@ public class SysUserController extends BaseController {
/**
*
*
*/
@ApiOperation("根据用户编号获取授权角色")
@SaCheckPermission("system:user:query")
@ -254,17 +254,17 @@ public class SysUserController extends BaseController {
/**
*
*
* @param reqUserId Id
* @param roleIds ID
* @param userRole
*/
@ApiOperation("用户授权角色")
@SaCheckPermission("system:user:edit")
@Log(title = "用户管理", businessType = BusinessType.GRANT)
@PostMapping("/authRole")
public void insertAuthRole(Request<Long> reqUserId, List<Long> roleIds) {
Long userId = reqUserId.getData();
public void insertAuthRole(@RequestBody @Validated Request<SysUserRolesBo> userRole) {
SysUserRolesBo data = userRole.getData();
Long userId = data.getUserId();
userService.checkUserDataScope(userId);
userService.insertUserAuth(userId, roleIds);
userService.insertUserAuth(userId, data.getRoleIds());
}
/**

View File

@ -27,7 +27,7 @@ public class SysDeptBo extends BaseDto {
* id
*/
@NotNull(message = "部门id不能为空", groups = { EditGroup.class })
private Long deptId;
private Long id;
/**
* ID

View File

@ -26,7 +26,7 @@ public class SysPostBo extends BaseDto {
* ID
*/
@NotNull(message = "岗位ID不能为空", groups = { EditGroup.class })
private Long postId;
private Long id;
/**
*

View File

@ -0,0 +1,35 @@
package cc.iotkit.system.dto.bo;
import cc.iotkit.common.api.BaseDto;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import java.util.List;
/**
*
*
* @author sjg
*/
@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class SysUserRolesBo extends BaseDto {
/**
* ID
*/
@NotNull(message = "用户ID不能为空")
private Long userId;
/**
*
*/
@Size(min = 1, message = "用户角色不能为空")
private List<Long> roleIds;
}

View File

@ -1,6 +1,7 @@
package cc.iotkit.system.service.impl;
import cc.iotkit.common.constant.UserConstants;
import cc.iotkit.common.enums.ErrCode;
import cc.iotkit.common.exception.BizException;
import cc.iotkit.common.satoken.utils.LoginHelper;
import cc.iotkit.common.service.DeptService;
@ -11,9 +12,7 @@ import cc.iotkit.common.utils.TreeBuildUtils;
import cc.iotkit.data.system.ISysDeptData;
import cc.iotkit.data.system.ISysRoleData;
import cc.iotkit.data.system.ISysUserData;
import cc.iotkit.data.util.PredicateBuilder;
import cc.iotkit.model.system.SysDept;
import cc.iotkit.model.system.SysRole;
import cc.iotkit.system.dto.bo.SysDeptBo;
import cc.iotkit.system.dto.vo.SysDeptVo;
import cc.iotkit.system.service.ISysDeptService;
@ -28,8 +27,6 @@ import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import static cc.iotkit.data.model.QTbSysDept.tbSysDept;
/**
*
*
@ -178,7 +175,7 @@ public class SysDeptServiceImpl implements ISysDeptService, DeptService {
*/
@Override
public boolean checkDeptNameUnique(SysDeptBo dept) {
return sysDeptData.checkDeptNameUnique( dept.getDeptName(), dept.getParentId(), dept.getDeptId());
return sysDeptData.checkDeptNameUnique( dept.getDeptName(), dept.getParentId(), dept.getId());
}
/**
@ -226,26 +223,30 @@ public class SysDeptServiceImpl implements ISysDeptService, DeptService {
// @CacheEvict(cacheNames = CacheNames.SYS_DEPT, key = "#bo.deptId")
@Override
public void updateDept(SysDeptBo bo) {
// SysDept dept = MapstructUtils.convert(bo, SysDept.class);
// SysDept oldDept = baseMapper.selectById(dept.getDeptId());
// if (!oldDept.getParentId().equals(dept.getParentId())) {
// // 如果是新父部门 则校验是否具有新父部门权限 避免越权
// this.checkDeptDataScope(dept.getParentId());
// SysDept newParentDept = baseMapper.selectById(dept.getParentId());
// if (ObjectUtil.isNotNull(newParentDept) && ObjectUtil.isNotNull(oldDept)) {
// String newAncestors = newParentDept.getAncestors() + StringUtils.SEPARATOR + newParentDept.getDeptId();
// String oldAncestors = oldDept.getAncestors();
// dept.setAncestors(newAncestors);
// updateDeptChildren(dept.getDeptId(), newAncestors, oldAncestors);
// }
// }
// int result = baseMapper.updateById(dept);
// if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()) && StringUtils.isNotEmpty(dept.getAncestors())
// && !StringUtils.equals(UserConstants.DEPT_NORMAL, dept.getAncestors())) {
// // 如果该部门是启用状态,则启用该部门的所有上级部门
// updateParentDeptStatusNormal(dept);
// }
// return result;
SysDept dept = MapstructUtils.convert(bo, SysDept.class);
SysDept oldDept = sysDeptData.findById(bo.getId());
if(oldDept==null){
throw new BizException(ErrCode.DATA_NOT_EXIST);
}
if (!oldDept.getParentId().equals(dept.getParentId())) {
// 如果是新父部门 则校验是否具有新父部门权限 避免越权
this.checkDeptDataScope(dept.getParentId());
SysDept newParentDept = sysDeptData.findById(dept.getParentId());
if (ObjectUtil.isNotNull(newParentDept) && ObjectUtil.isNotNull(oldDept)) {
String newAncestors = newParentDept.getAncestors() + StringUtils.SEPARATOR + newParentDept.getId();
String oldAncestors = oldDept.getAncestors();
dept.setAncestors(newAncestors);
updateDeptChildren(dept.getId(), newAncestors, oldAncestors);
}
}
sysDeptData.save(dept);
if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()) && StringUtils.isNotEmpty(dept.getAncestors())
&& !StringUtils.equals(UserConstants.DEPT_NORMAL, dept.getAncestors())) {
// 如果该部门是启用状态,则启用该部门的所有上级部门
updateParentDeptStatusNormal(dept);
}
}
/**