Skip to content

Commit 505bdee

Browse files
gjtorikianclaude
andcommitted
refactor: Use PaginatedResource for RBAC list methods
Migrate listPermissions and listEnvironmentRoles to use PaginatedResource from #316. Also adds pagination params to listEnvironmentRoles and restores type hint on getPermission. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent e03d6ee commit 505bdee

2 files changed

Lines changed: 38 additions & 22 deletions

File tree

lib/RBAC.php

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function createPermission(
5353
*
5454
* @throws Exception\WorkOSException
5555
*
56-
* @return array{?string, ?string, Resource\Permission[]}
56+
* @return Resource\PaginatedResource
5757
*/
5858
public function listPermissions(
5959
int $limit = self::DEFAULT_PAGE_SIZE,
@@ -72,13 +72,7 @@ public function listPermissions(
7272

7373
$response = Client::request(Client::METHOD_GET, $path, null, $params, true);
7474

75-
$permissions = [];
76-
list($before, $after) = Util\Request::parsePaginationArgs($response);
77-
foreach ($response["data"] as $responseData) {
78-
\array_push($permissions, Resource\Permission::constructFromResponse($responseData));
79-
}
80-
81-
return [$before, $after, $permissions];
75+
return Resource\PaginatedResource::constructFromResponse($response, Resource\Permission::class, 'permissions');
8276
}
8377

8478
/**
@@ -189,22 +183,33 @@ public function createEnvironmentRole(
189183
/**
190184
* List Environment Roles.
191185
*
186+
* @param int $limit Maximum number of records to return
187+
* @param null|string $before Role ID to look before
188+
* @param null|string $after Role ID to look after
189+
* @param null|string $order The order in which to paginate records
190+
*
192191
* @throws Exception\WorkOSException
193192
*
194-
* @return Resource\Role[]
193+
* @return Resource\PaginatedResource
195194
*/
196-
public function listEnvironmentRoles()
197-
{
195+
public function listEnvironmentRoles(
196+
int $limit = self::DEFAULT_PAGE_SIZE,
197+
?string $before = null,
198+
?string $after = null,
199+
?string $order = null
200+
) {
198201
$path = "authorization/roles";
199202

200-
$response = Client::request(Client::METHOD_GET, $path, null, null, true);
203+
$params = [
204+
"limit" => $limit,
205+
"before" => $before,
206+
"after" => $after,
207+
"order" => $order,
208+
];
201209

202-
$roles = [];
203-
foreach ($response["data"] as $responseData) {
204-
\array_push($roles, Resource\Role::constructFromResponse($responseData));
205-
}
210+
$response = Client::request(Client::METHOD_GET, $path, null, $params, true);
206211

207-
return $roles;
212+
return Resource\PaginatedResource::constructFromResponse($response, Resource\Role::class, 'roles');
208213
}
209214

210215
/**

tests/WorkOS/RBACTest.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ public function testListPermissions()
7171

7272
$permission = $this->permissionFixture();
7373

74-
list($before, $after, $permissions) = $this->rbac->listPermissions();
75-
$this->assertSame($permission, $permissions[0]->toArray());
74+
$response = $this->rbac->listPermissions();
75+
$this->assertSame($permission, $response->permissions[0]->toArray());
7676
}
7777

7878
public function testGetPermission()
@@ -170,19 +170,26 @@ public function testListEnvironmentRoles()
170170

171171
$result = $this->rolesListResponseFixture();
172172

173+
$params = [
174+
"limit" => RBAC::DEFAULT_PAGE_SIZE,
175+
"before" => null,
176+
"after" => null,
177+
"order" => null,
178+
];
179+
173180
$this->mockRequest(
174181
Client::METHOD_GET,
175182
$path,
176183
null,
177-
null,
184+
$params,
178185
true,
179186
$result
180187
);
181188

182189
$role = $this->roleFixture();
183190

184-
$roles = $this->rbac->listEnvironmentRoles();
185-
$this->assertSame($role, $roles[0]->toArray());
191+
$response = $this->rbac->listEnvironmentRoles();
192+
$this->assertSame($role, $response->roles[0]->toArray());
186193
}
187194

188195
public function testGetEnvironmentRole()
@@ -369,6 +376,10 @@ private function rolesListResponseFixture()
369376
"created_at" => "2024-01-01T00:00:00.000Z",
370377
"updated_at" => "2024-01-01T00:00:00.000Z"
371378
]
379+
],
380+
"list_metadata" => [
381+
"before" => null,
382+
"after" => null
372383
]
373384
]);
374385
}

0 commit comments

Comments
 (0)