[MM-36786] Check channel permissions before thread follow/unfollow (#18460)

Этот коммит содержится в:
Ashish Bhate
2021-09-29 19:30:23 +05:30
коммит произвёл GitHub
родитель 8a2832223c
Коммит a519b86e8f
2 изменённых файлов: 35 добавлений и 0 удалений

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

@@ -3059,6 +3059,11 @@ func unfollowThreadByUser(c *Context, w http.ResponseWriter, r *http.Request) {
return
}
if !c.App.SessionHasPermissionToChannelByPost(*c.AppContext.Session(), c.Params.ThreadId, model.PermissionReadChannel) {
c.SetPermissionError(model.PermissionReadChannel)
return
}
err := c.App.UpdateThreadFollowForUser(c.Params.UserId, c.Params.TeamId, c.Params.ThreadId, false)
if err != nil {
c.Err = err
@@ -3087,6 +3092,11 @@ func followThreadByUser(c *Context, w http.ResponseWriter, r *http.Request) {
return
}
if !c.App.SessionHasPermissionToChannelByPost(*c.AppContext.Session(), c.Params.ThreadId, model.PermissionReadChannel) {
c.SetPermissionError(model.PermissionReadChannel)
return
}
err := c.App.UpdateThreadFollowForUser(c.Params.UserId, c.Params.TeamId, c.Params.ThreadId, true)
if err != nil {
c.Err = err

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

@@ -5870,6 +5870,31 @@ func TestFollowThreads(t *testing.T) {
require.GreaterOrEqual(t, uss.Threads[0].LastViewedAt, uss.Threads[0].LastReplyAt)
})
t.Run("No permission to channel", func(t *testing.T) {
// Add user1 to private channel
_, appErr := th.App.AddUserToChannel(th.BasicUser, th.BasicPrivateChannel2, false)
require.Nil(t, appErr)
defer th.App.RemoveUserFromChannel(th.Context, th.BasicUser.Id, "", th.BasicPrivateChannel2)
// create thread in private channel
rpost, resp, err := th.Client.CreatePost(&model.Post{ChannelId: th.BasicPrivateChannel2.Id, Message: "root post"})
require.NoError(t, err)
CheckCreatedStatus(t, resp)
_, resp, err = th.Client.CreatePost(&model.Post{ChannelId: th.BasicPrivateChannel2.Id, Message: "testReply", RootId: rpost.Id})
require.NoError(t, err)
CheckCreatedStatus(t, resp)
// Try to follow thread as other user who is not in the private channel
resp, err = th.Client.UpdateThreadFollowForUser(th.BasicUser2.Id, th.BasicTeam.Id, rpost.Id, true)
require.Error(t, err)
CheckForbiddenStatus(t, resp)
// Try to unfollow thread as other user who is not in the private channel
resp, err = th.Client.UpdateThreadFollowForUser(th.BasicUser2.Id, th.BasicTeam.Id, rpost.Id, false)
require.Error(t, err)
CheckForbiddenStatus(t, resp)
})
}
func checkThreadListReplies(t *testing.T, th *TestHelper, client *model.Client4, userId string, expectedReplies, expectedThreads int, options *model.GetUserThreadsOpts) (*model.Threads, *model.Response) {