Skip to content

Commit

Permalink
Group: Add GetMemberGroupAsStrings func
Browse files Browse the repository at this point in the history
  • Loading branch information
TerraTalpi committed Dec 14, 2021
1 parent 05fa1b9 commit 6d480b1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
6 changes: 3 additions & 3 deletions GraphClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,15 +365,15 @@ func (g *GraphClient) ListGroups(opts ...ListQueryOption) (Groups, error) {
// You can specify the securityGroupsEnabled parameter to only return security group IDs.
//
// Reference: https://docs.microsoft.com/en-us/graph/api/directoryobject-getmembergroups?view=graph-rest-1.0&tabs=http
func (g *GraphClient) getMemberGroups(identifier string, securityGroupsEnabeled bool, opts ...GetQueryOption) ([]string, error) {
resource := fmt.Sprintf("/users/%v/getMemberGroups", identifier)
func (g *GraphClient) getMemberGroups(identifier string, securityEnabledOnly bool, opts ...GetQueryOption) ([]string, error) {
resource := fmt.Sprintf("/directoryObjects/%v/getMemberGroups", identifier)
var post struct {
SecurityEnabledOnly bool `json:"securityEnabledOnly"`
}
var marsh struct {
Groups []string `json:"value"`
}
post.SecurityEnabledOnly = securityGroupsEnabeled
post.SecurityEnabledOnly = securityEnabledOnly
bodyBytes, err := json.Marshal(post)
if err != nil {
return marsh.Groups, err
Expand Down
12 changes: 12 additions & 0 deletions Group.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ func (g Group) ListTransitiveMembers(opts ...ListQueryOption) (Users, error) {
return marsh.Users, g.graphClient.makeGETAPICall(resource, compileListQueryOptions(opts), &marsh)
}

// GetMemberGroupsAsStrings returns a list of all group IDs the user is a member of.
//
// opts ...GetQueryOption - only msgraph.GetWithContext is supported.
//
// Reference: https://docs.microsoft.com/en-us/graph/api/directoryobject-getmembergroups?view=graph-rest-1.0&tabs=http
func (g Group) GetMemberGroupsAsStrings(opts ...GetQueryOption) ([]string, error) {
if g.graphClient == nil {
return nil, ErrNotGraphClientSourced
}
return g.graphClient.getMemberGroups(g.ID, false, opts...) // securityEnabledOnly is not supported for Groups, see documentation / API-reference
}

// UnmarshalJSON implements the json unmarshal to be used by the json-library
func (g *Group) UnmarshalJSON(data []byte) error {
tmp := struct {
Expand Down
37 changes: 37 additions & 0 deletions Group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,40 @@ func TestGroup_ListTransitiveMembers(t *testing.T) {
})
}
}

func TestGroup_GetMemberGroupsAsStrings(t *testing.T) {
testGroup := GetTestGroup(t)

tests := []struct {
name string
g Group
opts []GetQueryOption
wantErr bool
}{
{
name: "Test group func GetMembershipGroupsAsStrings",
g: testGroup,
wantErr: false,
}, {
name: "Test group func GetMembershipGroupsAsStrings - no securityGroupsEnabeledF",
g: testGroup,
wantErr: false,
},
{
name: "Group not initialized by GraphClient",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.g.GetMemberGroupsAsStrings(tt.opts...)
if (err != nil) != tt.wantErr {
t.Errorf("Group.GetMemberGroupsAsStrings() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && len(got) == 0 {
t.Errorf("Group.GetMemberGroupsAsStrings() = %v, len(%d), want at least one value", got, len(got))
}
})
}
}

0 comments on commit 6d480b1

Please sign in to comment.