Skip to content

Commit 2aab862

Browse files
Add roles to directory user and SSO profile (#311)
* Add roles to directory user and profile * fix roles serialization on org membership
1 parent b37dad7 commit 2aab862

File tree

6 files changed

+115
-19
lines changed

6 files changed

+115
-19
lines changed

lib/Resource/DirectoryUser.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22

33
namespace WorkOS\Resource;
44

5+
use WorkOS\Resource\RoleResponse;
6+
57
/**
68
* Class DirectoryUser.
9+
*
10+
* @property RoleResponse|null $role
11+
* @property array<RoleResponse>|null $roles
712
*/
813
class DirectoryUser extends BaseWorkOSResource
914
{
@@ -36,6 +41,8 @@ class DirectoryUser extends BaseWorkOSResource
3641
"jobTitle",
3742
"state",
3843
"idpId",
44+
"role",
45+
"roles",
3946
"groups",
4047
"directoryId",
4148
"organizationId"
@@ -53,11 +60,32 @@ class DirectoryUser extends BaseWorkOSResource
5360
"job_title" => "jobTitle",
5461
"state" => "state",
5562
"idp_id" => "idpId",
63+
"role" => "role",
64+
"roles" => "roles",
5665
"groups" => "groups",
5766
"directory_id" => "directoryId",
5867
"organization_id" => "organizationId"
5968
];
6069

70+
public static function constructFromResponse($response)
71+
{
72+
$instance = parent::constructFromResponse($response);
73+
74+
if (isset($response["role"])) {
75+
$instance->values["role"] = new RoleResponse($response["role"]["slug"]);
76+
}
77+
78+
if (isset($response["roles"])) {
79+
$roles = [];
80+
foreach ($response["roles"] as $role) {
81+
$roles[] = new RoleResponse($role["slug"]);
82+
}
83+
$instance->values["roles"] = $roles;
84+
}
85+
86+
return $instance;
87+
}
88+
6189
/**
6290
* @deprecated 4.22.0 Use `email` property instead.
6391
*

lib/Resource/OrganizationMembership.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace WorkOS\Resource;
44

5+
use WorkOS\Resource\RoleResponse;
6+
57
/**
68
* Class OrganizationMembership.
79
*
@@ -42,4 +44,23 @@ class OrganizationMembership extends BaseWorkOSResource
4244
"created_at" => "createdAt",
4345
"updated_at" => "updatedAt"
4446
];
47+
48+
public static function constructFromResponse($response)
49+
{
50+
$instance = parent::constructFromResponse($response);
51+
52+
if (isset($response["role"])) {
53+
$instance->values["role"] = new RoleResponse($response["role"]["slug"]);
54+
}
55+
56+
if (isset($response["roles"])) {
57+
$roles = [];
58+
foreach ($response["roles"] as $role) {
59+
$roles[] = new RoleResponse($role["slug"]);
60+
}
61+
$instance->values["roles"] = $roles;
62+
}
63+
64+
return $instance;
65+
}
4566
}

lib/Resource/Profile.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* @property string $connectionType
1717
* @property string $idpId
1818
* @property RoleResponse|null $role
19+
* @property array<RoleResponse>|null $roles
1920
* @property array $groups
2021
* @property array $rawAttributes
2122
*/
@@ -33,6 +34,7 @@ class Profile extends BaseWorkOSResource
3334
"connectionType",
3435
"idpId",
3536
"role",
37+
"roles",
3638
"groups",
3739
"customAttributes",
3840
"rawAttributes"
@@ -48,6 +50,7 @@ class Profile extends BaseWorkOSResource
4850
"connection_type" => "connectionType",
4951
"idp_id" => "idpId",
5052
"role" => "role",
53+
"roles" => "roles",
5154
"groups" => "groups",
5255
"custom_attributes" => "customAttributes",
5356
"raw_attributes" => "rawAttributes"
@@ -61,6 +64,14 @@ public static function constructFromResponse($response)
6164
$instance->values["role"] = new RoleResponse($response["role"]["slug"]);
6265
}
6366

67+
if (isset($response["roles"])) {
68+
$roles = [];
69+
foreach ($response["roles"] as $role) {
70+
$roles[] = new RoleResponse($role["slug"]);
71+
}
72+
$instance->values["roles"] = $roles;
73+
}
74+
6475
return $instance;
6576
}
6677
}

tests/WorkOS/DirectorySyncTest.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace WorkOS;
44

55
use PHPUnit\Framework\TestCase;
6+
use WorkOS\Resource\RoleResponse;
67

