Skip to content

Commit c9ac713

Browse files
Add roles to directory user & profile (#1401)
## Description Adds `roles` property to directory user and SSO profile to support multiple roles. ## Documentation Does this require changes to the WorkOS Docs? E.g. the [API Reference](https://workos.com/docs/reference) or code snippets need updates. - [X] Yes If yes, link a related docs PR and add a docs maintainer as a reviewer. Their approval is required.
1 parent 3a637d2 commit c9ac713

File tree

7 files changed

+27
-3
lines changed

7 files changed

+27
-3
lines changed

src/directory-sync/directory-sync.spec.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ describe('DirectorySync', () => {
128128
updated_at: '2021-12-13 12:15:45.531847',
129129
};
130130

131-
const userWithRole: DirectoryUserWithGroups = {
131+
const userWithRoles: DirectoryUserWithGroups = {
132132
object: 'directory_user',
133133
id: 'directory_user_456',
134134
customAttributes: {
@@ -153,6 +153,7 @@ describe('DirectorySync', () => {
153153
state: 'active',
154154
username: 'jonsnow',
155155
role: { slug: 'super_admin' },
156+
roles: [{ slug: 'super_admin' }],
156157
createdAt: '2021-10-27 15:21:50.640959',
157158
updatedAt: '2021-12-13 12:15:45.531847',
158159
};
@@ -182,6 +183,7 @@ describe('DirectorySync', () => {
182183
state: 'active',
183184
username: 'jonsnow',
184185
role: { slug: 'super_admin' },
186+
roles: [{ slug: 'super_admin' }],
185187
created_at: '2021-10-27 15:21:50.640959',
186188
updated_at: '2021-12-13 12:15:45.531847',
187189
};
@@ -465,15 +467,15 @@ describe('DirectorySync', () => {
465467
expect(subject).toEqual(userWithGroup);
466468
});
467469

468-
describe('with a Role', () => {
470+
describe('with roles', () => {
469471
it(`requests a Directory User`, async () => {
470472
fetchOnce(userWithRoleResponse);
471473

472474
const subject = await workos.directorySync.getUser(
473475
'directory_user_456',
474476
);
475477

476-
expect(subject).toEqual(userWithRole);
478+
expect(subject).toEqual(userWithRoles);
477479
});
478480
});
479481
});

src/directory-sync/interfaces/directory-user.interface.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export interface DirectoryUser<
4141
jobTitle: string | null;
4242
state: 'active' | 'inactive';
4343
role?: RoleResponse;
44+
roles?: RoleResponse[];
4445
createdAt: string;
4546
updatedAt: string;
4647
}
@@ -80,6 +81,7 @@ export interface DirectoryUserResponse<
8081
job_title: string | null;
8182
state: 'active' | 'inactive';
8283
role?: RoleResponse;
84+
roles?: RoleResponse[];
8385
created_at: string;
8486
updated_at: string;
8587
}

src/directory-sync/serializers/directory-user.serializer.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export const deserializeDirectoryUser = <
2929
jobTitle: directoryUser.job_title,
3030
state: directoryUser.state,
3131
role: directoryUser.role,
32+
roles: directoryUser.roles,
3233
createdAt: directoryUser.created_at,
3334
updatedAt: directoryUser.updated_at,
3435
});
@@ -60,6 +61,7 @@ export const deserializeUpdatedEventDirectoryUser = (
6061
jobTitle: directoryUser.job_title,
6162
state: directoryUser.state,
6263
role: directoryUser.role,
64+
roles: directoryUser.roles,
6365
createdAt: directoryUser.created_at,
6466
updatedAt: directoryUser.updated_at,
6567
previousAttributes: directoryUser.previous_attributes,

src/sso/__snapshots__/sso.spec.ts.snap

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ exports[`SSO SSO getProfileAndToken with all information provided sends a reques
5757
"role": {
5858
"slug": "admin",
5959
},
60+
"roles": [
61+
{
62+
"slug": "admin",
63+
},
64+
],
6065
}
6166
`;
6267

@@ -91,5 +96,10 @@ exports[`SSO SSO getProfileAndToken without a groups attribute sends a request t
9196
"role": {
9297
"slug": "admin",
9398
},
99+
"roles": [
100+
{
101+
"slug": "admin",
102+
},
103+
],
94104
}
95105
`;

src/sso/interfaces/profile.interface.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface Profile<CustomAttributesType extends UnknownRecord> {
1212
firstName?: string;
1313
lastName?: string;
1414
role?: RoleResponse;
15+
roles?: RoleResponse[];
1516
groups?: string[];
1617
customAttributes?: CustomAttributesType;
1718
rawAttributes?: { [key: string]: any };
@@ -27,6 +28,7 @@ export interface ProfileResponse<CustomAttributesType extends UnknownRecord> {
2728
first_name?: string;
2829
last_name?: string;
2930
role?: RoleResponse;
31+
roles?: RoleResponse[];
3032
groups?: string[];
3133
custom_attributes?: CustomAttributesType;
3234
raw_attributes?: { [key: string]: any };

src/sso/serializers/profile.serializer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export const deserializeProfile = <CustomAttributesType extends UnknownRecord>(
1313
firstName: profile.first_name,
1414
lastName: profile.last_name,
1515
role: profile.role,
16+
roles: profile.roles,
1617
groups: profile.groups,
1718
customAttributes: profile.custom_attributes,
1819
rawAttributes: profile.raw_attributes,

src/sso/sso.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ describe('SSO', () => {
278278
role: {
279279
slug: 'admin',
280280
},
281+
roles: [{ slug: 'admin' }],
281282
groups: ['Admins', 'Developers'],
282283
raw_attributes: {
283284
email: 'foo@test.com',
@@ -323,6 +324,7 @@ describe('SSO', () => {
323324
role: {
324325
slug: 'admin',
325326
},
327+
roles: [{ slug: 'admin' }],
326328
raw_attributes: {
327329
email: 'foo@test.com',
328330
first_name: 'foo',
@@ -363,6 +365,7 @@ describe('SSO', () => {
363365
role: {
364366
slug: 'admin',
365367
},
368+
roles: [{ slug: 'admin' }],
366369
groups: ['Admins', 'Developers'],
367370
raw_attributes: {
368371
email: 'foo@test.com',
@@ -415,6 +418,7 @@ describe('SSO', () => {
415418
role: {
416419
slug: 'admin',
417420
},
421+
roles: [{ slug: 'admin' }],
418422
raw_attributes: {
419423
email: 'foo@test.com',
420424
first_name: 'foo',
@@ -453,6 +457,7 @@ describe('SSO', () => {
453457
role: {
454458
slug: 'admin',
455459
},
460+
roles: [{ slug: 'admin' }],
456461
groups: ['Admins', 'Developers'],
457462
raw_attributes: {
458463
email: 'foo@test.com',

0 commit comments

Comments
 (0)