[MM-56174] Account for archived channels in channel member for post permission check (#25837)

* [MM-56174] Account for archived channels in channel member for post permission check

* Add tests
Этот коммит содержится в:
Devin Binnie
2024-01-10 15:50:00 -05:00
коммит произвёл GitHub
родитель 1d108f0d9f
Коммит 43cca04f04
9 изменённых файлов: 129 добавлений и 21 удалений

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

@@ -172,7 +172,7 @@ func (a *App) SessionHasPermissionToGroup(session model.Session, groupID string,
}
func (a *App) SessionHasPermissionToChannelByPost(session model.Session, postID string, permission *model.Permission) bool {
if channelMember, err := a.Srv().Store().Channel().GetMemberForPost(postID, session.UserId); err == nil {
if channelMember, err := a.Srv().Store().Channel().GetMemberForPost(postID, session.UserId, *a.Config().TeamSettings.ExperimentalViewArchivedChannels); err == nil {
if a.RolesGrantPermission(channelMember.GetRoles(), permission.Id) {
return true
}
@@ -278,7 +278,7 @@ func (a *App) HasPermissionToChannel(c request.CTX, askingUserId string, channel
}
func (a *App) HasPermissionToChannelByPost(c request.CTX, askingUserId string, postID string, permission *model.Permission) bool {
if channelMember, err := a.Srv().Store().Channel().GetMemberForPost(postID, askingUserId); err == nil {
if channelMember, err := a.Srv().Store().Channel().GetMemberForPost(postID, askingUserId, *a.Config().TeamSettings.ExperimentalViewArchivedChannels); err == nil {
if a.RolesGrantPermission(channelMember.GetRoles(), permission.Id) {
return true
}

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

@@ -667,3 +667,107 @@ func TestHasPermissionToReadChannel(t *testing.T) {
})
}
}
func TestSessionHasPermissionToChannelByPost(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
session, err := th.App.CreateSession(th.Context, &model.Session{
UserId: th.BasicUser.Id,
Roles: model.SystemUserRoleId,
})
require.Nil(t, err)
session2, err := th.App.CreateSession(th.Context, &model.Session{
UserId: th.BasicUser2.Id,
Roles: model.SystemUserRoleId,
})
require.Nil(t, err)
channel := th.CreateChannel(th.Context, th.BasicTeam)
th.App.AddUserToChannel(th.Context, th.BasicUser, channel, false)
post := th.CreatePost(channel)
archivedChannel := th.CreateChannel(th.Context, th.BasicTeam)
archivedPost := th.CreatePost(archivedChannel)
th.App.DeleteChannel(th.Context, archivedChannel, th.SystemAdminUser.Id)
t.Run("read channel", func(t *testing.T) {
require.Equal(t, true, th.App.SessionHasPermissionToChannelByPost(*session, post.Id, model.PermissionReadChannel))
require.Equal(t, false, th.App.SessionHasPermissionToChannelByPost(*session2, post.Id, model.PermissionReadChannel))
})
t.Run("read archived channel - setting off", func(t *testing.T) {
th.App.UpdateConfig(func(cfg *model.Config) {
cfg.TeamSettings.ExperimentalViewArchivedChannels = model.NewBool(false)
})
require.Equal(t, false, th.App.SessionHasPermissionToChannelByPost(*session, archivedPost.Id, model.PermissionReadChannel))
require.Equal(t, false, th.App.SessionHasPermissionToChannelByPost(*session2, archivedPost.Id, model.PermissionReadChannel))
})
t.Run("read archived channel - setting on", func(t *testing.T) {
th.App.UpdateConfig(func(cfg *model.Config) {
cfg.TeamSettings.ExperimentalViewArchivedChannels = model.NewBool(true)
})
require.Equal(t, true, th.App.SessionHasPermissionToChannelByPost(*session, archivedPost.Id, model.PermissionReadChannel))
require.Equal(t, false, th.App.SessionHasPermissionToChannelByPost(*session2, archivedPost.Id, model.PermissionReadChannel))
})
t.Run("read public channel", func(t *testing.T) {
require.Equal(t, true, th.App.SessionHasPermissionToChannelByPost(*session, post.Id, model.PermissionReadPublicChannel))
require.Equal(t, true, th.App.SessionHasPermissionToChannelByPost(*session2, post.Id, model.PermissionReadPublicChannel))
})
t.Run("read channel - user is admin", func(t *testing.T) {
adminSession, err := th.App.CreateSession(th.Context, &model.Session{
UserId: th.SystemAdminUser.Id,
Roles: model.SystemAdminRoleId,
})
require.Nil(t, err)
require.Equal(t, true, th.App.SessionHasPermissionToChannelByPost(*adminSession, post.Id, model.PermissionReadChannel))
})
}
func TestHasPermissionToChannelByPost(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
channel := th.CreateChannel(th.Context, th.BasicTeam)
th.App.AddUserToChannel(th.Context, th.BasicUser, channel, false)
post := th.CreatePost(channel)
archivedChannel := th.CreateChannel(th.Context, th.BasicTeam)
archivedPost := th.CreatePost(archivedChannel)
th.App.DeleteChannel(th.Context, archivedChannel, th.SystemAdminUser.Id)
t.Run("read channel", func(t *testing.T) {
require.Equal(t, true, th.App.HasPermissionToChannelByPost(th.Context, th.BasicUser.Id, post.Id, model.PermissionReadChannel))
require.Equal(t, false, th.App.HasPermissionToChannelByPost(th.Context, th.BasicUser2.Id, post.Id, model.PermissionReadChannel))
})
t.Run("read archived channel - setting off", func(t *testing.T) {
th.App.UpdateConfig(func(cfg *model.Config) {
cfg.TeamSettings.ExperimentalViewArchivedChannels = model.NewBool(false)
})
require.Equal(t, false, th.App.HasPermissionToChannelByPost(th.Context, th.BasicUser.Id, archivedPost.Id, model.PermissionReadChannel))
require.Equal(t, false, th.App.HasPermissionToChannelByPost(th.Context, th.BasicUser2.Id, archivedPost.Id, model.PermissionReadChannel))
})
t.Run("read archived channel - setting on", func(t *testing.T) {
th.App.UpdateConfig(func(cfg *model.Config) {
cfg.TeamSettings.ExperimentalViewArchivedChannels = model.NewBool(true)
})
require.Equal(t, true, th.App.HasPermissionToChannelByPost(th.Context, th.BasicUser.Id, archivedPost.Id, model.PermissionReadChannel))
require.Equal(t, false, th.App.HasPermissionToChannelByPost(th.Context, th.BasicUser2.Id, archivedPost.Id, model.PermissionReadChannel))
})
t.Run("read public channel", func(t *testing.T) {
require.Equal(t, true, th.App.HasPermissionToChannelByPost(th.Context, th.BasicUser.Id, post.Id, model.PermissionReadPublicChannel))
require.Equal(t, true, th.App.HasPermissionToChannelByPost(th.Context, th.BasicUser2.Id, post.Id, model.PermissionReadPublicChannel))
})
t.Run("read channel - user is admin", func(t *testing.T) {
require.Equal(t, true, th.App.HasPermissionToChannelByPost(th.Context, th.SystemAdminUser.Id, post.Id, model.PermissionReadChannel))
})
}