78
class DirectorySyncTest extends TestCase
89
{
@@ -143,7 +144,7 @@ public function testGetUser()
143144
$user = $this->ds->getUser($directoryUser);
144145
$userFixture = $this->userFixture();
145146

146-
$this->assertSame($userFixture, $user->toArray());
147+
$this->assertEquals($userFixture, $user->toArray());
147148
}
148149

149150
public function testGetUserPrimaryEmail()
@@ -214,7 +215,7 @@ public function testListUsers()
214215
$user = $this->userFixture();
215216

216217
list($before, $after, $users) = $this->ds->listUsers();
217-
$this->assertSame($user, $users[0]->toArray());
218+
$this->assertEquals($user, $users[0]->toArray());
218219
}
219220

220221
public function testDeleteDirectory()
@@ -397,6 +398,14 @@ private function usersResponseFixture()
397398
"custom_attributes" => [
398399
"fullName" => "Yoon Seri"
399400
],
401+
"role" => [
402+
"slug" => "admin"
403+
],
404+
"roles" => [
405+
[
406+
"slug" => "admin"
407+
]
408+
],
400409
"id" => "directory_usr_id"
401410
]
402411
]
@@ -453,6 +462,14 @@ private function userResponseFixture()
453462
"custom_attributes" => [
454463
"fullName" => "Yoon Seri"
455464
],
465+
"role" => [
466+
"slug" => "admin"
467+
],
468+
"roles" => [
469+
[
470+
"slug" => "admin"
471+
]
472+
],
456473
"id" => "directory_usr_id"
457474
]);
458475
}
@@ -501,6 +518,14 @@ private function userResponseFixtureNoEmail()
501518
"custom_attributes" => [
502519
"fullName" => "Yoon Seri"
503520
],
521+
"role" => [
522+
"slug" => "admin"
523+
],
524+
"roles" => [
525+
[
526+
"slug" => "admin"
527+
]
528+
],
504529
"id" => "directory_usr_id"
505530
]);
506531
}
@@ -553,6 +578,10 @@ private function userFixture()
553578
"jobTitle" => "Software Engineer",
554579
"state" => "active",
555580
"idpId" => null,
581+
"role" => new RoleResponse("admin"),
582+
"roles" => [
583+
new RoleResponse("admin"),
584+
],
556585
"groups" => null,
557586
"directoryId" => "dir_123",
558587
"organizationId" => "org_123",

tests/WorkOS/SSOTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,14 @@ private function profileAndTokenResponseFixture()
224224
"connection_id" => "conn_01EMH8WAK20T42N2NBMNBCYHAG",
225225
"connection_type" => "GoogleOAuth",
226226
"idp_id" => "randomalphanum",
227-
"role" => new RoleResponse("admin"),
227+
"role" => [
228+
"slug" => "admin"
229+
],
230+
"roles" => [
231+
[
232+
"slug" => "admin"
233+
]
234+
],
228235
"groups" => array("Admins", "Developers"),
229236
"custom_attributes" => array("license" => "professional"),
230237
"raw_attributes" => array(
@@ -251,6 +258,9 @@ private function profileFixture()
251258
"connectionType" => "GoogleOAuth",
252259
"idpId" => "randomalphanum",
253260
"role" => new RoleResponse("admin"),
261+
"roles" => [
262+
new RoleResponse("admin"),
263+
],
254264
"groups" => array("Admins", "Developers"),
255265
"customAttributes" => array("license" => "professional"),
256266
"rawAttributes" => array(

tests/WorkOS/UserManagementTest.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHPUnit\Framework\Attributes\DataProvider;
66
use PHPUnit\Framework\TestCase;
7+
use WorkOS\Resource\RoleResponse;
78

89
class UserManagementTest extends TestCase
910
{
@@ -1010,7 +1011,7 @@ public function testCreateOrganizationMembership()
10101011

10111012
$response = $this->userManagement->createOrganizationMembership($userId, $orgId, $roleSlug);
10121013

1013-
$this->assertSame($organizationMembership, $response->toArray());
1014+
$this->assertEquals($organizationMembership, $response->toArray());
10141015
}
10151016

10161017
public function testCreateOrganizationMembershipWithRoleSlugs()
@@ -1041,7 +1042,7 @@ public function testCreateOrganizationMembershipWithRoleSlugs()
10411042

10421043
$response = $this->userManagement->createOrganizationMembership($userId, $orgId, null, $roleSlugs);
10431044

1044-
$this->assertSame($organizationMembership, $response->toArray());
1045+
$this->assertEquals($organizationMembership, $response->toArray());
10451046
}
10461047

10471048
public function testCreateOrganizationMembershipWithNullRoleParams()
@@ -1093,7 +1094,7 @@ public function testGetOrganizationMembership()
10931094

10941095
$response = $this->userManagement->getOrganizationMembership($organizationMembershipId);
10951096

1096-
$this->assertSame($organizationMembership, $response->toArray());
1097+
$this->assertEquals($organizationMembership, $response->toArray());
10971098
}
10981099

