From b99ea53ca1a9e17177ce59cd1228cbdad380e169 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Thu, 5 Sep 2019 21:41:20 -0700 Subject: [PATCH] Update client API endpoints to not use deprecated function --- .../Api/Client/ClientController.php | 8 +-- .../Api/Client/Servers/FileController.php | 59 +++++++++---------- .../Api/Client/Servers/NetworkController.php | 13 ++-- .../Api/Client/Servers/PowerController.php | 20 +++---- .../Servers/ResourceUtilizationController.php | 7 +-- .../Api/Client/Servers/ServerController.php | 5 +- .../Api/Application/ApplicationApiRequest.php | 1 + .../Wings/DaemonPowerRepository.php | 19 ++++++ 8 files changed, 74 insertions(+), 58 deletions(-) diff --git a/app/Http/Controllers/Api/Client/ClientController.php b/app/Http/Controllers/Api/Client/ClientController.php index d271c4cd7..828792c95 100644 --- a/app/Http/Controllers/Api/Client/ClientController.php +++ b/app/Http/Controllers/Api/Client/ClientController.php @@ -3,23 +3,23 @@ namespace Pterodactyl\Http\Controllers\Api\Client; use Pterodactyl\Models\User; +use Pterodactyl\Repositories\Eloquent\ServerRepository; use Pterodactyl\Transformers\Api\Client\ServerTransformer; use Pterodactyl\Http\Requests\Api\Client\GetServersRequest; -use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; class ClientController extends ClientApiController { /** - * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface + * @var \Pterodactyl\Repositories\Eloquent\ServerRepository */ private $repository; /** * ClientController constructor. * - * @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository + * @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository */ - public function __construct(ServerRepositoryInterface $repository) + public function __construct(ServerRepository $repository) { parent::__construct(); diff --git a/app/Http/Controllers/Api/Client/Servers/FileController.php b/app/Http/Controllers/Api/Client/Servers/FileController.php index f734cc127..0dbd6ca89 100644 --- a/app/Http/Controllers/Api/Client/Servers/FileController.php +++ b/app/Http/Controllers/Api/Client/Servers/FileController.php @@ -8,11 +8,10 @@ use Illuminate\Http\Response; use Pterodactyl\Models\Server; use Illuminate\Http\JsonResponse; use GuzzleHttp\Exception\TransferException; +use Pterodactyl\Repositories\Wings\DaemonFileRepository; use Pterodactyl\Transformers\Daemon\FileObjectTransformer; use Illuminate\Contracts\Cache\Repository as CacheRepository; -use Illuminate\Contracts\Config\Repository as ConfigRepository; use Pterodactyl\Http\Controllers\Api\Client\ClientApiController; -use Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface; use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException; use Pterodactyl\Http\Requests\Api\Client\Servers\Files\CopyFileRequest; use Pterodactyl\Http\Requests\Api\Client\Servers\Files\ListFilesRequest; @@ -31,28 +30,21 @@ class FileController extends ClientApiController private $cache; /** - * @var \Illuminate\Contracts\Config\Repository - */ - private $config; - - /** - * @var \Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface + * @var \Pterodactyl\Repositories\Wings\DaemonFileRepository */ private $fileRepository; /** * FileController constructor. * - * @param \Illuminate\Contracts\Config\Repository $config - * @param \Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface $fileRepository + * @param \Pterodactyl\Repositories\Wings\DaemonFileRepository $fileRepository * @param \Illuminate\Contracts\Cache\Repository $cache */ - public function __construct(ConfigRepository $config, FileRepositoryInterface $fileRepository, CacheRepository $cache) + public function __construct(DaemonFileRepository $fileRepository, CacheRepository $cache) { parent::__construct(); $this->cache = $cache; - $this->config = $config; $this->fileRepository = $fileRepository; } @@ -60,15 +52,16 @@ class FileController extends ClientApiController * Returns a listing of files in a given directory. * * @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\ListFilesRequest $request + * @param \Pterodactyl\Models\Server $server * @return array * * @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException */ - public function listDirectory(ListFilesRequest $request): array + public function listDirectory(ListFilesRequest $request, Server $server): array { try { $contents = $this->fileRepository - ->setServer($request->getModel(Server::class)) + ->setServer($server) ->getDirectory($request->get('directory') ?? '/'); } catch (TransferException $exception) { throw new DaemonConnectionException($exception, true); @@ -83,13 +76,15 @@ class FileController extends ClientApiController * Return the contents of a specified file for the user. * * @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\GetFileContentsRequest $request + * @param \Pterodactyl\Models\Server $server * @return \Illuminate\Http\Response + * @throws \Pterodactyl\Exceptions\Http\Server\FileSizeTooLargeException */ - public function getFileContents(GetFileContentsRequest $request): Response + public function getFileContents(GetFileContentsRequest $request, Server $server): Response { return Response::create( - $this->fileRepository->setServer($request->getModel(Server::class))->getContent( - $request->get('file'), $this->config->get('pterodactyl.files.max_edit_size') + $this->fileRepository->setServer($server)->getContent( + $request->get('file'), config('pterodactyl.files.max_edit_size') ) ); } @@ -98,11 +93,12 @@ class FileController extends ClientApiController * Writes the contents of the specified file to the server. * * @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\WriteFileContentRequest $request + * @param \Pterodactyl\Models\Server $server * @return \Illuminate\Http\Response */ - public function writeFileContents(WriteFileContentRequest $request): Response + public function writeFileContents(WriteFileContentRequest $request, Server $server): Response { - $this->fileRepository->setServer($request->getModel(Server::class))->putContent( + $this->fileRepository->setServer($server)->putContent( $request->get('file'), $request->getContent() ); @@ -114,12 +110,13 @@ class FileController extends ClientApiController * Creates a new folder on the server. * * @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\CreateFolderRequest $request + * @param \Pterodactyl\Models\Server $server * @return \Illuminate\Http\Response */ - public function createFolder(CreateFolderRequest $request): Response + public function createFolder(CreateFolderRequest $request, Server $server): Response { $this->fileRepository - ->setServer($request->getModel(Server::class)) + ->setServer($server) ->createDirectory($request->input('name'), $request->input('directory', '/')); return Response::create('', Response::HTTP_NO_CONTENT); @@ -129,12 +126,13 @@ class FileController extends ClientApiController * Renames a file on the remote machine. * * @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\RenameFileRequest $request + * @param \Pterodactyl\Models\Server $server * @return \Illuminate\Http\Response */ - public function renameFile(RenameFileRequest $request): Response + public function renameFile(RenameFileRequest $request, Server $server): Response { $this->fileRepository - ->setServer($request->getModel(Server::class)) + ->setServer($server) ->renameFile($request->input('rename_from'), $request->input('rename_to')); return Response::create('', Response::HTTP_NO_CONTENT); @@ -144,12 +142,13 @@ class FileController extends ClientApiController * Copies a file on the server. * * @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\CopyFileRequest $request + * @param \Pterodactyl\Models\Server $server * @return \Illuminate\Http\Response */ - public function copyFile(CopyFileRequest $request): Response + public function copyFile(CopyFileRequest $request, Server $server): Response { $this->fileRepository - ->setServer($request->getModel(Server::class)) + ->setServer($server) ->copyFile($request->input('location')); return Response::create('', Response::HTTP_NO_CONTENT); @@ -159,12 +158,13 @@ class FileController extends ClientApiController * Deletes a file or folder from the server. * * @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\DeleteFileRequest $request + * @param \Pterodactyl\Models\Server $server * @return \Illuminate\Http\Response */ - public function delete(DeleteFileRequest $request): Response + public function delete(DeleteFileRequest $request, Server $server): Response { $this->fileRepository - ->setServer($request->getModel(Server::class)) + ->setServer($server) ->deleteFile($request->input('location')); return Response::create('', Response::HTTP_NO_CONTENT); @@ -178,13 +178,12 @@ class FileController extends ClientApiController * Returns the token that needs to be used when downloading the file. * * @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\DownloadFileRequest $request + * @param \Pterodactyl\Models\Server $server * @return \Illuminate\Http\JsonResponse * @throws \Exception */ - public function download(DownloadFileRequest $request): JsonResponse + public function download(DownloadFileRequest $request, Server $server): JsonResponse { - /** @var \Pterodactyl\Models\Server $server */ - $server = $request->getModel(Server::class); $token = Uuid::uuid4()->toString(); $this->cache->put( diff --git a/app/Http/Controllers/Api/Client/Servers/NetworkController.php b/app/Http/Controllers/Api/Client/Servers/NetworkController.php index e7ecd1072..fbea03033 100644 --- a/app/Http/Controllers/Api/Client/Servers/NetworkController.php +++ b/app/Http/Controllers/Api/Client/Servers/NetworkController.php @@ -3,24 +3,24 @@ namespace Pterodactyl\Http\Controllers\Api\Client\Servers; use Pterodactyl\Models\Server; +use Pterodactyl\Repositories\Eloquent\AllocationRepository; use Pterodactyl\Transformers\Api\Client\AllocationTransformer; use Pterodactyl\Http\Controllers\Api\Client\ClientApiController; -use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface; use Pterodactyl\Http\Requests\Api\Client\Servers\Network\GetNetworkRequest; class NetworkController extends ClientApiController { /** - * @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface + * @var \Pterodactyl\Repositories\Eloquent\AllocationRepository */ private $repository; /** * NetworkController constructor. * - * @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $repository + * @param \Pterodactyl\Repositories\Eloquent\AllocationRepository $repository */ - public function __construct(AllocationRepositoryInterface $repository) + public function __construct(AllocationRepository $repository) { parent::__construct(); @@ -32,12 +32,11 @@ class NetworkController extends ClientApiController * not they are currently assigned as the primary for this server. * * @param \Pterodactyl\Http\Requests\Api\Client\Servers\Network\GetNetworkRequest $request + * @param \Pterodactyl\Models\Server $server * @return array */ - public function index(GetNetworkRequest $request): array + public function index(GetNetworkRequest $request, Server $server): array { - $server = $request->getModel(Server::class); - $allocations = $this->repository->findWhere([ ['server_id', '=', $server->id], ]); diff --git a/app/Http/Controllers/Api/Client/Servers/PowerController.php b/app/Http/Controllers/Api/Client/Servers/PowerController.php index 7a96a2a4e..82cf8b334 100644 --- a/app/Http/Controllers/Api/Client/Servers/PowerController.php +++ b/app/Http/Controllers/Api/Client/Servers/PowerController.php @@ -4,23 +4,23 @@ namespace Pterodactyl\Http\Controllers\Api\Client\Servers; use Illuminate\Http\Response; use Pterodactyl\Models\Server; +use Pterodactyl\Repositories\Wings\DaemonPowerRepository; use Pterodactyl\Http\Controllers\Api\Client\ClientApiController; use Pterodactyl\Http\Requests\Api\Client\Servers\SendPowerRequest; -use Pterodactyl\Contracts\Repository\Daemon\PowerRepositoryInterface; class PowerController extends ClientApiController { /** - * @var \Pterodactyl\Contracts\Repository\Daemon\PowerRepositoryInterface + * @var \Pterodactyl\Repositories\Wings\DaemonPowerRepository */ private $repository; /** * PowerController constructor. * - * @param \Pterodactyl\Contracts\Repository\Daemon\PowerRepositoryInterface $repository + * @param \Pterodactyl\Repositories\Wings\DaemonPowerRepository $repository */ - public function __construct(PowerRepositoryInterface $repository) + public function __construct(DaemonPowerRepository $repository) { parent::__construct(); @@ -31,16 +31,14 @@ class PowerController extends ClientApiController * Send a power action to a server. * * @param \Pterodactyl\Http\Requests\Api\Client\Servers\SendPowerRequest $request + * @param \Pterodactyl\Models\Server $server * @return \Illuminate\Http\Response - * - * @throws \Pterodactyl\Exceptions\Repository\Daemon\InvalidPowerSignalException */ - public function index(SendPowerRequest $request): Response + public function index(SendPowerRequest $request, Server $server): Response { - $server = $request->getModel(Server::class); - $token = $request->attributes->get('server_token'); - - $this->repository->setServer($server)->setToken($token)->sendSignal($request->input('signal')); + $this->repository->setServer($server)->send( + $request->input('signal') + ); return $this->returnNoContent(); } diff --git a/app/Http/Controllers/Api/Client/Servers/ResourceUtilizationController.php b/app/Http/Controllers/Api/Client/Servers/ResourceUtilizationController.php index c4f3f9015..0d56c21bd 100644 --- a/app/Http/Controllers/Api/Client/Servers/ResourceUtilizationController.php +++ b/app/Http/Controllers/Api/Client/Servers/ResourceUtilizationController.php @@ -31,15 +31,14 @@ class ResourceUtilizationController extends ClientApiController * Return the current resource utilization for a server. * * @param \Pterodactyl\Http\Requests\Api\Client\Servers\GetServerRequest $request + * @param \Pterodactyl\Models\Server $server * @return array * * @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException */ - public function __invoke(GetServerRequest $request): array + public function __invoke(GetServerRequest $request, Server $server): array { - $stats = $this->repository - ->setServer($request->getModel(Server::class)) - ->getDetails(); + $stats = $this->repository->setServer($server)->getDetails(); return $this->fractal->item($stats) ->transformWith($this->getTransformer(StatsTransformer::class)) diff --git a/app/Http/Controllers/Api/Client/Servers/ServerController.php b/app/Http/Controllers/Api/Client/Servers/ServerController.php index ce4502e3a..ffb5a1bde 100644 --- a/app/Http/Controllers/Api/Client/Servers/ServerController.php +++ b/app/Http/Controllers/Api/Client/Servers/ServerController.php @@ -14,11 +14,12 @@ class ServerController extends ClientApiController * client using the API. * * @param \Pterodactyl\Http\Requests\Api\Client\Servers\GetServerRequest $request + * @param \Pterodactyl\Models\Server $server * @return array */ - public function index(GetServerRequest $request): array + public function index(GetServerRequest $request, Server $server): array { - return $this->fractal->item($request->getModel(Server::class)) + return $this->fractal->item($server) ->transformWith($this->getTransformer(ServerTransformer::class)) ->toArray(); } diff --git a/app/Http/Requests/Api/Application/ApplicationApiRequest.php b/app/Http/Requests/Api/Application/ApplicationApiRequest.php index 2471a8fdc..133532718 100644 --- a/app/Http/Requests/Api/Application/ApplicationApiRequest.php +++ b/app/Http/Requests/Api/Application/ApplicationApiRequest.php @@ -89,6 +89,7 @@ abstract class ApplicationApiRequest extends FormRequest * * @param string $model * @return mixed + * @deprecated * * @throws \Symfony\Component\Routing\Exception\InvalidParameterException */ diff --git a/app/Repositories/Wings/DaemonPowerRepository.php b/app/Repositories/Wings/DaemonPowerRepository.php index a2e13b478..1338cd31e 100644 --- a/app/Repositories/Wings/DaemonPowerRepository.php +++ b/app/Repositories/Wings/DaemonPowerRepository.php @@ -2,6 +2,25 @@ namespace Pterodactyl\Repositories\Wings; +use Webmozart\Assert\Assert; +use Pterodactyl\Models\Server; +use Psr\Http\Message\ResponseInterface; + class DaemonPowerRepository extends DaemonRepository { + /** + * Sends a power action to the server instance. + * + * @param string $action + * @return \Psr\Http\Message\ResponseInterface + */ + public function send(string $action): ResponseInterface + { + Assert::isInstanceOf($this->server, Server::class); + + return $this->getHttpClient()->post( + sprintf('/api/servers/%s/power', $this->server->id), + ['json' => ['action' => $action]] + ); + } }