!22 个人信息 修改信息、修改密码

Merge pull request !22 from niu_dehua/feat_userprofile
This commit is contained in:
芋道源码 2021-03-16 00:57:28 +08:00 committed by Gitee
commit 7d45cfa33a
10 changed files with 154 additions and 77 deletions

View File

@ -3,8 +3,9 @@ package cn.iocoder.dashboard.modules.system.controller.user;
import cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil;
import cn.iocoder.dashboard.common.pojo.CommonResult;
import cn.iocoder.dashboard.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserProfileRespVO;
import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserProfileUpdateReqVO;
import cn.iocoder.dashboard.modules.system.controller.user.vo.profile.SysUserProfileRespVO;
import cn.iocoder.dashboard.modules.system.controller.user.vo.profile.SysUserProfileUpdatePasswordReqVo;
import cn.iocoder.dashboard.modules.system.controller.user.vo.profile.SysUserProfileUpdateReqVO;
import cn.iocoder.dashboard.modules.system.convert.auth.SysAuthConvert;
import cn.iocoder.dashboard.modules.system.convert.user.SysUserConvert;
import cn.iocoder.dashboard.modules.system.dal.dataobject.permission.SysRoleDO;
@ -16,11 +17,17 @@ import cn.iocoder.dashboard.util.collection.CollectionUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
@ -57,12 +64,20 @@ public class SysUserProfileController {
@PutMapping("/update")
@ApiOperation("修改用户个人信息")
public CommonResult<Boolean> updateUserProfile(@RequestBody SysUserProfileUpdateReqVO reqVO, HttpServletRequest request) {
public CommonResult<Boolean> updateUserProfile(@Valid @RequestBody SysUserProfileUpdateReqVO reqVO, HttpServletRequest request) {
userService.updateUserProfile(reqVO);
SecurityFrameworkUtils.setLoginUser(SysAuthConvert.INSTANCE.convert(reqVO), request);
return success(true);
}
@PutMapping("/update-password")
@ApiOperation("修改用户个人密码")
public CommonResult<Boolean> updateUserProfilePassword(@Valid @RequestBody SysUserProfileUpdatePasswordReqVo reqVO, HttpServletRequest request) {
userService.updateUserPassword(reqVO);
SecurityFrameworkUtils.setLoginUser(SysAuthConvert.INSTANCE.convert(reqVO), request);
return success(true);
}
@PutMapping("/upload-avatar")
@ApiOperation("上传用户个人头像")
public CommonResult<Boolean> updateUserAvatar(@RequestParam("avatarFile") MultipartFile file) throws IOException {

View File

@ -0,0 +1,53 @@
package cn.iocoder.dashboard.modules.system.controller.user.vo.profile;
import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserBaseVO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import java.util.Date;
import java.util.Set;
@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
@ApiModel("用户个人中心信息 Response VO")
public class SysUserProfileRespVO extends SysUserBaseVO {
@ApiModelProperty(value = "用户编号", required = true, example = "1")
private Long id;
@ApiModelProperty(value = "状态", required = true, example = "1", notes = "参见 SysCommonStatusEnum 枚举类")
private Integer status;
@ApiModelProperty(value = "最后登陆 IP", required = true, example = "192.168.1.1")
private String loginIp;
@ApiModelProperty(value = "最后登录时间", required = true, example = "时间戳格式")
private Date loginDate;
@ApiModelProperty(value = "创建时间", required = true, example = "时间戳格式")
private Date createTime;
/**
* 所属角色
*/
@ApiModelProperty(value = "所属角色", required = true, example = "123456")
private Set<Role> roles;
@ApiModel("角色")
@Data
public static class Role {
@ApiModelProperty(value = "角色编号", required = true, example = "1")
private Long id;
@ApiModelProperty(value = "角色名称", required = true, example = "普通角色")
private String name;
}
}

View File

@ -0,0 +1,36 @@
package cn.iocoder.dashboard.modules.system.controller.user.vo.profile;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
/**
* 类名称SysUserUpdatePersonalPasswordReqVo
* ***********************
* <p>
* 类描述更新用户个人密码
*
* @author deng on 2021/3/15 22:04
*/
@ApiModel("用户个人中心更新密码 Response VO")
@Data
public class SysUserProfileUpdatePasswordReqVo {
@ApiModelProperty(value = "用户编号", required = true, example = "1024")
@NotNull(message = "用户编号不能为空")
private Long id;
@ApiModelProperty(value = "旧密码", required = true, example = "123456")
@NotEmpty(message = "旧密码不能为空")
@Length(min = 4, max = 16, message = "密码长度为 4-16 位")
private String oldPassword;
@ApiModelProperty(value = "新密码", required = true, example = "654321")
@NotEmpty(message = "新密码不能为空")
@Length(min = 4, max = 16, message = "密码长度为 4-16 位")
private String newPassword;
}

View File

@ -1,4 +1,4 @@
package cn.iocoder.dashboard.modules.system.controller.user.vo.user;
package cn.iocoder.dashboard.modules.system.controller.user.vo.profile;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ -6,6 +6,7 @@ import lombok.Data;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
@ApiModel("用户个人信息更新 Request VO")
@ -26,7 +27,7 @@ public class SysUserProfileUpdateReqVO {
private String email;
@ApiModelProperty(value = "手机号码", example = "15601691300")
@Size(max = 11, message = "手机号码长度不能超过11个字符")
@Pattern(regexp = "^1(3|4|5|7|8)\\d{9}$",message = "手机号码格式错误")
private String mobile;
@ApiModelProperty(value = "用户性别", example = "1", notes = "参见 SysSexEnum 枚举类")
@ -35,10 +36,4 @@ public class SysUserProfileUpdateReqVO {
@ApiModelProperty(value = "用户头像", example = "http://www.iocoder.cn/xxx.png")
private String avatar;
@ApiModelProperty(value = "旧密码", required = true, example = "123456")
private String oldPassword;
@ApiModelProperty(value = "新密码", required = true, example = "654321")
private String newPassword;
}

View File

@ -5,6 +5,7 @@ import lombok.Data;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.util.Set;
@ -39,7 +40,7 @@ public class SysUserBaseVO {
private String email;
@ApiModelProperty(value = "手机号码", example = "15601691300")
@Size(max = 11, message = "手机号码长度不能超过11个字符")
@Pattern(regexp = "^1(3|4|5|7|8)\\d{9}$",message = "手机号码格式错误")
private String mobile;
@ApiModelProperty(value = "用户性别", example = "1", notes = "参见 SysSexEnum 枚举类")
@ -48,5 +49,4 @@ public class SysUserBaseVO {
@ApiModelProperty(value = "用户头像", example = "http://www.iocoder.cn/xxx.png")
private String avatar;
}

View File

@ -1,37 +0,0 @@
package cn.iocoder.dashboard.modules.system.controller.user.vo.user;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import java.util.Set;
@ApiModel("用户个人中心信息 Response VO")
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class SysUserProfileRespVO extends SysUserRespVO {
/**
* 所属角色
*/
@ApiModelProperty(value = "所属角色", required = true, example = "123456")
private Set<Role> roles;
@ApiModel("角色")
@Data
public static class Role {
@ApiModelProperty(value = "角色编号", required = true, example = "1")
private Long id;
@ApiModelProperty(value = "角色名称", required = true, example = "普通角色")
private String name;
}
}

View File

@ -3,7 +3,8 @@ package cn.iocoder.dashboard.modules.system.convert.auth;
import cn.iocoder.dashboard.framework.security.core.LoginUser;
import cn.iocoder.dashboard.modules.system.controller.auth.vo.auth.SysAuthMenuRespVO;
import cn.iocoder.dashboard.modules.system.controller.auth.vo.auth.SysAuthPermissionInfoRespVO;
import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserProfileUpdateReqVO;
import cn.iocoder.dashboard.modules.system.controller.user.vo.profile.SysUserProfileUpdatePasswordReqVo;
import cn.iocoder.dashboard.modules.system.controller.user.vo.profile.SysUserProfileUpdateReqVO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.permission.SysMenuDO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.permission.SysRoleDO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.user.SysUserDO;
@ -41,6 +42,8 @@ public interface SysAuthConvert {
LoginUser convert(SysUserProfileUpdateReqVO reqVO);
LoginUser convert(SysUserProfileUpdatePasswordReqVo reqVO);
/**
* 将菜单列表构建成菜单树
*

View File

@ -1,11 +1,12 @@
package cn.iocoder.dashboard.modules.system.convert.user;
import cn.iocoder.dashboard.modules.system.controller.user.vo.profile.SysUserProfileRespVO;
import cn.iocoder.dashboard.modules.system.controller.user.vo.profile.SysUserProfileUpdatePasswordReqVo;
import cn.iocoder.dashboard.modules.system.controller.user.vo.profile.SysUserProfileUpdateReqVO;
import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserCreateReqVO;
import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserExcelVO;
import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserImportExcelVO;
import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserPageItemRespVO;
import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserProfileRespVO;
import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserProfileUpdateReqVO;
import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserUpdateReqVO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.dept.SysDeptDO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.permission.SysRoleDO;
@ -36,6 +37,7 @@ public interface SysUserConvert {
SysUserDO convert(SysUserProfileUpdateReqVO bean);
SysUserDO convert(SysUserProfileUpdatePasswordReqVo bean);
}

View File

@ -2,7 +2,14 @@ package cn.iocoder.dashboard.modules.system.service.user;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.dashboard.common.pojo.PageResult;
import cn.iocoder.dashboard.modules.system.controller.user.vo.user.*;
import cn.iocoder.dashboard.modules.system.controller.user.vo.profile.SysUserProfileUpdatePasswordReqVo;
import cn.iocoder.dashboard.modules.system.controller.user.vo.profile.SysUserProfileUpdateReqVO;
import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserCreateReqVO;
import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserExportReqVO;
import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserImportExcelVO;
import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserImportRespVO;
import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserPageReqVO;
import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserUpdateReqVO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.user.SysUserDO;
import cn.iocoder.dashboard.util.collection.CollectionUtils;
@ -41,6 +48,14 @@ public interface SysUserService {
*/
void updateUserProfile(SysUserProfileUpdateReqVO reqVO);
/**
* 修改用户个人密码
*
* @param reqVO 更新用户个人密码
*/
void updateUserPassword(SysUserProfileUpdatePasswordReqVo reqVO);
/**
* 更新用户头像
*

View File

@ -9,12 +9,13 @@ import cn.iocoder.dashboard.common.exception.ServiceException;
import cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil;
import cn.iocoder.dashboard.common.pojo.PageResult;
import cn.iocoder.dashboard.modules.infra.service.file.InfFileService;
import cn.iocoder.dashboard.modules.system.controller.user.vo.profile.SysUserProfileUpdatePasswordReqVo;
import cn.iocoder.dashboard.modules.system.controller.user.vo.profile.SysUserProfileUpdateReqVO;
import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserCreateReqVO;
import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserExportReqVO;
import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserImportExcelVO;
import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserImportRespVO;
import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserPageReqVO;
import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserProfileUpdateReqVO;
import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserUpdateReqVO;
import cn.iocoder.dashboard.modules.system.convert.user.SysUserConvert;
import cn.iocoder.dashboard.modules.system.dal.dataobject.dept.SysDeptDO;
@ -70,7 +71,7 @@ public class SysUserServiceImpl implements SysUserService {
public Long createUser(SysUserCreateReqVO reqVO) {
// 校验正确性
this.checkCreateOrUpdate(null, reqVO.getUsername(), reqVO.getMobile(), reqVO.getEmail(),
reqVO.getDeptId(), reqVO.getPostIds());
reqVO.getDeptId(), reqVO.getPostIds());
// 插入用户
SysUserDO user = SysUserConvert.INSTANCE.convert(reqVO);
user.setStatus(CommonStatusEnum.ENABLE.getStatus()); // 默认开启
@ -83,7 +84,7 @@ public class SysUserServiceImpl implements SysUserService {
public void updateUser(SysUserUpdateReqVO reqVO) {
// 校验正确性
this.checkCreateOrUpdate(reqVO.getId(), reqVO.getUsername(), reqVO.getMobile(), reqVO.getEmail(),
reqVO.getDeptId(), reqVO.getPostIds());
reqVO.getDeptId(), reqVO.getPostIds());
// 更新用户
SysUserDO updateObj = SysUserConvert.INSTANCE.convert(reqVO);
userMapper.updateById(updateObj);
@ -95,16 +96,17 @@ public class SysUserServiceImpl implements SysUserService {
this.checkUserExists(reqVO.getId());
this.checkEmailUnique(reqVO.getId(), reqVO.getEmail());
this.checkMobileUnique(reqVO.getId(), reqVO.getMobile());
// 校验填写密码
String encode = null;
if (this.checkOldPassword(reqVO.getId(), reqVO.getOldPassword(), reqVO.getNewPassword())) {
// 更新密码
encode = passwordEncoder.encode(reqVO.getNewPassword());
}
SysUserDO updateObj = SysUserConvert.INSTANCE.convert(reqVO);
if (StrUtil.isNotBlank(encode)) {
updateObj.setPassword(encode);
}
userMapper.updateById(SysUserConvert.INSTANCE.convert(reqVO));
}
@Override
public void updateUserPassword(SysUserProfileUpdatePasswordReqVo reqVO) {
// 校验旧密码密码
this.checkOldPassword(reqVO.getId(), reqVO.getOldPassword());
SysUserDO updateObj = new SysUserDO();
updateObj.setId(reqVO.getId());
// 加密密码
updateObj.setPassword(passwordEncoder.encode(reqVO.getNewPassword()));
userMapper.updateById(updateObj);
}
@ -314,26 +316,19 @@ public class SysUserServiceImpl implements SysUserService {
}
/**
* 校验旧密码新密码
* 校验旧密码
*
* @param id 用户 id
* @param oldPassword 旧密码
* @param newPassword 新密码
* @return 校验结果
*/
private boolean checkOldPassword(Long id, String oldPassword, String newPassword) {
if (id == null || StrUtil.isBlank(oldPassword) || StrUtil.isBlank(newPassword)) {
return false;
}
private void checkOldPassword(Long id, String oldPassword) {
SysUserDO user = userMapper.selectById(id);
if (user == null) {
throw ServiceExceptionUtil.exception(USER_NOT_EXISTS);
}
if (!passwordEncoder.matches(oldPassword, user.getPassword())) {
throw ServiceExceptionUtil.exception(USER_PASSWORD_FAILED);
}
return true;
}
@Override