From 026553e4f87bfc647a5c03129752e30fc523fa07 Mon Sep 17 00:00:00 2001 From: Christopher Speller Date: Tue, 6 Dec 2016 10:49:34 -0500 Subject: Improving command line interface (#4689) --- api/apitestlib.go | 9 +++--- api/cli_test.go | 65 ++++++++++++++++++++++++-------------------- api/command_invite_people.go | 2 +- api/server.go | 6 +++- api/status_test.go | 11 ++++---- api/team.go | 56 ++++++++++++++------------------------ api/team_test.go | 12 ++++---- api/user.go | 23 ++++++---------- api/user_test.go | 7 ++--- api/web_hub.go | 4 ++- 10 files changed, 92 insertions(+), 103 deletions(-) (limited to 'api') diff --git a/api/apitestlib.go b/api/apitestlib.go index 9345d3fc4..f11613a90 100644 --- a/api/apitestlib.go +++ b/api/apitestlib.go @@ -37,6 +37,8 @@ func SetupEnterprise() *TestHelper { utils.DisableDebugLogForTest() utils.License.Features.SetDefaults() NewServer() + InitStores() + InitRouter() StartServer() utils.InitHTML() InitApi() @@ -58,6 +60,8 @@ func Setup() *TestHelper { *utils.Cfg.RateLimitSettings.Enable = false utils.DisableDebugLogForTest() NewServer() + InitStores() + InitRouter() StartServer() InitApi() utils.EnableDebugLogForTest() @@ -90,10 +94,7 @@ func (me *TestHelper) InitSystemAdmin() *TestHelper { me.SystemAdminUser = me.CreateUser(me.SystemAdminClient) LinkUserToTeam(me.SystemAdminUser, me.SystemAdminTeam) me.SystemAdminClient.SetTeamId(me.SystemAdminTeam.Id) - c := &Context{} - c.RequestId = model.NewId() - c.IpAddress = "cmd_line" - UpdateUserRoles(c, me.SystemAdminUser, model.ROLE_SYSTEM_USER.Id+" "+model.ROLE_SYSTEM_ADMIN.Id) + UpdateUserRoles(me.SystemAdminUser, model.ROLE_SYSTEM_USER.Id+" "+model.ROLE_SYSTEM_ADMIN.Id) me.SystemAdminUser.Password = "Password1" me.LoginSystemAdmin() me.SystemAdminChannel = me.CreateChannel(me.SystemAdminClient, me.SystemAdminTeam) diff --git a/api/cli_test.go b/api/cli_test.go index 4613988f3..ed8f764a3 100644 --- a/api/cli_test.go +++ b/api/cli_test.go @@ -17,7 +17,7 @@ func TestCliVersion(t *testing.T) { return } - cmd := exec.Command("bash", "-c", `go run ../mattermost.go -version`) + cmd := exec.Command("bash", "-c", `go run ../cmd/platform/*.go version`) output, err := cmd.CombinedOutput() if err != nil { t.Log(string(output)) @@ -33,10 +33,10 @@ func TestCliCreateTeam(t *testing.T) { th := Setup().InitSystemAdmin() id := model.NewId() - email := "success+" + id + "@simulator.amazonses.com" name := "name" + id + displayName := "Name " + id - cmd := exec.Command("bash", "-c", `go run ../mattermost.go -create_team -team_name="`+name+`" -email="`+email+`"`) + cmd := exec.Command("bash", "-c", `go run ../cmd/platform/*.go team create --name "`+name+`" --display_name "`+displayName+`"`) output, err := cmd.CombinedOutput() if err != nil { t.Log(string(output)) @@ -61,13 +61,20 @@ func TestCliCreateUserWithTeam(t *testing.T) { email := "success+" + id + "@simulator.amazonses.com" username := "name" + id - cmd := exec.Command("bash", "-c", `go run ../mattermost.go -create_user -team_name="`+th.SystemAdminTeam.Name+`" -email="`+email+`" -password="mypassword1" -username="`+username+`"`) + cmd := exec.Command("bash", "-c", `go run ../cmd/platform/*.go user create --email "`+email+`" --password "mypassword1" --username "`+username+`"`) output, err := cmd.CombinedOutput() if err != nil { t.Log(string(output)) t.Fatal(err) } + cmd2 := exec.Command("bash", "-c", `go run ../cmd/platform/*.go team add `+th.SystemAdminTeam.Id+" "+email) + output2, err2 := cmd2.CombinedOutput() + if err2 != nil { + t.Log(string(output2)) + t.Fatal(err2) + } + profiles := th.SystemAdminClient.Must(th.SystemAdminClient.GetProfilesInTeam(th.SystemAdminTeam.Id, 0, 1000, "")).Data.(map[string]*model.User) found := false @@ -94,7 +101,7 @@ func TestCliCreateUserWithoutTeam(t *testing.T) { email := "success+" + id + "@simulator.amazonses.com" username := "name" + id - cmd := exec.Command("bash", "-c", `go run ../mattermost.go -create_user -email="`+email+`" -password="mypassword1" -username="`+username+`"`) + cmd := exec.Command("bash", "-c", `go run ../cmd/platform/*.go user create --email "`+email+`" --password "mypassword1" --username "`+username+`"`) output, err := cmd.CombinedOutput() if err != nil { t.Log(string(output)) @@ -118,7 +125,7 @@ func TestCliAssignRole(t *testing.T) { th := Setup().InitBasic() - cmd := exec.Command("bash", "-c", `go run ../mattermost.go -assign_role -email="`+th.BasicUser.Email+`" -role="system_user system_admin"`) + cmd := exec.Command("bash", "-c", "go run ../cmd/platform/*.go roles system_admin "+th.BasicUser.Email) output, err := cmd.CombinedOutput() if err != nil { t.Log(string(output)) @@ -129,7 +136,7 @@ func TestCliAssignRole(t *testing.T) { t.Fatal() } else { user := result.Data.(*model.User) - if user.Roles != "system_user system_admin" { + if user.Roles != "system_admin system_user" { t.Fatal() } } @@ -145,7 +152,7 @@ func TestCliJoinChannel(t *testing.T) { // These test cannot run since this feature requires an enteprise license - // cmd := exec.Command("bash", "-c", `go run ../mattermost.go -join_channel -team_name="`+th.BasicTeam.Name+`" -channel_name="`+channel.Name+`" -email="`+th.BasicUser2.Email+`"`) + // cmd := exec.Command("bash", "-c", `go run ../*.go -join_channel -team_name="`+th.BasicTeam.Name+`" -channel_name="`+channel.Name+`" -email="`+th.BasicUser2.Email+`"`) // output, err := cmd.CombinedOutput() // if err != nil { // t.Log(string(output)) @@ -153,7 +160,7 @@ func TestCliJoinChannel(t *testing.T) { // } // // Joining twice should succeed - // cmd1 := exec.Command("bash", "-c", `go run ../mattermost.go -join_channel -team_name="`+th.BasicTeam.Name+`" -channel_name="`+channel.Name+`" -email="`+th.BasicUser2.Email+`"`) + // cmd1 := exec.Command("bash", "-c", `go run ../*.go -join_channel -team_name="`+th.BasicTeam.Name+`" -channel_name="`+channel.Name+`" -email="`+th.BasicUser2.Email+`"`) // output1, err1 := cmd1.CombinedOutput() // if err1 != nil { // t.Log(string(output1)) @@ -161,7 +168,7 @@ func TestCliJoinChannel(t *testing.T) { // } // should fail because channel does not exist - cmd2 := exec.Command("bash", "-c", `go run ../mattermost.go -join_channel -team_name="`+th.BasicTeam.Name+`" -channel_name="`+channel.Name+`asdf" -email="`+th.BasicUser2.Email+`"`) + cmd2 := exec.Command("bash", "-c", "go run ../cmd/platform/*.go channel add "+th.BasicTeam.Name+":"+channel.Name+"asdf "+th.BasicUser2.Email) output2, err2 := cmd2.CombinedOutput() if err2 == nil { t.Log(string(output2)) @@ -169,7 +176,7 @@ func TestCliJoinChannel(t *testing.T) { } // should fail because channel does not have license - cmd3 := exec.Command("bash", "-c", `go run ../mattermost.go -join_channel -team_name="`+th.BasicTeam.Name+`" -channel_name="`+channel.Name+`" -email="`+th.BasicUser2.Email+`"`) + cmd3 := exec.Command("bash", "-c", "go run ../cmd/platform/*.go channel add "+th.BasicTeam.Name+":"+channel.Name+" "+th.BasicUser2.Email) output3, err3 := cmd3.CombinedOutput() if err3 == nil { t.Log(string(output3)) @@ -187,14 +194,14 @@ func TestCliRemoveChannel(t *testing.T) { // These test cannot run since this feature requires an enteprise license - // cmd := exec.Command("bash", "-c", `go run ../mattermost.go -join_channel -team_name="`+th.BasicTeam.Name+`" -channel_name="`+channel.Name+`" -email="`+th.BasicUser2.Email+`"`) + // cmd := exec.Command("bash", "-c", `go run ../*.go -join_channel -team_name="`+th.BasicTeam.Name+`" -channel_name="`+channel.Name+`" -email="`+th.BasicUser2.Email+`"`) // output, err := cmd.CombinedOutput() // if err != nil { // t.Log(string(output)) // t.Fatal(err) // } - // cmd0 := exec.Command("bash", "-c", `go run ../mattermost.go -leave_channel -team_name="`+th.BasicTeam.Name+`" -channel_name="`+channel.Name+`" -email="`+th.BasicUser2.Email+`"`) + // cmd0 := exec.Command("bash", "-c", `go run ../*.go -leave_channel -team_name="`+th.BasicTeam.Name+`" -channel_name="`+channel.Name+`" -email="`+th.BasicUser2.Email+`"`) // output0, err0 := cmd0.CombinedOutput() // if err0 != nil { // t.Log(string(output0)) @@ -202,7 +209,7 @@ func TestCliRemoveChannel(t *testing.T) { // } // // Leaving twice should succeed - // cmd1 := exec.Command("bash", "-c", `go run ../mattermost.go -leave_channel -team_name="`+th.BasicTeam.Name+`" -channel_name="`+channel.Name+`" -email="`+th.BasicUser2.Email+`"`) + // cmd1 := exec.Command("bash", "-c", `go run ../*.go -leave_channel -team_name="`+th.BasicTeam.Name+`" -channel_name="`+channel.Name+`" -email="`+th.BasicUser2.Email+`"`) // output1, err1 := cmd1.CombinedOutput() // if err1 != nil { // t.Log(string(output1)) @@ -210,7 +217,7 @@ func TestCliRemoveChannel(t *testing.T) { // } // cannot leave town-square - cmd1a := exec.Command("bash", "-c", `go run ../mattermost.go -leave_channel -team_name="`+th.BasicTeam.Name+`" -channel_name="town-square" -email="`+th.BasicUser2.Email+`"`) + cmd1a := exec.Command("bash", "-c", "go run ../cmd/platform/*.go channel remove "+th.BasicTeam.Name+":town-square "+th.BasicUser2.Email) output1a, err1a := cmd1a.CombinedOutput() if err1a == nil { t.Log(string(output1a)) @@ -218,7 +225,7 @@ func TestCliRemoveChannel(t *testing.T) { } // should fail because channel does not exist - cmd2 := exec.Command("bash", "-c", `go run ../mattermost.go -leave_channel -team_name="`+th.BasicTeam.Name+`" -channel_name="`+channel.Name+`asdf" -email="`+th.BasicUser2.Email+`"`) + cmd2 := exec.Command("bash", "-c", "go run ../cmd/platform/*.go channel remove "+th.BasicTeam.Name+":doesnotexist "+th.BasicUser2.Email) output2, err2 := cmd2.CombinedOutput() if err2 == nil { t.Log(string(output2)) @@ -226,7 +233,7 @@ func TestCliRemoveChannel(t *testing.T) { } // should fail because channel does not have license - cmd3 := exec.Command("bash", "-c", `go run ../mattermost.go -leave_channel -team_name="`+th.BasicTeam.Name+`" -channel_name="`+channel.Name+`" -email="`+th.BasicUser2.Email+`"`) + cmd3 := exec.Command("bash", "-c", "go run ../cmd/platform/*.go channel remove "+th.BasicTeam.Name+":"+channel.Name+" "+th.BasicUser2.Email) output3, err3 := cmd3.CombinedOutput() if err3 == nil { t.Log(string(output3)) @@ -245,7 +252,7 @@ func TestCliListChannels(t *testing.T) { // These test cannot run since this feature requires an enteprise license - // cmd := exec.Command("bash", "-c", `go run ../mattermost.go -list_channels -team_name="`+th.BasicTeam.Name+`"`) + // cmd := exec.Command("bash", "-c", `go run ../*.go -list_channels -team_name="`+th.BasicTeam.Name+`"`) // output, err := cmd.CombinedOutput() // if err != nil { // t.Log(string(output)) @@ -261,7 +268,7 @@ func TestCliListChannels(t *testing.T) { // } // should fail because channel does not have license - cmd3 := exec.Command("bash", "-c", `go run ../mattermost.go -list_channels -team_name="`+th.BasicTeam.Name+``) + cmd3 := exec.Command("bash", "-c", "go run ../cmd/platform/*.go channel list "+th.BasicTeam.Name) output3, err3 := cmd3.CombinedOutput() if err3 == nil { t.Log(string(output3)) @@ -280,7 +287,7 @@ func TestCliRestoreChannel(t *testing.T) { // These test cannot run since this feature requires an enteprise license - // cmd := exec.Command("bash", "-c", `go run ../mattermost.go -restore_channel -team_name="`+th.BasicTeam.Name+`" -channel_name="`+channel.Name+`"`) + // cmd := exec.Command("bash", "-c", `go run ../*.go -restore_channel -team_name="`+th.BasicTeam.Name+`" -channel_name="`+channel.Name+`"`) // output, err := cmd.CombinedOutput() // if err != nil { // t.Log(string(output)) @@ -288,7 +295,7 @@ func TestCliRestoreChannel(t *testing.T) { // } // // restoring twice should succeed - // cmd1 := exec.Command("bash", "-c", `go run ../mattermost.go -restore_channel -team_name="`+th.BasicTeam.Name+`" -channel_name="`+channel.Name+`"`) + // cmd1 := exec.Command("bash", "-c", `go run ../*.go -restore_channel -team_name="`+th.BasicTeam.Name+`" -channel_name="`+channel.Name+`"`) // output1, err1 := cmd1.CombinedOutput() // if err1 != nil { // t.Log(string(output1)) @@ -296,7 +303,7 @@ func TestCliRestoreChannel(t *testing.T) { // } // should fail because channel does not have license - cmd3 := exec.Command("bash", "-c", `go run ../mattermost.go -restore_channel -team_name="`+th.BasicTeam.Name+`" -channel_name="`+channel.Name+`"`) + cmd3 := exec.Command("bash", "-c", "go run ../cmd/platform/*.go channel restore "+th.BasicTeam.Name+":"+channel.Name) output3, err3 := cmd3.CombinedOutput() if err3 == nil { t.Log(string(output3)) @@ -311,7 +318,7 @@ func TestCliJoinTeam(t *testing.T) { th := Setup().InitSystemAdmin().InitBasic() - cmd := exec.Command("bash", "-c", `go run ../mattermost.go -join_team -team_name="`+th.SystemAdminTeam.Name+`" -email="`+th.BasicUser.Email+`"`) + cmd := exec.Command("bash", "-c", "go run ../cmd/platform/*.go team add "+th.SystemAdminTeam.Name+" "+th.BasicUser.Email) output, err := cmd.CombinedOutput() if err != nil { t.Log(string(output)) @@ -341,7 +348,7 @@ func TestCliLeaveTeam(t *testing.T) { th := Setup().InitBasic() - cmd := exec.Command("bash", "-c", `go run ../mattermost.go -leave_team -team_name="`+th.BasicTeam.Name+`" -email="`+th.BasicUser.Email+`"`) + cmd := exec.Command("bash", "-c", "go run ../cmd/platform/*.go team remove "+th.BasicTeam.Name+" "+th.BasicUser.Email) output, err := cmd.CombinedOutput() if err != nil { t.Log(string(output)) @@ -378,7 +385,7 @@ func TestCliResetPassword(t *testing.T) { th := Setup().InitBasic() - cmd := exec.Command("bash", "-c", `go run ../mattermost.go -reset_password -email="`+th.BasicUser.Email+`" -password="password2"`) + cmd := exec.Command("bash", "-c", "go run ../cmd/platform/*.go user password "+th.BasicUser.Email+" password2") output, err := cmd.CombinedOutput() if err != nil { t.Log(string(output)) @@ -401,7 +408,7 @@ func TestCliCreateChannel(t *testing.T) { name := "name" + id // should fail because channel does not have license - cmd := exec.Command("bash", "-c", `go run ../mattermost.go -create_channel -email="`+th.BasicUser.Email+`" -team_name="`+th.BasicTeam.Name+`" -channel_type="O" -channel_name="`+name+`"`) + cmd := exec.Command("bash", "-c", "go run ../cmd/platform/*.go channel create --display_name "+name+" --team "+th.BasicTeam.Name+" --name "+name) output, err := cmd.CombinedOutput() if err == nil { t.Log(string(output)) @@ -410,7 +417,7 @@ func TestCliCreateChannel(t *testing.T) { // should fail because channel does not have license name = name + "-private" - cmd2 := exec.Command("bash", "-c", `go run ../mattermost.go -create_channel -email="`+th.BasicUser.Email+`" -team_name="`+th.BasicTeam.Name+`" -channel_type="P" -channel_name="`+name+`"`) + cmd2 := exec.Command("bash", "-c", "go run ../cmd/platform/*.go channel create --display_name="+name+" --team "+th.BasicTeam.Name+" --private --name "+name) output2, err2 := cmd2.CombinedOutput() if err2 == nil { t.Log(string(output2)) @@ -426,7 +433,7 @@ func TestCliMakeUserActiveAndInactive(t *testing.T) { th := Setup().InitBasic() // first inactivate the user - cmd := exec.Command("bash", "-c", `go run ../mattermost.go -activate_user -inactive -email="`+th.BasicUser.Email+`"`) + cmd := exec.Command("bash", "-c", "go run ../cmd/platform/*.go user deactivate "+th.BasicUser.Email) output, err := cmd.CombinedOutput() if err != nil { t.Log(string(output)) @@ -434,7 +441,7 @@ func TestCliMakeUserActiveAndInactive(t *testing.T) { } // activate the inactive user - cmd2 := exec.Command("bash", "-c", `go run ../mattermost.go -activate_user -email="`+th.BasicUser.Email+`"`) + cmd2 := exec.Command("bash", "-c", "go run ../cmd/platform/*.go user activate "+th.BasicUser.Email) output2, err2 := cmd2.CombinedOutput() if err2 != nil { t.Log(string(output2)) diff --git a/api/command_invite_people.go b/api/command_invite_people.go index 4aa1d82f8..93f8f85eb 100644 --- a/api/command_invite_people.go +++ b/api/command_invite_people.go @@ -72,7 +72,7 @@ func (me *InvitePeopleProvider) DoCommand(c *Context, channelId string, message user = result.Data.(*model.User) } - go InviteMembers(c, team, user, emailList) + go InviteMembers(team, user.GetDisplayName(), emailList) return &model.CommandResponse{ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, Text: c.T("api.command.invite_people.sent")} } diff --git a/api/server.go b/api/server.go index f5a374507..2698f159c 100644 --- a/api/server.go +++ b/api/server.go @@ -37,12 +37,16 @@ const TIME_TO_WAIT_FOR_CONNECTIONS_TO_CLOSE_ON_SERVER_SHUTDOWN = time.Second var Srv *Server func NewServer() { - l4g.Info(utils.T("api.server.new_server.init.info")) Srv = &Server{} +} + +func InitStores() { Srv.Store = store.NewSqlStore() +} +func InitRouter() { Srv.Router = mux.NewRouter() Srv.Router.NotFoundHandler = http.HandlerFunc(Handle404) } diff --git a/api/status_test.go b/api/status_test.go index 2aa866a47..2cf8c624b 100644 --- a/api/status_test.go +++ b/api/status_test.go @@ -4,12 +4,13 @@ package api import ( - "github.com/mattermost/platform/model" - "github.com/mattermost/platform/store" - "github.com/mattermost/platform/utils" "strings" "testing" "time" + + "github.com/mattermost/platform/model" + "github.com/mattermost/platform/store" + "github.com/mattermost/platform/utils" ) func TestStatuses(t *testing.T) { @@ -63,7 +64,7 @@ func TestStatuses(t *testing.T) { t.Fatal(err2) } - time.Sleep(500 * time.Millisecond) + time.Sleep(1000 * time.Millisecond) WebSocketClient.GetStatuses() if resp := <-WebSocketClient.ResponseChannel; resp.Error != nil { @@ -149,7 +150,7 @@ func TestStatuses(t *testing.T) { SetStatusAwayIfNeeded(th.BasicUser.Id, false) SetStatusOnline(th.BasicUser.Id, "junk", false) - time.Sleep(300 * time.Millisecond) + time.Sleep(1500 * time.Millisecond) WebSocketClient.GetStatuses() if resp := <-WebSocketClient.ResponseChannel; resp.Error != nil { diff --git a/api/team.go b/api/team.go index 8abb66e59..eb184285d 100644 --- a/api/team.go +++ b/api/team.go @@ -180,7 +180,7 @@ func createTeamFromSignup(c *Context, w http.ResponseWriter, r *http.Request) { JoinUserToTeam(rteam, ruser) - InviteMembers(c, rteam, ruser, teamSignup.Invites) + InviteMembers(rteam, ruser.GetDisplayName(), teamSignup.Invites) teamSignup.Team = *rteam teamSignup.User = *ruser @@ -210,6 +210,10 @@ func createTeam(c *Context, w http.ResponseWriter, r *http.Request) { } } + if !isTeamCreationAllowed(c, team.Email) { + return + } + rteam := CreateTeam(c, team) if c.Err != nil { return @@ -227,16 +231,6 @@ func createTeam(c *Context, w http.ResponseWriter, r *http.Request) { } func CreateTeam(c *Context, team *model.Team) *model.Team { - - if team == nil { - c.SetInvalidParam("createTeam", "team") - return nil - } - - if !isTeamCreationAllowed(c, team.Email) { - return nil - } - if result := <-Srv.Store.Team().Save(team); result.Err != nil { c.Err = result.Err return nil @@ -522,7 +516,7 @@ func inviteMembers(c *Context, w http.ResponseWriter, r *http.Request) { emailList = append(emailList, invite["email"]) } - InviteMembers(c, team, user, emailList) + InviteMembers(team, user.GetDisplayName(), emailList) w.Write([]byte(invites.ToJson())) } @@ -708,25 +702,22 @@ func findTeamByName(c *Context, w http.ResponseWriter, r *http.Request) { } } -func InviteMembers(c *Context, team *model.Team, user *model.User, invites []string) { +func InviteMembers(team *model.Team, senderName string, invites []string) { for _, invite := range invites { if len(invite) > 0 { + senderRole := utils.T("api.team.invite_members.member") - sender := user.GetDisplayName() + subject := utils.T("api.templates.invite_subject", + map[string]interface{}{"SenderName": senderName, "TeamDisplayName": team.DisplayName, "SiteName": utils.ClientCfg["SiteName"]}) - senderRole := c.T("api.team.invite_members.member") - - subject := c.T("api.templates.invite_subject", - map[string]interface{}{"SenderName": sender, "TeamDisplayName": team.DisplayName, "SiteName": utils.ClientCfg["SiteName"]}) - - bodyPage := utils.NewHTMLTemplate("invite_body", c.Locale) - bodyPage.Props["SiteURL"] = c.GetSiteURL() - bodyPage.Props["Title"] = c.T("api.templates.invite_body.title") - bodyPage.Html["Info"] = template.HTML(c.T("api.templates.invite_body.info", - map[string]interface{}{"SenderStatus": senderRole, "SenderName": sender, "TeamDisplayName": team.DisplayName})) - bodyPage.Props["Button"] = c.T("api.templates.invite_body.button") - bodyPage.Html["ExtraInfo"] = template.HTML(c.T("api.templates.invite_body.extra_info", - map[string]interface{}{"TeamDisplayName": team.DisplayName, "TeamURL": c.GetTeamURL()})) + bodyPage := utils.NewHTMLTemplate("invite_body", model.DEFAULT_LOCALE) + bodyPage.Props["SiteURL"] = *utils.Cfg.ServiceSettings.SiteURL + bodyPage.Props["Title"] = utils.T("api.templates.invite_body.title") + bodyPage.Html["Info"] = template.HTML(utils.T("api.templates.invite_body.info", + map[string]interface{}{"SenderStatus": senderRole, "SenderName": senderName, "TeamDisplayName": team.DisplayName})) + bodyPage.Props["Button"] = utils.T("api.templates.invite_body.button") + bodyPage.Html["ExtraInfo"] = template.HTML(utils.T("api.templates.invite_body.extra_info", + map[string]interface{}{"TeamDisplayName": team.DisplayName, "TeamURL": *utils.Cfg.ServiceSettings.SiteURL + "/" + team.Name})) props := make(map[string]string) props["email"] = invite @@ -736,7 +727,7 @@ func InviteMembers(c *Context, team *model.Team, user *model.User, invites []str props["time"] = fmt.Sprintf("%v", model.GetMillis()) data := model.MapToJson(props) hash := model.HashPassword(fmt.Sprintf("%v:%v", data, utils.Cfg.EmailSettings.InviteSalt)) - bodyPage.Props["Link"] = fmt.Sprintf("%s/signup_user_complete/?d=%s&h=%s", c.GetSiteURL(), url.QueryEscape(data), url.QueryEscape(hash)) + bodyPage.Props["Link"] = fmt.Sprintf("%s/signup_user_complete/?d=%s&h=%s", *utils.Cfg.ServiceSettings.SiteURL, url.QueryEscape(data), url.QueryEscape(hash)) if !utils.Cfg.EmailSettings.SendEmailNotifications { l4g.Info(utils.T("api.team.invite_members.sending.info"), invite, bodyPage.Props["Link"]) @@ -848,11 +839,7 @@ func updateMemberRoles(c *Context, w http.ResponseWriter, r *http.Request) { w.Write([]byte(model.MapToJson(rdata))) } -func PermanentDeleteTeam(c *Context, team *model.Team) *model.AppError { - l4g.Warn(utils.T("api.team.permanent_delete_team.attempting.warn"), team.Name, team.Id) - c.Path = "/teams/permanent_delete" - c.LogAuditWithUserId("", fmt.Sprintf("attempt teamId=%v", team.Id)) - +func PermanentDeleteTeam(team *model.Team) *model.AppError { team.DeleteAt = model.GetMillis() if result := <-Srv.Store.Team().Update(team); result.Err != nil { return result.Err @@ -870,9 +857,6 @@ func PermanentDeleteTeam(c *Context, team *model.Team) *model.AppError { return result.Err } - l4g.Warn(utils.T("api.team.permanent_delete_team.deleted.warn"), team.Name, team.Id) - c.LogAuditWithUserId("", fmt.Sprintf("success teamId=%v", team.Id)) - return nil } diff --git a/api/team_test.go b/api/team_test.go index 910b58041..bb7d19e4f 100644 --- a/api/team_test.go +++ b/api/team_test.go @@ -5,11 +5,12 @@ package api import ( "fmt" + "strings" + "testing" + "github.com/mattermost/platform/model" "github.com/mattermost/platform/store" "github.com/mattermost/platform/utils" - "strings" - "testing" ) func TestSignupTeam(t *testing.T) { @@ -315,10 +316,7 @@ func TestGetAllTeamListings(t *testing.T) { } } - c := &Context{} - c.RequestId = model.NewId() - c.IpAddress = "cmd_line" - UpdateUserRoles(c, user, model.ROLE_SYSTEM_ADMIN.Id) + UpdateUserRoles(user, model.ROLE_SYSTEM_ADMIN.Id) Client.Login(user.Email, "passwd1") Client.SetTeamId(team.Id) @@ -371,7 +369,7 @@ func TestTeamPermDelete(t *testing.T) { c.RequestId = model.NewId() c.IpAddress = "test" - err := PermanentDeleteTeam(c, team) + err := PermanentDeleteTeam(team) if err != nil { t.Fatal(err) } diff --git a/api/user.go b/api/user.go index 83bdb8c68..5c92cf47a 100644 --- a/api/user.go +++ b/api/user.go @@ -1557,9 +1557,10 @@ func updateRoles(c *Context, w http.ResponseWriter, r *http.Request) { user = result.Data.(*model.User) } - UpdateUserRoles(c, user, newRoles) - if c.Err != nil { + if _, err := UpdateUserRoles(user, newRoles); err != nil { return + } else { + c.LogAuditWithUserId(user.Id, "roles="+newRoles) } rdata := map[string]string{} @@ -1567,7 +1568,7 @@ func updateRoles(c *Context, w http.ResponseWriter, r *http.Request) { w.Write([]byte(model.MapToJson(rdata))) } -func UpdateUserRoles(c *Context, user *model.User, newRoles string) *model.User { +func UpdateUserRoles(user *model.User, newRoles string) (*model.User, *model.AppError) { user.Roles = newRoles uchan := Srv.Store.User().Update(user, true) @@ -1575,10 +1576,8 @@ func UpdateUserRoles(c *Context, user *model.User, newRoles string) *model.User var ruser *model.User if result := <-uchan; result.Err != nil { - c.Err = result.Err - return nil + return nil, result.Err } else { - c.LogAuditWithUserId(user.Id, "roles="+newRoles) ruser = result.Data.([2]*model.User)[0] } @@ -1589,7 +1588,7 @@ func UpdateUserRoles(c *Context, user *model.User, newRoles string) *model.User RemoveAllSessionsForUserId(user.Id) - return ruser + return ruser, nil } func updateActive(c *Context, w http.ResponseWriter, r *http.Request) { @@ -1664,11 +1663,8 @@ func UpdateActive(user *model.User, active bool) (*model.User, *model.AppError) } } -func PermanentDeleteUser(c *Context, user *model.User) *model.AppError { +func PermanentDeleteUser(user *model.User) *model.AppError { l4g.Warn(utils.T("api.user.permanent_delete_user.attempting.warn"), user.Email, user.Id) - c.Path = "/users/permanent_delete" - c.LogAuditWithUserId(user.Id, fmt.Sprintf("attempt userId=%v", user.Id)) - c.LogAuditWithUserId("", fmt.Sprintf("attempt userId=%v", user.Id)) if user.IsInRole(model.ROLE_SYSTEM_ADMIN.Id) { l4g.Warn(utils.T("api.user.permanent_delete_user.system_admin.warn"), user.Email) } @@ -1726,18 +1722,17 @@ func PermanentDeleteUser(c *Context, user *model.User) *model.AppError { } l4g.Warn(utils.T("api.user.permanent_delete_user.deleted.warn"), user.Email, user.Id) - c.LogAuditWithUserId("", fmt.Sprintf("success userId=%v", user.Id)) return nil } -func PermanentDeleteAllUsers(c *Context) *model.AppError { +func PermanentDeleteAllUsers() *model.AppError { if result := <-Srv.Store.User().GetAll(); result.Err != nil { return result.Err } else { users := result.Data.([]*model.User) for _, user := range users { - PermanentDeleteUser(c, user) + PermanentDeleteUser(user) } } diff --git a/api/user_test.go b/api/user_test.go index 13ae45f6e..85af7e598 100644 --- a/api/user_test.go +++ b/api/user_test.go @@ -523,10 +523,7 @@ func TestGetUser(t *testing.T) { t.Fatal("shouldn't have accss") } - c := &Context{} - c.RequestId = model.NewId() - c.IpAddress = "cmd_line" - UpdateUserRoles(c, ruser.Data.(*model.User), model.ROLE_SYSTEM_ADMIN.Id) + UpdateUserRoles(ruser.Data.(*model.User), model.ROLE_SYSTEM_ADMIN.Id) Client.Login(user.Email, "passwd1") @@ -1236,7 +1233,7 @@ func TestUserPermDelete(t *testing.T) { c.RequestId = model.NewId() c.IpAddress = "test" - err := PermanentDeleteUser(c, user1) + err := PermanentDeleteUser(user1) if err != nil { t.Fatal(err) } diff --git a/api/web_hub.go b/api/web_hub.go index 4136eaf7c..cad77395b 100644 --- a/api/web_hub.go +++ b/api/web_hub.go @@ -119,7 +119,9 @@ func InvalidateCacheForUserSkipClusterSend(userId string) { Srv.Store.Channel().InvalidateAllChannelMembersForUser(userId) Srv.Store.User().InvalidateProfilesInChannelCacheByUser(userId) - GetHubForUserId(userId).InvalidateUser(userId) + if len(hubs) != 0 { + GetHubForUserId(userId).InvalidateUser(userId) + } } func (h *Hub) Register(webConn *WebConn) { -- cgit v1.2.3-1-g7c22