Skip to content

Commit dd7dca2

Browse files
csrbarberclaudegjtorikian
authored
feat: Add RBAC permissions API support (#337)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Garen J. Torikian <gjtorikian@users.noreply.github.com>
1 parent 0e9fb95 commit dd7dca2

5 files changed

Lines changed: 398 additions & 0 deletions

File tree

lib/Client.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Client
1313
public const METHOD_POST = "post";
1414
public const METHOD_DELETE = "delete";
1515
public const METHOD_PUT = "put";
16+
public const METHOD_PATCH = "patch";
1617

1718
private static $_requestClient;
1819

lib/RBAC.php

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?php
2+
3+
namespace WorkOS;
4+
5+
class RBAC
6+
{
7+
public const DEFAULT_PAGE_SIZE = 10;
8+
9+
/**
10+
* Create a Permission.
11+
*
12+
* @param string $slug The slug of the Permission
13+
* @param string $name The name of the Permission
14+
* @param null|string $description The description of the Permission
15+
* @param null|string $resourceTypeSlug The resource type slug of the Permission
16+
*
17+
* @throws Exception\WorkOSException
18+
*
19+
* @return Resource\Permission
20+
*/
21+
public function createPermission(
22+
string $slug,
23+
string $name,
24+
?string $description = null,
25+
?string $resourceTypeSlug = null
26+
) {
27+
$path = "authorization/permissions";
28+
29+
$params = [
30+
"slug" => $slug,
31+
"name" => $name,
32+
];
33+
34+
if (isset($description)) {
35+
$params["description"] = $description;
36+
}
37+
if (isset($resourceTypeSlug)) {
38+
$params["resource_type_slug"] = $resourceTypeSlug;
39+
}
40+
41+
$response = Client::request(Client::METHOD_POST, $path, null, $params, true);
42+
43+
return Resource\Permission::constructFromResponse($response);
44+
}
45+
46+
/**
47+
* List Permissions.
48+
*
49+
* @param int $limit Maximum number of records to return
50+
* @param null|string $before Permission ID to look before
51+
* @param null|string $after Permission ID to look after
52+
* @param null|string $order The order in which to paginate records
53+
*
54+
* @throws Exception\WorkOSException
55+
*
56+
* @return array{?string, ?string, Resource\Permission[]}
57+
*/
58+
public function listPermissions(
59+
int $limit = self::DEFAULT_PAGE_SIZE,
60+
?string $before = null,
61+
?string $after = null,
62+
?string $order = null
63+
) {
64+
$path = "authorization/permissions";
65+
66+
$params = [
67+
"limit" => $limit,
68+
"before" => $before,
69+
"after" => $after,
70+
"order" => $order,
71+
];
72+
73+
$response = Client::request(Client::METHOD_GET, $path, null, $params, true);
74+
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];
82+
}
83+
84+
/**
85+
* Get a Permission.
86+
*
87+
* @param string $slug The slug of the Permission
88+
*
89+
* @throws Exception\WorkOSException
90+
*
91+
* @return Resource\Permission
92+
*/
93+
public function getPermission(string $slug)
94+
{
95+
$path = "authorization/permissions/{$slug}";
96+
97+
$response = Client::request(Client::METHOD_GET, $path, null, null, true);
98+
99+
return Resource\Permission::constructFromResponse($response);
100+
}
101+
102+
/**
103+
* Update a Permission.
104+
*
105+
* @param string $slug The slug of the Permission to update
106+
* @param null|string $name The updated name of the Permission
107+
* @param null|string $description The updated description of the Permission
108+
*
109+
* @throws Exception\WorkOSException
110+
*
111+
* @return Resource\Permission
112+
*/
113+
public function updatePermission(
114+
string $slug,
115+
?string $name = null,
116+
?string $description = null
117+
) {
118+
$path = "authorization/permissions/{$slug}";
119+
120+
$params = [];
121+
122+
if (isset($name)) {
123+
$params["name"] = $name;
124+
}
125+
if (isset($description)) {
126+
$params["description"] = $description;
127+
}
128+
129+
$response = Client::request(Client::METHOD_PATCH, $path, null, $params, true);
130+
131+
return Resource\Permission::constructFromResponse($response);
132+
}
133+
134+
/**
135+
* Delete a Permission.
136+
*
137+
* @param string $slug The slug of the Permission to delete
138+
*
139+
* @throws Exception\WorkOSException
140+
*
141+
* @return array
142+
*/
143+
public function deletePermission(string $slug)
144+
{
145+
$path = "authorization/permissions/{$slug}";
146+
147+
$response = Client::request(Client::METHOD_DELETE, $path, null, null, true);
148+
149+
return $response;
150+
}
151+
}

lib/RequestClient/CurlRequestClient.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ public function request($method, $url, ?array $headers = null, ?array $params =
7171
}
7272

7373
break;
74+
75+
case Client::METHOD_PATCH:
76+
\array_push($headers, "Content-Type: application/json");
77+
$opts[\CURLOPT_CUSTOMREQUEST] = 'PATCH';
78+
$opts[\CURLOPT_POST] = 1;
79+
if (!empty($params)) {
80+
$opts[\CURLOPT_POSTFIELDS] = \json_encode($params);
81+
}
82+
break;
7483
}
7584

7685
$opts[\CURLOPT_HTTPHEADER] = $headers;

lib/Resource/Permission.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace WorkOS\Resource;
4+
5+
/**
6+
* Class Permission.
7+
*
8+
* @property string $id
9+
* @property string $slug
10+
* @property string $name
11+
* @property string $description
12+
* @property string $resource_type_slug
13+
* @property bool $system
14+
* @property string $created_at
15+
* @property string $updated_at
16+
*/
17+
18+
class Permission extends BaseWorkOSResource
19+
{
20+
public const RESOURCE_TYPE = "permission";
21+
22+
public const RESOURCE_ATTRIBUTES = [
23+
"id",
24+
"slug",
25+
"name",
26+
"description",
27+
"resource_type_slug",
28+
"system",
29+
"created_at",
30+
"updated_at"
31+
];
32+
33+
public const RESPONSE_TO_RESOURCE_KEY = [
34+
"id" => "id",
35+
"slug" => "slug",
36+
"name" => "name",
37+
"description" => "description",
38+
"resource_type_slug" => "resource_type_slug",
39+
"system" => "system",
40+
"created_at" => "created_at",
41+
"updated_at" => "updated_at"
42+
];
43+
}

0 commit comments

Comments
 (0)