Massively simplify API binding logic

Changes the API internals to use normal Laravel binding which automatically supports nested-models and can determine their relationships. This removes a lot of confusingly complex internal logic and replaces it with standard Laravel code.

This also removes a deprecated "getModel" method and fully replaces it with a "parameter" method that does stricter type-checking.
This commit is contained in:
DaneEveritt 2022-05-22 14:10:01 -04:00
parent f1235c7f88
commit e313dff674
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
53 changed files with 290 additions and 604 deletions

View file

@ -75,9 +75,9 @@ class LocationController extends ApplicationApiController
/**
* Return a single location.
*/
public function view(GetLocationRequest $request): array
public function view(GetLocationRequest $request, Location $location): array
{
return $this->fractal->item($request->getModel(Location::class))
return $this->fractal->item($location)
->transformWith($this->getTransformer(LocationTransformer::class))
->toArray();
}
@ -108,9 +108,9 @@ class LocationController extends ApplicationApiController
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function update(UpdateLocationRequest $request): array
public function update(UpdateLocationRequest $request, Location $location): array
{
$location = $this->updateService->handle($request->getModel(Location::class), $request->validated());
$location = $this->updateService->handle($location, $request->validated());
return $this->fractal->item($location)
->transformWith($this->getTransformer(LocationTransformer::class))
@ -122,9 +122,9 @@ class LocationController extends ApplicationApiController
*
* @throws \Pterodactyl\Exceptions\Service\Location\HasActiveNodesException
*/
public function delete(DeleteLocationRequest $request): Response
public function delete(DeleteLocationRequest $request, Location $location): Response
{
$this->deletionService->handle($request->getModel(Location::class));
$this->deletionService->handle($location);
return response('', 204);
}

View file

@ -4,7 +4,6 @@ namespace Pterodactyl\Http\Controllers\Api\Application\Nests;
use Pterodactyl\Models\Egg;
use Pterodactyl\Models\Nest;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Transformers\Api\Application\EggTransformer;
use Pterodactyl\Http\Requests\Api\Application\Nests\Eggs\GetEggRequest;
use Pterodactyl\Http\Requests\Api\Application\Nests\Eggs\GetEggsRequest;
@ -12,31 +11,12 @@ use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
class EggController extends ApplicationApiController
{
/**
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface
*/
private $repository;
/**
* EggController constructor.
*/
public function __construct(EggRepositoryInterface $repository)
{
parent::__construct();
$this->repository = $repository;
}
/**
* Return all eggs that exist for a given nest.
*/
public function index(GetEggsRequest $request): array
public function index(GetEggsRequest $request, Nest $nest): array
{
$eggs = $this->repository->findWhere([
['nest_id', '=', $request->getModel(Nest::class)->id],
]);
return $this->fractal->collection($eggs)
return $this->fractal->collection($nest->eggs)
->transformWith($this->getTransformer(EggTransformer::class))
->toArray();
}
@ -44,9 +24,9 @@ class EggController extends ApplicationApiController
/**
* Return a single egg that exists on the specified nest.
*/
public function view(GetEggRequest $request): array
public function view(GetEggRequest $request, Nest $nest, Egg $egg): array
{
return $this->fractal->item($request->getModel(Egg::class))
return $this->fractal->item($egg)
->transformWith($this->getTransformer(EggTransformer::class))
->toArray();
}

View file

@ -40,9 +40,9 @@ class NestController extends ApplicationApiController
/**
* Return information about a single Nest model.
*/
public function view(GetNestsRequest $request): array
public function view(GetNestsRequest $request, Nest $nest): array
{
return $this->fractal->item($request->getModel(Nest::class))
return $this->fractal->item($nest)
->transformWith($this->getTransformer(NestTransformer::class))
->toArray();
}

View file

@ -105,12 +105,10 @@ class DatabaseController extends ApplicationApiController
/**
* Handle a request to delete a specific server database from the Panel.
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function delete(ServerDatabaseWriteRequest $request): Response
public function delete(ServerDatabaseWriteRequest $request, Server $server, Database $database): Response
{
$this->databaseManagementService->delete($request->getModel(Database::class));
$this->databaseManagementService->delete($database);
return response('', 204);
}

View file

@ -2,6 +2,7 @@
namespace Pterodactyl\Http\Controllers\Api\Application\Servers;
use Pterodactyl\Models\Server;
use Pterodactyl\Transformers\Api\Application\ServerTransformer;
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
use Pterodactyl\Http\Requests\Api\Application\Servers\GetExternalServerRequest;
@ -11,9 +12,11 @@ class ExternalServerController extends ApplicationApiController
/**
* Retrieve a specific server from the database using its external ID.
*/
public function index(GetExternalServerRequest $request): array
public function index(GetExternalServerRequest $request, string $external_id): array
{
return $this->fractal->item($request->getServerModel())
$server = Server::query()->where('external_id', $external_id)->firstOrFail();
return $this->fractal->item($server)
->transformWith($this->getTransformer(ServerTransformer::class))
->toArray();
}

View file

@ -86,9 +86,9 @@ class ServerController extends ApplicationApiController
/**
* Show a single server transformed for the application API.
*/
public function view(GetServerRequest $request): array
public function view(GetServerRequest $request, Server $server): array
{
return $this->fractal->item($request->getModel(Server::class))
return $this->fractal->item($server)
->transformWith($this->getTransformer(ServerTransformer::class))
->toArray();
}

View file

@ -42,14 +42,14 @@ class ServerDetailsController extends ApplicationApiController
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function details(UpdateServerDetailsRequest $request): array
public function details(UpdateServerDetailsRequest $request, Server $server): array
{
$server = $this->detailsModificationService->returnUpdatedModel()->handle(
$request->getModel(Server::class),
$updated = $this->detailsModificationService->returnUpdatedModel()->handle(
$server,
$request->validated()
);
return $this->fractal->item($server)
return $this->fractal->item($updated)
->transformWith($this->getTransformer(ServerTransformer::class))
->toArray();
}

View file

@ -34,11 +34,11 @@ class StartupController extends ApplicationApiController
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function index(UpdateServerStartupRequest $request): array
public function index(UpdateServerStartupRequest $request, Server $server): array
{
$server = $this->modificationService
->setUserLevel(User::USER_LEVEL_ADMIN)
->handle($request->getModel(Server::class), $request->validated());
->handle($server, $request->validated());
return $this->fractal->item($server)
->transformWith($this->getTransformer(ServerTransformer::class))

View file

@ -2,6 +2,7 @@
namespace Pterodactyl\Http\Controllers\Api\Application\Users;
use Pterodactyl\Models\User;
use Pterodactyl\Transformers\Api\Application\UserTransformer;
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
use Pterodactyl\Http\Requests\Api\Application\Users\GetExternalUserRequest;
@ -11,9 +12,11 @@ class ExternalUserController extends ApplicationApiController
/**
* Retrieve a specific user from the database using their external ID.
*/
public function index(GetExternalUserRequest $request): array
public function index(GetExternalUserRequest $request, string $external_id): array
{
return $this->fractal->item($request->getUserModel())
$user = User::query()->where('external_id', $external_id)->firstOrFail();
return $this->fractal->item($user)
->transformWith($this->getTransformer(UserTransformer::class))
->toArray();
}

View file

@ -52,8 +52,7 @@ class ScheduleController extends ClientApiController
*/
public function index(ViewScheduleRequest $request, Server $server)
{
$schedules = $server->schedule;
$schedules->loadMissing('tasks');
$schedules = $server->schedules->loadMissing('tasks');
return $this->fractal->collection($schedules)
->transformWith($this->getTransformer(ScheduleTransformer::class))

View file

@ -2,8 +2,8 @@
namespace Pterodactyl\Http\Controllers\Api\Remote;
use Pterodactyl\Models\User;
use Illuminate\Http\Request;
use Pterodactyl\Models\User;
use Pterodactyl\Models\Server;
use Illuminate\Http\JsonResponse;
use Pterodactyl\Models\Permission;