From 3f19a8c011298f3cdc5c31391c40d827055ced2c Mon Sep 17 00:00:00 2001 From: Ibrahim Serdar Acikgoz Date: Mon, 10 Aug 2020 18:44:13 +0300 Subject: [PATCH] [MM-27376] api4/channel: add move channel to local mode (#15200) * api4/channel: add move channel to local mode * app/channel: reflect review comments --- api4/channel_local.go | 71 +++++++++++++++++++++++++++++++++++++++++++ api4/channel_test.go | 20 ++++++------ app/channel.go | 13 ++++++-- 3 files changed, 91 insertions(+), 13 deletions(-) diff --git a/api4/channel_local.go b/api4/channel_local.go index d35b796b97..56fc7a9308 100644 --- a/api4/channel_local.go +++ b/api4/channel_local.go @@ -17,6 +17,7 @@ func (api *API) InitChannelLocal() { api.BaseRoutes.ChannelByName.Handle("", api.ApiLocal(getChannelByName)).Methods("GET") api.BaseRoutes.Channel.Handle("", api.ApiLocal(deleteChannel)).Methods("DELETE") api.BaseRoutes.Channel.Handle("/patch", api.ApiLocal(localPatchChannel)).Methods("PUT") + api.BaseRoutes.Channel.Handle("/move", api.ApiLocal(localMoveChannel)).Methods("POST") api.BaseRoutes.ChannelMember.Handle("", api.ApiLocal(localRemoveChannelMember)).Methods("DELETE") api.BaseRoutes.ChannelMember.Handle("", api.ApiLocal(getChannelMember)).Methods("GET") @@ -206,3 +207,73 @@ func localPatchChannel(c *Context, w http.ResponseWriter, r *http.Request) { w.Write([]byte(rchannel.ToJson())) } + +func localMoveChannel(c *Context, w http.ResponseWriter, r *http.Request) { + c.RequireChannelId() + if c.Err != nil { + return + } + + channel, err := c.App.GetChannel(c.Params.ChannelId) + if err != nil { + c.Err = err + return + } + + props := model.StringInterfaceFromJson(r.Body) + teamId, ok := props["team_id"].(string) + if !ok { + c.SetInvalidParam("team_id") + return + } + + force, ok := props["force"].(bool) + if !ok { + c.SetInvalidParam("force") + return + } + + team, err := c.App.GetTeam(teamId) + if err != nil { + c.Err = err + return + } + + auditRec := c.MakeAuditRecord("localMoveChannel", audit.Fail) + defer c.LogAuditRec(auditRec) + auditRec.AddMeta("channel_id", channel.Id) + auditRec.AddMeta("channel_name", channel.Name) + auditRec.AddMeta("team_id", team.Id) + auditRec.AddMeta("team_name", team.Name) + + if channel.Type == model.CHANNEL_DIRECT || channel.Type == model.CHANNEL_GROUP || channel.Type == model.CHANNEL_PRIVATE { + c.Err = model.NewAppError("moveChannel", "api.channel.move_channel.type.invalid", nil, "", http.StatusForbidden) + return + } + + err = c.App.RemoveAllDeactivatedMembersFromChannel(channel) + if err != nil { + c.Err = err + return + } + + if force { + err = c.App.RemoveUsersFromChannelNotMemberOfTeam(nil, channel, team) + if err != nil { + c.Err = err + return + } + } + + err = c.App.MoveChannel(team, channel, nil) + if err != nil { + c.Err = err + return + } + + auditRec.Success() + c.LogAudit("channel=" + channel.Name) + c.LogAudit("team=" + team.Name) + + w.Write([]byte(channel.ToJson())) +} diff --git a/api4/channel_test.go b/api4/channel_test.go index c07ae812b7..c776fb1418 100644 --- a/api4/channel_test.go +++ b/api4/channel_test.go @@ -3932,35 +3932,35 @@ func TestMoveChannel(t *testing.T) { CheckErrorMessage(t, resp, "api.context.permissions.app_error") }) - t.Run("Should fail to move channel due to a member not member of target team", func(t *testing.T) { + th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) { publicChannel := th.CreatePublicChannel() user := th.BasicUser - _, resp := th.SystemAdminClient.RemoveTeamMember(team2.Id, user.Id) + _, resp := client.RemoveTeamMember(team2.Id, user.Id) CheckNoError(t, resp) - _, resp = th.SystemAdminClient.AddChannelMember(publicChannel.Id, user.Id) + _, resp = client.AddChannelMember(publicChannel.Id, user.Id) CheckNoError(t, resp) - _, resp = th.SystemAdminClient.MoveChannel(publicChannel.Id, team2.Id, false) + _, resp = client.MoveChannel(publicChannel.Id, team2.Id, false) require.NotNil(t, resp.Error) CheckErrorMessage(t, resp, "app.channel.move_channel.members_do_not_match.error") - }) + }, "Should fail to move channel due to a member not member of target team") - t.Run("Should be able to (force) move channel by a member that is not member of target team", func(t *testing.T) { + th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) { publicChannel := th.CreatePublicChannel() user := th.BasicUser - _, resp := th.SystemAdminClient.RemoveTeamMember(team2.Id, user.Id) + _, resp := client.RemoveTeamMember(team2.Id, user.Id) CheckNoError(t, resp) - _, resp = th.SystemAdminClient.AddChannelMember(publicChannel.Id, user.Id) + _, resp = client.AddChannelMember(publicChannel.Id, user.Id) CheckNoError(t, resp) - newChannel, resp := th.SystemAdminClient.MoveChannel(publicChannel.Id, team2.Id, true) + newChannel, resp := client.MoveChannel(publicChannel.Id, team2.Id, true) require.Nil(t, resp.Error) require.Equal(t, team2.Id, newChannel.TeamId) - }) + }, "Should be able to (force) move channel by a member that is not member of target team") } func TestUpdateCategoryForTeamForUser(t *testing.T) { diff --git a/app/channel.go b/app/channel.go index 14e19ab1dc..55ad278f9f 100644 --- a/app/channel.go +++ b/app/channel.go @@ -2444,8 +2444,10 @@ func (a *App) MoveChannel(team *model.Team, channel *model.Channel, user *model. mlog.Warn("error while removing non-team member users", mlog.Err(err)) } - if err := a.postChannelMoveMessage(user, channel, previousTeam); err != nil { - mlog.Warn("error while posting move channel message", mlog.Err(err)) + if user != nil { + if err := a.postChannelMoveMessage(user, channel, previousTeam); err != nil { + mlog.Warn("error while posting move channel message", mlog.Err(err)) + } } return nil @@ -2493,8 +2495,13 @@ func (a *App) RemoveUsersFromChannelNotMemberOfTeam(remover *model.User, channel for _, teamMember := range teamMembers { delete(channelMemberMap, teamMember.UserId) } + + var removerId string + if remover != nil { + removerId = remover.Id + } for userId := range channelMemberMap { - if err := a.removeUserFromChannel(userId, remover.Id, channel); err != nil { + if err := a.removeUserFromChannel(userId, removerId, channel); err != nil { return err } }