Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature-edit-password #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ package com.frontleaves.fantasticeditor.constant
enum class MailTemplateEnum(val subject: String, val template: String, val description: String, val hashCode: Boolean) {
USER_REGISTER("用户注册", "user-register", "用户进行注册时候所发送的内容", true),
USER_LOGIN("用户登录", "user-login", "用户进行登录时候所发送的内容", true),
USER_EDIT_PASSWORD("用户修改密码", "user-edit-password", "用户进行修改密码时候所发送的内容", true),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

若不是发送邮件最后的布尔是 false,已提交 javaDoc 文档修正

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import com.frontleaves.fantasticeditor.exceptions.library.CheckFailureException;
import com.frontleaves.fantasticeditor.models.dto.UserCurrentDTO;
import com.frontleaves.fantasticeditor.models.vo.api.auth.AuthUserEditPasswordVO;
import com.frontleaves.fantasticeditor.models.vo.api.auth.AuthUserLoginVO;
import com.frontleaves.fantasticeditor.models.vo.api.auth.AuthUserRegisterVO;
import com.frontleaves.fantasticeditor.services.interfaces.SmsService;
Expand Down Expand Up @@ -108,4 +109,25 @@ public ResponseEntity<BaseResponse<Void>> sendRegisterSmsCode(
userService.sendRegisterPhoneCode(phone);
return ResultUtil.success("发送成功");
}

/**
* 修改密码
* <p>
* 修改密码
*
* @param authUserEditPasswordVO 修改密码信息
* @return 修改结果
*/
@PostMapping("/edit/password")
public ResponseEntity<BaseResponse<Void>> editPassword(
@NotNull @RequestBody final AuthUserEditPasswordVO authUserEditPasswordVO,
@RequestHeader("X-USER-UUID") final String userUuid
) {
// 修改密码
if (userService.editPassword(authUserEditPasswordVO, userUuid)) {
return ResultUtil.success("修改密码成功");
} else {
throw new CheckFailureException("修改密码失败");
}
}
}
18 changes: 18 additions & 0 deletions src/main/kotlin/com/frontleaves/fantasticeditor/dao/UserDAO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,22 @@ class UserDAO(
this.getOne(QueryWrapper<SqlUserDO>().eq("phone", phone))
}
}

/**
* ## 修改密码
* 用户通过旧密码进行修改密码
*
* @param uuid 用户UUID
* @param password 新密码
* @return 是否修改成功
*/
fun editPassword(uuid: String, password: String): Boolean {
val user = this.getUserByUUID(uuid)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在服务层已判断,无需重复判断

return if (user != null) {
user.password = password
this.update(user, QueryWrapper<SqlUserDO>().eq("uuid", uuid))
} else {
false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ import org.apache.ibatis.annotations.Mapper
*/
@Mapper
interface UserMapper : BaseMapper<SqlUserDO>

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.frontleaves.fantasticeditor.models.vo.api.auth;

import jakarta.validation.constraints.NotBlank;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* 用户修改密码VO
* <p>
* 用于接收用户修改密码的信息
*
* @author DC_DC
* @version v1.0.0
* @since v1.0.0
*/
@Data
@NoArgsConstructor
public class AuthUserEditPasswordVO {
@NotBlank(message = "原密码不能为空")
public String oldPassword;
@NotBlank(message = "新密码不能为空")
public String newPassword;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.frontleaves.fantasticeditor.models.entity.redis.RedisSmsCodeDO;
import com.frontleaves.fantasticeditor.models.entity.sql.SqlRoleDO;
import com.frontleaves.fantasticeditor.models.entity.sql.SqlUserDO;
import com.frontleaves.fantasticeditor.models.vo.api.auth.AuthUserEditPasswordVO;
import com.frontleaves.fantasticeditor.models.vo.api.auth.AuthUserLoginVO;
import com.frontleaves.fantasticeditor.models.vo.api.auth.AuthUserRegisterVO;
import com.frontleaves.fantasticeditor.services.interfaces.MailService;
Expand All @@ -42,6 +43,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -255,4 +257,34 @@ public void sendMailVerify(@NotNull final String email) {
// 发送验证码
mailService.sendVerifyCodeMail(email, getNumberCode, MailTemplateEnum.USER_REGISTER);
}

@Override
public boolean editPassword(
@NotNull final AuthUserEditPasswordVO authUserEditPasswordVO,
@NotNull final String uuid
) {
// 获取用户信息
SqlUserDO getUser = userDAO.getUserByUUID(uuid);
if (getUser == null) {
throw new BusinessException("用户不存在", ErrorCode.USER_NOT_EXIST);
}
// 校验旧密码是否正确
if (!Util.INSTANCE.verifyPassword(authUserEditPasswordVO.getOldPassword(), getUser.getPassword())) {
throw new BusinessException("旧密码错误", ErrorCode.OPERATION_FAILED);
}
// 判断新旧密码是否相同
if (authUserEditPasswordVO.getOldPassword().equals(authUserEditPasswordVO.getNewPassword())) {
throw new BusinessException("新旧密码相同", ErrorCode.OPERATION_FAILED);
}
// 修改密码
if (userDAO.editPassword(uuid, Util.INSTANCE.encryptPassword(authUserEditPasswordVO.getNewPassword()))) {
mailService.sendMail(
getUser.getEmail(),
MailTemplateEnum.USER_EDIT_PASSWORD, Map.of("username", getUser.getUsername())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

要换行就都换

);
return true;
} else {
throw new BusinessException("修改失败", ErrorCode.OPERATION_FAILED);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.frontleaves.fantasticeditor.services.interfaces

import com.frontleaves.fantasticeditor.models.dto.UserCurrentDTO
import com.frontleaves.fantasticeditor.models.vo.api.auth.AuthUserEditPasswordVO
import com.frontleaves.fantasticeditor.models.vo.api.auth.AuthUserLoginVO
import com.frontleaves.fantasticeditor.models.vo.api.auth.AuthUserRegisterVO

Expand Down Expand Up @@ -72,4 +73,5 @@ interface UserService {
* @return 成功发送返回真,否则返回假
*/
fun sendMailVerify(email: String)
fun editPassword(authUserEditPasswordVO: AuthUserEditPasswordVO, uuid: String): Boolean
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

javaDoc

}
23 changes: 23 additions & 0 deletions src/main/resources/templates/mail/user-edit-password.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!--
~ ********************************************************************************
~ Copyright (C) 2024-NOW(至今) 妙笔智编
~ Author: 锋楪技术团队
~
~ 本文件包含 妙笔智编「FantasticEditor」 的源代码,该项目的所有源代码均遵循MIT开源许可证协议。
~ 本代码仅允许在十三届软件杯比赛授权比赛方可直接使用
~ ********************************************************************************
~ 免责声明:
~ 使用本软件的风险由用户自担。作者或版权持有人在法律允许的最大范围内,
~ 对因使用本软件内容而导致的任何直接或间接的损失不承担任何责任。
~ ********************************************************************************
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>修改密码成功</title>
</head>
<body>

</body>
</html>