From 13b807efd794277938f94fed27073943c48b49b3 Mon Sep 17 00:00:00 2001 From: Hoang Date: Tue, 9 Nov 2021 09:38:24 +0700 Subject: [PATCH 1/3] Change status and password method --- action_update.go | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ db.go | 9 ++++++++ 2 files changed, 68 insertions(+) diff --git a/action_update.go b/action_update.go index d47818f..682530c 100644 --- a/action_update.go +++ b/action_update.go @@ -2,6 +2,7 @@ package usermngmt import ( "context" + "errors" "github.com/Selly-Modules/mongodb" "go.mongodb.org/mongo-driver/bson" @@ -55,3 +56,61 @@ func (s Service) UpdateByUserID(userID string, payload UpdateOptions) error { return nil } + +// ChangePasswordByUserID ... +func (s Service) ChangePasswordByUserID(userID, oldPassword, newPassword string) error { + var ( + ctx = context.Background() + id, _ = mongodb.NewIDFromString(userID) + ) + + if oldPassword == "" || newPassword == "" { + return errors.New("new password and old password cannot be empty") + } + + // Find user + user, _ := s.userFindByID(ctx, id) + if user.ID.IsZero() { + return errors.New("user does not exist") + } + + // Check old password + isValid := checkPasswordHash(oldPassword, user.HashedPassword) + if !isValid { + return errors.New("the password is incorrect") + } + + // Update password + err := s.userUpdateOneByCondition(ctx, bson.M{"_id": user.ID}, bson.M{ + "$set": bson.M{ + "hashedPassword": hashPassword(newPassword), + "updatedAt": now(), + }, + }) + if err != nil { + return err + } + + return nil +} + +// ChangeStatusByUserID ... +func (s Service) ChangeStatusByUserID(userID, newStatus string) error { + var ( + ctx = context.Background() + id, _ = mongodb.NewIDFromString(userID) + ) + + // Update password + err := s.userUpdateOneByCondition(ctx, bson.M{"_id": id}, bson.M{ + "$set": bson.M{ + "status": newStatus, + "updatedAt": now(), + }, + }) + if err != nil { + return err + } + + return nil +} diff --git a/db.go b/db.go index 8e8c25e..1a25e54 100644 --- a/db.go +++ b/db.go @@ -100,4 +100,13 @@ func (s Service) userUpdateOneByCondition(ctx context.Context, cond interface{}, } return err +} + +func (s Service) userFindByID(ctx context.Context, id primitive.ObjectID) (User, error) { + var ( + col = s.getUserCollection() + doc User + ) + err := col.FindOne(ctx, bson.M{"_id": id}).Decode(&doc) + return doc, err } \ No newline at end of file -- 2.34.1 From c4becfc5dd3a3bd90d87ff2fa0e1768a939279ec Mon Sep 17 00:00:00 2001 From: Hoang Date: Tue, 9 Nov 2021 10:22:15 +0700 Subject: [PATCH 2/3] fix comment --- action_update.go | 44 ++++++++++++++++++++++++++++---------------- validate.go | 22 +++++++++++++++++++++- 2 files changed, 49 insertions(+), 17 deletions(-) diff --git a/action_update.go b/action_update.go index 682530c..f726b85 100644 --- a/action_update.go +++ b/action_update.go @@ -17,6 +17,12 @@ type UpdateOptions struct { Other string } +// ChangePasswordOptions ... +type ChangePasswordOptions struct { + OldPassword string + NewPassword string +} + // UpdateByUserID ... func (s Service) UpdateByUserID(userID string, payload UpdateOptions) error { var ( @@ -57,33 +63,34 @@ func (s Service) UpdateByUserID(userID string, payload UpdateOptions) error { return nil } -// ChangePasswordByUserID ... -func (s Service) ChangePasswordByUserID(userID, oldPassword, newPassword string) error { +// ChangeUserPassword ... +func (s Service) ChangeUserPassword(userID string, opt ChangePasswordOptions) error { var ( - ctx = context.Background() - id, _ = mongodb.NewIDFromString(userID) + ctx = context.Background() ) - if oldPassword == "" || newPassword == "" { - return errors.New("new password and old password cannot be empty") + // Validate payload + err := opt.validate(userID) + if err != nil { + return err } // Find user + id, _ := mongodb.NewIDFromString(userID) user, _ := s.userFindByID(ctx, id) if user.ID.IsZero() { - return errors.New("user does not exist") + return errors.New("user not found") } // Check old password - isValid := checkPasswordHash(oldPassword, user.HashedPassword) - if !isValid { + if isValid := checkPasswordHash(opt.OldPassword, user.HashedPassword); !isValid { return errors.New("the password is incorrect") } // Update password - err := s.userUpdateOneByCondition(ctx, bson.M{"_id": user.ID}, bson.M{ + err = s.userUpdateOneByCondition(ctx, bson.M{"_id": user.ID}, bson.M{ "$set": bson.M{ - "hashedPassword": hashPassword(newPassword), + "hashedPassword": hashPassword(opt.NewPassword), "updatedAt": now(), }, }) @@ -94,14 +101,19 @@ func (s Service) ChangePasswordByUserID(userID, oldPassword, newPassword string) return nil } -// ChangeStatusByUserID ... -func (s Service) ChangeStatusByUserID(userID, newStatus string) error { +// ChangeUserStatus ... +func (s Service) ChangeUserStatus(userID, newStatus string) error { var ( - ctx = context.Background() - id, _ = mongodb.NewIDFromString(userID) + ctx = context.Background() ) - // Update password + // Validate userID + id, isValid := mongodb.NewIDFromString(userID) + if !isValid { + return errors.New("invalid user id data") + } + + // Update status err := s.userUpdateOneByCondition(ctx, bson.M{"_id": id}, bson.M{ "$set": bson.M{ "status": newStatus, diff --git a/validate.go b/validate.go index c559f08..a856ab3 100644 --- a/validate.go +++ b/validate.go @@ -122,4 +122,24 @@ func (co UpdateOptions) validate(ctx context.Context) error { } return nil -} \ No newline at end of file +} + +func (co ChangePasswordOptions) validate(userID string) error { + // OldPassword, NewPassword + if co.OldPassword == "" || co.NewPassword == "" { + logger.Error("usermngmt - ChangePassword: old or new password cannot be empty", logger.LogData{ + "payload": co, + }) + return errors.New("old or new password cannot be empty") + } + + // UserID + if _, isValid := mongodb.NewIDFromString(userID); !isValid { + logger.Error("usermngmt - ChangePassword: invalid userID data", logger.LogData{ + "payload": co, + }) + return errors.New("invalid user id data") + } + + return nil +} -- 2.34.1 From bc0e3b4076de0ea44b5f2de14c3db008e08e15a8 Mon Sep 17 00:00:00 2001 From: Hoang Date: Tue, 9 Nov 2021 10:54:14 +0700 Subject: [PATCH 3/3] fix comment againt --- action_create.go | 6 ++---- action_update.go | 16 ++++++---------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/action_create.go b/action_create.go index cc0cb4a..27a17fc 100644 --- a/action_create.go +++ b/action_create.go @@ -24,8 +24,7 @@ func (s Service) Create(payload CreateOptions) error { ) // Validate payload - err := payload.validate(ctx) - if err != nil { + if err := payload.validate(ctx); err != nil { return err } @@ -36,8 +35,7 @@ func (s Service) Create(payload CreateOptions) error { } // Create user - err = s.userCreate(ctx, doc) - if err != nil { + if err = s.userCreate(ctx, doc); err != nil { return err } diff --git a/action_update.go b/action_update.go index f726b85..064f091 100644 --- a/action_update.go +++ b/action_update.go @@ -30,8 +30,7 @@ func (s Service) UpdateByUserID(userID string, payload UpdateOptions) error { ) // Validate payload - err := payload.validate(ctx) - if err != nil { + if err := payload.validate(ctx); err != nil { return err } @@ -55,8 +54,7 @@ func (s Service) UpdateByUserID(userID string, payload UpdateOptions) error { } // Update - err = s.userUpdateOneByCondition(ctx, cond, updateData) - if err != nil { + if err := s.userUpdateOneByCondition(ctx, cond, updateData); err != nil { return err } @@ -88,13 +86,12 @@ func (s Service) ChangeUserPassword(userID string, opt ChangePasswordOptions) er } // Update password - err = s.userUpdateOneByCondition(ctx, bson.M{"_id": user.ID}, bson.M{ + if err = s.userUpdateOneByCondition(ctx, bson.M{"_id": user.ID}, bson.M{ "$set": bson.M{ "hashedPassword": hashPassword(opt.NewPassword), "updatedAt": now(), }, - }) - if err != nil { + }); err != nil { return err } @@ -114,13 +111,12 @@ func (s Service) ChangeUserStatus(userID, newStatus string) error { } // Update status - err := s.userUpdateOneByCondition(ctx, bson.M{"_id": id}, bson.M{ + if err := s.userUpdateOneByCondition(ctx, bson.M{"_id": id}, bson.M{ "$set": bson.M{ "status": newStatus, "updatedAt": now(), }, - }) - if err != nil { + }); err != nil { return err } -- 2.34.1