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 (