2022-04-22 15:26:39 +08:00
|
|
|
package net.maku.system.controller;
|
|
|
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
|
import lombok.AllArgsConstructor;
|
|
|
|
import net.maku.framework.common.page.PageResult;
|
|
|
|
import net.maku.framework.common.utils.Result;
|
|
|
|
import net.maku.framework.security.user.SecurityUser;
|
|
|
|
import net.maku.framework.security.user.UserDetail;
|
|
|
|
import net.maku.system.convert.SysUserConvert;
|
|
|
|
import net.maku.system.entity.SysUserEntity;
|
|
|
|
import net.maku.system.service.SysUserPostService;
|
|
|
|
import net.maku.system.service.SysUserRoleService;
|
|
|
|
import net.maku.system.service.SysUserService;
|
|
|
|
import net.maku.system.vo.user.*;
|
2022-04-23 23:01:54 +08:00
|
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
2022-04-22 15:26:39 +08:00
|
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
import javax.validation.Valid;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 用户管理
|
|
|
|
*
|
|
|
|
* @author 阿沐 babamu@126.com
|
|
|
|
*/
|
|
|
|
@RestController
|
|
|
|
@RequestMapping("sys/user")
|
|
|
|
@AllArgsConstructor
|
|
|
|
@Tag(name="用户管理")
|
|
|
|
public class SysUserController {
|
|
|
|
private final SysUserService sysUserService;
|
|
|
|
private final SysUserRoleService sysUserRoleService;
|
|
|
|
private final SysUserPostService sysUserPostService;
|
|
|
|
private final PasswordEncoder passwordEncoder;
|
|
|
|
|
|
|
|
@GetMapping("page")
|
|
|
|
@Operation(summary = "分页")
|
|
|
|
public Result<PageResult<SysUserVO>> page(@Valid SysUserQuery query){
|
|
|
|
PageResult<SysUserVO> page = sysUserService.page(query);
|
|
|
|
|
|
|
|
return Result.ok(page);
|
|
|
|
}
|
|
|
|
|
|
|
|
@GetMapping("{id}")
|
|
|
|
@Operation(summary = "信息")
|
2022-04-23 23:01:54 +08:00
|
|
|
@PreAuthorize("hasAuthority('sys:user:info')")
|
2022-04-22 15:26:39 +08:00
|
|
|
public Result<SysUserVO> get(@PathVariable("id") Long id){
|
|
|
|
SysUserEntity entity = sysUserService.getById(id);
|
|
|
|
|
|
|
|
SysUserVO vo = SysUserConvert.INSTANCE.convert(entity);
|
|
|
|
|
|
|
|
// 用户角色列表
|
|
|
|
List<Long> roleIdList = sysUserRoleService.getRoleIdList(id);
|
|
|
|
vo.setRoleIdList(roleIdList);
|
|
|
|
|
|
|
|
// 用户岗位列表
|
|
|
|
List<Long> postIdList = sysUserPostService.getPostIdList(id);
|
|
|
|
vo.setPostIdList(postIdList);
|
|
|
|
|
|
|
|
return Result.ok(vo);
|
|
|
|
}
|
|
|
|
|
|
|
|
@GetMapping("info")
|
|
|
|
@Operation(summary = "登录用户")
|
|
|
|
public Result<SysUserVO> info(){
|
|
|
|
SysUserVO user = SysUserConvert.INSTANCE.convert(SecurityUser.getUser());
|
|
|
|
|
|
|
|
return Result.ok(user);
|
|
|
|
}
|
|
|
|
|
|
|
|
@PutMapping("password")
|
|
|
|
@Operation(summary = "修改密码")
|
|
|
|
public Result<String> password(@RequestBody @Valid SysUserPasswordVO vo){
|
|
|
|
// 原密码不正确
|
|
|
|
UserDetail user = SecurityUser.getUser();
|
|
|
|
if(!passwordEncoder.matches(vo.getPassword(), user.getPassword())){
|
|
|
|
return Result.error("原密码不正确");
|
|
|
|
}
|
|
|
|
|
|
|
|
// 修改密码
|
|
|
|
sysUserService.updatePassword(user.getId(), passwordEncoder.encode(vo.getNewPassword()));
|
|
|
|
|
|
|
|
return Result.ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
@PostMapping
|
|
|
|
@Operation(summary = "保存")
|
2022-04-23 23:01:54 +08:00
|
|
|
@PreAuthorize("hasAuthority('sys:user:save')")
|
2022-04-22 15:26:39 +08:00
|
|
|
public Result<String> save(@RequestBody @Valid SysUserPostVO vo){
|
|
|
|
// 新增密码不能为空
|
|
|
|
if (StrUtil.isBlank(vo.getPassword())){
|
|
|
|
Result.error("密码不能为空");
|
|
|
|
}
|
|
|
|
|
|
|
|
// 密码加密
|
|
|
|
vo.setPassword(passwordEncoder.encode(vo.getPassword()));
|
|
|
|
|
|
|
|
// 保存
|
|
|
|
sysUserService.save(vo);
|
|
|
|
|
|
|
|
return Result.ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
@PutMapping
|
|
|
|
@Operation(summary = "修改")
|
2022-04-23 23:01:54 +08:00
|
|
|
@PreAuthorize("hasAuthority('sys:user:update')")
|
2022-04-22 15:26:39 +08:00
|
|
|
public Result<String> update(@RequestBody @Valid SysUserPutVO vo){
|
|
|
|
// 如果密码不为空,则进行加密处理
|
|
|
|
if(StrUtil.isBlank(vo.getPassword())){
|
|
|
|
vo.setPassword(null);
|
|
|
|
}else{
|
|
|
|
vo.setPassword(passwordEncoder.encode(vo.getPassword()));
|
|
|
|
}
|
|
|
|
|
|
|
|
sysUserService.update(vo);
|
|
|
|
|
|
|
|
return Result.ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
@DeleteMapping
|
|
|
|
@Operation(summary = "删除")
|
2022-04-23 23:01:54 +08:00
|
|
|
@PreAuthorize("hasAuthority('sys:user:delete')")
|
2022-04-22 15:26:39 +08:00
|
|
|
public Result<String> delete(@RequestBody List<Long> idList){
|
|
|
|
sysUserService.delete(idList);
|
|
|
|
|
|
|
|
return Result.ok();
|
|
|
|
}
|
|
|
|
}
|