From ffcbb534f76c342cb0070952fcc5856af13b55f2 Mon Sep 17 00:00:00 2001 From: DC_DC Date: Sun, 26 May 2024 20:44:06 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat(=E4=BF=AE=E6=94=B9=E5=AF=86=E7=A0=81):?= =?UTF-8?q?=20=E7=94=A8=E6=88=B7=E9=80=9A=E8=BF=87=E6=97=A7=E5=AF=86?= =?UTF-8?q?=E7=A0=81=E8=BF=9B=E8=A1=8C=E4=BF=AE=E6=94=B9=E5=AF=86=E7=A0=81?= =?UTF-8?q?,=E4=BF=AE=E6=94=B9=E6=88=90=E5=8A=9F=E4=B9=8B=E5=90=8E?= =?UTF-8?q?=E9=82=AE=E4=BB=B6=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../constant/MailTemplateEnum.kt | 1 + .../controllers/v1/AuthController.java | 22 +++++++++++++ .../fantasticeditor/dao/UserDAO.kt | 18 +++++++++++ .../fantasticeditor/mappers/UserMapper.kt | 1 + .../vo/api/auth/AuthUserEditPasswordVO.java | 23 +++++++++++++ .../services/UserServiceImpl.java | 32 +++++++++++++++++++ .../services/interfaces/UserService.kt | 2 ++ .../templates/mail/user-edit-password.html | 23 +++++++++++++ 8 files changed, 122 insertions(+) create mode 100644 src/main/kotlin/com/frontleaves/fantasticeditor/models/vo/api/auth/AuthUserEditPasswordVO.java create mode 100644 src/main/resources/templates/mail/user-edit-password.html diff --git a/src/main/kotlin/com/frontleaves/fantasticeditor/constant/MailTemplateEnum.kt b/src/main/kotlin/com/frontleaves/fantasticeditor/constant/MailTemplateEnum.kt index d639b61..db317fa 100644 --- a/src/main/kotlin/com/frontleaves/fantasticeditor/constant/MailTemplateEnum.kt +++ b/src/main/kotlin/com/frontleaves/fantasticeditor/constant/MailTemplateEnum.kt @@ -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), } diff --git a/src/main/kotlin/com/frontleaves/fantasticeditor/controllers/v1/AuthController.java b/src/main/kotlin/com/frontleaves/fantasticeditor/controllers/v1/AuthController.java index eaa25bd..a057768 100644 --- a/src/main/kotlin/com/frontleaves/fantasticeditor/controllers/v1/AuthController.java +++ b/src/main/kotlin/com/frontleaves/fantasticeditor/controllers/v1/AuthController.java @@ -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; @@ -108,4 +109,25 @@ public ResponseEntity> sendRegisterSmsCode( userService.sendRegisterPhoneCode(phone); return ResultUtil.success("发送成功"); } + + /** + * 修改密码 + *

+ * 修改密码 + * + * @param authUserEditPasswordVO 修改密码信息 + * @return 修改结果 + */ + @PostMapping("/edit/password") + public ResponseEntity> 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("修改密码失败"); + } + } } diff --git a/src/main/kotlin/com/frontleaves/fantasticeditor/dao/UserDAO.kt b/src/main/kotlin/com/frontleaves/fantasticeditor/dao/UserDAO.kt index e365d44..68e55e0 100644 --- a/src/main/kotlin/com/frontleaves/fantasticeditor/dao/UserDAO.kt +++ b/src/main/kotlin/com/frontleaves/fantasticeditor/dao/UserDAO.kt @@ -98,4 +98,22 @@ class UserDAO( this.getOne(QueryWrapper().eq("phone", phone)) } } + + /** + * ## 修改密码 + * 用户通过旧密码进行修改密码 + * + * @param uuid 用户UUID + * @param password 新密码 + * @return 是否修改成功 + */ + fun editPassword(uuid: String, password: String): Boolean { + val user = this.getUserByUUID(uuid) + return if (user != null) { + user.password = password + this.update(user, QueryWrapper().eq("uuid", uuid)) + } else { + false + } + } } diff --git a/src/main/kotlin/com/frontleaves/fantasticeditor/mappers/UserMapper.kt b/src/main/kotlin/com/frontleaves/fantasticeditor/mappers/UserMapper.kt index ad3ba79..899570d 100644 --- a/src/main/kotlin/com/frontleaves/fantasticeditor/mappers/UserMapper.kt +++ b/src/main/kotlin/com/frontleaves/fantasticeditor/mappers/UserMapper.kt @@ -28,3 +28,4 @@ import org.apache.ibatis.annotations.Mapper */ @Mapper interface UserMapper : BaseMapper + diff --git a/src/main/kotlin/com/frontleaves/fantasticeditor/models/vo/api/auth/AuthUserEditPasswordVO.java b/src/main/kotlin/com/frontleaves/fantasticeditor/models/vo/api/auth/AuthUserEditPasswordVO.java new file mode 100644 index 0000000..cc15737 --- /dev/null +++ b/src/main/kotlin/com/frontleaves/fantasticeditor/models/vo/api/auth/AuthUserEditPasswordVO.java @@ -0,0 +1,23 @@ +package com.frontleaves.fantasticeditor.models.vo.api.auth; + +import jakarta.validation.constraints.NotBlank; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * 用户修改密码VO + *

