feat: user password
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"lone-services/pkg/modelbase"
|
||||
"lone-services/pkg/utils"
|
||||
"lone-services/pkg/validate"
|
||||
user "lone-services/rpc/user/pb"
|
||||
"lone-services/services/user/internal/dao"
|
||||
"lone-services/services/user/internal/model"
|
||||
"lone-services/services/user/internal/svc"
|
||||
"lone-services/services/user/validator"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type PasswordLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewPasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PasswordLogic {
|
||||
return &PasswordLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *PasswordLogic) Password(in *user.PasswordReq) (*user.Response, error) {
|
||||
var req validator.PasswordValidator
|
||||
if msg := validate.ValidateFromProto(in, &req); msg != utils.StringEmpty {
|
||||
return outResponse(utils.ErrorParams, msg), nil
|
||||
}
|
||||
|
||||
loginUser := utils.GetUserFromCtx(l.ctx)
|
||||
if loginUser.ID < utils.NumberOne {
|
||||
return failResponse(utils.ErrorNoLoginInfo), nil
|
||||
}
|
||||
|
||||
subjectType, subjectErr := l.resolveSubjectType(loginUser)
|
||||
if subjectErr != nil {
|
||||
l.Errorf("password resolve subject: %v", subjectErr)
|
||||
return failResponse(utils.Fail), nil
|
||||
}
|
||||
if subjectType == utils.StringEmpty {
|
||||
return outResponse(utils.ErrorParams, "无法识别用户主体类型"), nil
|
||||
}
|
||||
|
||||
row, userErr := loadEnabledUser(loginUser.ID)
|
||||
if userErr != nil {
|
||||
l.Errorf("password load user: %v", userErr)
|
||||
return failResponse(utils.ErrorNotFund), nil
|
||||
}
|
||||
if row.Mobile == utils.StringEmpty {
|
||||
return outResponse(utils.ErrorParams, "用户未绑定手机号,无法修改密码"), nil
|
||||
}
|
||||
|
||||
cred, credErr := findCredential(dao.CredentialTypePassword, row.Mobile, subjectType)
|
||||
if credErr != nil {
|
||||
l.Errorf("password find credential: %v", credErr)
|
||||
return failResponse(utils.Fail), nil
|
||||
}
|
||||
if cred == nil || cred.UserId != loginUser.ID {
|
||||
return outResponse(utils.ErrorNotFund, "密码凭证不存在"), nil
|
||||
}
|
||||
if cred.Status == dao.StatusDisabled {
|
||||
return outResponse(utils.Fail, "登录凭证已禁用"), nil
|
||||
}
|
||||
if !utils.EqualsPassword(req.OldPassword, cred.Secret) {
|
||||
return failResponse(utils.ErrorPwdError), nil
|
||||
}
|
||||
if req.OldPassword == req.Password {
|
||||
return outResponse(utils.ErrorParams, "新密码不能与原密码相同"), nil
|
||||
}
|
||||
|
||||
hashed, hashErr := utils.EncryptPassword(req.Password)
|
||||
if hashErr != nil {
|
||||
l.Errorf("password encrypt: %v", hashErr)
|
||||
return failResponse(utils.ErrorEncryptAesError), nil
|
||||
}
|
||||
|
||||
credModel := model.UserCredentialModel{}.Init()
|
||||
_, editErr := credModel.Edit(modelbase.Params{
|
||||
Eq: map[string]string{"id": strconv.FormatInt(cred.Id, utils.NumberTen)},
|
||||
}, map[string]interface{}{
|
||||
"secret": hashed,
|
||||
"status": dao.StatusEnabled,
|
||||
})
|
||||
if editErr != nil {
|
||||
l.Errorf("password update: %v", editErr)
|
||||
return failResponse(utils.Fail), nil
|
||||
}
|
||||
|
||||
return okResponse(nil), nil
|
||||
}
|
||||
|
||||
func (l *PasswordLogic) resolveSubjectType(loginUser utils.UserInfo) (string, error) {
|
||||
if loginUser.ClientCode != utils.StringEmpty {
|
||||
client, err := loadEnabledClient(loginUser.ClientCode)
|
||||
if err != nil {
|
||||
return utils.StringEmpty, err
|
||||
}
|
||||
if client.SubjectType != utils.StringEmpty {
|
||||
return client.SubjectType, nil
|
||||
}
|
||||
}
|
||||
|
||||
switch loginUser.Type {
|
||||
case dao.TypeSale:
|
||||
return dao.SubjectTypeSale, nil
|
||||
case dao.TypeStore:
|
||||
return dao.SubjectTypeStore, nil
|
||||
case dao.TypeUser:
|
||||
return dao.SubjectTypeUser, nil
|
||||
default:
|
||||
return utils.StringEmpty, nil
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user