username validation fixes #1418

This commit is contained in:
Brad Rydzewski
2017-07-17 00:01:35 -04:00
parent 0f693cb66d
commit 631cd10033
3 changed files with 74 additions and 0 deletions

View File

@@ -1,5 +1,15 @@
package model
import (
"errors"
"regexp"
)
// validate a username (e.g. from github)
var reUsername = regexp.MustCompile("^[a-zA-Z0-9-_]+$")
var errUserLoginInvalid = errors.New("Invalid User Login")
// User represents a registered user.
//
// swagger:model user
@@ -49,3 +59,17 @@ type User struct {
// DEPRECATED Admin indicates the user is a system administrator.
XAdmin bool `json:"-" meddler:"user_admin"`
}
// Validate validates the required fields and formats.
func (u *User) Validate() error {
switch {
case len(u.Login) == 0:
return errUserLoginInvalid
case len(u.Login) > 250:
return errUserLoginInvalid
case !reUsername.MatchString(u.Login):
return errUserLoginInvalid
default:
return nil
}
}