Skip to content

Commit

Permalink
Add API to get organization members and member associated organizations
Browse files Browse the repository at this point in the history
  • Loading branch information
WilfredDube committed Dec 31, 2024
1 parent e96f5e2 commit 7dc2ccd
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
38 changes: 38 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
88 changes: 88 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions gocloak_iface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7dc2ccd

Please sign in to comment.