fix: system management module - add dept/permission delete api, role assignment update, null-safety fixes
This commit is contained in:
@@ -95,17 +95,41 @@ public class TSysDeptController {
|
||||
@PermitAll
|
||||
public Result<TSysDept> update(TSysDept tSysDept) {
|
||||
log.info("先查询出父级部门,将要更新的子级部门level+1");
|
||||
Long parentId = 0L;
|
||||
if (parentId.equals(tSysDept.getParentId())){
|
||||
tSysDept.setLevel(1);
|
||||
}else {
|
||||
QueryWrapper<TSysDept> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("id",tSysDept.getParentId());
|
||||
TSysDept parentDept = tSysDeptService.getOne(queryWrapper);
|
||||
tSysDept.setLevel(parentDept.getLevel() + 1);
|
||||
if (tSysDept.getParentId() != null){
|
||||
Long parentId = 0L;
|
||||
if (parentId.equals(tSysDept.getParentId())){
|
||||
tSysDept.setLevel(1);
|
||||
}else {
|
||||
QueryWrapper<TSysDept> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("id",tSysDept.getParentId());
|
||||
TSysDept parentDept = tSysDeptService.getOne(queryWrapper);
|
||||
if (parentDept == null){
|
||||
return Result.error().message("父级部门不存在");
|
||||
}
|
||||
tSysDept.setLevel(parentDept.getLevel() + 1);
|
||||
}
|
||||
}
|
||||
boolean update = tSysDeptService.updateById(tSysDept);
|
||||
return Result.ok().setData(update);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除部门(逻辑删除)
|
||||
*
|
||||
* @param tSysDept
|
||||
* @return Result
|
||||
*/
|
||||
@RequestMapping("/delete")
|
||||
@PermitAll
|
||||
public Result<TSysDept> delete(TSysDept tSysDept) {
|
||||
if (tSysDept.getId() == null){
|
||||
return Result.error().message("部门ID不能为空");
|
||||
}
|
||||
TSysDept dept = new TSysDept();
|
||||
dept.setId(tSysDept.getId());
|
||||
dept.setDelFlag(true);
|
||||
boolean update = tSysDeptService.updateById(dept);
|
||||
return Result.ok().setData(update);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -123,19 +123,41 @@ public class TSysPermissionController extends BaseController {
|
||||
@PermitAll
|
||||
public Result<TSysPermission> update(TSysPermission tSysPermission) {
|
||||
log.info("先查询出父级权限,将要更新的子级部门level+1");
|
||||
Long parentId = 0L;
|
||||
if (parentId.equals(tSysPermission.getParentId())){
|
||||
tSysPermission.setLevel(1);
|
||||
}else {
|
||||
QueryWrapper<TSysPermission> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("id",tSysPermission.getParentId());
|
||||
TSysPermission parentDept = tSysPermissionService.getOne(queryWrapper);
|
||||
tSysPermission.setLevel(parentDept.getLevel() + 1);
|
||||
if (tSysPermission.getParentId() != null){
|
||||
Long parentId = 0L;
|
||||
if (parentId.equals(tSysPermission.getParentId())){
|
||||
tSysPermission.setLevel(1);
|
||||
}else {
|
||||
QueryWrapper<TSysPermission> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("id",tSysPermission.getParentId());
|
||||
TSysPermission parentPermission = tSysPermissionService.getOne(queryWrapper);
|
||||
if (parentPermission == null){
|
||||
return Result.error().message("父级菜单不存在");
|
||||
}
|
||||
tSysPermission.setLevel(parentPermission.getLevel() + 1);
|
||||
}
|
||||
}
|
||||
boolean update = tSysPermissionService.updateById(tSysPermission);
|
||||
return Result.ok().setData(update);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除菜单权限(前端 /delete 与 /remove 兼容)
|
||||
*
|
||||
* @param tSysPermission
|
||||
* @return Result
|
||||
*/
|
||||
@RequestMapping("/delete")
|
||||
@PermitAll
|
||||
public Result<TSysPermission> delete(TSysPermission tSysPermission) {
|
||||
try {
|
||||
boolean remove = tSysPermissionService.removeByPermissionId(tSysPermission.getId());
|
||||
return Result.ok().setData(remove);
|
||||
} catch (Exception e) {
|
||||
return Result.error();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除页面
|
||||
*
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.freedom.admin.base.BaseController;
|
||||
import com.freedom.admin.common.Result;
|
||||
import com.freedom.admin.config.MyException;
|
||||
import com.freedom.admin.entity.TSysRole;
|
||||
import com.freedom.admin.entity.TSysRolePermission;
|
||||
import com.freedom.admin.entity.TSysUser;
|
||||
import com.freedom.admin.service.TSysRolePermissionService;
|
||||
import com.freedom.admin.service.TSysRoleService;
|
||||
@@ -102,4 +103,32 @@ public class TSysRoleController extends BaseController {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除角色(逻辑删除 + 清理角色权限关联)
|
||||
* */
|
||||
@RequestMapping("/delete")
|
||||
@PermitAll
|
||||
public Result delete(TSysRole tSysRole){
|
||||
try {
|
||||
if (tSysRole.getId() == null) {
|
||||
return Result.error().message("角色ID不能为空");
|
||||
}
|
||||
// 删除角色权限关联
|
||||
QueryWrapper<TSysRolePermission> rolePermissionWrapper = new QueryWrapper<>();
|
||||
rolePermissionWrapper.eq("role_id", tSysRole.getId());
|
||||
tSysRolePermissionService.remove(rolePermissionWrapper);
|
||||
// 逻辑删除角色
|
||||
TSysRole role = new TSysRole();
|
||||
role.setId(tSysRole.getId());
|
||||
role.setDelFlag(true);
|
||||
boolean update = tSysRoleService.updateById(role);
|
||||
if (!update) {
|
||||
return Result.error().message("删除角色失败");
|
||||
}
|
||||
return Result.ok();
|
||||
} catch (Exception e) {
|
||||
return Result.error().message("删除角色失败");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ import com.freedom.admin.base.BaseController;
|
||||
import com.freedom.admin.common.Result;
|
||||
import com.freedom.admin.config.MyException;
|
||||
import com.freedom.admin.entity.TSysUser;
|
||||
import com.freedom.admin.entity.TSysUserRole;
|
||||
import com.freedom.admin.mapper.TSysUserRoleMapper;
|
||||
import com.freedom.admin.service.TSysUserService;
|
||||
import com.freedom.admin.service.UserService;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
@@ -13,10 +15,13 @@ import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.security.PermitAll;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author zhangjiaxun
|
||||
@@ -34,6 +39,9 @@ public class TSysUserController extends BaseController {
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private TSysUserRoleMapper tSysUserRoleMapper;
|
||||
|
||||
/**
|
||||
* 用户注册
|
||||
*
|
||||
@@ -68,6 +76,69 @@ public class TSysUserController extends BaseController {
|
||||
return Result.ok().setData(user);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前登录用户个人中心信息
|
||||
* */
|
||||
@RequestMapping("getPersonalInfo")
|
||||
@PermitAll
|
||||
public Result<TSysUser> getPersonalInfo(){
|
||||
TSysUser user = getUser();
|
||||
if (user == null) {
|
||||
return Result.error().message("用户未登录");
|
||||
}
|
||||
user.setPassword(null);
|
||||
return Result.ok().setData(user);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改密码
|
||||
* */
|
||||
@RequestMapping("updatePassword")
|
||||
@PermitAll
|
||||
public Result updatePassword(TSysUser tSysUser, String oldPassword, String newPassword){
|
||||
try {
|
||||
if (StringUtils.isBlank(oldPassword) || StringUtils.isBlank(newPassword)) {
|
||||
return Result.error().message("旧密码和新密码不能为空");
|
||||
}
|
||||
QueryWrapper<TSysUser> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("del_flag",0);
|
||||
queryWrapper.eq("id", tSysUser.getId());
|
||||
TSysUser user = tSysUserService.getOne(queryWrapper);
|
||||
if (user == null) {
|
||||
return Result.error().message("用户不存在");
|
||||
}
|
||||
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
|
||||
if (!bCryptPasswordEncoder.matches(oldPassword, user.getPassword())) {
|
||||
return Result.error().code(419).message("旧密码不正确");
|
||||
}
|
||||
user.setPassword(bCryptPasswordEncoder.encode(newPassword));
|
||||
user.setEncodeType("BCrypt");
|
||||
tSysUserService.updateById(user);
|
||||
return Result.ok();
|
||||
} catch (Exception e) {
|
||||
return Result.error().message("修改密码失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置密码
|
||||
* */
|
||||
@RequestMapping("resetPassword")
|
||||
@PermitAll
|
||||
public Result resetPassword(TSysUser tSysUser){
|
||||
try {
|
||||
TSysUser user = new TSysUser();
|
||||
user.setId(tSysUser.getId());
|
||||
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
|
||||
user.setPassword(bCryptPasswordEncoder.encode("123456"));
|
||||
user.setEncodeType("BCrypt");
|
||||
tSysUserService.updateById(user);
|
||||
return Result.ok().message("密码已重置为 123456");
|
||||
} catch (Exception e) {
|
||||
return Result.error().message("重置密码失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户数据列表
|
||||
* */
|
||||
@@ -80,6 +151,21 @@ public class TSysUserController extends BaseController {
|
||||
queryWrapper.eq("del_flag",0);
|
||||
queryWrapper.eq(StringUtils.isNotBlank(tSysUser.getUsername()),"username", tSysUser.getUsername());
|
||||
Page<TSysUser> page = tSysUserService.page(new Page<>(pageNum,pageSize), queryWrapper);
|
||||
// 填充每个用户的角色ID,用于编辑回显
|
||||
if (page.getRecords() != null) {
|
||||
for (TSysUser user : page.getRecords()) {
|
||||
QueryWrapper<TSysUserRole> roleWrapper = new QueryWrapper<>();
|
||||
roleWrapper.eq("user_id", user.getId());
|
||||
List<TSysUserRole> userRoleList = tSysUserRoleMapper.selectList(roleWrapper);
|
||||
if (userRoleList != null && !userRoleList.isEmpty()) {
|
||||
List<String> roleIdList = new ArrayList<>();
|
||||
for (TSysUserRole ur : userRoleList) {
|
||||
roleIdList.add(String.valueOf(ur.getRoleId()));
|
||||
}
|
||||
user.setRoleIds(String.join(",", roleIdList));
|
||||
}
|
||||
}
|
||||
}
|
||||
return Result.ok().setData(page);
|
||||
}
|
||||
|
||||
@@ -107,9 +193,24 @@ public class TSysUserController extends BaseController {
|
||||
|
||||
@RequestMapping("update")
|
||||
@PermitAll
|
||||
public Result update(TSysUser tSysUser){
|
||||
public Result update(TSysUser tSysUser,String userRoleStr){
|
||||
try {
|
||||
tSysUserService.updateById(tSysUser);
|
||||
// 更新用户角色关联
|
||||
if (StringUtils.isNotBlank(userRoleStr)) {
|
||||
QueryWrapper<TSysUserRole> delWrapper = new QueryWrapper<>();
|
||||
delWrapper.eq("user_id", tSysUser.getId());
|
||||
tSysUserRoleMapper.delete(delWrapper);
|
||||
List<TSysUserRole> roleList = com.alibaba.fastjson.JSONArray.parseArray(userRoleStr, TSysUserRole.class);
|
||||
if (roleList != null && !roleList.isEmpty()) {
|
||||
for (TSysUserRole ur : roleList) {
|
||||
ur.setUserId(tSysUser.getId());
|
||||
ur.setCreateBy(getUsername());
|
||||
ur.setCreateTime(new java.util.Date());
|
||||
tSysUserRoleMapper.insert(ur);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
return Result.error();
|
||||
}
|
||||
|
||||
@@ -90,5 +90,11 @@ public class TSysUser implements Serializable {
|
||||
*/
|
||||
private Boolean delFlag;
|
||||
|
||||
/**
|
||||
* 角色ID集合(非表字段,用于列表回显)
|
||||
*/
|
||||
@com.baomidou.mybatisplus.annotation.TableField(exist = false)
|
||||
private String roleIds;
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
}
|
||||
Reference in New Issue
Block a user