feat(user):基本完成用户重设密码,待测。

This commit is contained in:
徐涛
2022-08-12 13:20:23 +08:00
parent 8dd9654d82
commit cd18170ee6
5 changed files with 174 additions and 23 deletions

View File

@@ -25,6 +25,8 @@ func InitializeUserController(router *gin.Engine) {
}
UserController.Router.POST("/login", UserController.Login)
UserController.Router.DELETE("/logout", security.MustAuthenticated, UserController.Logout)
UserController.Router.DELETE("/password/:uid", security.OPSAuthorize, UserController.InvalidUserPassword)
UserController.Router.PUT("/password", UserController.ResetUserPassword)
}
type LoginFormData struct {
@@ -77,3 +79,53 @@ func (_UserController) Logout(c *gin.Context) {
}
result.Success("用户已成功登出系统。")
}
func (_UserController) InvalidUserPassword(c *gin.Context) {
result := response.NewResult(c)
targetUserId := c.Param("uid")
verifyCode, err := service.UserService.InvalidUserPassword(targetUserId)
if errors.Is(err, &exceptions.NotFoundError{}) {
result.NotFound("未找到指定用户。")
return
}
if errors.Is(err, &exceptions.UnsuccessfulOperationError{}) {
result.Error(500, "未能成功更新用户的密码。")
return
}
if err != nil {
result.Error(500, err.Error())
return
}
result.QuickJson(http.StatusOK, http.StatusAccepted, "用户密码已经失效", gin.H{"verify": verifyCode})
}
type ResetPasswordFormData struct {
VerifyCode string `json:"verifyCode"`
Username string `json:"uname"`
NewPassword string `json:"newPass"`
}
func (_UserController) ResetUserPassword(c *gin.Context) {
result := response.NewResult(c)
resetForm := new(ResetPasswordFormData)
c.BindJSON(resetForm)
verified, err := service.UserService.VerifyUserPassword(resetForm.Username, resetForm.VerifyCode)
if !verified {
result.Error(http.StatusUnauthorized, "验证码不正确。")
return
}
if err != nil {
result.Error(http.StatusInternalServerError, err.Error())
return
}
completed, err := service.UserService.ResetUserPassword(resetForm.Username, resetForm.NewPassword)
if err != nil {
result.Error(http.StatusInternalServerError, err.Error())
return
}
if completed {
result.Success("用户凭据已更新。")
return
}
result.Error(http.StatusNotAcceptable, "用户凭据未能成功更新。")
}