#3515 use alert instead 500 for duplicated login source name
This commit is contained in:
		
							parent
							
								
									cd9b926af7
								
							
						
					
					
						commit
						99c2ae7b35
					
				| @ -1028,6 +1028,7 @@ auths.delete_auth_title = Authentication Deletion | ||||
| auths.delete_auth_desc = This authentication is going to be deleted, do you want to continue? | ||||
| auths.still_in_used = This authentication is still used by some users, please delete or convert these users to another login type first. | ||||
| auths.deletion_success = Authentication has been deleted successfully! | ||||
| auths.login_source_exist = Login source '%s' already exists. | ||||
| 
 | ||||
| config.server_config = Server Configuration | ||||
| config.app_name = Application Name | ||||
|  | ||||
| @ -602,24 +602,37 @@ func (err ErrAttachmentNotExist) Error() string { | ||||
| 	return fmt.Sprintf("attachment does not exist [id: %d, uuid: %s]", err.ID, err.UUID) | ||||
| } | ||||
| 
 | ||||
| //    _____          __  .__                   __  .__               __  .__
 | ||||
| //   /  _  \  __ ___/  |_|  |__   ____   _____/  |_|__| ____ _____ _/  |_|__| ____   ____
 | ||||
| //  /  /_\  \|  |  \   __\  |  \_/ __ \ /    \   __\  |/ ___\\__  \\   __\  |/  _ \ /    \
 | ||||
| // /    |    \  |  /|  | |   Y  \  ___/|   |  \  | |  \  \___ / __ \|  | |  (  <_> )   |  \
 | ||||
| // \____|__  /____/ |__| |___|  /\___  >___|  /__| |__|\___  >____  /__| |__|\____/|___|  /
 | ||||
| //         \/                 \/     \/     \/             \/     \/                    \/
 | ||||
| // .____                 .__           _________
 | ||||
| // |    |    ____   ____ |__| ____    /   _____/ ____  __ _________   ____  ____
 | ||||
| // |    |   /  _ \ / ___\|  |/    \   \_____  \ /  _ \|  |  \_  __ \_/ ___\/ __ \
 | ||||
| // |    |__(  <_> ) /_/  >  |   |  \  /        (  <_> )  |  /|  | \/\  \__\  ___/
 | ||||
| // |_______ \____/\___  /|__|___|  / /_______  /\____/|____/ |__|    \___  >___  >
 | ||||
| //         \/    /_____/         \/          \/                          \/    \/
 | ||||
| 
 | ||||
