From 6d480b1b49449822bd5d7eed9ed3bd9240591d0d Mon Sep 17 00:00:00 2001 From: Felix Hartung <7132415+TerraTalpi@users.noreply.github.com> Date: Tue, 14 Dec 2021 17:04:27 +0000 Subject: [PATCH] Group: Add GetMemberGroupAsStrings func --- GraphClient.go | 6 +++--- Group.go | 12 ++++++++++++ Group_test.go | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/GraphClient.go b/GraphClient.go index c443fbb..d829bf0 100644 --- a/GraphClient.go +++ b/GraphClient.go @@ -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 diff --git a/Group.go b/Group.go index 7c8f102..96df753 100644 --- a/Group.go +++ b/Group.go @@ -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 { diff --git a/Group_test.go b/Group_test.go index 196dc83..fefa8f7 100644 --- a/Group_test.go +++ b/Group_test.go @@ -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)) + } + }) + } +}