Skip to content
Closed
Show file tree
Hide file tree
Changes from 7 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
65 changes: 35 additions & 30 deletions cmd/harbor/root/user/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,36 @@ import (
"github.com/spf13/cobra"
)

type UserCreator interface {
FillUser(opts *create.CreateView)
UserCreate(opts create.CreateView) error
}
type DefaultUserCreator struct{}

func (d *DefaultUserCreator) FillUser(opts *create.CreateView) {
create.CreateUserView(opts)
}

func (d *DefaultUserCreator) UserCreate(opts create.CreateView) error {
return api.CreateUser(opts)
}
func CreateUser(opts *create.CreateView, userCreator UserCreator) {
var err error

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

err = userCreator.UserCreate(*opts)

if err != nil {
if isUnauthorizedError(err) {
log.Error("Permission denied: Admin privileges are required to execute this command.")
Copy link
Collaborator

Choose a reason for hiding this comment

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

robot accounts can also create users - this is not limited to admins.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok I will correct this 👍

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

Expand All @@ -31,30 +61,8 @@ 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.Comment != "" && 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)
}
}
d := &DefaultUserCreator{}
CreateUser(&opts, d)
},
}

Expand All @@ -67,12 +75,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