新增:新增修改密码功能,并优化部分以往代码

This commit is contained in:
2023-01-10 23:25:58 +08:00
parent 519124a3c7
commit a08fd7773e
14 changed files with 331 additions and 12 deletions

View File

@@ -44,9 +44,9 @@ public class LoginRequest implements Serializable {
private String username;
/**
* 密码
* 密码(加密后)
*/
@Schema(description = "密码")
@Schema(description = "密码(加密后)")
@NotBlank(message = "密码不能为空")
private String password;

View File

@@ -0,0 +1,52 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.system.model.request;
import java.io.Serializable;
import javax.validation.constraints.NotBlank;
import lombok.Data;
import io.swagger.v3.oas.annotations.media.Schema;
/**
* 修改密码信息
*
* @author Charles7c
* @since 2023/1/9 23:28
*/
@Data
@Schema(description = "修改密码信息")
public class UpdatePasswordRequest implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 当前密码(加密后)
*/
@Schema(description = "当前密码(加密后)")
@NotBlank(message = "当前密码不能为空")
private String oldPassword;
/**
* 新密码(加密后)
*/
@Schema(description = "新密码(加密后)")
@NotBlank(message = "新密码不能为空")
private String newPassword;
}

View File

@@ -55,4 +55,16 @@ public interface UserService {
* 用户信息
*/
void update(SysUser user);
/**
* 修改密码
*
* @param oldPassword
* 当前密码
* @param newPassword
* 新密码
* @param userId
* 用户 ID
*/
void updatePassword(String oldPassword, String newPassword, Long userId);
}

View File

@@ -17,6 +17,7 @@
package top.charles7c.cnadmin.system.service.impl;
import java.io.File;
import java.time.LocalDateTime;
import lombok.RequiredArgsConstructor;
@@ -34,8 +35,10 @@ import cn.hutool.core.util.StrUtil;
import top.charles7c.cnadmin.common.config.properties.LocalStorageProperties;
import top.charles7c.cnadmin.common.model.dto.LoginUser;
import top.charles7c.cnadmin.common.util.FileUtils;
import top.charles7c.cnadmin.common.util.SecureUtils;
import top.charles7c.cnadmin.common.util.helper.LoginHelper;
import top.charles7c.cnadmin.common.util.validate.CheckUtils;
import top.charles7c.cnadmin.common.util.validate.ValidationUtils;
import top.charles7c.cnadmin.system.mapper.UserMapper;
import top.charles7c.cnadmin.system.model.entity.SysUser;
import top.charles7c.cnadmin.system.service.UserService;
@@ -91,9 +94,43 @@ public class UserServiceImpl implements UserService {
userMapper.updateById(user);
// 更新登录用户信息
SysUser sysUser = userMapper.selectById(user.getUserId());
SysUser sysUser = this.getById(user.getUserId());
LoginUser loginUser = LoginHelper.getLoginUser();
BeanUtil.copyProperties(sysUser, loginUser);
LoginHelper.updateLoginUser(loginUser);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void updatePassword(String oldPassword, String newPassword, Long userId) {
SysUser sysUser = this.getById(userId);
ValidationUtils.exIfNotEqual(sysUser.getPassword(), SecureUtils.md5Salt(oldPassword, userId.toString()),
"当前密码错误");
// 更新密码和密码重置时间
LocalDateTime now = LocalDateTime.now();
userMapper.update(null,
new LambdaUpdateWrapper<SysUser>()
.set(SysUser::getPassword, SecureUtils.md5Salt(newPassword, userId.toString()))
.set(SysUser::getPwdResetTime, now).eq(SysUser::getUserId, userId));
// 更新登录用户信息
LoginUser loginUser = LoginHelper.getLoginUser();
loginUser.setPwdResetTime(now);
LoginHelper.updateLoginUser(loginUser);
}
/**
* 根据 ID 查询
*
* @param userId
* 用户 ID
* @return 用户信息
*/
private SysUser getById(Long userId) {
ValidationUtils.exIfNull(userId, "用户不存在");
SysUser sysUser = userMapper.selectById(userId);
ValidationUtils.exIfNull(sysUser, String.format("ID为 [%s] 的用户已不存在", userId));
return sysUser;
}
}