10991100
public function testListOrganizationMemberships()
@@ -1127,7 +1128,7 @@ public function testListOrganizationMemberships()
11271128

11281129
list($before, $after, $organizationMemberships) = $this->userManagement->listOrganizationMemberships($userId, $orgId);
11291130

1130-
$this->assertSame($organizationMembership, $organizationMemberships[0]->toArray());
1131+
$this->assertEquals($organizationMembership, $organizationMemberships[0]->toArray());
11311132
}
11321133

11331134
public function testListOrganizationMembershipsWithStatuses()
@@ -1162,7 +1163,7 @@ public function testListOrganizationMembershipsWithStatuses()
11621163

11631164
list($before, $after, $organizationMemberships) = $this->userManagement->listOrganizationMemberships($userId, $orgId, $statuses);
11641165

1165-
$this->assertSame($organizationMembership, $organizationMemberships[0]->toArray());
1166+
$this->assertEquals($organizationMembership, $organizationMemberships[0]->toArray());
11661167
}
11671168

11681169
public function testListOrganizationMembershipsWithStatus()
@@ -1197,7 +1198,7 @@ public function testListOrganizationMembershipsWithStatus()
11971198

11981199
list($before, $after, $organizationMemberships) = $this->userManagement->listOrganizationMemberships($userId, $orgId, $statuses);
11991200

1200-
$this->assertSame($organizationMembership, $organizationMemberships[0]->toArray());
1201+
$this->assertEquals($organizationMembership, $organizationMemberships[0]->toArray());
12011202
}
12021203

12031204
public function testDeleteOrganizationMembership()
@@ -1238,7 +1239,7 @@ public function testUpdateOrganizationMembership()
12381239
);
12391240

12401241
$response = $this->userManagement->updateOrganizationMembership($organizationMembershipId, $roleSlug);
1241-
$this->assertSame($this->organizationMembershipFixture(), $response->toArray());
1242+
$this->assertEquals($this->organizationMembershipFixture(), $response->toArray());
12421243
}
12431244

12441245
public function testUpdateOrganizationMembershipWithRoleSlugs()
@@ -1259,7 +1260,7 @@ public function testUpdateOrganizationMembershipWithRoleSlugs()
12591260
);
12601261

12611262
$response = $this->userManagement->updateOrganizationMembership($organizationMembershipId, null, $roleSlugs);
1262-
$this->assertSame($this->organizationMembershipFixture(), $response->toArray());
1263+
$this->assertEquals($this->organizationMembershipFixture(), $response->toArray());
12631264
}
12641265

12651266
public function testUpdateOrganizationMembershipWithNullRoleParams()
@@ -1303,7 +1304,7 @@ public function testDeactivateOrganizationMembership()
13031304

13041305
$response = $this->userManagement->deactivateOrganizationMembership($organizationMembershipId);
13051306

1306-
$this->assertSame(array_merge($organizationMembership, array("status" => "inactive")), $response->toArray());
1307+
$this->assertEquals(array_merge($organizationMembership, array("status" => "inactive")), $response->toArray());
13071308
}
13081309

13091310
public function testReactivateOrganizationMembership()
@@ -1326,7 +1327,7 @@ public function testReactivateOrganizationMembership()
13261327

13271328
$response = $this->userManagement->reactivateOrganizationMembership($organizationMembershipId);
13281329

1329-
$this->assertSame($organizationMembership, $response->toArray());
1330+
$this->assertEquals($organizationMembership, $response->toArray());
13301331
}
13311332

13321333
public function testSendInvitation()
@@ -1792,13 +1793,9 @@ private function organizationMembershipFixture()
17921793
"id" => "om_01E4ZCR3C56J083X43JQXF3JK5",
17931794
"userId" => "user_01H7X1M4TZJN5N4HG4XXMA1234",
17941795
"organizationId" => "org_01EHQMYV6MBK39QC5PZXHY59C3",
1795-
"role" => [
1796-
"slug" => "admin",
1797-
],
1796+
"role" => new RoleResponse("admin"),
17981797
"roles" => [
1799-
[
1800-
"slug" => "admin",
1801-
],
1798+
new RoleResponse("admin"),
18021799
],
18031800
"status" => "active",
18041801
"createdAt" => "2021-06-25T19:07:33.155Z",

0 commit comments

Comments
 (0)