MM-11172: Don't allow reacting in read-only town square. (#9106)

Этот коммит содержится в:
George Goldberg
2018-07-16 13:04:52 +01:00
коммит произвёл GitHub
родитель 10f571784b
Коммит 9d9fcd9ac5
3 изменённых файлов: 104 добавлений и 0 удалений

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

@@ -172,6 +172,33 @@ func TestSaveReaction(t *testing.T) {
}
th.AddPermissionToRole(model.PERMISSION_ADD_REACTION.Id, model.CHANNEL_USER_ROLE_ID)
})
t.Run("unable-to-react-in-read-only-town-square", func(t *testing.T) {
th.LoginBasic()
channel, err := th.App.GetChannelByName("town-square", th.BasicTeam.Id)
assert.Nil(t, err)
post := th.CreatePostWithClient(th.Client, channel)
th.App.SetLicense(model.NewTestLicense())
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.ExperimentalTownSquareIsReadOnly = true })
reaction := &model.Reaction{
UserId: userId,
PostId: post.Id,
EmojiName: "smile",
}
_, resp := Client.SaveReaction(reaction)
CheckForbiddenStatus(t, resp)
if reactions, err := th.App.GetReactionsForPost(postId); err != nil || len(reactions) != 3 {
t.Fatal("should have not created a reactions")
}
th.App.RemoveLicense()
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.ExperimentalTownSquareIsReadOnly = false })
})
}
func TestGetReactions(t *testing.T) {
@@ -451,4 +478,39 @@ func TestDeleteReaction(t *testing.T) {
}
th.AddPermissionToRole(model.PERMISSION_REMOVE_OTHERS_REACTIONS.Id, model.SYSTEM_ADMIN_ROLE_ID)
})
t.Run("unable-to-delete-reactions-in-read-only-town-square", func(t *testing.T) {
th.LoginBasic()
channel, err := th.App.GetChannelByName("town-square", th.BasicTeam.Id)
assert.Nil(t, err)
post := th.CreatePostWithClient(th.Client, channel)
th.App.SetLicense(model.NewTestLicense())
reaction := &model.Reaction{
UserId: userId,
PostId: post.Id,
EmojiName: "smile",
}
r1, resp := Client.SaveReaction(reaction)
CheckNoError(t, resp)
if reactions, err := th.App.GetReactionsForPost(postId); err != nil || len(reactions) != 1 {
t.Fatal("should have created a reactions")
}
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.ExperimentalTownSquareIsReadOnly = true })
_, resp = th.SystemAdminClient.DeleteReaction(r1)
CheckForbiddenStatus(t, resp)
if reactions, err := th.App.GetReactionsForPost(postId); err != nil || len(reactions) != 1 {
t.Fatal("should have not deleted a reactions")
}
th.App.RemoveLicense()
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.ExperimentalTownSquareIsReadOnly = false })
})
}

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

@@ -4,6 +4,8 @@
package app
import (
"net/http"
"github.com/mattermost/mattermost-server/model"
)
@@ -13,6 +15,24 @@ func (a *App) SaveReactionForPost(reaction *model.Reaction) (*model.Reaction, *m
return nil, err
}
if a.License() != nil && *a.Config().TeamSettings.ExperimentalTownSquareIsReadOnly {
var channel *model.Channel
if channel, err = a.GetChannel(post.ChannelId); err != nil {
return nil, err
}
if channel.Name == model.DEFAULT_CHANNEL {
var user *model.User
if user, err = a.GetUser(reaction.UserId); err != nil {
return nil, err
}
if !a.RolesGrantPermission(user.GetRoles(), model.PERMISSION_MANAGE_SYSTEM.Id) {
return nil, model.NewAppError("saveReactionForPost", "api.reaction.town_square_read_only", nil, "", http.StatusForbidden)
}
}
}
if result := <-a.Srv.Store.Reaction().Save(reaction); result.Err != nil {
return nil, result.Err
} else {
@@ -40,6 +60,24 @@ func (a *App) DeleteReactionForPost(reaction *model.Reaction) *model.AppError {
return err
}
if a.License() != nil && *a.Config().TeamSettings.ExperimentalTownSquareIsReadOnly {
var channel *model.Channel
if channel, err = a.GetChannel(post.ChannelId); err != nil {
return err
}
if channel.Name == model.DEFAULT_CHANNEL {
var user *model.User
if user, err = a.GetUser(reaction.UserId); err != nil {
return err
}
if !a.RolesGrantPermission(user.GetRoles(), model.PERMISSION_MANAGE_SYSTEM.Id) {
return model.NewAppError("deleteReactionForPost", "api.reaction.town_square_read_only", nil, "", http.StatusForbidden)
}
}
}
hasReactions := true
if reactions, _ := a.GetReactionsForPost(post.Id); len(reactions) <= 1 {
hasReactions = false

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

@@ -159,6 +159,10 @@
"id": "api.channel.delete_channel.deleted.app_error",
"translation": "The channel has been archived or deleted"
},
{
"id": "api.reaction.town_square_read_only",
"translation": "Reacting to posts is not possible in read-only channels."
},
{
"id": "api.channel.delete_channel.type.invalid",
"translation": "Cannot delete direct or group message channels"