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 d47818f..064f091 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" @@ -16,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 ( @@ -23,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 } @@ -48,8 +54,69 @@ 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 + } + + return nil +} + +// ChangeUserPassword ... +func (s Service) ChangeUserPassword(userID string, opt ChangePasswordOptions) error { + var ( + ctx = context.Background() + ) + + // 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 not found") + } + + // Check old password + if isValid := checkPasswordHash(opt.OldPassword, user.HashedPassword); !isValid { + return errors.New("the password is incorrect") + } + + // Update password + if err = s.userUpdateOneByCondition(ctx, bson.M{"_id": user.ID}, bson.M{ + "$set": bson.M{ + "hashedPassword": hashPassword(opt.NewPassword), + "updatedAt": now(), + }, + }); err != nil { + return err + } + + return nil +} + +// ChangeUserStatus ... +func (s Service) ChangeUserStatus(userID, newStatus string) error { + var ( + ctx = context.Background() + ) + + // Validate userID + id, isValid := mongodb.NewIDFromString(userID) + if !isValid { + return errors.New("invalid user id data") + } + + // Update status + if err := s.userUpdateOneByCondition(ctx, bson.M{"_id": id}, bson.M{ + "$set": bson.M{ + "status": newStatus, + "updatedAt": now(), + }, + }); err != nil { return err } 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 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 +}