|
| 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 | +} |
0 commit comments