Skip to content

Commit

Permalink
Add GET /organization/:orgId/roles support.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattgd committed Dec 24, 2024
1 parent 303d9e7 commit e406359
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/Organizations.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,33 @@ public function deleteOrganization($organization)

return $response;
}

/**
* List roles for an organization.
*
* @param string $organizationId WorkOS organization ID to fetch roles for
*
* @throws Exception\WorkOSException
*
* @return array An array containing the following:
* array \WorkOS\Resource\Role instances
*/
public function listOrganizationRoles($organizationId) {
$organizationRolesPath = "organizations/{$organizationId}/roles";

$response = Client::request(
Client::METHOD_GET,
$organizationRolesPath,
null,
null,
true
);

$roles = [];
foreach ($response["data"] as $responseData) {
\array_push($roles, Resource\Role::constructFromResponse($responseData));
}

return [$roles];
}
}
28 changes: 28 additions & 0 deletions lib/Resource/Role.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace WorkOS\Resource;

/**
* Class Role.
*/

class Role extends BaseWorkOSResource
{
public const RESOURCE_TYPE = "role";

public const RESOURCE_ATTRIBUTES = [
"id",
"name",
"slug",
"description",
"type"
];

public const RESPONSE_TO_RESOURCE_KEY = [
"id" => "id",
"name" => "name",
"slug" => "slug",
"description" => "description",
"type" => "type"
];
}
73 changes: 73 additions & 0 deletions tests/WorkOS/OrganizationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,27 @@ public function testListOrganizations()
$this->assertSame($organization, $organizations[0]->toArray());
}

public function testListOrganizationRoles()
{
$organizationRolesPath = "organizations/org_01EHQMYV6MBK39QC5PZXHY59C3/roles";

$result = $this->organizationRolesResponseFixture();

$this->mockRequest(
Client::METHOD_GET,
$organizationRolesPath,
null,
null,
true,
$result
);

$role = $this->roleFixture();

list($roles) = $this->roles->listOrganizationRoles("org_01EHQMYV6MBK39QC5PZXHY59C3");
$this->assertSame($role, $roles[0]->toArray());
}

// Fixtures

private function createOrganizationResponseFixture()
Expand Down Expand Up @@ -247,4 +268,56 @@ private function organizationFixture()
]
];
}

private function organizationRolesResponseFixture()
{
return json_encode([
"object" => "list",
"data" => [
[
"object" => "role",
"id" => "role_01EHQMYV6MBK39QC5PZXHY59C2",
"name" => "Admin",
"slug" => "admin",
"description" => "Admin role",
"type" => "EnvironmentRole",
"created_at" => "2024-01-01T00:00:00.000Z",
"updated_at" => "2024-01-01T00:00:00.000Z"
],
[
"object" => "role",
"id" => "role_01EHQMYV6MBK39QC5PZXHY59C3",
"name" => "Member",
"slug" => "member",
"description" => "Member role",
"type" => "EnvironmentRole",
"created_at" => "2024-01-01T00:00:00.000Z",
"updated_at" => "2024-01-01T00:00:00.000Z"
],
[
"object" => "role",
"id" => "role_01EHQMYV6MBK39QC5PZXHY59C3",
"name" => "Org. Member",
"slug" => "org-member",
"description" => "Organization member role",
"type" => "OrganizationRole",
"created_at" => "2024-01-01T00:00:00.000Z",
"updated_at" => "2024-01-01T00:00:00.000Z"
],
],
]);
}

private function roleFixture()
{
return [
"id" => "role_01EHQMYV6MBK39QC5PZXHY59C3",
"name" => "Member",
"slug" => "member",
"description" => "Member role",
"type" => "EnvironmentRole",
"created_at" => "2024-01-01T00:00:00.000Z",
"updated_at" => "2024-01-01T00:00:00.000Z"
];
}
}

0 comments on commit e406359

Please sign in to comment.