Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions pkg/api/tenants.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,27 @@ func (t *Tenants) Delete(ctx context.Context, tenantKey string) error {
}
return nil
}

// ListTenantUsers lists all users within a specific tenant under the context's environment.
// Usage Example:
// `users, err := PermitClient.Api.Tenants.ListTenantUsers(ctx, "tenant-key", 1, 10)`
func (t *Tenants) ListTenantUsers(ctx context.Context, tenantKey string, page int, perPage int) ([]models.UserRead, error) {
perPageLimit := int32(DefaultPerPageLimit)
if !isPaginationInLimit(int32(page), int32(perPage), perPageLimit) {
err := errors.NewPermitPaginationError()
t.logger.Error("error listing tenant users - max per page: "+string(perPageLimit), zap.Error(err))
return nil, err
}
err := t.lazyLoadPermitContext(ctx)
if err != nil {
t.logger.Error("", zap.Error(err))
return nil, err
}
users, httpRes, err := t.client.TenantsApi.ListTenantUsers(ctx, t.config.Context.ProjectId, tenantKey, t.config.Context.EnvironmentId).Page(int32(page)).PerPage(int32(perPage)).Execute()
err = errors.HttpErrorHandle(err, httpRes)
if err != nil {
t.logger.Error("error listing tenant users for tenant: "+tenantKey, zap.Error(err))
return nil, err
}
return users.GetData(), nil
}
9 changes: 9 additions & 0 deletions pkg/tests/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,15 @@ func TestIntegration(t *testing.T) {
assert.NoError(t, err)
time.Sleep(30 * time.Second)

// Testing List Tenants Users
// Note - Dependent on the user creation above -- consider decoupling this test from the user creation

// Test 1: Basic functionality - tenant with assigned user
tenantUsers, err := permitClient.Api.Tenants.ListTenantUsers(ctx, tenantKey, 1, 100)
assert.NoError(t, err)
assert.Len(t, tenantUsers, 1)
assert.Equal(t, userKey, tenantUsers[0].GetKey())

userPermissions, err := permitClient.GetUserPermissions(enforcement.UserBuilder(userKey).Build())
assert.NoError(t, err)
userPermissionsInTenant, found := userPermissions["__tenant:"+tenantKey]
Expand Down
Loading