85 lines
2.5 KiB
Go
85 lines
2.5 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
|
|
"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/svc"
|
|
"lone-services/services/user/validator"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
type InternalVerifyPasswordLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewInternalVerifyPasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *InternalVerifyPasswordLogic {
|
|
return &InternalVerifyPasswordLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *InternalVerifyPasswordLogic) InternalVerifyPassword(in *user.InternalVerifyPasswordReq) (*user.InternalVerifyPasswordData, error) {
|
|
var req validator.InternalVerifyPasswordValidator
|
|
if msg := validate.ValidateFromProto(in, &req); msg != utils.StringEmpty {
|
|
return nil, status.Error(codes.InvalidArgument, msg)
|
|
}
|
|
|
|
subjectType := req.SubjectType
|
|
if subjectType == utils.StringEmpty {
|
|
subjectType = dao.SubjectTypeUser
|
|
}
|
|
|
|
mobile := req.Username
|
|
if len(mobile) != utils.NumberEleven {
|
|
return nil, status.Error(codes.NotFound, utils.ErrorNotFund.Msg)
|
|
}
|
|
encryptMobile, encErr := utils.EncryptPhone(mobile)
|
|
if encErr != nil {
|
|
l.Errorf("internal verify password encrypt: %v", encErr)
|
|
return nil, status.Error(codes.Internal, utils.Fail.Msg)
|
|
}
|
|
|
|
cred, credErr := findCredential(dao.CredentialTypePassword, encryptMobile, subjectType)
|
|
if credErr != nil {
|
|
l.Errorf("internal verify password find credential: %v", credErr)
|
|
return nil, status.Error(codes.Internal, utils.Fail.Msg)
|
|
}
|
|
if cred == nil {
|
|
return nil, status.Error(codes.NotFound, utils.ErrorNotFund.Msg)
|
|
}
|
|
if cred.Status == dao.StatusDisabled {
|
|
return nil, status.Error(codes.FailedPrecondition, "登录凭证已禁用")
|
|
}
|
|
if !utils.EqualsPassword(req.Password, cred.Secret) {
|
|
return nil, status.Error(codes.InvalidArgument, utils.ErrorPwdError.Msg)
|
|
}
|
|
|
|
row, userErr := loadEnabledUser(cred.UserId)
|
|
if userErr != nil {
|
|
l.Errorf("internal verify password load user: %v", userErr)
|
|
return nil, status.Error(codes.NotFound, utils.ErrorNotFund.Msg)
|
|
}
|
|
|
|
out := &user.InternalVerifyPasswordData{UserId: row.Id}
|
|
if req.ClientCode != utils.StringEmpty {
|
|
bizId, bizErr := loadUserClientBizId(row.Id, req.ClientCode)
|
|
if bizErr != nil {
|
|
l.Errorf("internal verify password load biz: %v", bizErr)
|
|
return nil, status.Error(codes.Internal, utils.Fail.Msg)
|
|
}
|
|
out.BizId = bizId
|
|
}
|
|
return out, nil
|
|
}
|