diff --git a/client.go b/client.go index 01a18262..f6302e58 100644 --- a/client.go +++ b/client.go @@ -4679,3 +4679,41 @@ func (g *GoCloak) GetOrganizationMemberByID(ctx context.Context, token, realm, i return result, err } + +// GetOrganizationMembers returns a paginated list of organization members filtered according to the specified parameters +func (g *GoCloak) GetOrganizationMembers(ctx context.Context, token, realm, idOfOrganization string, params GetMembersParams) ([]*MemberRepresentation, error) { + const errMessage = "could not get organization members" + + queryParams, err := GetQueryParams(params) + if err != nil { + return nil, errors.Wrap(err, errMessage) + } + + var result []*MemberRepresentation + + resp, err := g.GetRequestWithBearerAuth(ctx, token). + SetResult(&result). + SetQueryParams(queryParams). + Get(g.getAdminRealmURL(realm, "organizations", idOfOrganization, "members")) + if err := checkForError(resp, err, errMessage); err != nil { + return nil, errors.Wrap(err, errMessage) + } + + return result, err +} + +// GetMemberAssociatedOrganizations returns the organizations associated with the user that has the specified id +func (g *GoCloak) GetMemberAssociatedOrganizations(ctx context.Context, token, realm, idOfUser string) ([]*OrganizationRepresentation, error) { + const errMessage = "could not get member's associated organizations" + + var result []*OrganizationRepresentation + + resp, err := g.GetRequestWithBearerAuth(ctx, token). + SetResult(&result). + Get(g.getAdminRealmURL(realm, "organizations", "members", idOfUser, "organizations")) + if err := checkForError(resp, err, errMessage); err != nil { + return nil, errors.Wrap(err, errMessage) + } + + return result, err +} diff --git a/client_test.go b/client_test.go index d41f6966..f4df4e59 100644 --- a/client_test.go +++ b/client_test.go @@ -7441,3 +7441,91 @@ func Test_GetOrganizationMemberByID(t *testing.T) { require.Equal(t, *member.ID, userID) require.NoError(t, err, "GetOrganizationMemberByID failed") } + +func Test_GetOrganizationMembers(t *testing.T) { + t.Parallel() + cfg := GetConfig(t) + client := NewClientWithDebug(t) + token := GetAdminToken(t, client) + + td, userID := CreateUser(t, client) + defer td() + + td2, userID2 := CreateUser(t, client) + defer td2() + + tearDown, orgID := CreateOrganization(t, client, "Test Inc", "test-inc", "test.com") + defer tearDown() + + ctx := context.Background() + err := client.AddUserToOrganization( + ctx, + token.AccessToken, + cfg.GoCloak.Realm, + orgID, + userID) + require.NoError(t, err, "AddUserToOrganization failed") + + err = client.AddUserToOrganization( + ctx, + token.AccessToken, + cfg.GoCloak.Realm, + orgID, + userID2) + require.NoError(t, err, "AddUserToOrganization failed") + + members, err := client.GetOrganizationMembers( + ctx, + token.AccessToken, + cfg.GoCloak.Realm, + orgID, + gocloak.GetMembersParams{}) + require.NoError(t, err, "GetOrganizationMembers failed") + + fmt.Println(members) + require.GreaterOrEqual(t, len(members), 1) +} + +func Test_GetMemberAssociatedOrganizations(t *testing.T) { + t.Parallel() + cfg := GetConfig(t) + client := NewClientWithDebug(t) + token := GetAdminToken(t, client) + + td, userID := CreateUser(t, client) + defer td() + + // Create two organizations + tearDown1, orgID1 := CreateOrganization(t, client, "Test Inc", "test-inc", "test.com") + defer tearDown1() + + tearDown2, orgID2 := CreateOrganization(t, client, "Another Inc", "another-inc", "another.com") + defer tearDown2() + + // Add user to both organizations + ctx := context.Background() + err := client.AddUserToOrganization( + ctx, + token.AccessToken, + cfg.GoCloak.Realm, + orgID1, + userID) + require.NoError(t, err, "AddUserToOrganization failed") + + err = client.AddUserToOrganization( + ctx, + token.AccessToken, + cfg.GoCloak.Realm, + orgID2, + userID) + require.NoError(t, err, "AddUserToOrganization failed") + + organizations, err := client.GetMemberAssociatedOrganizations( + ctx, + token.AccessToken, + cfg.GoCloak.Realm, + userID) + require.NoError(t, err, "GetMemberAssociatedOrganizations failed") + + require.GreaterOrEqual(t, len(organizations), 1) +} diff --git a/gocloak_iface.go b/gocloak_iface.go index 4b191c93..f625d7bd 100644 --- a/gocloak_iface.go +++ b/gocloak_iface.go @@ -607,4 +607,8 @@ type GoCloakIface interface { // Searches for auser with the given id. If one is found, and is currently a member of the organization, returns it. // Otherwise,an error response with status NOT_FOUND is returned GetOrganizationMemberByID(ctx context.Context, token, realm, idOfOrganization, idOfUser string) (*MemberRepresentation, error) + // GetOrganizationMembers returns a paginated list of organization members filtered according to the specified parameters + GetOrganizationMembers(ctx context.Context, token, realm, idOfOrganization string, params GetMembersParams) ([]*MemberRepresentation, error) + // GetMemberAssociatedOrganizations returns the organizations associated with the user that has the specified id + GetMemberAssociatedOrganizations(ctx context.Context, token, realm, idOfUser string) ([]*OrganizationRepresentation, error) }