2023-08-11 09:39:18 +00:00
|
|
|
package logic
|
|
|
|
|
|
|
|
import (
|
2023-09-05 07:00:45 +00:00
|
|
|
"fmt"
|
|
|
|
"fusenapi/model/gmodel"
|
2023-08-11 09:39:18 +00:00
|
|
|
"fusenapi/utils/auth"
|
|
|
|
"fusenapi/utils/basic"
|
2023-09-05 07:00:45 +00:00
|
|
|
"fusenapi/utils/wevent"
|
2023-09-04 08:59:43 +00:00
|
|
|
"time"
|
2023-08-11 09:39:18 +00:00
|
|
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"fusenapi/server/auth/internal/svc"
|
|
|
|
"fusenapi/server/auth/internal/types"
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
2023-09-05 07:00:45 +00:00
|
|
|
"gorm.io/gorm"
|
2023-08-11 09:39:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type UserResetPasswordLogic struct {
|
|
|
|
logx.Logger
|
|
|
|
ctx context.Context
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUserResetPasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserResetPasswordLogic {
|
|
|
|
return &UserResetPasswordLogic{
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
ctx: ctx,
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 处理进入前逻辑w,r
|
|
|
|
// func (l *UserResetPasswordLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// }
|
|
|
|
|
2023-08-14 06:09:38 +00:00
|
|
|
func (l *UserResetPasswordLogic) UserResetPassword(req *types.RequestUserResetPassword, userinfo *auth.UserInfo) (resp *basic.Response) {
|
2023-08-11 09:39:18 +00:00
|
|
|
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
|
|
|
// userinfo 传入值时, 一定不为null
|
|
|
|
|
2023-09-05 07:00:45 +00:00
|
|
|
rt, err := l.svcCtx.ResetTokenManger.Decrypt(req.ResetToken) // ResetToken
|
2023-08-14 06:09:38 +00:00
|
|
|
if err != nil {
|
|
|
|
logx.Error(err)
|
2023-09-05 07:00:45 +00:00
|
|
|
return resp.SetStatus(basic.CodeOAuthResetTokenDecryptErr, err.Error())
|
2023-08-14 06:09:38 +00:00
|
|
|
}
|
|
|
|
|
2023-09-05 07:00:45 +00:00
|
|
|
// TODO: 存储
|
|
|
|
if rt.OperateType != auth.OpTypeResetToken {
|
|
|
|
return resp.SetStatus(basic.CodeOAuthTypeErr, "error OperateType: rt.OperateType != auth.OpTypeResetToken")
|
2023-08-14 06:09:38 +00:00
|
|
|
}
|
|
|
|
|
2023-09-05 07:00:45 +00:00
|
|
|
if time.Since(rt.CreateAt) > 30*time.Minute {
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeOAuthConfirmationTimeoutErr, "Verification links expire after 30 minute.")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = l.svcCtx.AllModels.FsUser.Transaction(l.ctx, func(tx *gorm.DB) error {
|
2023-09-05 08:20:16 +00:00
|
|
|
user := &gmodel.FsUser{}
|
2023-09-05 08:37:04 +00:00
|
|
|
err := tx.Where("id = ?", rt.UserId).Take(user).Error
|
2023-09-05 07:00:45 +00:00
|
|
|
if err != nil {
|
2023-09-05 08:16:57 +00:00
|
|
|
logx.Error(err)
|
2023-09-05 07:00:45 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-09-05 08:37:04 +00:00
|
|
|
|
2023-09-05 07:00:45 +00:00
|
|
|
if *user.PasswordHash != rt.OldPassword {
|
|
|
|
return fmt.Errorf("password had been reset")
|
|
|
|
}
|
2023-09-05 08:37:04 +00:00
|
|
|
|
2023-09-05 08:51:32 +00:00
|
|
|
if *user.PasswordHash == req.NewPassword {
|
2023-09-05 08:37:04 +00:00
|
|
|
return fmt.Errorf("the password is the same as the old one. It needs to be changed")
|
|
|
|
}
|
|
|
|
|
2023-09-05 08:51:32 +00:00
|
|
|
return tx.Where("id = ?", rt.UserId).Update("PasswordHash", req.NewPassword).Error
|
2023-09-05 07:00:45 +00:00
|
|
|
})
|
|
|
|
|
2023-08-14 06:09:38 +00:00
|
|
|
if err != nil {
|
2023-09-05 08:14:43 +00:00
|
|
|
logx.Error(err)
|
2023-09-05 09:07:42 +00:00
|
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, err.Error())
|
2023-09-05 07:00:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
event := wevent.NewWebsocketEventSuccess(wevent.UserResetToken, rt.TraceId)
|
|
|
|
err = CommonNotify(l.svcCtx.Config.MainAddress, rt.Wid, event)
|
|
|
|
if err != nil {
|
|
|
|
logx.Error(err, rt.TraceId)
|
2023-09-05 09:07:42 +00:00
|
|
|
return resp.SetStatusWithMessage(basic.CodeResetPasswordErr, err.Error())
|
2023-08-14 06:09:38 +00:00
|
|
|
}
|
|
|
|
|
2023-09-05 07:00:45 +00:00
|
|
|
// token := &auth.ResetToken{
|
|
|
|
// // 操作的类型, 验证的token 必须要继承这个
|
|
|
|
// OperateType: auth.OpTypeResetToken,
|
|
|
|
// UserId: userinfo.UserId,
|
|
|
|
// Wid: rt.Wid,
|
|
|
|
// Email: rt.Email,
|
|
|
|
// OldPassword: *user.PasswordHash,
|
|
|
|
// TraceId: uuid.NewString(),
|
|
|
|
// CreateAt: time.Now().UTC(),
|
|
|
|
// }
|
|
|
|
|
|
|
|
// clurl, err := l.svcCtx.ResetTokenManger.Encrypt(token)
|
|
|
|
// if err != nil {
|
|
|
|
// logx.Error(err)
|
|
|
|
// return resp.SetStatus(basic.CodeOAuthResetTokenEncryptErr, err.Error())
|
|
|
|
// }
|
|
|
|
|
|
|
|
// userName := *user.FirstName + " " + *user.LastName
|
|
|
|
// // 进入发送邮箱的系统
|
|
|
|
// EmailManager.EmailTasks <- &EmailFormat{
|
|
|
|
// TemplateName: "reset_password.tpl",
|
|
|
|
// UniqueKey: "register-" + req.Email,
|
|
|
|
// TargetEmail: req.Email,
|
|
|
|
// CompanyName: "fusen",
|
|
|
|
// ConfirmationLink: clurl,
|
|
|
|
// SenderName: "support@fusenpack.com",
|
|
|
|
// SenderTitle: "register-valid",
|
|
|
|
// Extend: map[string]string{
|
|
|
|
// "UserName": userName,
|
|
|
|
// },
|
|
|
|
// } // email进入队
|
2023-09-04 08:59:43 +00:00
|
|
|
|
2023-08-11 09:39:18 +00:00
|
|
|
return resp.SetStatus(basic.CodeOK)
|
2023-09-04 08:59:43 +00:00
|
|
|
|
|
|
|
// if !userinfo.IsUser() {
|
|
|
|
// return resp.SetStatus(basic.CodeUnAuth)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// rt, err := l.svcCtx.ResetTokenManger.Decrypt(req.ResetToken)
|
|
|
|
// if err != nil {
|
|
|
|
// logx.Error(err)
|
|
|
|
// return resp.SetStatus(basic.CodeOAuthResetTokenDecryptErr, err.Error())
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // TODO: 存储
|
|
|
|
// if rt.OperateType != auth.OpTypeResetToken {
|
|
|
|
// return resp.SetStatus(basic.CodeOAuthTypeErr, "error OperateType: rt.OperateType != auth.OpTypeResetToken")
|
|
|
|
// }
|
|
|
|
|
|
|
|
// err = l.svcCtx.AllModels.FsUser.Transaction(l.ctx, func(tx *gorm.DB) error {
|
|
|
|
// user := &gmodel.FsUser{Id: int64(rt.UserId)}
|
|
|
|
// err := tx.Take(user).Error
|
|
|
|
// if err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
// if *user.PasswordHash != rt.OldPassword {
|
|
|
|
// return fmt.Errorf("password had beed updated")
|
|
|
|
// }
|
|
|
|
// return tx.Update("PasswordHash", req.Password).Error
|
|
|
|
// })
|
|
|
|
|
|
|
|
// if err != nil {
|
|
|
|
// return resp.SetStatus(basic.CodeDbSqlErr, err.Error())
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return resp.SetStatus(basic.CodeOK)
|
2023-08-11 09:39:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
|
|
|
// func (l *UserResetPasswordLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
|
|
|
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
|
|
|
// }
|