[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
Этот коммит содержится в:
Ashish Bhate
2020-08-14 08:42:39 +00:00
коммит произвёл GitHub
родитель 11513a8d0d
Коммит d76039db23
7 изменённых файлов: 113 добавлений и 2 удалений

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

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

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

@@ -2345,10 +2345,18 @@ func (a *App) PermanentDeleteChannel(channel *model.Channel) *model.AppError {
return model.NewAppError("PermanentDeleteChannel", "app.webhooks.permanent_delete_outgoing_by_channel.app_error", nil, err.Error(), http.StatusInternalServerError)
}
deleteAt := model.GetMillis()
if nErr := a.Srv().Store.Channel().PermanentDelete(channel.Id); nErr != nil {
return model.NewAppError("PermanentDeleteChannel", "app.channel.permanent_delete.app_error", nil, nErr.Error(), http.StatusInternalServerError)
}
a.invalidateCacheForChannel(channel)
message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_CHANNEL_DELETED, channel.TeamId, "", "", nil)
message.Add("channel_id", channel.Id)
message.Add("delete_at", deleteAt)
a.Publish(message)
return nil
}

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

@@ -2754,6 +2754,10 @@
"id": "api.user.create_user.signup_link_invalid.app_error",
"translation": "The signup link does not appear to be valid."
},
{
"id": "api.user.delete_channel.not_enabled.app_error",
"translation": "Permanent channel deletion feature is not enabled. Please contact your System Administrator."
},
{
"id": "api.user.delete_team.not_enabled.app_error",
"translation": "Permanent team deletion feature is not enabled. Please contact your System Administrator."

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

@@ -2562,6 +2562,16 @@ func (c *Client4) DeleteChannel(channelId string) (bool, *Response) {
return CheckStatusOK(r), BuildResponse(r)
}
// PermanentDeleteChannel deletes a channel based on the provided channel id string.
func (c *Client4) PermanentDeleteChannel(channelId string) (bool, *Response) {
r, err := c.DoApiDelete(c.GetChannelRoute(channelId) + "?permanent=" + c.boolString(true))
if err != nil {
return false, BuildErrorResponse(r, err)
}
defer closeBody(r)
return CheckStatusOK(r), BuildResponse(r)
}
// MoveChannel moves the channel to the destination team.
func (c *Client4) MoveChannel(channelId, teamId string, force bool) (*Channel, *Response) {
requestBody := map[string]interface{}{

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

@@ -329,6 +329,7 @@ type ServiceSettings struct {
DEPRECATED_DO_NOT_USE_ImageProxyOptions *string `json:"ImageProxyOptions" mapstructure:"ImageProxyOptions"` // This field is deprecated and must not be used.
EnableAPITeamDeletion *bool
EnableAPIUserDeletion *bool
EnableAPIChannelDeletion *bool
ExperimentalEnableHardenedMode *bool
DisableLegacyMFA *bool `restricted:"true"`
ExperimentalStrictCSRFEnforcement *bool `restricted:"true"`
@@ -705,6 +706,10 @@ func (s *ServiceSettings) SetDefaults(isUpdate bool) {
s.EnableAPIUserDeletion = NewBool(false)
}
if s.EnableAPIChannelDeletion == nil {
s.EnableAPIChannelDeletion = NewBool(false)
}
if s.ExperimentalEnableHardenedMode == nil {
s.ExperimentalEnableHardenedMode = NewBool(false)
}