Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reset subcommand for user command #284

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/harbor/root/user/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ func User() *cobra.Command {
UserCreateCmd(),
UserDeleteCmd(),
ElevateUserCmd(),
UserResetCmd(),
UpdateUserCmd(),
)

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

}
72 changes: 72 additions & 0 deletions cmd/harbor/root/user/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package user

import (
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/prompt"
"github.com/goharbor/harbor-cli/pkg/views/user/update"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func UpdateUserCmd() *cobra.Command {

opts := &models.UserProfile{}

cmd := &cobra.Command{
Use: "update",
Short: "update user's profile",
Long: "update user's profile by username",
Example: "harbor user update [username]",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var err error
var UserId int64

if len(args) > 0 {
UserId, err = api.GetUsersIdByName(args[0])
if err != nil {
logrus.Errorf("fail to get user id by username: %s", args[0])
return
}
} else {
UserId = prompt.GetUserIdFromUser()
}

existingUser := api.GetUserProfileById(UserId)
if existingUser == nil {
logrus.Errorf("user is not found")
return
}

updateView := &models.UserProfile{
Comment: existingUser.Comment,
Email: existingUser.Email,
Realname: existingUser.Realname,
}

flags := cmd.Flags()
if flags.Changed("comment") {
updateView.Comment = opts.Comment
}
if flags.Changed("email") {
updateView.Email = opts.Email
}
if flags.Changed("realname") {
updateView.Realname = opts.Realname
}

update.UpdateUserProfileView(updateView)
err = api.UpdateUserProfile(updateView, UserId)
if err != nil {
logrus.Errorf("failed to update user's profile: %v", err)
}
},
}
flags := cmd.Flags()
flags.StringVarP(&opts.Comment, "comment", "m", "", "Comment of the user")
flags.StringVarP(&opts.Email, "email", "e", "", "Email of the user")
flags.StringVarP(&opts.Realname, "realname", "r", "", "Realname of the user")

return cmd
}
54 changes: 54 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 Expand Up @@ -106,3 +127,36 @@ func GetUsersIdByName(userName string) (int64, error) {

return 0, err
}

func GetUserProfileById(userId int64) *models.UserResp {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return nil
}

resp, err := client.User.GetUser(ctx, &user.GetUserParams{UserID: userId})
if err != nil {
return nil
}

return resp.GetPayload()
}

func UpdateUserProfile(profile *models.UserProfile, userId int64) error {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return err
}

_, err = client.User.UpdateUserProfile(ctx, &user.UpdateUserProfileParams{
UserID: userId,
Profile: profile,
})
if err != nil {
return err
}

log.Info("user's profile updated successfully")

return nil
}
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)
}
}
50 changes: 50 additions & 0 deletions pkg/views/user/update/view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package update

import (
"errors"
"strings"

"github.com/charmbracelet/huh"
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/utils"
log "github.com/sirupsen/logrus"
)

func UpdateUserProfileView(updateView *models.UserProfile) {
theme := huh.ThemeCharm()
err := huh.NewForm(
huh.NewGroup(
huh.NewInput().
Title("Email").
Value(&updateView.Email).
Validate(func(str string) error {
if strings.TrimSpace(str) == "" {
return errors.New("email cannot be empty or only spaces")
}
if isVaild := utils.ValidateEmail(str); !isVaild {
return errors.New("please enter correct email format")
}
return nil
}),
huh.NewInput().
Title("Realname").
Value(&updateView.Realname).
Validate(func(str string) error {
if strings.TrimSpace(str) == "" {
return errors.New("real name cannot be empty")
}
if isValid := utils.ValidateFL(str); !isValid {
return errors.New("please enter correct first and last name format, like `Bob Dylan`")
}
return nil
}),
huh.NewInput().
Title("Comment").
Value(&updateView.Comment),
),
).WithTheme(theme).Run()

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