Merge pull request #2419 from mattermost/plt-2044

PLT-2044 Auto-create account if team allows sign-up from login page and oauth …
This commit is contained in:
Christopher Speller
2016-03-16 18:22:50 -04:00
5 changed files with 25 additions and 10 deletions

View File

@@ -249,7 +249,7 @@ func CreateUser(team *model.Team, user *model.User) (*model.User, *model.AppErro
}
}
func CreateOAuthUser(c *Context, w http.ResponseWriter, r *http.Request, service string, userData io.ReadCloser, team *model.Team) *model.User {
func CreateOAuthUser(c *Context, w http.ResponseWriter, r *http.Request, service string, userData io.Reader, team *model.Team) *model.User {
var user *model.User
provider := einterfaces.GetOauthProvider(service)
if provider == nil {
@@ -481,7 +481,10 @@ func LoginByUsername(c *Context, w http.ResponseWriter, r *http.Request, usernam
return nil
}
func LoginByOAuth(c *Context, w http.ResponseWriter, r *http.Request, service string, userData io.ReadCloser, team *model.Team) *model.User {
func LoginByOAuth(c *Context, w http.ResponseWriter, r *http.Request, service string, userData io.Reader, team *model.Team) *model.User {
buf := bytes.Buffer{}
buf.ReadFrom(userData)
authData := ""
provider := einterfaces.GetOauthProvider(service)
if provider == nil {
@@ -489,7 +492,7 @@ func LoginByOAuth(c *Context, w http.ResponseWriter, r *http.Request, service st
map[string]interface{}{"Service": service}, "")
return nil
} else {
authData = provider.GetAuthDataFromJson(userData)
authData = provider.GetAuthDataFromJson(bytes.NewReader(buf.Bytes()))
}
if len(authData) == 0 {
@@ -500,6 +503,9 @@ func LoginByOAuth(c *Context, w http.ResponseWriter, r *http.Request, service st
var user *model.User
if result := <-Srv.Store.User().GetByAuth(team.Id, authData, service); result.Err != nil {
if result.Err.Id == store.MISSING_AUTH_ACCOUNT_ERROR && team.AllowOpenInvite {
return CreateOAuthUser(c, w, r, service, bytes.NewReader(buf.Bytes()), team)
}
c.Err = result.Err
return nil
} else {

View File

@@ -3072,7 +3072,11 @@
"translation": "We encountered an error finding the account"
},
{
"id": "store.sql_user.get_by_auth.app_error",
"id": "store.sql_user.get_by_auth.other.app_error",
"translation": "We encountered an error trying to find the account by authentication type."
},
{
"id": "store.sql_user.get_by_auth.missing_account.app_error",
"translation": "We couldn't find an existing account matching your authentication type for this team. This team may require an invite from the team owner to join."
},
{

View File

@@ -3068,7 +3068,7 @@
"translation": "Encontramos un error buscando la cuenta"
},
{
"id": "store.sql_user.get_by_auth.app_error",
"id": "store.sql_user.get_by_auth.missing_account.app_error",
"translation": "No pudimos encontrar una cuenta existente que coincida con tu tipo de autenticación para este equipo. Es posible que necesites una invitación por parte del dueño del equipo para unirte."
},
{

View File

@@ -3068,7 +3068,7 @@
"translation": "Encontramos um erro ao procurar a conta"
},
{
"id": "store.sql_user.get_by_auth.app_error",
"id": "store.sql_user.get_by_auth.missing_account.app_error",
"translation": "Não foi possível encontrar uma conta correspondente ao seu tipo de autenticação para esta equipe. Esta equipe pode exigir um convite do dono da equipe para participar."
},
{
@@ -3607,4 +3607,4 @@
"id": "web.watcher_fail.error",
"translation": "Falha ao adicionar diretório observador %v"
}
]
]

View File

@@ -4,6 +4,7 @@
package store
import (
"database/sql"
"fmt"
"github.com/mattermost/platform/model"
"github.com/mattermost/platform/utils"
@@ -11,7 +12,8 @@ import (
)
const (
MISSING_ACCOUNT_ERROR = "store.sql_user.missing_account.const"
MISSING_ACCOUNT_ERROR = "store.sql_user.missing_account.const"
MISSING_AUTH_ACCOUNT_ERROR = "store.sql_user.get_by_auth.missing_account.app_error"
)
type SqlUserStore struct {
@@ -481,8 +483,11 @@ func (us SqlUserStore) GetByAuth(teamId string, authData string, authService str
user := model.User{}
if err := us.GetReplica().SelectOne(&user, "SELECT * FROM Users WHERE TeamId = :TeamId AND AuthData = :AuthData AND AuthService = :AuthService", map[string]interface{}{"TeamId": teamId, "AuthData": authData, "AuthService": authService}); err != nil {
result.Err = model.NewLocAppError("SqlUserStore.GetByAuth", "store.sql_user.get_by_auth.app_error",
nil, "teamId="+teamId+", authData="+authData+", authService="+authService+", "+err.Error())
if err == sql.ErrNoRows {
result.Err = model.NewLocAppError("SqlUserStore.GetByAuth", MISSING_AUTH_ACCOUNT_ERROR, nil, "teamId="+teamId+", authData="+authData+", authService="+authService+", "+err.Error())
} else {
result.Err = model.NewLocAppError("SqlUserStore.GetByAuth", "store.sql_user.get_by_auth.other.app_error", nil, "teamId="+teamId+", authData="+authData+", authService="+authService+", "+err.Error())
}
}
result.Data = &user