[MM-25659] ability to permanent delete channel through client (#15202)
Summary
Ability to permanent delete channel through client
Ticket Link
https://mattermost.atlassian.net/browse/MM-25659
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
11513a8d0d
Коммит
d76039db23
@@ -1074,7 +1074,15 @@ func deleteChannel(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
err = c.App.DeleteChannel(channel, c.App.Session().UserId)
|
||||
if c.Params.Permanent {
|
||||
if *c.App.Config().ServiceSettings.EnableAPIChannelDeletion {
|
||||
err = c.App.PermanentDeleteChannel(channel)
|
||||
} else {
|
||||
err = model.NewAppError("deleteChannel", "api.user.delete_channel.not_enabled.app_error", nil, "channelId="+c.Params.ChannelId, http.StatusUnauthorized)
|
||||
}
|
||||
} else {
|
||||
err = c.App.DeleteChannel(channel, c.App.Session().UserId)
|
||||
}
|
||||
if err != nil {
|
||||
c.Err = err
|
||||
return
|
||||
|
||||
@@ -15,7 +15,7 @@ func (api *API) InitChannelLocal() {
|
||||
api.BaseRoutes.Channels.Handle("", api.ApiLocal(localCreateChannel)).Methods("POST")
|
||||
api.BaseRoutes.Channel.Handle("", api.ApiLocal(getChannel)).Methods("GET")
|
||||
api.BaseRoutes.ChannelByName.Handle("", api.ApiLocal(getChannelByName)).Methods("GET")
|
||||
api.BaseRoutes.Channel.Handle("", api.ApiLocal(deleteChannel)).Methods("DELETE")
|
||||
api.BaseRoutes.Channel.Handle("", api.ApiLocal(localDeleteChannel)).Methods("DELETE")
|
||||
api.BaseRoutes.Channel.Handle("/patch", api.ApiLocal(localPatchChannel)).Methods("PUT")
|
||||
api.BaseRoutes.Channel.Handle("/move", api.ApiLocal(localMoveChannel)).Methods("POST")
|
||||
|
||||
@@ -277,3 +277,40 @@ func localMoveChannel(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
w.Write([]byte(channel.ToJson()))
|
||||
}
|
||||
|
||||
func localDeleteChannel(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
|
||||
}
|
||||
|
||||
auditRec := c.MakeAuditRecord("localDeleteChannel", audit.Fail)
|
||||
defer c.LogAuditRec(auditRec)
|
||||
auditRec.AddMeta("channeld", channel)
|
||||
|
||||
if channel.Type == model.CHANNEL_DIRECT || channel.Type == model.CHANNEL_GROUP {
|
||||
c.Err = model.NewAppError("localDeleteChannel", "api.channel.delete_channel.type.invalid", nil, "", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if c.Params.Permanent {
|
||||
err = c.App.PermanentDeleteChannel(channel)
|
||||
} else {
|
||||
err = c.App.DeleteChannel(channel, "")
|
||||
}
|
||||
if err != nil {
|
||||
c.Err = err
|
||||
return
|
||||
}
|
||||
|
||||
auditRec.Success()
|
||||
c.LogAudit("name=" + channel.Name)
|
||||
|
||||
ReturnStatusOK(w)
|
||||
}
|
||||
|
||||
@@ -1629,6 +1629,45 @@ func TestDeleteChannel2(t *testing.T) {
|
||||
CheckForbiddenStatus(t, resp)
|
||||
}
|
||||
|
||||
func TestPermanentDeleteChannel(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
enableAPIChannelDeletion := *th.App.Config().ServiceSettings.EnableAPIChannelDeletion
|
||||
defer func() {
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableAPIChannelDeletion = &enableAPIChannelDeletion })
|
||||
}()
|
||||
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableAPIChannelDeletion = false })
|
||||
|
||||
publicChannel1 := th.CreatePublicChannel()
|
||||
t.Run("Permanent deletion not available through API if EnableAPIChannelDeletion is not set", func(t *testing.T) {
|
||||
_, resp := th.SystemAdminClient.PermanentDeleteChannel(publicChannel1.Id)
|
||||
CheckUnauthorizedStatus(t, resp)
|
||||
})
|
||||
|
||||
t.Run("Permanent deletion available through local mode even if EnableAPIChannelDeletion is not set", func(t *testing.T) {
|
||||
ok, resp := th.LocalClient.PermanentDeleteChannel(publicChannel1.Id)
|
||||
CheckNoError(t, resp)
|
||||
assert.True(t, ok)
|
||||
})
|
||||
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableAPIChannelDeletion = true })
|
||||
th.TestForSystemAdminAndLocal(t, func(t *testing.T, c *model.Client4) {
|
||||
publicChannel := th.CreatePublicChannel()
|
||||
ok, resp := c.PermanentDeleteChannel(publicChannel.Id)
|
||||
CheckNoError(t, resp)
|
||||
assert.True(t, ok)
|
||||
|
||||
_, err := th.App.GetChannel(publicChannel.Id)
|
||||
assert.NotNil(t, err)
|
||||
|
||||
ok, resp = c.PermanentDeleteChannel("junk")
|
||||
CheckBadRequestStatus(t, resp)
|
||||
require.False(t, ok, "should have returned false")
|
||||
}, "Permanent deletion with EnableAPIChannelDeletion set")
|
||||
}
|
||||
|
||||
func TestConvertChannelToPrivate(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
Ссылка в новой задаче
Block a user