Auto join teams if coming from team sign-up page to login for GitLab (#3284)
Этот коммит содержится в:
76
api/oauth.go
76
api/oauth.go
@@ -204,7 +204,10 @@ func completeOAuth(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
break
|
break
|
||||||
case model.OAUTH_ACTION_LOGIN:
|
case model.OAUTH_ACTION_LOGIN:
|
||||||
LoginByOAuth(c, w, r, service, body)
|
user := LoginByOAuth(c, w, r, service, body)
|
||||||
|
if len(teamId) > 0 {
|
||||||
|
c.Err = JoinUserToTeamById(teamId, user)
|
||||||
|
}
|
||||||
if c.Err == nil {
|
if c.Err == nil {
|
||||||
http.Redirect(w, r, GetProtocol(r)+"://"+r.Host, http.StatusTemporaryRedirect)
|
http.Redirect(w, r, GetProtocol(r)+"://"+r.Host, http.StatusTemporaryRedirect)
|
||||||
}
|
}
|
||||||
@@ -424,8 +427,17 @@ func loginWithOAuth(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
service := params["service"]
|
service := params["service"]
|
||||||
loginHint := r.URL.Query().Get("login_hint")
|
loginHint := r.URL.Query().Get("login_hint")
|
||||||
|
|
||||||
|
teamId, err := getTeamIdFromQuery(r.URL.Query())
|
||||||
|
if err != nil {
|
||||||
|
c.Err = err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
stateProps := map[string]string{}
|
stateProps := map[string]string{}
|
||||||
stateProps["action"] = model.OAUTH_ACTION_LOGIN
|
stateProps["action"] = model.OAUTH_ACTION_LOGIN
|
||||||
|
if len(teamId) != 0 {
|
||||||
|
stateProps["team_id"] = teamId
|
||||||
|
}
|
||||||
|
|
||||||
if authUrl, err := GetAuthorizationCode(c, service, stateProps, loginHint); err != nil {
|
if authUrl, err := GetAuthorizationCode(c, service, stateProps, loginHint); err != nil {
|
||||||
c.Err = err
|
c.Err = err
|
||||||
@@ -435,6 +447,36 @@ func loginWithOAuth(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getTeamIdFromQuery(query url.Values) (string, *model.AppError) {
|
||||||
|
hash := query.Get("h")
|
||||||
|
inviteId := query.Get("id")
|
||||||
|
|
||||||
|
if len(hash) > 0 {
|
||||||
|
data := query.Get("d")
|
||||||
|
props := model.MapFromJson(strings.NewReader(data))
|
||||||
|
|
||||||
|
if !model.ComparePassword(hash, fmt.Sprintf("%v:%v", data, utils.Cfg.EmailSettings.InviteSalt)) {
|
||||||
|
return "", model.NewLocAppError("getTeamIdFromQuery", "web.singup_with_oauth.invalid_link.app_error", nil, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
t, err := strconv.ParseInt(props["time"], 10, 64)
|
||||||
|
if err != nil || model.GetMillis()-t > 1000*60*60*48 { // 48 hours
|
||||||
|
return "", model.NewLocAppError("getTeamIdFromQuery", "web.singup_with_oauth.expired_link.app_error", nil, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
return props["id"], nil
|
||||||
|
} else if len(inviteId) > 0 {
|
||||||
|
if result := <-Srv.Store.Team().GetByInviteId(inviteId); result.Err != nil {
|
||||||
|
// soft fail, so we still create user but don't auto-join team
|
||||||
|
l4g.Error("%v", result.Err)
|
||||||
|
} else {
|
||||||
|
return result.Data.(*model.Team).Id, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
func signupWithOAuth(c *Context, w http.ResponseWriter, r *http.Request) {
|
func signupWithOAuth(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||||
params := mux.Vars(r)
|
params := mux.Vars(r)
|
||||||
service := params["service"]
|
service := params["service"]
|
||||||
@@ -445,34 +487,10 @@ func signupWithOAuth(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
hash := r.URL.Query().Get("h")
|
teamId, err := getTeamIdFromQuery(r.URL.Query())
|
||||||
|
if err != nil {
|
||||||
teamId := ""
|
c.Err = err
|
||||||
inviteId := r.URL.Query().Get("id")
|
return
|
||||||
|
|
||||||
if len(hash) > 0 {
|
|
||||||
data := r.URL.Query().Get("d")
|
|
||||||
props := model.MapFromJson(strings.NewReader(data))
|
|
||||||
|
|
||||||
if !model.ComparePassword(hash, fmt.Sprintf("%v:%v", data, utils.Cfg.EmailSettings.InviteSalt)) {
|
|
||||||
c.Err = model.NewLocAppError("signupWithOAuth", "web.singup_with_oauth.invalid_link.app_error", nil, "")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
t, err := strconv.ParseInt(props["time"], 10, 64)
|
|
||||||
if err != nil || model.GetMillis()-t > 1000*60*60*48 { // 48 hours
|
|
||||||
c.Err = model.NewLocAppError("signupWithOAuth", "web.singup_with_oauth.expired_link.app_error", nil, "")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
teamId = props["id"]
|
|
||||||
} else if len(inviteId) != 0 {
|
|
||||||
if result := <-Srv.Store.Team().GetByInviteId(inviteId); result.Err != nil {
|
|
||||||
// soft fail, so we still create user but don't auto-join team
|
|
||||||
l4g.Error("%v", result.Err)
|
|
||||||
} else {
|
|
||||||
teamId = result.Data.(*model.Team).Id
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
stateProps := map[string]string{}
|
stateProps := map[string]string{}
|
||||||
|
|||||||
12
api/team.go
12
api/team.go
@@ -17,6 +17,7 @@ import (
|
|||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
|
|
||||||
"github.com/mattermost/platform/model"
|
"github.com/mattermost/platform/model"
|
||||||
|
"github.com/mattermost/platform/store"
|
||||||
"github.com/mattermost/platform/utils"
|
"github.com/mattermost/platform/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -247,6 +248,14 @@ func CreateTeam(c *Context, team *model.Team) *model.Team {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func JoinUserToTeamById(teamId string, user *model.User) *model.AppError {
|
||||||
|
if result := <-Srv.Store.Team().Get(teamId); result.Err != nil {
|
||||||
|
return result.Err
|
||||||
|
} else {
|
||||||
|
return JoinUserToTeam(result.Data.(*model.Team), user)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func JoinUserToTeam(team *model.Team, user *model.User) *model.AppError {
|
func JoinUserToTeam(team *model.Team, user *model.User) *model.AppError {
|
||||||
|
|
||||||
tm := &model.TeamMember{TeamId: team.Id, UserId: user.Id}
|
tm := &model.TeamMember{TeamId: team.Id, UserId: user.Id}
|
||||||
@@ -258,6 +267,9 @@ func JoinUserToTeam(team *model.Team, user *model.User) *model.AppError {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if tmr := <-Srv.Store.Team().SaveMember(tm); tmr.Err != nil {
|
if tmr := <-Srv.Store.Team().SaveMember(tm); tmr.Err != nil {
|
||||||
|
if tmr.Err.Id == store.TEAM_MEMBER_EXISTS_ERROR {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
return tmr.Err
|
return tmr.Err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
23
api/user.go
23
api/user.go
@@ -285,11 +285,6 @@ func CreateOAuthUser(c *Context, w http.ResponseWriter, r *http.Request, service
|
|||||||
suchan := Srv.Store.User().GetByAuth(user.AuthData, service)
|
suchan := Srv.Store.User().GetByAuth(user.AuthData, service)
|
||||||
euchan := Srv.Store.User().GetByEmail(user.Email)
|
euchan := Srv.Store.User().GetByEmail(user.Email)
|
||||||
|
|
||||||
var tchan store.StoreChannel
|
|
||||||
if len(teamId) != 0 {
|
|
||||||
tchan = Srv.Store.Team().Get(teamId)
|
|
||||||
}
|
|
||||||
|
|
||||||
found := true
|
found := true
|
||||||
count := 0
|
count := 0
|
||||||
for found {
|
for found {
|
||||||
@@ -319,20 +314,14 @@ func CreateOAuthUser(c *Context, w http.ResponseWriter, r *http.Request, service
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if tchan != nil {
|
if len(teamId) > 0 {
|
||||||
if result := <-tchan; result.Err != nil {
|
err = JoinUserToTeamById(teamId, user)
|
||||||
c.Err = result.Err
|
if err != nil {
|
||||||
|
c.Err = err
|
||||||
return nil
|
return nil
|
||||||
} else {
|
|
||||||
team := result.Data.(*model.Team)
|
|
||||||
err = JoinUserToTeam(team, user)
|
|
||||||
if err != nil {
|
|
||||||
c.Err = err
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
go addDirectChannels(team.Id, user)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
go addDirectChannels(teamId, user)
|
||||||
}
|
}
|
||||||
|
|
||||||
doLogin(c, w, r, ruser, "")
|
doLogin(c, w, r, ruser, "")
|
||||||
|
|||||||
@@ -10,6 +10,10 @@ import (
|
|||||||
"github.com/mattermost/platform/utils"
|
"github.com/mattermost/platform/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
TEAM_MEMBER_EXISTS_ERROR = "store.sql_team.save_member.exists.app_error"
|
||||||
|
)
|
||||||
|
|
||||||
type SqlTeamStore struct {
|
type SqlTeamStore struct {
|
||||||
*SqlStore
|
*SqlStore
|
||||||
}
|
}
|
||||||
@@ -372,8 +376,8 @@ func (s SqlTeamStore) SaveMember(member *model.TeamMember) StoreChannel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := s.GetMaster().Insert(member); err != nil {
|
if err := s.GetMaster().Insert(member); err != nil {
|
||||||
if IsUniqueConstraintError(err.Error(), []string{"TeamId", "teammembers_pkey"}) {
|
if IsUniqueConstraintError(err.Error(), []string{"TeamId", "teammembers_pkey", "PRIMARY"}) {
|
||||||
result.Err = model.NewLocAppError("SqlTeamStore.SaveMember", "store.sql_team.save_member.exists.app_error", nil, "team_id="+member.TeamId+", user_id="+member.UserId+", "+err.Error())
|
result.Err = model.NewLocAppError("SqlTeamStore.SaveMember", TEAM_MEMBER_EXISTS_ERROR, nil, "team_id="+member.TeamId+", user_id="+member.UserId+", "+err.Error())
|
||||||
} else {
|
} else {
|
||||||
result.Err = model.NewLocAppError("SqlTeamStore.SaveMember", "store.sql_team.save_member.save.app_error", nil, "team_id="+member.TeamId+", user_id="+member.UserId+", "+err.Error())
|
result.Err = model.NewLocAppError("SqlTeamStore.SaveMember", "store.sql_team.save_member.save.app_error", nil, "team_id="+member.TeamId+", user_id="+member.UserId+", "+err.Error())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -444,7 +444,7 @@ export default class LoginController extends React.Component {
|
|||||||
<a
|
<a
|
||||||
className='btn btn-custom-login gitlab'
|
className='btn btn-custom-login gitlab'
|
||||||
key='gitlab'
|
key='gitlab'
|
||||||
href={Client.getOAuthRoute() + '/gitlab/login'}
|
href={Client.getOAuthRoute() + '/gitlab/login' + this.props.location.search}
|
||||||
>
|
>
|
||||||
<span className='icon'/>
|
<span className='icon'/>
|
||||||
<span>
|
<span>
|
||||||
@@ -529,4 +529,4 @@ export default class LoginController extends React.Component {
|
|||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user