1
0
mirror of https://github.com/go-gitea/gitea.git synced 2024-10-29 08:17:31 -04:00

fix 500 when use a duplicat email instead of giving an error tip (#1040)

This commit is contained in:
Lunny Xiao 2017-02-25 22:57:06 +08:00 committed by GitHub
parent cd1821a7e2
commit 19b3c45ca7
2 changed files with 30 additions and 18 deletions

View File

@ -20,9 +20,9 @@ import (
"github.com/go-xorm/xorm" "github.com/go-xorm/xorm"
"code.gitea.io/gitea/modules/auth/ldap" "code.gitea.io/gitea/modules/auth/ldap"
"code.gitea.io/gitea/modules/auth/oauth2"
"code.gitea.io/gitea/modules/auth/pam" "code.gitea.io/gitea/modules/auth/pam"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/auth/oauth2"
) )
// LoginType represents an login type. // LoginType represents an login type.
@ -498,7 +498,7 @@ func LoginViaSMTP(user *User, login, password string, sourceID int64, cfg *SMTPC
idx := strings.Index(login, "@") idx := strings.Index(login, "@")
if idx == -1 { if idx == -1 {
return nil, ErrUserNotExist{0, login, 0} return nil, ErrUserNotExist{0, login, 0}
} else if !com.IsSliceContainsStr(strings.Split(cfg.AllowedDomains, ","), login[idx + 1:]) { } else if !com.IsSliceContainsStr(strings.Split(cfg.AllowedDomains, ","), login[idx+1:]) {
return nil, ErrUserNotExist{0, login, 0} return nil, ErrUserNotExist{0, login, 0}
} }
} }
@ -598,7 +598,7 @@ type OAuth2Provider struct {
// key is used to map the OAuth2Provider with the goth provider type (also in LoginSource.OAuth2Config.Provider) // key is used to map the OAuth2Provider with the goth provider type (also in LoginSource.OAuth2Config.Provider)
// value is used to store display data // value is used to store display data
var OAuth2Providers = map[string]OAuth2Provider{ var OAuth2Providers = map[string]OAuth2Provider{
"github": {Name: "github", DisplayName:"GitHub", Image: "/img/github.png"}, "github": {Name: "github", DisplayName: "GitHub", Image: "/img/github.png"},
} }
// ExternalUserLogin attempts a login using external source types. // ExternalUserLogin attempts a login using external source types.
@ -624,6 +624,16 @@ func UserSignIn(username, password string) (*User, error) {
var user *User var user *User
if strings.Contains(username, "@") { if strings.Contains(username, "@") {
user = &User{Email: strings.ToLower(strings.TrimSpace(username))} user = &User{Email: strings.ToLower(strings.TrimSpace(username))}
// check same email
cnt, err := x.Count(user)
if err != nil {
return nil, err
}
if cnt > 1 {
return nil, ErrEmailAlreadyUsed{
Email: user.Email,
}
}
} else { } else {
user = &User{LowerName: strings.ToLower(strings.TrimSpace(username))} user = &User{LowerName: strings.ToLower(strings.TrimSpace(username))}
} }

View File

@ -7,20 +7,20 @@ package user
import ( import (
"errors" "errors"
"fmt" "fmt"
"net/http"
"net/url" "net/url"
"strings"
"github.com/go-macaron/captcha"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/auth" "code.gitea.io/gitea/modules/auth"
"code.gitea.io/gitea/modules/auth/oauth2"
"code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"net/http"
"code.gitea.io/gitea/modules/auth/oauth2" "github.com/go-macaron/captcha"
"github.com/markbates/goth" "github.com/markbates/goth"
"strings"
) )
const ( const (
@ -66,7 +66,7 @@ func AutoSignIn(ctx *context.Context) (bool, error) {
} }
if val, _ := ctx.GetSuperSecureCookie( if val, _ := ctx.GetSuperSecureCookie(
base.EncodeMD5(u.Rands + u.Passwd), setting.CookieRememberName); val != u.Name { base.EncodeMD5(u.Rands+u.Passwd), setting.CookieRememberName); val != u.Name {
return false, nil return false, nil
} }
@ -144,6 +144,8 @@ func SignInPost(ctx *context.Context, form auth.SignInForm) {
if err != nil { if err != nil {
if models.IsErrUserNotExist(err) { if models.IsErrUserNotExist(err) {
ctx.RenderWithErr(ctx.Tr("form.username_password_incorrect"), tplSignIn, &form) ctx.RenderWithErr(ctx.Tr("form.username_password_incorrect"), tplSignIn, &form)
} else if models.IsErrEmailAlreadyUsed(err) {
ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplSignIn, &form)
} else { } else {
ctx.Handle(500, "UserSignIn", err) ctx.Handle(500, "UserSignIn", err)
} }
@ -296,7 +298,7 @@ func handleSignInFull(ctx *context.Context, u *models.User, remember bool, obeyR
if remember { if remember {
days := 86400 * setting.LogInRememberDays days := 86400 * setting.LogInRememberDays
ctx.SetCookie(setting.CookieUserName, u.Name, days, setting.AppSubURL) ctx.SetCookie(setting.CookieUserName, u.Name, days, setting.AppSubURL)
ctx.SetSuperSecureCookie(base.EncodeMD5(u.Rands + u.Passwd), ctx.SetSuperSecureCookie(base.EncodeMD5(u.Rands+u.Passwd),
setting.CookieRememberName, u.Name, days, setting.AppSubURL) setting.CookieRememberName, u.Name, days, setting.AppSubURL)
} }