+ * 用于接收用户修改密码的信息 + * + * @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; +} diff --git a/src/main/kotlin/com/frontleaves/fantasticeditor/services/UserServiceImpl.java b/src/main/kotlin/com/frontleaves/fantasticeditor/services/UserServiceImpl.java index af0aba8..42c46cc 100644 --- a/src/main/kotlin/com/frontleaves/fantasticeditor/services/UserServiceImpl.java +++ b/src/main/kotlin/com/frontleaves/fantasticeditor/services/UserServiceImpl.java @@ -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; @@ -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; @@ -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()) + ); + return true; + } else { + throw new BusinessException("修改失败", ErrorCode.OPERATION_FAILED); + } + } } diff --git a/src/main/kotlin/com/frontleaves/fantasticeditor/services/interfaces/UserService.kt b/src/main/kotlin/com/frontleaves/fantasticeditor/services/interfaces/UserService.kt index 7cb18f5..9afbbe9 100644 --- a/src/main/kotlin/com/frontleaves/fantasticeditor/services/interfaces/UserService.kt +++ b/src/main/kotlin/com/frontleaves/fantasticeditor/services/interfaces/UserService.kt @@ -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 @@ -72,4 +73,5 @@ interface UserService { * @return 成功发送返回真,否则返回假 */ fun sendMailVerify(email: String) + fun editPassword(authUserEditPasswordVO: AuthUserEditPasswordVO, uuid: String): Boolean } diff --git a/src/main/resources/templates/mail/user-edit-password.html b/src/main/resources/templates/mail/user-edit-password.html new file mode 100644 index 0000000..98914d0 --- /dev/null +++ b/src/main/resources/templates/mail/user-edit-password.html @@ -0,0 +1,23 @@ + + + + + + 修改密码成功 + + + + + \ No newline at end of file From 7279acb468b888a26d34db8424fd182131b1304b Mon Sep 17 00:00:00 2001 From: XiaoLFeng Date: Sun, 26 May 2024 22:21:14 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix(=E8=A1=A5=E4=B8=81):=20=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3=E9=82=AE=E4=BB=B6=E6=A8=A1=E6=9D=BF=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E4=BB=A5=E5=8F=8A=E8=A1=A5=E5=85=A8=E6=96=87=E6=A1=A3=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=EF=BC=9B=E4=BF=AE=E6=AD=A3=E9=83=A8=E5=88=86=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E8=A7=84=E8=8C=83=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../frontleaves/fantasticeditor/constant/MailTemplateEnum.kt | 4 +++- .../com/frontleaves/fantasticeditor/mappers/UserMapper.kt | 1 - .../frontleaves/fantasticeditor/services/MailServiceImpl.kt | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/com/frontleaves/fantasticeditor/constant/MailTemplateEnum.kt b/src/main/kotlin/com/frontleaves/fantasticeditor/constant/MailTemplateEnum.kt index db317fa..fe5beb9 100644 --- a/src/main/kotlin/com/frontleaves/fantasticeditor/constant/MailTemplateEnum.kt +++ b/src/main/kotlin/com/frontleaves/fantasticeditor/constant/MailTemplateEnum.kt @@ -21,9 +21,11 @@ package com.frontleaves.fantasticeditor.constant * @since v1.0.0 * @property description 邮件模板描述 * @property template 邮件模板名称 + * @property subject 邮件主题 + * @property hasCode 是否为验证码 * @author xiao_lfeng */ -enum class MailTemplateEnum(val subject: String, val template: String, val description: String, val hashCode: Boolean) { +enum class MailTemplateEnum(val subject: String, val template: String, val description: String, val hasCode: Boolean) { USER_REGISTER("用户注册", "user-register", "用户进行注册时候所发送的内容", true), USER_LOGIN("用户登录", "user-login", "用户进行登录时候所发送的内容", true), USER_EDIT_PASSWORD("用户修改密码", "user-edit-password", "用户进行修改密码时候所发送的内容", true), diff --git a/src/main/kotlin/com/frontleaves/fantasticeditor/mappers/UserMapper.kt b/src/main/kotlin/com/frontleaves/fantasticeditor/mappers/UserMapper.kt index 899570d..ad3ba79 100644 --- a/src/main/kotlin/com/frontleaves/fantasticeditor/mappers/UserMapper.kt +++ b/src/main/kotlin/com/frontleaves/fantasticeditor/mappers/UserMapper.kt @@ -28,4 +28,3 @@ import org.apache.ibatis.annotations.Mapper */ @Mapper interface UserMapper : BaseMapper - diff --git a/src/main/kotlin/com/frontleaves/fantasticeditor/services/MailServiceImpl.kt b/src/main/kotlin/com/frontleaves/fantasticeditor/services/MailServiceImpl.kt index f50233d..c8f4bbc 100644 --- a/src/main/kotlin/com/frontleaves/fantasticeditor/services/MailServiceImpl.kt +++ b/src/main/kotlin/com/frontleaves/fantasticeditor/services/MailServiceImpl.kt @@ -128,7 +128,7 @@ class MailServiceImpl( @Transactional override fun sendVerifyCodeMail(email: String, verifyCode: String, template: MailTemplateEnum) { // 检查该模板是否允许发送验证码 - if (!template.hashCode) { + if (!template.hasCode) { throw MailTemplateNotFoundException("该模板不支持发送验证码") } val parameters = mapOf("code" to verifyCode)