[MM-27376] api4/channel: add move channel to local mode (#15200)

* api4/channel: add move channel to local mode

* app/channel: reflect review comments
Этот коммит содержится в:
Ibrahim Serdar Acikgoz
2020-08-10 18:44:13 +03:00
коммит произвёл GitHub
родитель d906be1d6c
Коммит 3f19a8c011
3 изменённых файлов: 91 добавлений и 13 удалений

Просмотреть файл

@@ -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()))
}

Просмотреть файл

@@ -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) {

Просмотреть файл

@@ -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
}
}