From 4875c935b70defa6ae43b426d3f2d8e142fa3813 Mon Sep 17 00:00:00 2001 From: Hoang Date: Fri, 18 Feb 2022 11:28:27 +0700 Subject: [PATCH] add ResetAndRequireToChangeUserPassword method --- action.go | 5 +++++ user/handle.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/action.go b/action.go index caa2e78..dcc0776 100644 --- a/action.go +++ b/action.go @@ -49,6 +49,11 @@ func (s Service) ResetUserPassword(userID string, password string) error { return user.ResetUserPassword(userID, password) } +// ResetAndRequireToChangeUserPassword ... +func (s Service) ResetAndRequireToChangeUserPassword(userID string, password string) error { + return user.ResetAndRequireToChangeUserPassword(userID, password) +} + // ChangeUserStatus ... func (s Service) ChangeUserStatus(userID, newStatus string) error { return user.ChangeUserStatus(userID, newStatus) diff --git a/user/handle.go b/user/handle.go index 05dceb2..20a6ee0 100644 --- a/user/handle.go +++ b/user/handle.go @@ -403,6 +403,43 @@ func ResetUserPassword(userID string, password string) error { return nil } +// ResetAndRequireToChangeUserPassword ... +func ResetAndRequireToChangeUserPassword(userID string, password string) error { + var ( + ctx = context.Background() + ) + + // Validate Password + if password == "" { + return errors.New(internal.ErrorInvalidPassword) + } + + // Validate userID + if _, isValid := mongodb.NewIDFromString(userID); !isValid { + return errors.New(internal.ErrorInvalidUser) + } + + // Find user + id, _ := mongodb.NewIDFromString(userID) + user, _ := findByID(ctx, id) + if user.ID.IsZero() { + return errors.New(internal.ErrorNotFoundUser) + } + + // Update password + if err := updateOneByCondition(ctx, bson.M{"_id": user.ID}, bson.M{ + "$set": bson.M{ + "hashedPassword": internal.HashPassword(password), + "requireToChangePassword": true, + "updatedAt": internal.Now(), + }, + }); err != nil { + return err + } + + return nil +} + // ChangeUserStatus ... func ChangeUserStatus(userID, newStatus string) error { var (