diff --git a/COMMANDS.md b/COMMANDS.md index 3c23191..078e87c 100644 --- a/COMMANDS.md +++ b/COMMANDS.md @@ -598,27 +598,41 @@ $teamspeak->channelPermissions()->deleteForClient(channelId: 1, clientDatabaseId ## permissionlist -Not implemented. +```php +$teamspeak->permissions()->list(); +``` ## permidgetbyname -Not implemented. +```php +$teamspeak->permissions()->getIdByName(name: 'b_serverinstance_help_view'); +``` ## permoverview -Not implemented. +```php +$teamspeak->permissions()->overview(channelId: 1, clientDatabaseId: 2); +``` ## permget -Not implemented. +```php +$teamspeak->permissions()->get(id: 17835); +$teamspeak->permissions()->get(id: 'b_serverinstance_help_view'); +``` ## permfind -Not implemented. +```php +$teamspeak->permissions()->find(id: 17835); +$teamspeak->permissions()->find(id: 'b_serverinstance_help_view'); +``` ## permreset -Not implemented. +```php +$teamspeak->permissions()->reset(); +``` ## privilegekeylist diff --git a/src/Contracts/Resources/PermissionsContract.php b/src/Contracts/Resources/PermissionsContract.php index 0365c28..515a687 100644 --- a/src/Contracts/Resources/PermissionsContract.php +++ b/src/Contracts/Resources/PermissionsContract.php @@ -4,4 +4,48 @@ namespace TeamSpeak\WebQuery\Contracts\Resources; -interface PermissionsContract {} +use TeamSpeak\WebQuery\Responses\Permissions\FindResponse; +use TeamSpeak\WebQuery\Responses\Permissions\GetIdByNameResponse; +use TeamSpeak\WebQuery\Responses\Permissions\GetResponse; +use TeamSpeak\WebQuery\Responses\Permissions\ListResponse; +use TeamSpeak\WebQuery\Responses\Permissions\OverviewResponse; +use TeamSpeak\WebQuery\Responses\Permissions\ResetResponse; + +interface PermissionsContract +{ + /** + * Displays a list of all permissions available on the server. + */ + public function list(): ListResponse; + + /** + * Returns the ID of a permission specified by name. + */ + public function getIdByName(string $name): GetIdByNameResponse; + + /** + * Displays all permissions for a client in a specific channel. + */ + public function overview(int $channelId, int $clientDatabaseId): OverviewResponse; + + /** + * Displays the current value of a permission for the selected virtual server. + * + * A permission can be specified by ID or name. + */ + public function get(string|int $id): GetResponse; + + /** + * Displays all clients and groups assigned a specific permission. + * + * A permission can be specified by ID or name. + */ + public function find(string|int $id): FindResponse; + + /** + * Resets the permission settings of the virtual server to default values. + * + * Returns a new administrator privilege key. + */ + public function reset(): ResetResponse; +} diff --git a/src/Resources/Permissions.php b/src/Resources/Permissions.php index 4c1a334..e321273 100644 --- a/src/Resources/Permissions.php +++ b/src/Resources/Permissions.php @@ -5,8 +5,118 @@ namespace TeamSpeak\WebQuery\Resources; use TeamSpeak\WebQuery\Contracts\Resources\PermissionsContract; +use TeamSpeak\WebQuery\Enums\Query\Command; +use TeamSpeak\WebQuery\Responses\Permissions\FindResponse; +use TeamSpeak\WebQuery\Responses\Permissions\GetIdByNameResponse; +use TeamSpeak\WebQuery\Responses\Permissions\GetResponse; +use TeamSpeak\WebQuery\Responses\Permissions\ListResponse; +use TeamSpeak\WebQuery\Responses\Permissions\OverviewResponse; +use TeamSpeak\WebQuery\Responses\Permissions\ResetResponse; +use TeamSpeak\WebQuery\ValueObjects\Transporter\Payload; final class Permissions implements PermissionsContract { use Concerns\Transportable; + + /** + * Displays a list of all permissions available on the server. + */ + public function list(): ListResponse + { + $payload = new Payload(Command::PermissionList); + + /** @var \TeamSpeak\WebQuery\ValueObjects\Transporter\Response> $response */ + $response = $this->transporter->request($payload); + + return ListResponse::from($response->body()); + } + + /** + * Returns the ID of a permission specified by name. + */ + public function getIdByName(string $name): GetIdByNameResponse + { + $payload = new Payload( + command: Command::PermIdGetByName, + parameters: ['permsid' => $name], + ); + + /** @var \TeamSpeak\WebQuery\ValueObjects\Transporter\Response $response */ + $response = $this->transporter->request($payload); + + return GetIdByNameResponse::from($response->body()); + } + + /** + * Displays all permissions for a client in a specific channel. + */ + public function overview(int $channelId, int $clientDatabaseId): OverviewResponse + { + $payload = new Payload( + command: Command::PermOverview, + parameters: ['cid' => $channelId, 'cldbid' => $clientDatabaseId], + ); + + /** @var \TeamSpeak\WebQuery\ValueObjects\Transporter\Response> $response */ + $response = $this->transporter->request($payload); + + return OverviewResponse::from($response->body()); + } + + /** + * Displays the current value of a permission for the selected virtual server. + * + * A permission can be specified by ID or name. + */ + public function get(string|int $id): GetResponse + { + $payload = new Payload( + command: Command::PermGet, + parameters: [ + ...is_int($id) ? ['permid' => $id] : [], + ...is_string($id) ? ['permsid' => $id] : [], + ], + ); + + /** @var \TeamSpeak\WebQuery\ValueObjects\Transporter\Response $response */ + $response = $this->transporter->request($payload); + + return GetResponse::from($response->body()); + } + + /** + * Displays all clients and groups assigned a specific permission. + * + * A permission can be specified by ID or name. + */ + public function find(string|int $id): FindResponse + { + $payload = new Payload( + command: Command::PermFind, + parameters: [ + ...is_int($id) ? ['permid' => $id] : [], + ...is_string($id) ? ['permsid' => $id] : [], + ], + ); + + /** @var \TeamSpeak\WebQuery\ValueObjects\Transporter\Response> $response */ + $response = $this->transporter->request($payload); + + return FindResponse::from($response->body()); + } + + /** + * Resets the permission settings of the virtual server to default values. + * + * Returns a new administrator privilege key. + */ + public function reset(): ResetResponse + { + $payload = new Payload(Command::PermReset); + + /** @var \TeamSpeak\WebQuery\ValueObjects\Transporter\Response $response */ + $response = $this->transporter->request($payload); + + return ResetResponse::from($response->body()); + } } diff --git a/src/Responses/Permissions/FindResponse.php b/src/Responses/Permissions/FindResponse.php new file mode 100644 index 0000000..a57b58b --- /dev/null +++ b/src/Responses/Permissions/FindResponse.php @@ -0,0 +1,25 @@ + $assignments + */ + private function __construct( + public array $assignments, + ) {} + + /** + * @param list $attributes + */ + public static function from(array $attributes): self + { + return new self( + array_map(FindResponseAssignment::from(...), $attributes), + ); + } +} diff --git a/src/Responses/Permissions/FindResponseAssignment.php b/src/Responses/Permissions/FindResponseAssignment.php new file mode 100644 index 0000000..b73551d --- /dev/null +++ b/src/Responses/Permissions/FindResponseAssignment.php @@ -0,0 +1,28 @@ + $permissions + */ + private function __construct( + public array $permissions, + ) {} + + /** + * @param list $attributes + */ + public static function from(array $attributes): self + { + return new self( + array_map(ListResponsePermission::from(...), $attributes), + ); + } +} diff --git a/src/Responses/Permissions/ListResponsePermission.php b/src/Responses/Permissions/ListResponsePermission.php new file mode 100644 index 0000000..2026fef --- /dev/null +++ b/src/Responses/Permissions/ListResponsePermission.php @@ -0,0 +1,30 @@ + $permissions + */ + private function __construct( + public array $permissions, + ) {} + + /** + * @param list $attributes + */ + public static function from(array $attributes): self + { + return new self( + array_map(OverviewResponsePermission::from(...), $attributes), + ); + } +} diff --git a/src/Responses/Permissions/OverviewResponsePermission.php b/src/Responses/Permissions/OverviewResponsePermission.php new file mode 100644 index 0000000..9e4a6c6 --- /dev/null +++ b/src/Responses/Permissions/OverviewResponsePermission.php @@ -0,0 +1,34 @@ +client = Mockery::mock(ClientInterface::class); + + $apiKey = ApiKey::from('foo'); + + $this->http = new HttpTransporter( + $this->client, + BaseUri::from('teamspeak.com'), + Headers::withXApiKey($apiKey), + ); +}); + +test('list', function () { + $resource = new Permissions($this->http); + + $response = new Response(200, ['Content-Type' => 'application/json'], json_encode([ + 'body' => [[ + 'permid' => '1', + 'permname' => 'b_serverinstance_help_view', + 'permdesc' => 'Retrieve information about the server instance', + 'permtype' => '0', + 'permisflag' => '1', + ]], + 'status' => ['code' => 0, 'message' => 'ok'], + ])); + + $this->client + ->shouldReceive('sendRequest') + ->once() + ->withArgs(function (Psr7Request $request) { + expect($request->getBody()->getContents())->toBe(''); + + return true; + })->andReturn($response); + + expect($resource->list()->permissions)->toBeArray()->toHaveCount(1); +}); + +test('get id by name', function () { + $resource = new Permissions($this->http); + + $response = new Response(200, ['Content-Type' => 'application/json'], json_encode([ + 'body' => [[ + 'permid' => '1', + 'permsid' => 'b_serverinstance_help_view', + ]], + 'status' => ['code' => 0, 'message' => 'ok'], + ])); + + $this->client + ->shouldReceive('sendRequest') + ->once() + ->withArgs(function (Psr7Request $request) { + expect($request->getBody()->getContents())->toBeJson() + ->json() + ->toBe(['permsid' => 'b_serverinstance_help_view']); + + return true; + })->andReturn($response); + + $result = $resource->getIdByName('b_serverinstance_help_view'); + expect($result->id)->toBe(1) + ->and($result->name)->toBe('b_serverinstance_help_view'); +}); + +test('overview', function () { + $resource = new Permissions($this->http); + + $response = new Response(200, ['Content-Type' => 'application/json'], json_encode([ + 'body' => [[ + 't' => '0', + 'id1' => '0', + 'id2' => '0', + 'p' => '17', + 'v' => '1', + 'n' => '0', + 's' => '0', + ]], + 'status' => ['code' => 0, 'message' => 'ok'], + ])); + + $this->client + ->shouldReceive('sendRequest') + ->once() + ->withArgs(function (Psr7Request $request) { + expect($request->getBody()->getContents())->toBeJson() + ->json() + ->toBe(['cid' => '1', 'cldbid' => '2']); + + return true; + })->andReturn($response); + + expect($resource->overview(1, 2)->permissions)->toBeArray()->toHaveCount(1); +}); + +test('get by ID', function () { + $resource = new Permissions($this->http); + + $response = new Response(200, ['Content-Type' => 'application/json'], json_encode([ + 'body' => [[ + 'permid' => '17835', + 'permsid' => 'b_serverinstance_help_view', + 'permvalue' => '1', + ]], + 'status' => ['code' => 0, 'message' => 'ok'], + ])); + + $this->client + ->shouldReceive('sendRequest') + ->once() + ->withArgs(function (Psr7Request $request) { + expect($request->getBody()->getContents())->toBeJson() + ->json() + ->toBe(['permid' => '17835']); + + return true; + })->andReturn($response); + + $result = $resource->get(17835); + expect($result->id)->toBe(17835) + ->and($result->value)->toBe(1); +}); + +test('get by name', function () { + $resource = new Permissions($this->http); + + $response = new Response(200, ['Content-Type' => 'application/json'], json_encode([ + 'body' => [[ + 'permid' => '17835', + 'permsid' => 'b_serverinstance_help_view', + 'permvalue' => '1', + ]], + 'status' => ['code' => 0, 'message' => 'ok'], + ])); + + $this->client + ->shouldReceive('sendRequest') + ->once() + ->withArgs(function (Psr7Request $request) { + expect($request->getBody()->getContents())->toBeJson() + ->json() + ->toBe(['permsid' => 'b_serverinstance_help_view']); + + return true; + })->andReturn($response); + + $result = $resource->get('b_serverinstance_help_view'); + expect($result->name)->toBe('b_serverinstance_help_view'); +}); + +test('find by ID', function () { + $resource = new Permissions($this->http); + + $response = new Response(200, ['Content-Type' => 'application/json'], json_encode([ + 'body' => [[ + 't' => '0', + 'id1' => '0', + 'id2' => '0', + 'p' => '17835', + ]], + 'status' => ['code' => 0, 'message' => 'ok'], + ])); + + $this->client + ->shouldReceive('sendRequest') + ->once() + ->withArgs(function (Psr7Request $request) { + expect($request->getBody()->getContents())->toBeJson() + ->json() + ->toBe(['permid' => '17835']); + + return true; + })->andReturn($response); + + expect($resource->find(17835)->assignments)->toHaveCount(1); +}); + +test('find by name', function () { + $resource = new Permissions($this->http); + + $response = new Response(200, ['Content-Type' => 'application/json'], json_encode([ + 'body' => [[ + 't' => '0', + 'id1' => '0', + 'id2' => '0', + 'p' => '17835', + ]], + 'status' => ['code' => 0, 'message' => 'ok'], + ])); + + $this->client + ->shouldReceive('sendRequest') + ->once() + ->withArgs(function (Psr7Request $request) { + expect($request->getBody()->getContents())->toBeJson() + ->json() + ->toBe(['permsid' => 'b_serverinstance_help_view']); + + return true; + })->andReturn($response); + + expect($resource->find('b_serverinstance_help_view')->assignments)->toHaveCount(1); +}); + +test('reset', function () { + $resource = new Permissions($this->http); + + $response = new Response(200, ['Content-Type' => 'application/json'], json_encode([ + 'body' => [[ + 'token' => 'eKnFZQ9EK7G7EmPvt1Ch7vsXi5Uq+1Us7xrQKBVsMxM=', + ]], + 'status' => ['code' => 0, 'message' => 'ok'], + ])); + + $this->client + ->shouldReceive('sendRequest') + ->once() + ->withArgs(function (Psr7Request $request) { + expect($request->getBody()->getContents())->toBe(''); + + return true; + })->andReturn($response); + + expect($resource->reset()->token)->toBe('eKnFZQ9EK7G7EmPvt1Ch7vsXi5Uq+1Us7xrQKBVsMxM='); +}); diff --git a/tests/Unit/Responses/Permissions/FindResponseAssignmentTest.php b/tests/Unit/Responses/Permissions/FindResponseAssignmentTest.php new file mode 100644 index 0000000..2b0e675 --- /dev/null +++ b/tests/Unit/Responses/Permissions/FindResponseAssignmentTest.php @@ -0,0 +1,19 @@ + '2', + 'id1' => '6', + 'id2' => '3', + 'p' => '17835', + ]); + + expect($assignment->type)->toBe(2) + ->and($assignment->id1)->toBe(6) + ->and($assignment->id2)->toBe(3) + ->and($assignment->permId)->toBe(17835); +}); diff --git a/tests/Unit/Responses/Permissions/FindResponseTest.php b/tests/Unit/Responses/Permissions/FindResponseTest.php new file mode 100644 index 0000000..422ddba --- /dev/null +++ b/tests/Unit/Responses/Permissions/FindResponseTest.php @@ -0,0 +1,18 @@ + '0', + 'id1' => '0', + 'id2' => '0', + 'p' => '17835', + ]]); + + expect($response->assignments)->toHaveCount(1) + ->and($response->assignments[0])->toBeInstanceOf(FindResponseAssignment::class); +}); diff --git a/tests/Unit/Responses/Permissions/GetIdByNameResponseTest.php b/tests/Unit/Responses/Permissions/GetIdByNameResponseTest.php new file mode 100644 index 0000000..08e1ffa --- /dev/null +++ b/tests/Unit/Responses/Permissions/GetIdByNameResponseTest.php @@ -0,0 +1,15 @@ + '17835', + 'permsid' => 'b_serverinstance_help_view', + ]]); + + expect($response->id)->toBe(17835) + ->and($response->name)->toBe('b_serverinstance_help_view'); +}); diff --git a/tests/Unit/Responses/Permissions/GetResponseTest.php b/tests/Unit/Responses/Permissions/GetResponseTest.php new file mode 100644 index 0000000..bf09db4 --- /dev/null +++ b/tests/Unit/Responses/Permissions/GetResponseTest.php @@ -0,0 +1,17 @@ + '17835', + 'permsid' => 'b_serverinstance_help_view', + 'permvalue' => '1', + ]]); + + expect($response->id)->toBe(17835) + ->and($response->name)->toBe('b_serverinstance_help_view') + ->and($response->value)->toBe(1); +}); diff --git a/tests/Unit/Responses/Permissions/ListResponsePermissionTest.php b/tests/Unit/Responses/Permissions/ListResponsePermissionTest.php new file mode 100644 index 0000000..e82ef2e --- /dev/null +++ b/tests/Unit/Responses/Permissions/ListResponsePermissionTest.php @@ -0,0 +1,21 @@ + '1', + 'permname' => 'b_serverinstance_help_view', + 'permdesc' => 'Retrieve information about the server instance', + 'permtype' => '0', + 'permisflag' => '1', + ]); + + expect($permission->id)->toBe(1) + ->and($permission->name)->toBe('b_serverinstance_help_view') + ->and($permission->description)->toBe('Retrieve information about the server instance') + ->and($permission->type)->toBe(0) + ->and($permission->isFlag)->toBeTrue(); +}); diff --git a/tests/Unit/Responses/Permissions/ListResponseTest.php b/tests/Unit/Responses/Permissions/ListResponseTest.php new file mode 100644 index 0000000..6903044 --- /dev/null +++ b/tests/Unit/Responses/Permissions/ListResponseTest.php @@ -0,0 +1,19 @@ + '1', + 'permname' => 'b_serverinstance_help_view', + 'permdesc' => 'Retrieve information about the server instance', + 'permtype' => '0', + 'permisflag' => '1', + ]]); + + expect($response->permissions)->toHaveCount(1) + ->and($response->permissions[0])->toBeInstanceOf(ListResponsePermission::class); +}); diff --git a/tests/Unit/Responses/Permissions/OverviewResponsePermissionTest.php b/tests/Unit/Responses/Permissions/OverviewResponsePermissionTest.php new file mode 100644 index 0000000..2090b8e --- /dev/null +++ b/tests/Unit/Responses/Permissions/OverviewResponsePermissionTest.php @@ -0,0 +1,25 @@ + '1', + 'id1' => '5', + 'id2' => '3', + 'p' => '17', + 'v' => '75', + 'n' => '1', + 's' => '0', + ]); + + expect($permission->type)->toBe(1) + ->and($permission->id1)->toBe(5) + ->and($permission->id2)->toBe(3) + ->and($permission->permId)->toBe(17) + ->and($permission->value)->toBe(75) + ->and($permission->negated)->toBeTrue() + ->and($permission->skip)->toBeFalse(); +}); diff --git a/tests/Unit/Responses/Permissions/OverviewResponseTest.php b/tests/Unit/Responses/Permissions/OverviewResponseTest.php new file mode 100644 index 0000000..8891fab --- /dev/null +++ b/tests/Unit/Responses/Permissions/OverviewResponseTest.php @@ -0,0 +1,21 @@ + '0', + 'id1' => '5', + 'id2' => '0', + 'p' => '17', + 'v' => '75', + 'n' => '0', + 's' => '1', + ]]); + + expect($response->permissions)->toHaveCount(1) + ->and($response->permissions[0])->toBeInstanceOf(OverviewResponsePermission::class); +}); diff --git a/tests/Unit/Responses/Permissions/ResetResponseTest.php b/tests/Unit/Responses/Permissions/ResetResponseTest.php new file mode 100644 index 0000000..f87383d --- /dev/null +++ b/tests/Unit/Responses/Permissions/ResetResponseTest.php @@ -0,0 +1,13 @@ + 'eKnFZQ9EK7G7EmPvt1Ch7vsXi5Uq+1Us7xrQKBVsMxM=', + ]]); + + expect($response->token)->toBe('eKnFZQ9EK7G7EmPvt1Ch7vsXi5Uq+1Us7xrQKBVsMxM='); +});