diff --git a/lib/Organizations.php b/lib/Organizations.php index 9850196..5284f39 100644 --- a/lib/Organizations.php +++ b/lib/Organizations.php @@ -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]; + } } diff --git a/lib/Resource/Role.php b/lib/Resource/Role.php new file mode 100644 index 0000000..63ff00f --- /dev/null +++ b/lib/Resource/Role.php @@ -0,0 +1,28 @@ + "id", + "name" => "name", + "slug" => "slug", + "description" => "description", + "type" => "type" + ]; +} diff --git a/tests/WorkOS/OrganizationsTest.php b/tests/WorkOS/OrganizationsTest.php index 856bee6..c88fc44 100644 --- a/tests/WorkOS/OrganizationsTest.php +++ b/tests/WorkOS/OrganizationsTest.php @@ -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() @@ -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" + ]; + } }