Skip to content
Open
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
55 changes: 25 additions & 30 deletions cmd/harbor/root/user/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,27 @@ import (
"github.com/spf13/cobra"
)

var fillUser = create.CreateUserView

func CreateUser(opts *create.CreateView, createUserAPI func(opts create.CreateView) error) {
var err error

if opts.Email == "" || opts.Realname == "" || opts.Password == "" || opts.Username == "" {
fillUser(opts)
}

err = createUserAPI(*opts)

if err != nil {
if isUnauthorizedError(err) {
log.WithFields(log.Fields{
"action": "user create",
}).Error("Permission denied: The current account does not have the required permissions to create users.")
Copy link

Copilot AI Feb 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The unauthorized/permission-denied message here differs from other user commands (most log/return "Permission denied: Admin privileges are required to execute this command."). This inconsistency can be confusing for users and makes it harder to match errors across commands/tests. Consider standardizing the message (and logging style) across the user subcommands.

Suggested change
}).Error("Permission denied: The current account does not have the required permissions to create users.")
}).Error("Permission denied: Admin privileges are required to execute this command.")

Copilot uses AI. Check for mistakes.
} else {
log.Errorf("failed to create user: %v", err)
}
}
}
func UserCreateCmd() *cobra.Command {
var opts create.CreateView

Expand All @@ -31,30 +52,7 @@ func UserCreateCmd() *cobra.Command {
Short: "create user",
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
var err error
createView := &create.CreateView{
Email: opts.Email,
Realname: opts.Realname,
Comment: opts.Comment,
Password: opts.Password,
Username: opts.Username,
}

if opts.Email != "" && opts.Realname != "" && opts.Password != "" && opts.Username != "" {
err = api.CreateUser(opts)
} else {
err = createUserView(createView)
}

// Check if the error is due to unauthorized access.

if err != nil {
if isUnauthorizedError(err) {
log.Error("Permission denied: Admin privileges are required to execute this command.")
} else {
log.Errorf("failed to create user: %v", err)
}
}
CreateUser(&opts, api.CreateUser)
},
}

Expand All @@ -67,12 +65,9 @@ func UserCreateCmd() *cobra.Command {

return cmd
}

func createUserView(createView *create.CreateView) error {
create.CreateUserView(createView)
return api.CreateUser(*createView)
}

func isUnauthorizedError(err error) bool {
if err == nil {
return false
}
return strings.Contains(err.Error(), "403")
}
Loading
Loading