From 2017e640b662a6d60510ac70d77e24f8d044cca0 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Wed, 28 Feb 2018 22:51:04 -0600 Subject: [PATCH] Add client API --- .../Api/Client/Servers/PowerController.php | 57 +++++++++++++++++++ .../Api/Client/Servers/SendCommandRequest.php | 2 +- .../Api/Client/Servers/SendPowerRequest.php | 31 ++++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 app/Http/Controllers/Api/Client/Servers/PowerController.php create mode 100644 app/Http/Requests/Api/Client/Servers/SendPowerRequest.php diff --git a/app/Http/Controllers/Api/Client/Servers/PowerController.php b/app/Http/Controllers/Api/Client/Servers/PowerController.php new file mode 100644 index 000000000..113b83398 --- /dev/null +++ b/app/Http/Controllers/Api/Client/Servers/PowerController.php @@ -0,0 +1,57 @@ +keyProviderService = $keyProviderService; + $this->repository = $repository; + } + + /** + * Send a power action to a server. + * + * @param \Pterodactyl\Http\Requests\Api\Client\Servers\SendPowerRequest $request + * @return \Illuminate\Http\Response + * + * @throws \Pterodactyl\Exceptions\Model\DataValidationException + * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException + * @throws \Pterodactyl\Exceptions\Repository\Daemon\InvalidPowerSignalException + */ + public function index(SendPowerRequest $request): Response + { + $server = $request->getModel(Server::class); + $token = $this->keyProviderService->handle($server, $request->user()); + + $this->repository->setServer($server)->setToken($token)->sendSignal($request->input('signal')); + + return $this->returnNoContent(); + } +} diff --git a/app/Http/Requests/Api/Client/Servers/SendCommandRequest.php b/app/Http/Requests/Api/Client/Servers/SendCommandRequest.php index 68757658a..788f97739 100644 --- a/app/Http/Requests/Api/Client/Servers/SendCommandRequest.php +++ b/app/Http/Requests/Api/Client/Servers/SendCommandRequest.php @@ -24,7 +24,7 @@ class SendCommandRequest extends GetServerRequest public function rules(): array { return [ - 'command' => 'string|min:1', + 'command' => 'required|string|min:1', ]; } } diff --git a/app/Http/Requests/Api/Client/Servers/SendPowerRequest.php b/app/Http/Requests/Api/Client/Servers/SendPowerRequest.php new file mode 100644 index 000000000..19614d182 --- /dev/null +++ b/app/Http/Requests/Api/Client/Servers/SendPowerRequest.php @@ -0,0 +1,31 @@ +user()->can('power-' . $this->input('signal', '_undefined'), $this->getModel(Server::class)); + } + + /** + * Rules to validate this request aganist. + * + * @return array + */ + public function rules(): array + { + return [ + 'signal' => 'required|string|in:start,stop,restart,kill', + ]; + } +}