diff --git a/controllers/account.go b/controllers/account.go index 8fca5dba..091d0e52 100644 --- a/controllers/account.go +++ b/controllers/account.go @@ -203,12 +203,6 @@ func (c *ApiController) Signup() { } } - msg = object.CheckUsername(user.Name) - if msg != "" { - c.ResponseError(msg) - return - } - affected := object.AddUser(user) if !affected { c.ResponseError(fmt.Sprintf("Failed to create user, user information is invalid: %s", util.StructToJson(user))) diff --git a/controllers/auth.go b/controllers/auth.go index 6124f4fe..1ce5ea6e 100644 --- a/controllers/auth.go +++ b/controllers/auth.go @@ -411,12 +411,6 @@ func (c *ApiController) Login() { // sync info from 3rd-party if possible object.SetUserOAuthProperties(organization, user, provider.Type, userInfo) - msg := object.CheckUsername(user.Name) - if msg != "" { - c.ResponseError(msg) - return - } - affected := object.AddUser(user) if !affected { c.ResponseError(fmt.Sprintf("Failed to create user, user information is invalid: %s", util.StructToJson(user))) diff --git a/object/check.go b/object/check.go index a0261b0b..44f8a311 100644 --- a/object/check.go +++ b/object/check.go @@ -59,6 +59,11 @@ func CheckUserSignup(application *Application, organization *Organization, usern if reWhiteSpace.MatchString(username) { return "username cannot contain white spaces" } + msg := CheckUsername(username) + if msg != "" { + return msg + } + if HasUserByField(organization.Name, "name", username) { return "username already exists" } @@ -314,16 +319,16 @@ func CheckAccessPermission(userId string, application *Application) (bool, error return allowed, err } -func CheckUsername(name string) string { - if name == "" { +func CheckUsername(username string) string { + if username == "" { return "Empty username." - } else if len(name) > 39 { + } else if len(username) > 39 { return "Username is too long (maximum is 39 characters)." } // https://stackoverflow.com/questions/58726546/github-username-convention-using-regex re, _ := regexp.Compile("^[a-zA-Z0-9]+((?:-[a-zA-Z0-9]+)|(?:_[a-zA-Z0-9]+))*$") - if !re.MatchString(name) { + if !re.MatchString(username) { return "The username may only contain alphanumeric characters, underlines or hyphens, cannot have consecutive hyphens or underlines, and cannot begin or end with a hyphen or underline." }