Skip to content

Commit

Permalink
feature: add reset subcommand for user cOCommand
Browse files Browse the repository at this point in the history
Signed-off-by: Alan Tang <[email protected]>
  • Loading branch information
Standing-Man committed Dec 17, 2024
1 parent 22a748d commit eacc5c6
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/harbor/root/user/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func User() *cobra.Command {
UserCreateCmd(),
UserDeleteCmd(),
ElevateUserCmd(),
UserResetCmd(),
)

return cmd
Expand Down
42 changes: 42 additions & 0 deletions cmd/harbor/root/user/reset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package user

import (
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/prompt"
"github.com/goharbor/harbor-cli/pkg/views/user/reset"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func UserResetCmd() *cobra.Command {

cmd := &cobra.Command{
Use: "reset [username]",
Short: "reset user's password",
Long: "reset user's password by username",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var err error
var userId int64
resetView := &reset.ResetView{}

if len(args) > 0 {
userId, _ = api.GetUsersIdByName(args[0])
} else {
userId = prompt.GetUserIdFromUser()
}

reset.ResetUserView(resetView)

err = api.ResetPassword(userId, *resetView)

if err != nil {
log.Errorf("failed to reset user's password: %v", err)
}

},
}

return cmd

}
21 changes: 21 additions & 0 deletions pkg/api/user_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/goharbor/harbor-cli/pkg/views/user/create"
"github.com/goharbor/harbor-cli/pkg/views/user/reset"

log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -36,6 +37,26 @@ func CreateUser(opts create.CreateView) error {
return nil
}

func ResetPassword(userId int64, resetView reset.ResetView) error {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return err
}
_, err = client.User.UpdateUserPassword(ctx, &user.UpdateUserPasswordParams{
UserID: userId,
Password: &models.PasswordReq{
OldPassword: resetView.OldPassword,
NewPassword: resetView.NewPassword,
},
})

if err != nil {
return err
}
log.Infof("User reset password successfully with id %d", userId)
return nil
}

func DeleteUser(userId int64) error {
ctx, client, err := utils.ContextWithClient()
if err != nil {
Expand Down
64 changes: 64 additions & 0 deletions pkg/views/user/reset/view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package reset

import (
"errors"
"strings"

"github.com/charmbracelet/huh"
"github.com/goharbor/harbor-cli/pkg/utils"
log "github.com/sirupsen/logrus"
)

type ResetView struct {
OldPassword string
NewPassword string
Comfirm string
}

func ResetUserView(resetView *ResetView) {
theme := huh.ThemeCharm()
err := huh.NewForm(
huh.NewGroup(
huh.NewInput().
Title("Old Password").
EchoMode(huh.EchoModePassword).
Value(&resetView.OldPassword).
Validate(func(str string) error {
if strings.TrimSpace(str) == "" {
return errors.New("password cannot be empty or only spaces")
}
if err := utils.ValidatePassword(str); err != nil {
return err
}
return nil
}),
huh.NewInput().
Title("New Password").
EchoMode(huh.EchoModePassword).
Value(&resetView.NewPassword).
Validate(func(str string) error {
if strings.TrimSpace(str) == "" {
return errors.New("password cannot be empty or only spaces")
}
if err := utils.ValidatePassword(str); err != nil {
return err
}
return nil
}),
huh.NewInput().
Title("Comfirm Password").
EchoMode(huh.EchoModePassword).
Value(&resetView.Comfirm).
Validate(func(str string) error {
if resetView.Comfirm != resetView.NewPassword {
return errors.New("passwords do not match, please try again")
}
return nil
}),
),
).WithTheme(theme).Run()

if err != nil {
log.Fatal(err)
}
}

0 comments on commit eacc5c6

Please sign in to comment.