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
69 changes: 69 additions & 0 deletions src/authorization/authorization.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1739,6 +1739,47 @@ describe('Authorization', () => {
order: 'desc',
});
});

it('filters by resource id', async () => {
fetchOnce(listRoleAssignmentsFixture);

await workos.authorization.listRoleAssignments({
organizationMembershipId: testOrgMembershipId,
resourceId: testResourceId,
});

expect(fetchSearchParams()).toMatchObject({
resource_id: testResourceId,
});
});

it('filters by resource external id and resource type slug', async () => {
fetchOnce(listRoleAssignmentsFixture);

await workos.authorization.listRoleAssignments({
organizationMembershipId: testOrgMembershipId,
resourceExternalId: 'doc-456',
resourceTypeSlug: 'document',
});

expect(fetchSearchParams()).toMatchObject({
resource_external_id: 'doc-456',
resource_type_slug: 'document',
});
});

it('filters by resource type slug only', async () => {
fetchOnce(listRoleAssignmentsFixture);

await workos.authorization.listRoleAssignments({
organizationMembershipId: testOrgMembershipId,
resourceTypeSlug: 'document',
});

expect(fetchSearchParams()).toMatchObject({
resource_type_slug: 'document',
});
});
});

describe('listRoleAssignmentsForResource', () => {
Expand Down Expand Up @@ -1818,6 +1859,19 @@ describe('Authorization', () => {
order: 'desc',
});
});

it('filters by role slug', async () => {
fetchOnce(listRoleAssignmentsFixture);

await workos.authorization.listRoleAssignmentsForResource({
resourceId: testResourceId,
roleSlug: 'editor',
});

expect(fetchSearchParams()).toMatchObject({
role_slug: 'editor',
});
});
});

describe('listResourceRoleAssignments', () => {
Expand Down Expand Up @@ -1903,6 +1957,21 @@ describe('Authorization', () => {
order: 'desc',
});
});

it('filters by role slug', async () => {
fetchOnce(listRoleAssignmentsFixture);

await workos.authorization.listResourceRoleAssignments({
organizationId: testOrgId,
resourceTypeSlug: 'document',
externalId: 'doc-456',
roleSlug: 'editor',
});

expect(fetchSearchParams()).toMatchObject({
role_slug: 'editor',
});
});
});

