mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-03 04:10:20 +08:00
feat: fix username checks when organization.UseEmailAsUsername is enabled (#3329)
* feat: Username support email format * feat: Only fulfill the first requirement * fix: Improve code robustness
This commit is contained in:
@ -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")
|
||||
}
|
||||
|
Reference in New Issue
Block a user