MM-14897: Changes to be able to add and remove groups from channels. (#10794)

* MM-15162: Changes for LDAP groups removals phase.

* MM-14897: Changes to be able to add and remove groups from channels.

* Update model/client4.go

* MM-14897: PR-requested change to string interpolation.
Этот коммит содержится в:
Martin Kraft
2019-05-15 12:03:47 -04:00
коммит произвёл GitHub
родитель dd33bc13a7
Коммит 1b78f9debc
18 изменённых файлов: 475 добавлений и 143 удалений

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

@@ -483,18 +483,44 @@ func getGroupsByChannel(c *Context, w http.ResponseWriter, r *http.Request) {
return
}
if !c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_MANAGE_SYSTEM) {
c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
var permission *model.Permission
channel, err := c.App.GetChannel(c.Params.ChannelId)
if err != nil {
c.Err = err
return
}
if channel.Type == model.CHANNEL_PRIVATE {
permission = model.PERMISSION_MANAGE_PRIVATE_CHANNEL_MEMBERS
} else {
permission = model.PERMISSION_MANAGE_PUBLIC_CHANNEL_MEMBERS
}
if !c.App.SessionHasPermissionToChannel(c.App.Session, c.Params.ChannelId, permission) {
c.SetPermissionError(permission)
return
}
groups, err := c.App.GetGroupsByChannel(c.Params.ChannelId, c.Params.Page, c.Params.PerPage)
opts := model.GroupSearchOpts{
Q: c.Params.Q,
IncludeMemberCount: c.Params.IncludeMemberCount,
}
if c.Params.Paginate == nil || *c.Params.Paginate {
opts.PageOpts = &model.PageOpts{Page: c.Params.Page, PerPage: c.Params.PerPage}
}
groups, totalCount, err := c.App.GetGroupsByChannel(c.Params.ChannelId, opts)
if err != nil {
c.Err = err
return
}
b, marshalErr := json.Marshal(groups)
b, marshalErr := json.Marshal(struct {
Groups []*model.Group `json:"groups"`
Count int `json:"total_group_count"`
}{
Groups: groups,
Count: totalCount,
})
if marshalErr != nil {
c.Err = model.NewAppError("Api4.getGroupsByChannel", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
return
@@ -569,6 +595,26 @@ func getGroups(c *Context, w http.ResponseWriter, r *http.Request) {
opts.NotAssociatedToTeam = teamID
}
channelID := c.Params.NotAssociatedToChannel
if len(channelID) == 26 {
channel, err := c.App.GetChannel(channelID)
if err != nil {
c.Err = err
return
}
var permission *model.Permission
if channel.Type == model.CHANNEL_PRIVATE {
permission = model.PERMISSION_MANAGE_PRIVATE_CHANNEL_MEMBERS
} else {
permission = model.PERMISSION_MANAGE_PUBLIC_CHANNEL_MEMBERS
}
if !c.App.SessionHasPermissionToChannel(c.App.Session, channelID, permission) {
c.SetPermissionError(permission)
return
}
opts.NotAssociatedToChannel = channelID
}
groups, err := c.App.GetGroups(c.Params.Page, c.Params.PerPage, opts)
if err != nil {
c.Err = err

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

@@ -8,9 +8,10 @@ import (
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/assert"
"github.com/mattermost/mattermost-server/model"
)
@@ -654,25 +655,34 @@ func TestGetGroupsByChannel(t *testing.T) {
})
assert.Nil(t, err)
_, response := th.SystemAdminClient.GetGroupsByChannel("asdfasdf", 0, 60)
opts := model.GroupSearchOpts{
PageOpts: &model.PageOpts{
Page: 0,
PerPage: 60,
},
}
_, _, response := th.SystemAdminClient.GetGroupsByChannel("asdfasdf", opts)
CheckBadRequestStatus(t, response)
th.App.SetLicense(nil)
_, response = th.SystemAdminClient.GetGroupsByChannel(th.BasicChannel.Id, 0, 60)
_, _, response = th.SystemAdminClient.GetGroupsByChannel(th.BasicChannel.Id, opts)
CheckNotImplementedStatus(t, response)
th.App.SetLicense(model.NewTestLicense("ldap"))
_, response = th.Client.GetGroupsByChannel(th.BasicChannel.Id, 0, 60)
privateChannel := th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE)
_, _, response = th.Client.GetGroupsByChannel(privateChannel.Id, opts)
CheckForbiddenStatus(t, response)
groups, response := th.SystemAdminClient.GetGroupsByChannel(th.BasicChannel.Id, 0, 60)
groups, _, response := th.SystemAdminClient.GetGroupsByChannel(th.BasicChannel.Id, opts)
assert.Nil(t, response.Error)
assert.ElementsMatch(t, []*model.Group{group}, groups)
groups, response = th.SystemAdminClient.GetGroupsByChannel(model.NewId(), 0, 60)
assert.Nil(t, response.Error)
groups, _, response = th.SystemAdminClient.GetGroupsByChannel(model.NewId(), opts)
assert.Equal(t, "store.sql_channel.get.existing.app_error", response.Error.Id)
assert.Empty(t, groups)
}