describe('assignRole', () => {
Expand Down
19 changes: 13 additions & 6 deletions src/authorization/authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ import {
serializeListMembershipsForResourceOptions,
serializeListResourcesForMembershipOptions,
serializeListEffectivePermissionsOptions,
serializeListRoleAssignmentsOptions,
serializeListRoleAssignmentsForResourceOptions,
} from './serializers';
import {
AuthorizationOrganizationMembership,
Expand Down Expand Up @@ -857,12 +859,13 @@ export class Authorization {
): Promise<AutoPaginatable<RoleAssignment>> {
const { organizationMembershipId, ...queryOptions } = options;
const endpoint = `/authorization/organization_memberships/${organizationMembershipId}/role_assignments`;
const serializedOptions = serializeListRoleAssignmentsOptions(queryOptions);
return new AutoPaginatable(
await fetchAndDeserialize<RoleAssignmentResponse, RoleAssignment>(
this.workos,
endpoint,
deserializeRoleAssignment,
queryOptions,
serializedOptions,
),
(params) =>
fetchAndDeserialize<RoleAssignmentResponse, RoleAssignment>(
Expand All @@ -871,7 +874,7 @@ export class Authorization {
deserializeRoleAssignment,
params,
),
queryOptions,
serializedOptions,
);
}

Expand All @@ -894,12 +897,14 @@ export class Authorization {
): Promise<AutoPaginatable<RoleAssignment>> {
const { resourceId, ...queryOptions } = options;
const endpoint = `/authorization/resources/${resourceId}/role_assignments`;
const serializedOptions =
serializeListRoleAssignmentsForResourceOptions(queryOptions);
return new AutoPaginatable(
await fetchAndDeserialize<RoleAssignmentResponse, RoleAssignment>(
this.workos,
endpoint,
deserializeRoleAssignment,
queryOptions,
serializedOptions,
),
(params) =>
fetchAndDeserialize<RoleAssignmentResponse, RoleAssignment>(
Expand All @@ -908,7 +913,7 @@ export class Authorization {
deserializeRoleAssignment,
params,
),
queryOptions,
serializedOptions,
);
}

Expand Down Expand Up @@ -942,12 +947,14 @@ export class Authorization {
const { organizationId, resourceTypeSlug, externalId, ...queryOptions } =
options;
const endpoint = `/authorization/organizations/${organizationId}/resources/${resourceTypeSlug}/${externalId}/role_assignments`;
const serializedOptions =
serializeListRoleAssignmentsForResourceOptions(queryOptions);
return new AutoPaginatable(
await fetchAndDeserialize<RoleAssignmentResponse, RoleAssignment>(
this.workos,
endpoint,
deserializeRoleAssignment,
queryOptions,
serializedOptions,
),
(params) =>
fetchAndDeserialize<RoleAssignmentResponse, RoleAssignment>(
Expand All @@ -956,7 +963,7 @@ export class Authorization {
deserializeRoleAssignment,
params,
),
queryOptions,
serializedOptions,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ export interface ListRoleAssignmentsForResourceByExternalIdOptions extends Pagin
organizationId: string;
resourceTypeSlug: string;
externalId: string;
/** Filter role assignments to only those that grant this role. */
roleSlug?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@ import { PaginationOptions } from '../../common/interfaces/pagination-options.in

export interface ListRoleAssignmentsForResourceOptions extends PaginationOptions {
resourceId: string;
/** Filter role assignments to only those that grant this role. */
roleSlug?: string;
}

export interface SerializedListRoleAssignmentsForResourceOptions extends PaginationOptions {
role_slug?: string;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
import { PaginationOptions } from '../../common/interfaces/pagination-options.interface';

export interface ListRoleAssignmentsOptions extends PaginationOptions {
organizationMembershipId: string;
/** Filter role assignments to only those granted on the resource with this ID. */
resourceId?: string;
/** Filter role assignments to only those granted on the resource with this external ID. Can be used on its own or combined with `resourceTypeSlug`. */
resourceExternalId?: string;
/** Filter role assignments to only those granted on resources of this type. Can be used on its own or combined with `resourceExternalId`. */
resourceTypeSlug?: string;
}

export interface SerializedListRoleAssignmentsOptions extends PaginationOptions {
resource_id?: string;
resource_external_id?: string;
resource_type_slug?: string;
}
2 changes: 2 additions & 0 deletions src/authorization/serializers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export * from './authorization-check-options.serializer';
export * from './list-resources-for-membership-options.serializer';
export * from './list-memberships-for-resource-options.serializer';
export * from './role-assignment.serializer';
export * from './list-role-assignments-options.serializer';
export * from './list-role-assignments-for-resource-options.serializer';
export * from './assign-role-options.serializer';
export * from './remove-role-options.serializer';
export * from './list-effective-permissions-options.serializer';
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { PaginationOptions } from '../../common/interfaces/pagination-options.interface';
import { serializePaginationOptions } from '../../common/serializers';
import { SerializedListRoleAssignmentsForResourceOptions } from '../interfaces/list-role-assignments-for-resource-options.interface';

interface ListRoleAssignmentsForResourceQueryOptions extends PaginationOptions {
roleSlug?: string;
}

export const serializeListRoleAssignmentsForResourceOptions = (
options: ListRoleAssignmentsForResourceQueryOptions,
): SerializedListRoleAssignmentsForResourceOptions => ({
...(options.roleSlug && { role_slug: options.roleSlug }),
...serializePaginationOptions(options),
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { serializePaginationOptions } from '../../common/serializers';
import {
ListRoleAssignmentsOptions,
SerializedListRoleAssignmentsOptions,
} from '../interfaces/list-role-assignments-options.interface';

export const serializeListRoleAssignmentsOptions = (
options: Omit<ListRoleAssignmentsOptions, 'organizationMembershipId'>,
): SerializedListRoleAssignmentsOptions => ({
...(options.resourceId && { resource_id: options.resourceId }),
...(options.resourceExternalId && {
resource_external_id: options.resourceExternalId,
}),
...(options.resourceTypeSlug && {
resource_type_slug: options.resourceTypeSlug,
}),
...serializePaginationOptions(options),
});
Loading