aboutsummaryrefslogtreecommitdiff
path: root/models
diff options
context:
space:
mode:
authorUnknwon <u@gogs.io>2016-08-31 00:56:10 -0700
committerUnknwon <u@gogs.io>2016-08-31 00:56:10 -0700
commit99c2ae7b355556776f0a2231a3173a0c554e9ffe (patch)
tree56714e1c2105fe75a89fa63f2330c98b2981e101 /models
parentcd9b926af7d186c82997c26fea0bbdeed447b4dc (diff)
#3515 use alert instead 500 for duplicated login source name
Diffstat (limited to 'models')
-rw-r--r--models/error.go37
-rw-r--r--models/login.go18
-rw-r--r--models/user.go1
3 files changed, 37 insertions, 19 deletions
diff --git a/models/error.go b/models/error.go
index dd1d4a66..065857e0 100644
--- a/models/error.go
+++ b/models/error.go
@@ -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)
}
// ___________
diff --git a/models/login.go b/models/login.go
index 1f8d3c8d..bef1617d 100644
--- a/models/login.go
+++ b/models/login.go
@@ -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)
diff --git a/models/user.go b/models/user.go
index fa6bbacb..ef769de6 100644
--- a/models/user.go
+++ b/models/user.go
@@ -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")
)