MM-16258: Adds new API endpoint + (App & Client & Store) to retrieve … (#11176)
* MM-16258: Adds new API endpoint + (App & Client & Store) to retrieve Users who would be removed from a list of hypothetical group IDs representing the synced groups. * MM-16258: Adds roles to JSON response. * MM-16258: Updates GetByIDs to use Squirrel. * MM-16258: Puts as much as possible into Squirrel. * MM-16258: Changes names of methods, functions, and route. * MM-16258: Updates some comments. * MM-16258: Extra validation of group_ids parameter. * MM-16258: Changes validation of group_ids query param. * MM-16258: Rename a variable and a constant. * MM-16258: Fix test.
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
5ee1f6e2e0
Коммит
9d41c7a583
62
api4/team.go
62
api4/team.go
@@ -6,11 +6,14 @@ package api4
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
)
|
||||
@@ -18,8 +21,15 @@ import (
|
||||
const (
|
||||
MAX_ADD_MEMBERS_BATCH = 20
|
||||
MAXIMUM_BULK_IMPORT_SIZE = 10 * 1024 * 1024
|
||||
groupIDsParamPattern = "[^a-zA-Z0-9,]*"
|
||||
)
|
||||
|
||||
var groupIDsQueryParamRegex *regexp.Regexp
|
||||
|
||||
func init() {
|
||||
groupIDsQueryParamRegex = regexp.MustCompile(groupIDsParamPattern)
|
||||
}
|
||||
|
||||
func (api *API) InitTeam() {
|
||||
api.BaseRoutes.Teams.Handle("", api.ApiSessionRequired(createTeam)).Methods("POST")
|
||||
api.BaseRoutes.Teams.Handle("", api.ApiSessionRequired(getAllTeams)).Methods("GET")
|
||||
@@ -58,6 +68,8 @@ func (api *API) InitTeam() {
|
||||
api.BaseRoutes.Team.Handle("/invite/email", api.ApiSessionRequired(inviteUsersToTeam)).Methods("POST")
|
||||
api.BaseRoutes.Teams.Handle("/invites/email", api.ApiSessionRequired(invalidateAllEmailInvites)).Methods("DELETE")
|
||||
api.BaseRoutes.Teams.Handle("/invite/{invite_id:[A-Za-z0-9]+}", api.ApiHandler(getInviteInfo)).Methods("GET")
|
||||
|
||||
api.BaseRoutes.Teams.Handle("/{team_id:[A-Za-z0-9]+}/members_minus_group_members", api.ApiSessionRequired(teamMembersMinusGroupMembers)).Methods("GET")
|
||||
}
|
||||
|
||||
func createTeam(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
@@ -1082,3 +1094,53 @@ func updateTeamScheme(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
ReturnStatusOK(w)
|
||||
}
|
||||
|
||||
func teamMembersMinusGroupMembers(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
c.RequireTeamId()
|
||||
if c.Err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
groupIDsParam := groupIDsQueryParamRegex.ReplaceAllString(c.Params.GroupIDs, "")
|
||||
|
||||
if len(groupIDsParam) < 26 {
|
||||
c.SetInvalidParam("group_ids")
|
||||
return
|
||||
}
|
||||
|
||||
groupIDs := []string{}
|
||||
for _, gid := range strings.Split(c.Params.GroupIDs, ",") {
|
||||
if len(gid) != 26 {
|
||||
c.SetInvalidParam("group_ids")
|
||||
return
|
||||
}
|
||||
groupIDs = append(groupIDs, gid)
|
||||
}
|
||||
|
||||
if !c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_MANAGE_SYSTEM) {
|
||||
c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
|
||||
return
|
||||
}
|
||||
|
||||
users, totalCount, err := c.App.TeamMembersMinusGroupMembers(
|
||||
c.Params.TeamId,
|
||||
groupIDs,
|
||||
c.Params.Page,
|
||||
c.Params.PerPage,
|
||||
)
|
||||
if err != nil {
|
||||
c.Err = err
|
||||
return
|
||||
}
|
||||
|
||||
b, marshalErr := json.Marshal(&model.UsersWithGroupsAndCount{
|
||||
Users: users,
|
||||
Count: totalCount,
|
||||
})
|
||||
if marshalErr != nil {
|
||||
c.Err = model.NewAppError("Api4.teamMembersMinusGroupMembers", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Write(b)
|
||||
}
|
||||
|
||||
@@ -2488,3 +2488,96 @@ func TestUpdateTeamScheme(t *testing.T) {
|
||||
_, resp = th.SystemAdminClient.UpdateTeamScheme(team.Id, teamScheme.Id)
|
||||
CheckUnauthorizedStatus(t, resp)
|
||||
}
|
||||
|
||||
func TestTeamMembersMinusGroupMembers(t *testing.T) {
|
||||
th := Setup().InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
user1 := th.BasicUser
|
||||
user2 := th.BasicUser2
|
||||
|
||||
team := th.CreateTeam()
|
||||
team.GroupConstrained = model.NewBool(true)
|
||||
team, err := th.App.UpdateTeam(team)
|
||||
require.Nil(t, err)
|
||||
|
||||
_, err = th.App.AddTeamMember(team.Id, user1.Id)
|
||||
require.Nil(t, err)
|
||||
_, err = th.App.AddTeamMember(team.Id, user2.Id)
|
||||
require.Nil(t, err)
|
||||
|
||||
group1 := th.CreateGroup()
|
||||
group2 := th.CreateGroup()
|
||||
|
||||
_, err = th.App.CreateOrRestoreGroupMember(group1.Id, user1.Id)
|
||||
require.Nil(t, err)
|
||||
_, err = th.App.CreateOrRestoreGroupMember(group2.Id, user2.Id)
|
||||
require.Nil(t, err)
|
||||
|
||||
// No permissions
|
||||
_, _, res := th.Client.TeamMembersMinusGroupMembers(team.Id, []string{group1.Id, group2.Id}, 0, 100, "")
|
||||
require.Equal(t, "api.context.permissions.app_error", res.Error.Id)
|
||||
|
||||
testCases := map[string]struct {
|
||||
groupIDs []string
|
||||
page int
|
||||
perPage int
|
||||
length int
|
||||
count int
|
||||
otherAssertions func([]*model.UserWithGroups)
|
||||
}{
|
||||
"All groups, expect no users removed": {
|
||||
groupIDs: []string{group1.Id, group2.Id},
|
||||
page: 0,
|
||||
perPage: 100,
|
||||
length: 0,
|
||||
count: 0,
|
||||
},
|
||||
"Some nonexistent group, page 0": {
|
||||
groupIDs: []string{model.NewId()},
|
||||
page: 0,
|
||||
perPage: 1,
|
||||
length: 1,
|
||||
count: 2,
|
||||
},
|
||||
"Some nonexistent group, page 1": {
|
||||
groupIDs: []string{model.NewId()},
|
||||
page: 1,
|
||||
perPage: 1,
|
||||
length: 1,
|
||||
count: 2,
|
||||
},
|
||||
"One group, expect one user removed": {
|
||||
groupIDs: []string{group1.Id},
|
||||
page: 0,
|
||||
perPage: 100,
|
||||
length: 1,
|
||||
count: 1,
|
||||
otherAssertions: func(uwg []*model.UserWithGroups) {
|
||||
require.Equal(t, uwg[0].Id, user2.Id)
|
||||
},
|
||||
},
|
||||
"Other group, expect other user removed": {
|
||||
groupIDs: []string{group2.Id},
|
||||
page: 0,
|
||||
perPage: 100,
|
||||
length: 1,
|
||||
count: 1,
|
||||
otherAssertions: func(uwg []*model.UserWithGroups) {
|
||||
require.Equal(t, uwg[0].Id, user1.Id)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
uwg, count, res := th.SystemAdminClient.TeamMembersMinusGroupMembers(team.Id, tc.groupIDs, tc.page, tc.perPage, "")
|
||||
require.Nil(t, res.Error)
|
||||
require.Len(t, uwg, tc.length)
|
||||
require.Equal(t, tc.count, int(count))
|
||||
if tc.otherAssertions != nil {
|
||||
tc.otherAssertions(uwg)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user