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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ vendor/
# PHPUnit
/.phpunit.cache
.phpunit.result.cache

# IDEs
.idea/
3 changes: 3 additions & 0 deletions lib/Resource/OrganizationMembership.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* @property string $userId
* @property string $organizationId
* @property RoleResponse $role
* @property array<RoleResponse> $roles
* @property 'active'|'inactive'|'pending' $status
* @property string $createdAt
* @property string $updatedAt
Expand All @@ -24,6 +25,7 @@ class OrganizationMembership extends BaseWorkOSResource
"userId",
"organizationId",
"role",
"roles",
"status",
"createdAt",
"updatedAt"
Expand All @@ -35,6 +37,7 @@ class OrganizationMembership extends BaseWorkOSResource
"user_id" => "userId",
"organization_id" => "organizationId",
"role" => "role",
"roles" => "roles",
"status" => "status",
"created_at" => "createdAt",
"updated_at" => "updatedAt"
Expand Down
31 changes: 23 additions & 8 deletions lib/UserManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,21 +222,29 @@ public function deleteUser($userId)
* @param string $userId User ID
* @param string $organizationId Organization ID
* @param string|null $roleSlug Role Slug
* @param string|null $roleSlugs Role Slugs
*
* @throws Exception\WorkOSException
*
* @return Resource\OrganizationMembership
*/
public function createOrganizationMembership($userId, $organizationId, $roleSlug = null)
public function createOrganizationMembership($userId, $organizationId, $roleSlug = null, $roleSlugs = null)
{
$path = "user_management/organization_memberships";

$params = [
"organization_id" => $organizationId,
"user_id" => $userId,
"role_slug" => $roleSlug
"user_id" => $userId
];

if (!is_null($roleSlug)) {
$params["role_slug"] = $roleSlug;
}

if (!is_null($roleSlugs)) {
$params["role_slugs"] = $roleSlugs;
}

$response = Client::request(
Client::METHOD_POST,
$path,
Expand Down Expand Up @@ -300,19 +308,26 @@ public function deleteOrganizationMembership($organizationMembershipId)
* Update a User organization membership.
*
* @param string $organizationMembershipId Organization Membership ID
* @param string|null $role_slug The unique role identifier.
* @param string|null $role_slug The unique slug of the role to grant to this membership.
* @param string|null $role_slugs The unique slugs of the roles to grant to this membership.
*
* @throws Exception\WorkOSException
*
* @return Resource\OrganizationMembership
*/
public function updateOrganizationMembership($organizationMembershipId, $roleSlug = null)
public function updateOrganizationMembership($organizationMembershipId, $roleSlug = null, $roleSlugs = null)
{
$path = "user_management/organization_memberships/{$organizationMembershipId}";

$params = [
"role_slug" => $roleSlug
];
$params = [];

if (!is_null($roleSlug)) {
$params["role_slug"] = $roleSlug;
}

if (!is_null($roleSlugs)) {
$params["role_slugs"] = $roleSlugs;
}

$response = Client::request(
Client::METHOD_PUT,
Expand Down
70 changes: 69 additions & 1 deletion tests/WorkOS/UserManagementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@ public function testCreateOrganizationMembership()
$params = [
"organization_id" => $orgId,
"user_id" => $userId,
"role_slug" => $roleSlug
"role_slug" => $roleSlug,
];

$this->mockRequest(
Expand All @@ -912,6 +912,37 @@ public function testCreateOrganizationMembership()
$this->assertSame($organizationMembership, $response->toArray());
}

public function testCreateOrganizationMembershipWithRoleSlugs()
{
$userId = "user_01H7X1M4TZJN5N4HG4XXMA1234";
$orgId = "org_01EHQMYV6MBK39QC5PZXHY59C3";
$roleSlugs = ["admin"];
$path = "user_management/organization_memberships";

$result = $this->organizationMembershipResponseFixture();

$params = [
"organization_id" => $orgId,
"user_id" => $userId,
"role_slugs" => $roleSlugs,
];

$this->mockRequest(
Client::METHOD_POST,
$path,
null,
$params,
true,
$result
);

$organizationMembership = $this->organizationMembershipFixture();

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

$this->assertSame($organizationMembership, $response->toArray());
}

public function testGetOrganizationMembership()
{
$organizationMembershipId = "om_01E4ZCR3C56J083X43JQXF3JK5";
Expand Down Expand Up @@ -1080,6 +1111,28 @@ public function testUpdateOrganizationMembership()
$this->assertSame($this->organizationMembershipFixture(), $response->toArray());
}

public function testUpdateOrganizationMembershipWithRoleSlugs()
{
$organizationMembershipId = "om_01E4ZCR3C56J083X43JQXF3JK5";
$roleSlugs = ["admin"];
$path = "user_management/organization_memberships/{$organizationMembershipId}";

$result = $this->organizationMembershipResponseFixture();

$this->mockRequest(
Client::METHOD_PUT,
$path,
null,
["role_slugs" => $roleSlugs],
true,
$result
);

$response = $this->userManagement->updateOrganizationMembership($organizationMembershipId, null, $roleSlugs);
$this->assertSame($this->organizationMembershipFixture(), $response->toArray());
}


public function testDeactivateOrganizationMembership()
{
$organizationMembershipId = "om_01E4ZCR3C56J083X43JQXF3JK5";
Expand Down Expand Up @@ -1396,6 +1449,11 @@ private function organizationMembershipResponseFixture($status = "active")
"role" => [
"slug" => "admin",
],
"roles" => [
[
"slug" => "admin",
],
],
"status" => $status,
"created_at" => "2021-06-25T19:07:33.155Z",
"updated_at" => "2021-06-25T19:07:33.155Z",
Expand All @@ -1415,6 +1473,11 @@ private function organizationMembershipListResponseFixture()
"role" => [
"slug" => "admin",
],
"roles" => [
[
"slug" => "admin",
]
],
"status" => "active",
"created_at" => "2021-06-25T19:07:33.155Z",
"updated_at" => "2021-06-25T19:07:33.155Z",
Expand All @@ -1438,6 +1501,11 @@ private function organizationMembershipFixture()
"role" => [
"slug" => "admin",
],
"roles" => [
[
"slug" => "admin",
],
],
"status" => "active",
"createdAt" => "2021-06-25T19:07:33.155Z",
"updatedAt" => "2021-06-25T19:07:33.155Z",
Expand Down