mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-03 12:30:19 +08:00
feat: support i18n in backend err messages (#1232)
* feat: support i18n in backend err messages * use gofumpt to fmt code * fix review problems * support auto generate err message * delete beego/i18n moudle * fix Github action test problems * fix review problems * use gofumpt to format code * use gofumpt to fmt code
This commit is contained in:
@ -22,6 +22,7 @@ import (
|
||||
"unicode"
|
||||
|
||||
"github.com/casdoor/casdoor/cred"
|
||||
"github.com/casdoor/casdoor/i18n"
|
||||
"github.com/casdoor/casdoor/util"
|
||||
goldap "github.com/go-ldap/ldap/v3"
|
||||
)
|
||||
@ -41,89 +42,89 @@ func init() {
|
||||
reFieldWhiteList, _ = regexp.Compile(`^[A-Za-z0-9]+$`)
|
||||
}
|
||||
|
||||
func CheckUserSignup(application *Application, organization *Organization, username string, password string, displayName string, firstName string, lastName string, email string, phone string, affiliation string) string {
|
||||
func CheckUserSignup(application *Application, organization *Organization, username string, password string, displayName string, firstName string, lastName string, email string, phone string, affiliation string, lang string) string {
|
||||
if organization == nil {
|
||||
return "organization does not exist"
|
||||
return i18n.Translate(lang, "OrgErr.DoNotExist")
|
||||
}
|
||||
|
||||
if application.IsSignupItemVisible("Username") {
|
||||
if len(username) <= 1 {
|
||||
return "username must have at least 2 characters"
|
||||
return i18n.Translate(lang, "UserErr.NameLessThanTwoCharacters")
|
||||
}
|
||||
if unicode.IsDigit(rune(username[0])) {
|
||||
return "username cannot start with a digit"
|
||||
return i18n.Translate(lang, "UserErr.NameStartWithADigitErr")
|
||||
}
|
||||
if util.IsEmailValid(username) {
|
||||
return "username cannot be an email address"
|
||||
return i18n.Translate(lang, "UserErr.NameIsEmailErr")
|
||||
}
|
||||
if reWhiteSpace.MatchString(username) {
|
||||
return "username cannot contain white spaces"
|
||||
return i18n.Translate(lang, "UserErr.NameCantainWhitSpaceErr")
|
||||
}
|
||||
msg := CheckUsername(username)
|
||||
msg := CheckUsername(username, lang)
|
||||
if msg != "" {
|
||||
return msg
|
||||
}
|
||||
|
||||
if HasUserByField(organization.Name, "name", username) {
|
||||
return "username already exists"
|
||||
return i18n.Translate(lang, "UserErr.NameExistedErr")
|
||||
}
|
||||
if HasUserByField(organization.Name, "email", email) {
|
||||
return "email already exists"
|
||||
return i18n.Translate(lang, "EmailErr.ExistedErr")
|
||||
}
|
||||
if HasUserByField(organization.Name, "phone", phone) {
|
||||
return "phone already exists"
|
||||
return i18n.Translate(lang, "PhoneErr.ExistedErr")
|
||||
}
|
||||
}
|
||||
|
||||
if len(password) <= 5 {
|
||||
return "password must have at least 6 characters"
|
||||
return i18n.Translate(lang, "UserErr.PasswordLessThanSixCharacters")
|
||||
}
|
||||
|
||||
if application.IsSignupItemVisible("Email") {
|
||||
if email == "" {
|
||||
if application.IsSignupItemRequired("Email") {
|
||||
return "email cannot be empty"
|
||||
return i18n.Translate(lang, "EmailErr.EmptyErr")
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
if HasUserByField(organization.Name, "email", email) {
|
||||
return "email already exists"
|
||||
return i18n.Translate(lang, "EmailErr.ExistedErr")
|
||||
} else if !util.IsEmailValid(email) {
|
||||
return "email is invalid"
|
||||
return i18n.Translate(lang, "EmailErr.EmailInvalid")
|
||||
}
|
||||
}
|
||||
|
||||
if application.IsSignupItemVisible("Phone") {
|
||||
if phone == "" {
|
||||
if application.IsSignupItemRequired("Phone") {
|
||||
return "phone cannot be empty"
|
||||
return i18n.Translate(lang, "PhoneErr.EmptyErr")
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
if HasUserByField(organization.Name, "phone", phone) {
|
||||
return "phone already exists"
|
||||
return i18n.Translate(lang, "PhoneErr.ExistedErr")
|
||||
} else if organization.PhonePrefix == "86" && !util.IsPhoneCnValid(phone) {
|
||||
return "phone number is invalid"
|
||||
return i18n.Translate(lang, "PhoneErr.NumberInvalid")
|
||||
}
|
||||
}
|
||||
|
||||
if application.IsSignupItemVisible("Display name") {
|
||||
if application.GetSignupItemRule("Display name") == "First, last" && (firstName != "" || lastName != "") {
|
||||
if firstName == "" {
|
||||
return "firstName cannot be blank"
|
||||
return i18n.Translate(lang, "UserErr.FirstNameBlankErr")
|
||||
} else if lastName == "" {
|
||||
return "lastName cannot be blank"
|
||||
return i18n.Translate(lang, "UserErr.LastNameBlankErr")
|
||||
}
|
||||
} else {
|
||||
if displayName == "" {
|
||||
return "displayName cannot be blank"
|
||||
return i18n.Translate(lang, "UserErr.DisplayNameBlankErr")
|
||||
} else if application.GetSignupItemRule("Display name") == "Real name" {
|
||||
if !isValidRealName(displayName) {
|
||||
return "displayName is not valid real name"
|
||||
return i18n.Translate(lang, "UserErr.DisplayNameInvalid")
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -131,14 +132,14 @@ func CheckUserSignup(application *Application, organization *Organization, usern
|
||||
|
||||
if application.IsSignupItemVisible("Affiliation") {
|
||||
if affiliation == "" {
|
||||
return "affiliation cannot be blank"
|
||||
return i18n.Translate(lang, "UserErr.AffiliationBlankErr")
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
func checkSigninErrorTimes(user *User) string {
|
||||
func checkSigninErrorTimes(user *User, lang string) string {
|
||||
if user.SigninWrongTimes >= SigninWrongTimesLimit {
|
||||
lastSignWrongTime, _ := time.Parse(time.RFC3339, user.LastSigninWrongTime)
|
||||
passedTime := time.Now().UTC().Sub(lastSignWrongTime)
|
||||
@ -146,7 +147,7 @@ func checkSigninErrorTimes(user *User) string {
|
||||
|
||||
// deny the login if the error times is greater than the limit and the last login time is less than the duration
|
||||
if seconds > 0 {
|
||||
return fmt.Sprintf("You have entered the wrong password too many times, please wait for %d minutes %d seconds and try again", seconds/60, seconds%60)
|
||||
return fmt.Sprintf(i18n.Translate(lang, "AuthErr.WrongPasswordManyTimes"), seconds/60, seconds%60)
|
||||
}
|
||||
|
||||
// reset the error times
|
||||
@ -158,15 +159,15 @@ func checkSigninErrorTimes(user *User) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func CheckPassword(user *User, password string) string {
|
||||
func CheckPassword(user *User, password string, lang string) string {
|
||||
// check the login error times
|
||||
if msg := checkSigninErrorTimes(user); msg != "" {
|
||||
if msg := checkSigninErrorTimes(user, lang); msg != "" {
|
||||
return msg
|
||||
}
|
||||
|
||||
organization := GetOrganizationByUser(user)
|
||||
if organization == nil {
|
||||
return "organization does not exist"
|
||||
return i18n.Translate(lang, "OrgErr.DoNotExist")
|
||||
}
|
||||
|
||||
credManager := cred.GetCredManager(organization.PasswordType)
|
||||
@ -185,11 +186,11 @@ func CheckPassword(user *User, password string) string {
|
||||
|
||||
return recordSigninErrorInfo(user)
|
||||
} else {
|
||||
return fmt.Sprintf("unsupported password type: %s", organization.PasswordType)
|
||||
return fmt.Sprintf(i18n.Translate(lang, "LoginErr.UnsupportedPasswordType"), organization.PasswordType)
|
||||
}
|
||||
}
|
||||
|
||||
func checkLdapUserPassword(user *User, password string) (*User, string) {
|
||||
func checkLdapUserPassword(user *User, password string, lang string) (*User, string) {
|
||||
ldaps := GetLdaps(user.Owner)
|
||||
ldapLoginSuccess := false
|
||||
for _, ldapServer := range ldaps {
|
||||
@ -209,7 +210,7 @@ func checkLdapUserPassword(user *User, password string) (*User, string) {
|
||||
if len(searchResult.Entries) == 0 {
|
||||
continue
|
||||
} else if len(searchResult.Entries) > 1 {
|
||||
return nil, "Error: multiple accounts with same uid, please check your ldap server"
|
||||
return nil, i18n.Translate(lang, "LdapErr.MultipleAccounts")
|
||||
}
|
||||
|
||||
dn := searchResult.Entries[0].DN
|
||||
@ -220,26 +221,26 @@ func checkLdapUserPassword(user *User, password string) (*User, string) {
|
||||
}
|
||||
|
||||
if !ldapLoginSuccess {
|
||||
return nil, "ldap user name or password incorrect"
|
||||
return nil, i18n.Translate(lang, "LdapErr.PasswordWrong")
|
||||
}
|
||||
return user, ""
|
||||
}
|
||||
|
||||
func CheckUserPassword(organization string, username string, password string) (*User, string) {
|
||||
func CheckUserPassword(organization string, username string, password string, lang string) (*User, string) {
|
||||
user := GetUserByFields(organization, username)
|
||||
if user == nil || user.IsDeleted == true {
|
||||
return nil, "the user does not exist, please sign up first"
|
||||
return nil, i18n.Translate(lang, "UserErr.DoNotExistSignUp")
|
||||
}
|
||||
|
||||
if user.IsForbidden {
|
||||
return nil, "the user is forbidden to sign in, please contact the administrator"
|
||||
return nil, i18n.Translate(lang, "LoginErr.UserIsForbidden")
|
||||
}
|
||||
|
||||
if user.Ldap != "" {
|
||||
// ONLY for ldap users
|
||||
return checkLdapUserPassword(user, password)
|
||||
return checkLdapUserPassword(user, password, lang)
|
||||
} else {
|
||||
msg := CheckPassword(user, password)
|
||||
msg := CheckPassword(user, password, lang)
|
||||
if msg != "" {
|
||||
return nil, msg
|
||||
}
|
||||
@ -251,15 +252,15 @@ func filterField(field string) bool {
|
||||
return reFieldWhiteList.MatchString(field)
|
||||
}
|
||||
|
||||
func CheckUserPermission(requestUserId, userId, userOwner string, strict bool) (bool, error) {
|
||||
func CheckUserPermission(requestUserId, userId, userOwner string, strict bool, lang string) (bool, error) {
|
||||
if requestUserId == "" {
|
||||
return false, fmt.Errorf("please login first")
|
||||
return false, fmt.Errorf(i18n.Translate(lang, "LoginErr.LoginFirst"))
|
||||
}
|
||||
|
||||
if userId != "" {
|
||||
targetUser := GetUser(userId)
|
||||
if targetUser == nil {
|
||||
return false, fmt.Errorf("the user: %s doesn't exist", userId)
|
||||
return false, fmt.Errorf(i18n.Translate(lang, "UserErr.DoNotExist"), userId)
|
||||
}
|
||||
|
||||
userOwner = targetUser.Owner
|
||||
@ -271,7 +272,7 @@ func CheckUserPermission(requestUserId, userId, userOwner string, strict bool) (
|
||||
} else {
|
||||
requestUser := GetUser(requestUserId)
|
||||
if requestUser == nil {
|
||||
return false, fmt.Errorf("session outdated, please login again")
|
||||
return false, fmt.Errorf(i18n.Translate(lang, "LoginErr.SessionOutdated"))
|
||||
}
|
||||
if requestUser.IsGlobalAdmin {
|
||||
hasPermission = true
|
||||
@ -286,7 +287,7 @@ func CheckUserPermission(requestUserId, userId, userOwner string, strict bool) (
|
||||
}
|
||||
}
|
||||
|
||||
return hasPermission, fmt.Errorf("you don't have the permission to do this")
|
||||
return hasPermission, fmt.Errorf(i18n.Translate(lang, "LoginErr.NoPermission"))
|
||||
}
|
||||
|
||||
func CheckAccessPermission(userId string, application *Application) (bool, error) {
|
||||
@ -319,11 +320,11 @@ func CheckAccessPermission(userId string, application *Application) (bool, error
|
||||
return allowed, err
|
||||
}
|
||||
|
||||
func CheckUsername(username string) string {
|
||||
func CheckUsername(username string, lang string) string {
|
||||
if username == "" {
|
||||
return "Empty username."
|
||||
return i18n.Translate(lang, "UserErr.NameEmptyErr")
|
||||
} else if len(username) > 39 {
|
||||
return "Username is too long (maximum is 39 characters)."
|
||||
return i18n.Translate(lang, "UserErr.NameTooLang")
|
||||
}
|
||||
|
||||
exclude, _ := regexp.Compile("^[\u0021-\u007E]+$")
|
||||
@ -334,7 +335,7 @@ func CheckUsername(username string) string {
|
||||
// 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(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."
|
||||
return i18n.Translate(lang, "UserErr.NameFormatErr")
|
||||
}
|
||||
|
||||
return ""
|
||||
|
Reference in New Issue
Block a user