Skip to content

Commit

Permalink
fix: mobile phone number, email address, account modification verific…
Browse files Browse the repository at this point in the history
…ation (#578)

* update gomake version

* update gomake version

* update version

* fix: mobile phone number, email address, account modification verification
  • Loading branch information
withchao authored Sep 29, 2024
1 parent e5878ab commit d4dcc6f
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 37 deletions.
5 changes: 2 additions & 3 deletions internal/rpc/chat/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,6 @@ func (o *chatSvr) genVerifyCode() string {
}

func (o *chatSvr) RegisterUser(ctx context.Context, req *chat.RegisterUserReq) (*chat.RegisterUserResp, error) {
resp := &chat.RegisterUserResp{}

isAdmin, err := o.Admin.CheckNilOrAdmin(ctx)
ctx = o.WithAdminUser(ctx)
if err != nil {
Expand Down Expand Up @@ -389,6 +387,7 @@ func (o *chatSvr) RegisterUser(ctx context.Context, req *chat.RegisterUserReq) (
log.ZError(ctx, "UseInvitationCode", err, "userID", req.User.UserID, "invitationCode", req.InvitationCode)
}
}
var resp chat.RegisterUserResp
if req.AutoLogin {
chatToken, err := o.Admin.CreateToken(ctx, req.User.UserID, constant.NormalUser)
if err == nil {
Expand All @@ -398,7 +397,7 @@ func (o *chatSvr) RegisterUser(ctx context.Context, req *chat.RegisterUserReq) (
}
}
resp.UserID = req.User.UserID
return resp, nil
return &resp, nil
}

func (o *chatSvr) Login(ctx context.Context, req *chat.LoginReq) (*chat.LoginResp, error) {
Expand Down
6 changes: 3 additions & 3 deletions internal/rpc/chat/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ func ToDBAttributeUpdate(req *chat.UpdateUserInfoReq) (map[string]any, error) {
if req.GlobalRecvMsgOpt != nil {
update["global_recv_msg_opt"] = req.GlobalRecvMsgOpt.Value
}
if len(update) == 0 {
return nil, errs.ErrArgs.WrapMsg("no update info")
}
//if len(update) == 0 {
// return nil, errs.ErrArgs.WrapMsg("no update info")
//}
return update, nil
}
124 changes: 93 additions & 31 deletions internal/rpc/chat/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,95 @@ import (
"github.com/openimsdk/tools/errs"
)

func (o *chatSvr) checkUpdateInfo(ctx context.Context, req *chat.UpdateUserInfoReq) error {
attribute, err := o.Database.TakeAttributeByUserID(ctx, req.UserID)
if err != nil {
return err
}
checkEmail := func() error {
if req.Email == nil {
return nil
}
if req.Email.Value == attribute.Email {
req.Email = nil
return nil
}
if req.Email.Value == "" {
if !(attribute.Account != "" || (attribute.AreaCode != "" && attribute.PhoneNumber != "")) {
return errs.ErrArgs.WrapMsg("a login method must exist")
}
return nil
} else {
if _, err := o.Database.GetAttributeByEmail(ctx, req.Email.Value); err == nil {
return errs.ErrDuplicateKey.WrapMsg("email already exists")
} else if !dbutil.IsDBNotFound(err) {
return err
}
}
return nil
}
checkPhone := func() error {
if req.AreaCode == nil {
return nil
}
if req.AreaCode.Value == attribute.AreaCode && req.PhoneNumber.Value == attribute.PhoneNumber {
req.AreaCode = nil
req.PhoneNumber = nil
return nil
}
if req.AreaCode.Value == "" || req.PhoneNumber.Value == "" {
if attribute.Email == "" || attribute.Account == "" {
return errs.ErrArgs.WrapMsg("a login method must exist")
}
} else {
if _, err := o.Database.GetAttributeByPhone(ctx, req.AreaCode.Value, req.PhoneNumber.Value); err == nil {
return errs.ErrDuplicateKey.WrapMsg("phone number already exists")
} else if !dbutil.IsDBNotFound(err) {
return err
}
}
return nil
}
checkAccount := func() error {
if req.Account == nil {
return nil
}
if req.Account.Value == attribute.Account {
req.Account = nil
return nil
}
if req.Account.Value == "" {
if !(attribute.Email == "" && (attribute.AreaCode == "" || attribute.PhoneNumber == "")) {
return errs.ErrArgs.WrapMsg("a login method must exist")
}
} else {
if _, err := o.Database.GetAttributeByAccount(ctx, req.Account.Value); err == nil {
return errs.ErrDuplicateKey.WrapMsg("account already exists")
} else if !dbutil.IsDBNotFound(err) {
return err
}
}
return nil
}
for _, fn := range []func() error{checkEmail, checkPhone, checkAccount} {
if err := fn(); err != nil {
return err
}
}
return nil
}

func (o *chatSvr) UpdateUserInfo(ctx context.Context, req *chat.UpdateUserInfoReq) (*chat.UpdateUserInfoResp, error) {
resp := &chat.UpdateUserInfoResp{}
if req.AreaCode != nil || req.PhoneNumber != nil {
if !(req.AreaCode != nil && req.PhoneNumber != nil) {
return nil, errs.ErrArgs.WrapMsg("areaCode and phoneNumber must be set together")
}
if req.AreaCode.Value == "" || req.PhoneNumber.Value == "" {
if req.AreaCode.Value != req.PhoneNumber.Value {
return nil, errs.ErrArgs.WrapMsg("areaCode and phoneNumber must be set together")
}
}
}
opUserID, userType, err := mctx.Check(ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -65,44 +152,19 @@ func (o *chatSvr) UpdateUserInfo(ctx context.Context, req *chat.UpdateUserInfoRe
default:
return nil, errs.ErrNoPermission.WrapMsg("user type error")
}
update, err := ToDBAttributeUpdate(req)
if err != nil {
if err := o.checkUpdateInfo(ctx, req); err != nil {
return nil, err
}
attribute, err := o.Database.TakeAttributeByUserID(ctx, req.UserID)
update, err := ToDBAttributeUpdate(req)
if err != nil {
return nil, err
}
if req.Account != nil && req.Account.Value != attribute.Account {
_, err := o.Database.TakeAttributeByAccount(ctx, req.Account.Value)
if err == nil {
return nil, eerrs.ErrAccountAlreadyRegister.Wrap()
} else if !dbutil.IsDBNotFound(err) {
if len(update) > 0 {
if err := o.Database.UpdateUseInfo(ctx, req.UserID, update); err != nil {
return nil, err
}
}
if req.AreaCode != nil || req.PhoneNumber != nil {
areaCode := attribute.AreaCode
phoneNumber := attribute.PhoneNumber
if req.AreaCode != nil {
areaCode = req.AreaCode.Value
}
if req.PhoneNumber != nil {
phoneNumber = req.PhoneNumber.Value
}
if attribute.AreaCode != areaCode || attribute.PhoneNumber != phoneNumber {
_, err := o.Database.TakeAttributeByPhone(ctx, areaCode, phoneNumber)
if err == nil {
return nil, eerrs.ErrAccountAlreadyRegister.Wrap()
} else if !dbutil.IsDBNotFound(err) {
return nil, err
}
}
}
if err := o.Database.UpdateUseInfo(ctx, req.UserID, update); err != nil {
return nil, err
}
return resp, nil
return &chat.UpdateUserInfoResp{}, nil
}

func (o *chatSvr) FindUserPublicInfo(ctx context.Context, req *chat.FindUserPublicInfoReq) (*chat.FindUserPublicInfoResp, error) {
Expand Down

0 comments on commit d4dcc6f

Please sign in to comment.