From fb9a6c76113e5913d2ad7edb595a05e8567a3e85 Mon Sep 17 00:00:00 2001 From: JoramWilander Date: Fri, 11 Mar 2016 10:48:43 -0500 Subject: Auto-create account if team allows sign-up from login page and oauth account doesn't exist --- api/user.go | 12 +++++++++--- i18n/en.json | 6 +++++- i18n/es.json | 2 +- i18n/pt.json | 4 ++-- store/sql_user_store.go | 11 ++++++++--- 5 files changed, 25 insertions(+), 10 deletions(-) diff --git a/api/user.go b/api/user.go index b7e6220d8..353fdff3e 100644 --- a/api/user.go +++ b/api/user.go @@ -246,7 +246,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 { @@ -478,7 +478,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 { @@ -486,7 +489,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 { @@ -497,6 +500,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 { diff --git a/i18n/en.json b/i18n/en.json index bc33fc019..6a36e9084 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -3068,7 +3068,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." }, { diff --git a/i18n/es.json b/i18n/es.json index 4c0c1fd03..3d111931d 100644 --- a/i18n/es.json +++ b/i18n/es.json @@ -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." }, { diff --git a/i18n/pt.json b/i18n/pt.json index 8e45d5eaf..a76900a2b 100644 --- a/i18n/pt.json +++ b/i18n/pt.json @@ -3052,7 +3052,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." }, { @@ -3591,4 +3591,4 @@ "id": "web.watcher_fail.error", "translation": "Falha ao adicionar diretório observador %v" } -] \ No newline at end of file +] diff --git a/store/sql_user_store.go b/store/sql_user_store.go index 47c6ea61a..cc6829b94 100644 --- a/store/sql_user_store.go +++ b/store/sql_user_store.go @@ -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 -- cgit v1.2.3-1-g7c22