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
1 change: 1 addition & 0 deletions lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Client
public const METHOD_POST = "post";
public const METHOD_DELETE = "delete";
public const METHOD_PUT = "put";
public const METHOD_PATCH = "patch";

private static $_requestClient;

Expand Down
151 changes: 151 additions & 0 deletions lib/RBAC.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php

namespace WorkOS;

class RBAC
{
public const DEFAULT_PAGE_SIZE = 10;

/**
* Create a Permission.
*
* @param string $slug The slug of the Permission
* @param string $name The name of the Permission
* @param null|string $description The description of the Permission
* @param null|string $resourceTypeSlug The resource type slug of the Permission
*
* @throws Exception\WorkOSException
*
* @return Resource\Permission
*/
public function createPermission(
string $slug,
string $name,
?string $description = null,
?string $resourceTypeSlug = null
) {
$path = "authorization/permissions";

$params = [
"slug" => $slug,
"name" => $name,
];

if (isset($description)) {
$params["description"] = $description;
}
if (isset($resourceTypeSlug)) {
$params["resource_type_slug"] = $resourceTypeSlug;
}

$response = Client::request(Client::METHOD_POST, $path, null, $params, true);

return Resource\Permission::constructFromResponse($response);
}

/**
* List Permissions.
*
* @param int $limit Maximum number of records to return
* @param null|string $before Permission ID to look before
* @param null|string $after Permission ID to look after
* @param null|string $order The order in which to paginate records
*
* @throws Exception\WorkOSException
*
* @return array{?string, ?string, Resource\Permission[]}
*/
public function listPermissions(
int $limit = self::DEFAULT_PAGE_SIZE,
?string $before = null,
?string $after = null,
?string $order = null
) {
$path = "authorization/permissions";

$params = [
"limit" => $limit,
"before" => $before,
"after" => $after,
"order" => $order,
];

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

$permissions = [];
list($before, $after) = Util\Request::parsePaginationArgs($response);
foreach ($response["data"] as $responseData) {
\array_push($permissions, Resource\Permission::constructFromResponse($responseData));
}

return [$before, $after, $permissions];
}

/**
* Get a Permission.
*
* @param string $slug The slug of the Permission
*
* @throws Exception\WorkOSException
*
* @return Resource\Permission
*/
public function getPermission(string $slug)
{
$path = "authorization/permissions/{$slug}";

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

return Resource\Permission::constructFromResponse($response);
}

/**
* Update a Permission.
*
* @param string $slug The slug of the Permission to update
* @param null|string $name The updated name of the Permission
* @param null|string $description The updated description of the Permission
*
* @throws Exception\WorkOSException
*
* @return Resource\Permission
*/
public function updatePermission(
string $slug,
?string $name = null,
?string $description = null
) {
$path = "authorization/permissions/{$slug}";

$params = [];

if (isset($name)) {
$params["name"] = $name;
}
if (isset($description)) {
$params["description"] = $description;
}

$response = Client::request(Client::METHOD_PATCH, $path, null, $params, true);

return Resource\Permission::constructFromResponse($response);
}

/**
* Delete a Permission.
*
* @param string $slug The slug of the Permission to delete
*
* @throws Exception\WorkOSException
*
* @return array
*/
public function deletePermission(string $slug)
{
$path = "authorization/permissions/{$slug}";

$response = Client::request(Client::METHOD_DELETE, $path, null, null, true);

return $response;
}
}
9 changes: 9 additions & 0 deletions lib/RequestClient/CurlRequestClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ public function request($method, $url, ?array $headers = null, ?array $params =
}

break;

case Client::METHOD_PATCH:
\array_push($headers, "Content-Type: application/json");
$opts[\CURLOPT_CUSTOMREQUEST] = 'PATCH';
$opts[\CURLOPT_POST] = 1;
if (!empty($params)) {
$opts[\CURLOPT_POSTFIELDS] = \json_encode($params);
}
break;
}

$opts[\CURLOPT_HTTPHEADER] = $headers;
Expand Down
43 changes: 43 additions & 0 deletions lib/Resource/Permission.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace WorkOS\Resource;

/**
* Class Permission.
*
* @property string $id
* @property string $slug
* @property string $name
* @property string $description
* @property string $resource_type_slug
* @property bool $system
* @property string $created_at
* @property string $updated_at
*/

class Permission extends BaseWorkOSResource
{
public const RESOURCE_TYPE = "permission";

public const RESOURCE_ATTRIBUTES = [
"id",
"slug",
"name",
"description",
"resource_type_slug",
"system",
"created_at",
"updated_at"
];

public const RESPONSE_TO_RESOURCE_KEY = [
"id" => "id",
"slug" => "slug",
"name" => "name",
"description" => "description",
"resource_type_slug" => "resource_type_slug",
"system" => "system",
"created_at" => "created_at",
"updated_at" => "updated_at"
];
}
Loading