| type ErrAuthenticationNotExist struct { | ||||
| type ErrLoginSourceNotExist struct { | ||||
| 	ID int64 | ||||
| } | ||||
| 
 | ||||
| func IsErrAuthenticationNotExist(err error) bool { | ||||
| 	_, ok := err.(ErrAuthenticationNotExist) | ||||
| func IsErrLoginSourceNotExist(err error) bool { | ||||
| 	_, ok := err.(ErrLoginSourceNotExist) | ||||
| 	return ok | ||||
| } | ||||
| 
 | ||||
| func (err ErrAuthenticationNotExist) Error() string { | ||||
| 	return fmt.Sprintf("authentication does not exist [id: %d]", err.ID) | ||||
| func (err ErrLoginSourceNotExist) Error() string { | ||||
| 	return fmt.Sprintf("login source does not exist [id: %d]", err.ID) | ||||
| } | ||||
| 
 | ||||
| type ErrLoginSourceAlreadyExist struct { | ||||
| 	Name string | ||||
| } | ||||
| 
 | ||||
| func IsErrLoginSourceAlreadyExist(err error) bool { | ||||
| 	_, ok := err.(ErrLoginSourceAlreadyExist) | ||||
| 	return ok | ||||
| } | ||||
| 
 | ||||
| func (err ErrLoginSourceAlreadyExist) Error() string { | ||||
| 	return fmt.Sprintf("login source already exists [name: %s]", err.Name) | ||||
| } | ||||
| 
 | ||||
| // ___________
 | ||||
|  | ||||
| @ -25,8 +25,7 @@ import ( | ||||
| ) | ||||
| 
 | ||||
| var ( | ||||
| 	ErrAuthenticationAlreadyExist = errors.New("Authentication already exist") | ||||
| 	ErrAuthenticationUserUsed     = errors.New("Authentication has been used by some users") | ||||
| 	ErrAuthenticationUserUsed = errors.New("Authentication has been used by some users") | ||||
| ) | ||||
| 
 | ||||
| type LoginType int | ||||
| @ -230,8 +229,15 @@ func CountLoginSources() int64 { | ||||
| 	return count | ||||
| } | ||||
| 
 | ||||
| func CreateSource(source *LoginSource) error { | ||||
| 	_, err := x.Insert(source) | ||||
| func CreateLoginSource(source *LoginSource) error { | ||||
| 	has, err := x.Get(&LoginSource{Name: source.Name}) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} else if has { | ||||
| 		return ErrLoginSourceAlreadyExist{source.Name} | ||||
| 	} | ||||
| 
 | ||||
| 	_, err = x.Insert(source) | ||||
| 	return err | ||||
| } | ||||
| 
 | ||||
| @ -247,7 +253,7 @@ func GetLoginSourceByID(id int64) (*LoginSource, error) { | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} else if !has { | ||||
| 		return nil, ErrAuthenticationNotExist{id} | ||||
| 		return nil, ErrLoginSourceNotExist{id} | ||||
| 	} | ||||
| 	return source, nil | ||||
| } | ||||
| @ -542,7 +548,7 @@ func UserSignIn(uname, passwd string) (*User, error) { | ||||
| 			if err != nil { | ||||
| 				return nil, err | ||||
| 			} else if !hasSource { | ||||
| 				return nil, ErrLoginSourceNotExist | ||||
| 				return nil, ErrLoginSourceNotExist{u.LoginSource} | ||||
| 			} | ||||
| 
 | ||||
| 			return ExternalUserLogin(u, u.LoginName, passwd, &source, false) | ||||
|  | ||||
| @ -46,7 +46,6 @@ var ( | ||||
| 	ErrEmailNotExist         = errors.New("E-mail does not exist") | ||||
| 	ErrEmailNotActivated     = errors.New("E-mail address has not been activated") | ||||
| 	ErrUserNameIllegal       = errors.New("User name contains illegal characters") | ||||
| 	ErrLoginSourceNotExist   = errors.New("Login source does not exist") | ||||
| 	ErrLoginSourceNotActived = errors.New("Login source is not actived") | ||||
| 	ErrUnsupportedLoginType  = errors.New("Login source is unknown") | ||||
| ) | ||||
|  | ||||
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							| @ -146,13 +146,18 @@ func NewAuthSourcePost(ctx *context.Context, form auth.AuthenticationForm) { | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	if err := models.CreateSource(&models.LoginSource{ | ||||
| 	if err := models.CreateLoginSource(&models.LoginSource{ | ||||
| 		Type:      models.LoginType(form.Type), | ||||
| 		Name:      form.Name, | ||||
| 		IsActived: form.IsActive, | ||||
| 		Cfg:       config, | ||||
| 	}); err != nil { | ||||
| 		ctx.Handle(500, "CreateSource", err) | ||||
| 		if models.IsErrLoginSourceAlreadyExist(err) { | ||||
| 			ctx.Data["Err_Name"] = true | ||||
| 			ctx.RenderWithErr(ctx.Tr("admin.auths.login_source_exist", err.(models.ErrLoginSourceAlreadyExist).Name), AUTH_NEW, form) | ||||
| 		} else { | ||||
| 			ctx.Handle(500, "CreateSource", err) | ||||
| 		} | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
|  | ||||
| @ -21,7 +21,7 @@ func parseLoginSource(ctx *context.APIContext, u *models.User, sourceID int64, l | ||||
| 
 | ||||
| 	source, err := models.GetLoginSourceByID(sourceID) | ||||
| 	if err != nil { | ||||
| 		if models.IsErrAuthenticationNotExist(err) { | ||||
| 		if models.IsErrLoginSourceNotExist(err) { | ||||
| 			ctx.Error(422, "", err) | ||||
| 		} else { | ||||
| 			ctx.Error(500, "GetLoginSourceByID", err) | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user