diff --git a/app/Extensions/Spatie/Fractalistic/Fractal.php b/app/Extensions/Spatie/Fractalistic/Fractal.php new file mode 100644 index 000000000..cbe8f46b5 --- /dev/null +++ b/app/Extensions/Spatie/Fractalistic/Fractal.php @@ -0,0 +1,44 @@ +serializer)) { + $this->serializer = new JsonApiSerializer; + } + + // Automatically set the paginator on the response object if the + // data being provided implements a paginator. + if (is_null($this->paginator) && $this->data instanceof LengthAwarePaginator) { + $this->paginator = new IlluminatePaginatorAdapter($this->data); + } + + // Automatically set the resource name if the response data is a model + // and the resource name is available on the model. + if (is_null($this->resourceName) && $this->data instanceof Model) { + if (defined(get_class($this->data) . '::RESOURCE_NAME')) { + $this->resourceName = constant(get_class($this->data) . '::RESOURCE_NAME'); + } + } + + return parent::createData(); + } +} diff --git a/app/Http/Controllers/Admin/ApplicationApiController.php b/app/Http/Controllers/Admin/ApiController.php similarity index 98% rename from app/Http/Controllers/Admin/ApplicationApiController.php rename to app/Http/Controllers/Admin/ApiController.php index 10eb1e47c..bbf4bcbf8 100644 --- a/app/Http/Controllers/Admin/ApplicationApiController.php +++ b/app/Http/Controllers/Admin/ApiController.php @@ -14,7 +14,7 @@ use Pterodactyl\Services\Api\KeyCreationService; use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface; use Pterodactyl\Http\Requests\Admin\Api\StoreApplicationApiKeyRequest; -class ApplicationApiController extends Controller +class ApiController extends Controller { /** * @var \Prologue\Alerts\AlertsMessageBag diff --git a/app/Http/Controllers/Api/Application/Locations/LocationController.php b/app/Http/Controllers/Api/Application/Locations/LocationController.php index c051c267a..5af4c0782 100644 --- a/app/Http/Controllers/Api/Application/Locations/LocationController.php +++ b/app/Http/Controllers/Api/Application/Locations/LocationController.php @@ -11,8 +11,8 @@ use League\Fractal\Pagination\IlluminatePaginatorAdapter; use Pterodactyl\Services\Locations\LocationUpdateService; use Pterodactyl\Services\Locations\LocationCreationService; use Pterodactyl\Services\Locations\LocationDeletionService; -use Pterodactyl\Transformers\Api\Admin\LocationTransformer; use Pterodactyl\Contracts\Repository\LocationRepositoryInterface; +use Pterodactyl\Transformers\Api\Application\LocationTransformer; use Pterodactyl\Http\Requests\Api\Application\Locations\GetLocationsRequest; use Pterodactyl\Http\Requests\Api\Application\Locations\DeleteLocationRequest; use Pterodactyl\Http\Requests\Api\Application\Locations\UpdateLocationRequest; diff --git a/app/Http/Controllers/Api/Application/Nodes/AllocationController.php b/app/Http/Controllers/Api/Application/Nodes/AllocationController.php index 3c770a145..969d4b55d 100644 --- a/app/Http/Controllers/Api/Application/Nodes/AllocationController.php +++ b/app/Http/Controllers/Api/Application/Nodes/AllocationController.php @@ -8,11 +8,11 @@ use Illuminate\Http\Response; use Pterodactyl\Models\Allocation; use Pterodactyl\Http\Controllers\Controller; use League\Fractal\Pagination\IlluminatePaginatorAdapter; -use Pterodactyl\Transformers\Api\Admin\AllocationTransformer; use Pterodactyl\Services\Allocations\AllocationDeletionService; use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface; +use Pterodactyl\Transformers\Api\Application\AllocationTransformer; use Pterodactyl\Http\Requests\Api\Application\Allocations\GetAllocationsRequest; -use Pterodactyl\Http\Requests\Api\Application\Allocations\DeleteAllocationRequest; +use Pterodactyl\Http\Requests\Api\Application\Allocations\DeleteAllocationRequestApplication; class AllocationController extends Controller { @@ -66,14 +66,14 @@ class AllocationController extends Controller /** * Delete a specific allocation from the Panel. * - * @param \Pterodactyl\Http\Requests\Api\Application\Allocations\DeleteAllocationRequest $request - * @param \Pterodactyl\Models\Node $node - * @param \Pterodactyl\Models\Allocation $allocation + * @param \Pterodactyl\Http\Requests\Api\Application\Allocations\DeleteAllocationRequestApplication $request + * @param \Pterodactyl\Models\Node $node + * @param \Pterodactyl\Models\Allocation $allocation * @return \Illuminate\Http\Response * * @throws \Pterodactyl\Exceptions\Service\Allocation\ServerUsingAllocationException */ - public function delete(DeleteAllocationRequest $request, Node $node, Allocation $allocation): Response + public function delete(DeleteAllocationRequestApplication $request, Node $node, Allocation $allocation): Response { $this->deletionService->handle($allocation); diff --git a/app/Http/Controllers/Api/Application/Nodes/NodeController.php b/app/Http/Controllers/Api/Application/Nodes/NodeController.php index 1abbbe2c0..cf115db96 100644 --- a/app/Http/Controllers/Api/Application/Nodes/NodeController.php +++ b/app/Http/Controllers/Api/Application/Nodes/NodeController.php @@ -10,9 +10,9 @@ use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Services\Nodes\NodeUpdateService; use Pterodactyl\Services\Nodes\NodeCreationService; use Pterodactyl\Services\Nodes\NodeDeletionService; -use Pterodactyl\Transformers\Api\Admin\NodeTransformer; use League\Fractal\Pagination\IlluminatePaginatorAdapter; use Pterodactyl\Contracts\Repository\NodeRepositoryInterface; +use Pterodactyl\Transformers\Api\Application\NodeTransformer; use Pterodactyl\Http\Requests\Api\Application\Nodes\GetNodeRequest; use Pterodactyl\Http\Requests\Api\Application\Nodes\GetNodesRequest; use Pterodactyl\Http\Requests\Api\Application\Nodes\StoreNodeRequest; diff --git a/app/Http/Controllers/Api/Application/Servers/ServerController.php b/app/Http/Controllers/Api/Application/Servers/ServerController.php index 90a5ea39a..f1d7398a3 100644 --- a/app/Http/Controllers/Api/Application/Servers/ServerController.php +++ b/app/Http/Controllers/Api/Application/Servers/ServerController.php @@ -1,20 +1,18 @@ fractal = $fractal; + parent::__construct($fractal, $request); + $this->repository = $repository; } - public function index(Request $request): array + /** + * Return all of the servers that currently exist on the Panel. + * + * @param \Pterodactyl\Http\Requests\Api\Application\Servers\GetServersRequest $request + * @return array + */ + public function index(GetServersRequest $request): array { - $servers = $this->repository->paginated(50); + $servers = $this->repository->setSearchTerm($request->input('search'))->paginated(50); return $this->fractal->collection($servers) - ->transformWith((new ServerTransformer)->setKey()) - ->withResourceName('server') + ->transformWith($this->getTransformer(ServerTransformer::class)) + ->toArray(); + } + + /** + * Show a single server transformed for the application API. + * + * @param \Pterodactyl\Http\Requests\Api\Application\Servers\GetServerRequest $request + * @param \Pterodactyl\Models\Server $server + * @return array + */ + public function view(GetServerRequest $request, Server $server): array + { + return $this->fractal->item($server) + ->transformWith($this->getTransformer(ServerTransformer::class)) ->toArray(); } } diff --git a/app/Http/Controllers/Api/Application/Users/UserController.php b/app/Http/Controllers/Api/Application/Users/UserController.php index 48017ffc7..35c534536 100644 --- a/app/Http/Controllers/Api/Application/Users/UserController.php +++ b/app/Http/Controllers/Api/Application/Users/UserController.php @@ -11,9 +11,9 @@ use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Services\Users\UserUpdateService; use Pterodactyl\Services\Users\UserCreationService; use Pterodactyl\Services\Users\UserDeletionService; -use Pterodactyl\Transformers\Api\Admin\UserTransformer; use League\Fractal\Pagination\IlluminatePaginatorAdapter; use Pterodactyl\Contracts\Repository\UserRepositoryInterface; +use Pterodactyl\Transformers\Api\Application\UserTransformer; use Pterodactyl\Http\Requests\Api\Application\Users\GetUserRequest; use Pterodactyl\Http\Requests\Api\Application\Users\GetUsersRequest; use Pterodactyl\Http\Requests\Api\Application\Users\StoreUserRequest; diff --git a/app/Http/Requests/Api/Application/Allocations/DeleteAllocationRequest.php b/app/Http/Requests/Api/Application/Allocations/DeleteAllocationRequest.phpApplication similarity index 87% rename from app/Http/Requests/Api/Application/Allocations/DeleteAllocationRequest.php rename to app/Http/Requests/Api/Application/Allocations/DeleteAllocationRequest.phpApplication index 1ee934a82..21da6bfb3 100644 --- a/app/Http/Requests/Api/Application/Allocations/DeleteAllocationRequest.php +++ b/app/Http/Requests/Api/Application/Allocations/DeleteAllocationRequest.phpApplication @@ -5,9 +5,9 @@ namespace Pterodactyl\Http\Requests\Api\Application\Allocations; use Pterodactyl\Models\Node; use Pterodactyl\Models\Allocation; use Pterodactyl\Services\Acl\Api\AdminAcl; -use Pterodactyl\Http\Requests\Api\Application\ApiAdminRequest; +use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest; -class DeleteAllocationRequest extends ApiAdminRequest +class DeleteAllocationRequest extends ApplicationApiRequest { /** * @var string diff --git a/app/Http/Requests/Api/Application/Allocations/GetAllocationsRequest.php b/app/Http/Requests/Api/Application/Allocations/GetAllocationsRequest.php index f7af3edab..150bdb95e 100644 --- a/app/Http/Requests/Api/Application/Allocations/GetAllocationsRequest.php +++ b/app/Http/Requests/Api/Application/Allocations/GetAllocationsRequest.php @@ -4,9 +4,9 @@ namespace Pterodactyl\Http\Requests\Api\Application\Allocations; use Pterodactyl\Models\Node; use Pterodactyl\Services\Acl\Api\AdminAcl; -use Pterodactyl\Http\Requests\Api\Application\ApiAdminRequest; +use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest; -class GetAllocationsRequest extends ApiAdminRequest +class GetAllocationsRequest extends ApplicationApiRequest { /** * @var string diff --git a/app/Http/Requests/Api/Application/ApiAdminRequest.php b/app/Http/Requests/Api/Application/ApplicationApiRequest.php similarity index 97% rename from app/Http/Requests/Api/Application/ApiAdminRequest.php rename to app/Http/Requests/Api/Application/ApplicationApiRequest.php index 267d2a54d..2cf7c8478 100644 --- a/app/Http/Requests/Api/Application/ApiAdminRequest.php +++ b/app/Http/Requests/Api/Application/ApplicationApiRequest.php @@ -8,7 +8,7 @@ use Illuminate\Foundation\Http\FormRequest; use Pterodactyl\Exceptions\PterodactylException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; -abstract class ApiAdminRequest extends FormRequest +abstract class ApplicationApiRequest extends FormRequest { /** * The resource that should be checked when performing the authorization diff --git a/app/Http/Requests/Api/Application/Locations/DeleteLocationRequest.php b/app/Http/Requests/Api/Application/Locations/DeleteLocationRequest.php index ee788fa74..d1863eea7 100644 --- a/app/Http/Requests/Api/Application/Locations/DeleteLocationRequest.php +++ b/app/Http/Requests/Api/Application/Locations/DeleteLocationRequest.php @@ -4,9 +4,9 @@ namespace Pterodactyl\Http\Requests\Api\Application\Locations; use Pterodactyl\Models\Location; use Pterodactyl\Services\Acl\Api\AdminAcl; -use Pterodactyl\Http\Requests\Api\Application\ApiAdminRequest; +use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest; -class DeleteLocationRequest extends ApiAdminRequest +class DeleteLocationRequest extends ApplicationApiRequest { /** * @var string diff --git a/app/Http/Requests/Api/Application/Locations/GetLocationsRequest.php b/app/Http/Requests/Api/Application/Locations/GetLocationsRequest.php index ceb21c5b6..5edf00462 100644 --- a/app/Http/Requests/Api/Application/Locations/GetLocationsRequest.php +++ b/app/Http/Requests/Api/Application/Locations/GetLocationsRequest.php @@ -3,9 +3,9 @@ namespace Pterodactyl\Http\Requests\Api\Application\Locations; use Pterodactyl\Services\Acl\Api\AdminAcl; -use Pterodactyl\Http\Requests\Api\Application\ApiAdminRequest; +use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest; -class GetLocationsRequest extends ApiAdminRequest +class GetLocationsRequest extends ApplicationApiRequest { /** * @var string diff --git a/app/Http/Requests/Api/Application/Locations/StoreLocationRequest.php b/app/Http/Requests/Api/Application/Locations/StoreLocationRequest.php index 50683acb9..5a71883a3 100644 --- a/app/Http/Requests/Api/Application/Locations/StoreLocationRequest.php +++ b/app/Http/Requests/Api/Application/Locations/StoreLocationRequest.php @@ -4,9 +4,9 @@ namespace Pterodactyl\Http\Controllers\Api\Application\Locations; use Pterodactyl\Models\Location; use Pterodactyl\Services\Acl\Api\AdminAcl; -use Pterodactyl\Http\Requests\Api\Application\ApiAdminRequest; +use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest; -class StoreLocationRequest extends ApiAdminRequest +class StoreLocationRequest extends ApplicationApiRequest { /** * @var string diff --git a/app/Http/Requests/Api/Application/Nodes/DeleteNodeRequest.php b/app/Http/Requests/Api/Application/Nodes/DeleteNodeRequest.php index b7dabe2d5..77dd24eac 100644 --- a/app/Http/Requests/Api/Application/Nodes/DeleteNodeRequest.php +++ b/app/Http/Requests/Api/Application/Nodes/DeleteNodeRequest.php @@ -4,9 +4,9 @@ namespace Pterodactyl\Http\Requests\Api\Application\Nodes; use Pterodactyl\Models\Node; use Pterodactyl\Services\Acl\Api\AdminAcl; -use Pterodactyl\Http\Requests\Api\Application\ApiAdminRequest; +use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest; -class DeleteNodeRequest extends ApiAdminRequest +class DeleteNodeRequest extends ApplicationApiRequest { /** * @var string diff --git a/app/Http/Requests/Api/Application/Nodes/GetNodeRequest.php b/app/Http/Requests/Api/Application/Nodes/GetNodeRequest.php index 41d30043e..fbf957edd 100644 --- a/app/Http/Requests/Api/Application/Nodes/GetNodeRequest.php +++ b/app/Http/Requests/Api/Application/Nodes/GetNodeRequest.php @@ -3,9 +3,8 @@ namespace Pterodactyl\Http\Requests\Api\Application\Nodes; use Pterodactyl\Models\Node; -use Pterodactyl\Http\Requests\Api\Application\ApiAdminRequest; -class GetNodeRequest extends ApiAdminRequest +class GetNodeRequest extends GetNodesRequest { /** * Determine if the requested node exists on the Panel. diff --git a/app/Http/Requests/Api/Application/Nodes/GetNodesRequest.php b/app/Http/Requests/Api/Application/Nodes/GetNodesRequest.php index b6bc1db7e..fc5f5a38e 100644 --- a/app/Http/Requests/Api/Application/Nodes/GetNodesRequest.php +++ b/app/Http/Requests/Api/Application/Nodes/GetNodesRequest.php @@ -3,9 +3,9 @@ namespace Pterodactyl\Http\Requests\Api\Application\Nodes; use Pterodactyl\Services\Acl\Api\AdminAcl; -use Pterodactyl\Http\Requests\Api\Application\ApiAdminRequest; +use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest; -class GetNodesRequest extends ApiAdminRequest +class GetNodesRequest extends ApplicationApiRequest { /** * @var string diff --git a/app/Http/Requests/Api/Application/Nodes/StoreNodeRequest.php b/app/Http/Requests/Api/Application/Nodes/StoreNodeRequest.php index d1d720f04..20a29f199 100644 --- a/app/Http/Requests/Api/Application/Nodes/StoreNodeRequest.php +++ b/app/Http/Requests/Api/Application/Nodes/StoreNodeRequest.php @@ -4,9 +4,9 @@ namespace Pterodactyl\Http\Requests\Api\Application\Nodes; use Pterodactyl\Models\Node; use Pterodactyl\Services\Acl\Api\AdminAcl; -use Pterodactyl\Http\Requests\Api\Application\ApiAdminRequest; +use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest; -class StoreNodeRequest extends ApiAdminRequest +class StoreNodeRequest extends ApplicationApiRequest { /** * @var string diff --git a/app/Http/Requests/Api/Application/Servers/GetServerRequest.php b/app/Http/Requests/Api/Application/Servers/GetServerRequest.php new file mode 100644 index 000000000..42428ece7 --- /dev/null +++ b/app/Http/Requests/Api/Application/Servers/GetServerRequest.php @@ -0,0 +1,32 @@ +route()->parameter('server'); + + return $server instanceof Server && $server->exists; + } +} diff --git a/app/Http/Requests/Api/Application/Servers/GetServersRequest.php b/app/Http/Requests/Api/Application/Servers/GetServersRequest.php new file mode 100644 index 000000000..922610385 --- /dev/null +++ b/app/Http/Requests/Api/Application/Servers/GetServersRequest.php @@ -0,0 +1,29 @@ + 'string|max:100', + ]; + } +} diff --git a/app/Http/Requests/Api/Application/Users/DeleteUserRequest.php b/app/Http/Requests/Api/Application/Users/DeleteUserRequest.php index f46e97937..571b29c63 100644 --- a/app/Http/Requests/Api/Application/Users/DeleteUserRequest.php +++ b/app/Http/Requests/Api/Application/Users/DeleteUserRequest.php @@ -4,9 +4,9 @@ namespace Pterodactyl\Http\Requests\Api\Application\Users; use Pterodactyl\Models\User; use Pterodactyl\Services\Acl\Api\AdminAcl; -use Pterodactyl\Http\Requests\Api\Application\ApiAdminRequest; +use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest; -class DeleteUserRequest extends ApiAdminRequest +class DeleteUserRequest extends ApplicationApiRequest { /** * @var string diff --git a/app/Http/Requests/Api/Application/Users/GetUsersRequest.php b/app/Http/Requests/Api/Application/Users/GetUsersRequest.php index c0043f08f..8736a8e9d 100644 --- a/app/Http/Requests/Api/Application/Users/GetUsersRequest.php +++ b/app/Http/Requests/Api/Application/Users/GetUsersRequest.php @@ -3,9 +3,9 @@ namespace Pterodactyl\Http\Requests\Api\Application\Users; use Pterodactyl\Services\Acl\Api\AdminAcl as Acl; -use Pterodactyl\Http\Requests\Api\Application\ApiAdminRequest; +use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest; -class GetUsersRequest extends ApiAdminRequest +class GetUsersRequest extends ApplicationApiRequest { /** * @var string diff --git a/app/Http/Requests/Api/Application/Users/StoreUserRequest.php b/app/Http/Requests/Api/Application/Users/StoreUserRequest.php index 00a2c668b..e6416826c 100644 --- a/app/Http/Requests/Api/Application/Users/StoreUserRequest.php +++ b/app/Http/Requests/Api/Application/Users/StoreUserRequest.php @@ -4,9 +4,9 @@ namespace Pterodactyl\Http\Requests\Api\Application\Users; use Pterodactyl\Models\User; use Pterodactyl\Services\Acl\Api\AdminAcl; -use Pterodactyl\Http\Requests\Api\Application\ApiAdminRequest; +use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest; -class StoreUserRequest extends ApiAdminRequest +class StoreUserRequest extends ApplicationApiRequest { /** * @var string diff --git a/app/Models/Server.php b/app/Models/Server.php index 9f81584c8..01c5eef15 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -22,6 +22,12 @@ class Server extends Model implements CleansAttributes, ValidableContract { use BelongsToThrough, Eloquence, Notifiable, Validable; + /** + * The resource name for this model when it is transformed into an + * API representation using fractal. + */ + const RESOURCE_NAME = 'server'; + /** * The table associated with the model. * diff --git a/app/Transformers/Api/Admin/ServerVariableTransformer.php b/app/Transformers/Api/Admin/ServerVariableTransformer.php deleted file mode 100644 index 6f526f3f4..000000000 --- a/app/Transformers/Api/Admin/ServerVariableTransformer.php +++ /dev/null @@ -1,69 +0,0 @@ -. - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ - -namespace Pterodactyl\Transformers\Admin; - -use Illuminate\Http\Request; -use Pterodactyl\Models\ServerVariable; -use League\Fractal\TransformerAbstract; - -class ServerVariableTransformer extends TransformerAbstract -{ - /** - * List of resources that can be included. - * - * @var array - */ - protected $availableIncludes = ['parent']; - - /** - * The Illuminate Request object if provided. - * - * @var \Illuminate\Http\Request|bool - */ - protected $request; - - /** - * Setup request object for transformer. - * - * @param \Illuminate\Http\Request|bool $request - */ - public function __construct($request = false) - { - if (! $request instanceof Request && $request !== false) { - throw new DisplayException('Request passed to constructor must be of type Request or false.'); - } - - $this->request = $request; - } - - /** - * Return a generic transformed server variable array. - * - * @return array - */ - public function transform(ServerVariable $variable) - { - return $variable->toArray(); - } - - /** - * Return the parent service variable data. - * - * @return \Leauge\Fractal\Resource\Item - */ - public function includeParent(ServerVariable $variable) - { - if ($this->request && ! $this->request->apiKeyHasPermission('option-view')) { - return; - } - - return $this->item($variable->variable, new ServiceVariableTransformer($this->request), 'variable'); - } -} diff --git a/app/Transformers/Api/Admin/AllocationTransformer.php b/app/Transformers/Api/Application/AllocationTransformer.php similarity index 97% rename from app/Transformers/Api/Admin/AllocationTransformer.php rename to app/Transformers/Api/Application/AllocationTransformer.php index a1766ffd7..10d08def6 100644 --- a/app/Transformers/Api/Admin/AllocationTransformer.php +++ b/app/Transformers/Api/Application/AllocationTransformer.php @@ -1,6 +1,6 @@ call([$this, 'handle']); + } + } + /** * Set the HTTP request class being used for this request. * @@ -59,11 +70,11 @@ abstract class BaseTransformer extends TransformerAbstract * * @param string $abstract * @param array $parameters - * @return \Pterodactyl\Transformers\Api\Admin\BaseTransformer + * @return \Pterodactyl\Transformers\Api\Application\BaseTransformer */ protected function makeTransformer(string $abstract, array $parameters = []): self { - /** @var \Pterodactyl\Transformers\Api\Admin\BaseTransformer $transformer */ + /** @var \Pterodactyl\Transformers\Api\Application\BaseTransformer $transformer */ $transformer = Container::getInstance()->makeWith($abstract, $parameters); $transformer->setKey($this->getKey()); diff --git a/app/Transformers/Api/Application/EggVariableTransformer.php b/app/Transformers/Api/Application/EggVariableTransformer.php new file mode 100644 index 000000000..357f7de1b --- /dev/null +++ b/app/Transformers/Api/Application/EggVariableTransformer.php @@ -0,0 +1,13 @@ +toArray(); + } +} diff --git a/app/Transformers/Api/Admin/LocationTransformer.php b/app/Transformers/Api/Application/LocationTransformer.php similarity index 96% rename from app/Transformers/Api/Admin/LocationTransformer.php rename to app/Transformers/Api/Application/LocationTransformer.php index 81fa9d928..0c29f1801 100644 --- a/app/Transformers/Api/Admin/LocationTransformer.php +++ b/app/Transformers/Api/Application/LocationTransformer.php @@ -1,6 +1,6 @@ environmentService = $environmentService; + } + /** * Return a generic transformed server array. * * @param \Pterodactyl\Models\Server $server * @return array + * + * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException */ public function transform(Server $server): array { - return collect($server->toArray())->only($server->getTableColumns())->toArray(); + return [ + 'id' => $server->getKey(), + 'uuid' => $server->uuid, + 'identifier' => $server->uuidShort, + 'name' => $server->name, + 'description' => $server->description, + 'suspended' => (bool) $server->suspended, + 'limits' => [ + 'memory' => $server->memory, + 'swap' => $server->swap, + 'disk' => $server->disk, + 'io' => $server->io, + 'cpu' => $server->cpu, + ], + 'user' => $server->owner_id, + 'node' => $server->node_id, + 'allocation' => $server->allocation_id, + 'nest' => $server->nest_id, + 'egg' => $server->egg_id, + 'pack' => $server->pack_id, + 'container' => [ + 'startup_command' => $server->startup, + 'image' => $server->image, + 'installed' => (int) $server->installed === 1, + 'environment' => $this->environmentService->handle($server), + ], + 'created_at' => Chronos::createFromFormat(Chronos::DEFAULT_TO_STRING_FORMAT, $server->created_at)->setTimezone('UTC')->toIso8601String(), + 'updated_at' => Chronos::createFromFormat(Chronos::DEFAULT_TO_STRING_FORMAT, $server->updated_at)->setTimezone('UTC')->toIso8601String(), + ]; } /** @@ -94,16 +139,16 @@ class ServerTransformer extends BaseTransformer * @param \Pterodactyl\Models\Server $server * @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource */ - public function includePack(Server $server) - { - if (! $this->authorize(AdminAcl::RESOURCE_PACKS)) { - return $this->null(); - } - - $server->loadMissing('pack'); - - return $this->item($server->getRelation('pack'), $this->makeTransformer(PackTransformer::class), 'pack'); - } +// public function includePack(Server $server) +// { +// if (! $this->authorize(AdminAcl::RESOURCE_PACKS)) { +// return $this->null(); +// } +// +// $server->loadMissing('pack'); +// +// return $this->item($server->getRelation('pack'), $this->makeTransformer(PackTransformer::class), 'pack'); +// } /** * Return a generic array with nest information for this server. @@ -111,16 +156,16 @@ class ServerTransformer extends BaseTransformer * @param \Pterodactyl\Models\Server $server * @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource */ - public function includeNest(Server $server) - { - if (! $this->authorize(AdminAcl::RESOURCE_NESTS)) { - return $this->null(); - } - - $server->loadMissing('nest'); - - return $this->item($server->getRelation('nest'), $this->makeTransformer(NestTransformer::class), 'nest'); - } +// public function includeNest(Server $server) +// { +// if (! $this->authorize(AdminAcl::RESOURCE_NESTS)) { +// return $this->null(); +// } +// +// $server->loadMissing('nest'); +// +// return $this->item($server->getRelation('nest'), $this->makeTransformer(NestTransformer::class), 'nest'); +// } /** * Return a generic array with service option information for this server. @@ -136,14 +181,14 @@ class ServerTransformer extends BaseTransformer $server->loadMissing('egg'); - return $this->item($server->getRelation('egg'), $this->makeTransformer(EggTransformer::class), 'egg'); + return $this->item($server->getRelation('egg'), $this->makeTransformer(EggVariableTransformer::class), 'egg'); } /** * Return a generic array of data about subusers for this server. * * @param \Pterodactyl\Models\Server $server - * @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource + * @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource */ public function includeVariables(Server $server) { @@ -153,7 +198,7 @@ class ServerTransformer extends BaseTransformer $server->loadMissing('variables'); - return $this->item($server->getRelation('variables'), $this->makeTransformer(ServerVariableTransformer::class), 'server_variable'); + return $this->collection($server->getRelation('variables'), $this->makeTransformer(ServerVariableTransformer::class), 'server_variable'); } /** diff --git a/app/Transformers/Api/Application/ServerVariableTransformer.php b/app/Transformers/Api/Application/ServerVariableTransformer.php new file mode 100644 index 000000000..b965bc861 --- /dev/null +++ b/app/Transformers/Api/Application/ServerVariableTransformer.php @@ -0,0 +1,44 @@ +toArray(); + } + + /** + * Return the parent service variable data. + * + * @param \Pterodactyl\Models\ServerVariable $variable + * @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource + */ + public function includeParent(ServerVariable $variable) + { + if (! $this->authorize(AdminAcl::RESOURCE_EGGS)) { + return $this->null(); + } + + $variable->loadMissing('variable'); + + return $this->item($variable->getRelation('variable'), $this->makeTransformer(EggVariableTransformer::class), 'variable'); + } +} diff --git a/app/Transformers/Api/Admin/ServiceTransformer.php b/app/Transformers/Api/Application/ServiceTransformer.php similarity index 100% rename from app/Transformers/Api/Admin/ServiceTransformer.php rename to app/Transformers/Api/Application/ServiceTransformer.php diff --git a/app/Transformers/Api/Admin/ServiceVariableTransformer.php b/app/Transformers/Api/Application/ServiceVariableTransformer.php similarity index 100% rename from app/Transformers/Api/Admin/ServiceVariableTransformer.php rename to app/Transformers/Api/Application/ServiceVariableTransformer.php diff --git a/app/Transformers/Api/Admin/SubuserTransformer.php b/app/Transformers/Api/Application/SubuserTransformer.php similarity index 100% rename from app/Transformers/Api/Admin/SubuserTransformer.php rename to app/Transformers/Api/Application/SubuserTransformer.php diff --git a/app/Transformers/Api/Admin/UserTransformer.php b/app/Transformers/Api/Application/UserTransformer.php similarity index 95% rename from app/Transformers/Api/Admin/UserTransformer.php rename to app/Transformers/Api/Application/UserTransformer.php index 54a38469e..b6aac29b9 100644 --- a/app/Transformers/Api/Admin/UserTransformer.php +++ b/app/Transformers/Api/Application/UserTransformer.php @@ -1,6 +1,6 @@ name('admin.index'); | */ Route::group(['prefix' => 'api'], function () { - Route::get('/', 'ApplicationApiController@index')->name('admin.api.index'); - Route::get('/new', 'ApplicationApiController@create')->name('admin.api.new'); + Route::get('/', 'ApiController@index')->name('admin.api.index'); + Route::get('/new', 'ApiController@create')->name('admin.api.new'); - Route::post('/new', 'ApplicationApiController@store'); + Route::post('/new', 'ApiController@store'); - Route::delete('/revoke/{identifier}', 'ApplicationApiController@delete')->name('admin.api.delete'); + Route::delete('/revoke/{identifier}', 'ApiController@delete')->name('admin.api.delete'); }); /* diff --git a/routes/api-application.php b/routes/api-application.php index f98c797e6..9ba57216e 100644 --- a/routes/api-application.php +++ b/routes/api-application.php @@ -2,6 +2,7 @@ use Pterodactyl\Models\Node; use Pterodactyl\Models\User; +use Pterodactyl\Models\Server; use Pterodactyl\Models\Location; use Pterodactyl\Models\Allocation; @@ -18,13 +19,13 @@ Route::group(['prefix' => '/users'], function () { return User::find($value) ?? new User; }); - Route::get('/', 'Users\UserController@index')->name('api.admin.user.list'); - Route::get('/{user}', 'Users\UserController@view')->name('api.admin.user.view'); + Route::get('/', 'Users\UserController@index')->name('api.application.users'); + Route::get('/{user}', 'Users\UserController@view')->name('api.applications.users.view'); - Route::post('/', 'Users\UserController@store')->name('api.admin.user.store'); - Route::patch('/{user}', 'Users\UserController@update')->name('api.admin.user.update'); + Route::post('/', 'Users\UserController@store'); + Route::patch('/{user}', 'Users\UserController@update'); - Route::delete('/{user}', 'Users\UserController@delete')->name('api.admin.user.delete'); + Route::delete('/{user}', 'Users\UserController@delete'); }); /* @@ -40,22 +41,22 @@ Route::group(['prefix' => '/nodes'], function () { return Node::find($value) ?? new Node; }); - Route::get('/', 'Nodes\NodeController@index')->name('api.admin.node.list'); - Route::get('/{node}', 'Nodes\NodeController@view')->name('api.admin.node.view'); + Route::get('/', 'Nodes\NodeController@index')->name('api.application.nodes'); + Route::get('/{node}', 'Nodes\NodeController@view')->name('api.application.nodes.view'); - Route::post('/', 'Nodes\NodeController@store')->name('api.admin.node.store'); - Route::patch('/{node}', 'Nodes\NodeController@update')->name('api.admin.node.update'); + Route::post('/', 'Nodes\NodeController@store'); + Route::patch('/{node}', 'Nodes\NodeController@update'); - Route::delete('/{node}', 'Nodes\NodeController@delete')->name('api.admin.node.delete'); + Route::delete('/{node}', 'Nodes\NodeController@delete'); Route::group(['prefix' => '/{node}/allocations'], function () { Route::bind('allocation', function ($value) { return Allocation::find($value) ?? new Allocation; }); - Route::get('/', 'Nodes\AllocationController@index')->name('api.admin.node.allocations.list'); + Route::get('/', 'Nodes\AllocationController@index')->name('api.application.allocations'); - Route::delete('/{allocation}', 'Nodes\AllocationController@delete')->name('api.admin.node.allocations.delete'); + Route::delete('/{allocation}', 'Nodes\AllocationController@delete')->name('api.application.allocations.view'); }); }); @@ -72,11 +73,28 @@ Route::group(['prefix' => '/locations'], function () { return Location::find($value) ?? new Location; }); - Route::get('/', 'Locations\LocationController@index')->name('api.admin.location.list'); - Route::get('/{location}', 'Locations\LocationController@view')->name('api.admin.location.view'); + Route::get('/', 'Locations\LocationController@index')->name('api.applications.locations'); + Route::get('/{location}', 'Locations\LocationController@view')->name('api.application.locations.view'); - Route::post('/', 'Locations\LocationController@store')->name('api.admin.location.store'); - Route::patch('/{location}', 'Locations\LocationController@update')->name('api.admin.location.update'); + Route::post('/', 'Locations\LocationController@store'); + Route::patch('/{location}', 'Locations\LocationController@update'); - Route::delete('/{location}', 'Locations\LocationController@delete')->name('api.admin.location.delete'); + Route::delete('/{location}', 'Locations\LocationController@delete'); +}); + +/* +|-------------------------------------------------------------------------- +| Location Controller Routes +|-------------------------------------------------------------------------- +| +| Endpoint: /api/application/servers +| +*/ +Route::group(['prefix' => '/servers'], function () { + Route::bind('location', function ($value) { + return Server::find($value) ?? new Location; + }); + + Route::get('/', 'Servers\ServerController@index')->name('api.application.servers'); + Route::get('/{server}', 'Servers\ServerController@view')->name('api.application.servers'); });