diff --git a/object/check.go b/object/check.go index f414cbe7..4bf9eb3d 100644 --- a/object/check.go +++ b/object/check.go @@ -520,11 +520,41 @@ func CheckUsername(username string, lang string) string { return "" } +func CheckUsernameWithEmail(username string, lang string) string { + if username == "" { + return i18n.Translate(lang, "check:Empty username.") + } else if len(username) > 39 { + return i18n.Translate(lang, "check:Username is too long (maximum is 39 characters).") + } + + // https://stackoverflow.com/questions/58726546/github-username-convention-using-regex + + if !util.ReUserNameWithEmail.MatchString(username) { + return i18n.Translate(lang, "check:Username supports email format. Also 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. Also pay attention to the email format.") + } + return "" +} + func CheckUpdateUser(oldUser, user *User, lang string) string { if oldUser.Name != user.Name { - if msg := CheckUsername(user.Name, lang); msg != "" { - return msg + // Check if the organization uses email as the username + organization, err := GetOrganizationByUser(oldUser) + if err != nil { + return err.Error() } + if organization == nil { + return fmt.Sprintf(i18n.Translate(lang, "The organization: %s does not exist"), oldUser.Owner) + } + if organization.UseEmailAsUsername { + if msg := CheckUsernameWithEmail(user.Name, lang); msg != "" { + return msg + } + } else { + if msg := CheckUsername(user.Name, lang); msg != "" { + return msg + } + } + if HasUserByField(user.Owner, "name", user.Name) { return i18n.Translate(lang, "check:Username already exists") } diff --git a/util/validation.go b/util/validation.go index 665a9aea..c4bd19df 100644 --- a/util/validation.go +++ b/util/validation.go @@ -25,10 +25,11 @@ import ( ) var ( - rePhone *regexp.Regexp - ReWhiteSpace *regexp.Regexp - ReFieldWhiteList *regexp.Regexp - ReUserName *regexp.Regexp + rePhone *regexp.Regexp + ReWhiteSpace *regexp.Regexp + ReFieldWhiteList *regexp.Regexp + ReUserName *regexp.Regexp + ReUserNameWithEmail *regexp.Regexp ) func init() { @@ -36,6 +37,7 @@ func init() { ReWhiteSpace, _ = regexp.Compile(`\s`) ReFieldWhiteList, _ = regexp.Compile(`^[A-Za-z0-9]+$`) ReUserName, _ = regexp.Compile("^[a-zA-Z0-9]+([-._][a-zA-Z0-9]+)*$") + ReUserNameWithEmail, _ = regexp.Compile(`^([a-zA-Z0-9]+([-._][a-zA-Z0-9]+)*)|([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})$`) // Add support for email formats } func IsEmailValid(email string) bool {