Skip to content

Commit

Permalink
Restrict the input for creating an user
Browse files Browse the repository at this point in the history
Signed-off-by: JianMinTang <jmtangcs@gmail.com>
  • Loading branch information
JianMinTang committed Sep 25, 2024
1 parent 76bd3bf commit 301744f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 16 deletions.
32 changes: 25 additions & 7 deletions pkg/utils/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,45 @@ func FormatSize(size int64) string {
return fmt.Sprintf("%.2fMiB", mbSize)
}

func VaildUserName(username string) bool {
pattern := `^[a-zA-Z0-9]{1,255}$`
re := regexp.MustCompile(pattern)
return re.MatchString(username)
}

func VaildEmail(email string) bool {
pattern := `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`
re := regexp.MustCompile(pattern)
return re.MatchString(email)
}

func VaildFL(name string) bool {
pattern := `^[A-Za-z]{1,20}\s[A-Za-z]{1,20}$`
re := regexp.MustCompile(pattern)
return re.MatchString(name)
}

// check if the password format is vaild
func ValidatePassword(password string) (bool, error) {
func ValidatePassword(password string) error {
if len(password) < 8 || len(password) > 256 {
return false, errors.New("worong! the password length must be at least 8 characters and at most 256 characters")
return errors.New("worong! the password length must be at least 8 characters and at most 256 characters")
}
// checking the password has a minimum of one lower case letter
if done, _ := regexp.MatchString("([a-z])+", password); !done {
return false, errors.New("worong! the password doesn't have a lowercase letter")
return errors.New("worong! the password doesn't have a lowercase letter")
}

// checking the password has a minimmum of one upper case letter
if done, _ := regexp.MatchString("([A-Z])+", password); !done {
return false, errors.New("worong! the password doesn't have an upppercase letter")
return errors.New("worong! the password doesn't have an upppercase letter")
}

// checking if the password has a minimum of one digit
if done, _ := regexp.Match("([0-9])+", []byte(password)); !done {
return false, errors.New("worong! the password doesn't have a digit number")
return errors.New("worong! the password doesn't have a digit number")
}

return true, errors.New("")
return nil
}

// check if the tag name is valid
Expand All @@ -78,7 +96,7 @@ func ValidateTagName(tagName string) bool {

// check if the project name is valid
func ValidateProjectName(projectName string) bool {
pattern := `^[a-z0-9][a-z0-9._-]{0,254}$$`
pattern := `^[a-z0-9][a-z0-9._-]{0,254}$`

re := regexp.MustCompile(pattern)

Expand Down
3 changes: 1 addition & 2 deletions pkg/views/login/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ func CreateView(loginView *LoginView) {
if strings.TrimSpace(str) == "" {
return errors.New("password cannot be empty")
}
isVaild, err := utils.ValidatePassword(str)
if !isVaild {
if err := utils.ValidatePassword(str); err != nil {
return err
}
return nil
Expand Down
28 changes: 21 additions & 7 deletions pkg/views/user/create/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package create

import (
"errors"
"strings"

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

Expand All @@ -23,17 +25,23 @@ func CreateUserView(createView *CreateView) {
Title("User Name").
Value(&createView.Username).
Validate(func(str string) error {
if str == "" {
if strings.TrimSpace(str) == "" {
return errors.New("user name cannot be empty")
}
if isVaild := utils.VaildUserName(str); !isVaild {
return errors.New("username cannot contain special characters")
}
return nil
}),
huh.NewInput().
Title("Email").
Value(&createView.Email).
Validate(func(str string) error {
if str == "" {
return errors.New("email cannot be empty")
if strings.TrimSpace(str) == "" {
return errors.New("email cannot be empty or only spaces")
}
if isVaild := utils.VaildEmail(str); !isVaild {
return errors.New("please enter correct email format")
}
return nil
}),
Expand All @@ -42,18 +50,24 @@ func CreateUserView(createView *CreateView) {
Title("First and Last Name").
Value(&createView.Realname).
Validate(func(str string) error {
if str == "" {
if strings.TrimSpace(str) == "" {
return errors.New("real name cannot be empty")
}
if isValid := utils.VaildFL(str); !isValid {
return errors.New("please enter correct first and last name format, like John Cooper")
}
return nil
}),
huh.NewInput().
Title("Password").
EchoMode(huh.EchoModePassword).
EchoMode(huh.EchoModePassword).
Value(&createView.Password).
Validate(func(str string) error {
if str == "" {
return errors.New("password cannot be empty")
if strings.TrimSpace(str) == "" {
return errors.New("password cannot be empty or only space")
}
if err := utils.ValidatePassword(str); err != nil {
return err
}
return nil
}),
Expand Down

0 comments on commit 301744f

Please sign in to comment.