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:
parent
f1235c7f88
commit
e313dff674
53 changed files with 290 additions and 604 deletions
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -24,7 +24,6 @@ use Pterodactyl\Http\Middleware\MaintenanceMiddleware;
|
|||
use Pterodactyl\Http\Middleware\RedirectIfAuthenticated;
|
||||
use Illuminate\Auth\Middleware\AuthenticateWithBasicAuth;
|
||||
use Pterodactyl\Http\Middleware\Api\AuthenticateIPAccess;
|
||||
use Pterodactyl\Http\Middleware\Api\ApiSubstituteBindings;
|
||||
use Illuminate\Foundation\Http\Middleware\ValidatePostSize;
|
||||
use Pterodactyl\Http\Middleware\Api\HandleStatelessRequest;
|
||||
use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse;
|
||||
|
@ -32,7 +31,7 @@ use Pterodactyl\Http\Middleware\Api\Daemon\DaemonAuthenticate;
|
|||
use Pterodactyl\Http\Middleware\RequireTwoFactorAuthentication;
|
||||
use Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode;
|
||||
use Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull;
|
||||
use Pterodactyl\Http\Middleware\Api\Client\SubstituteClientApiBindings;
|
||||
use Pterodactyl\Http\Middleware\Api\Client\SubstituteClientBindings;
|
||||
use Pterodactyl\Http\Middleware\Api\Application\AuthenticateApplicationUser;
|
||||
|
||||
class Kernel extends HttpKernel
|
||||
|
@ -75,13 +74,13 @@ class Kernel extends HttpKernel
|
|||
VerifyCsrfToken::class,
|
||||
],
|
||||
'application-api' => [
|
||||
ApiSubstituteBindings::class,
|
||||
SubstituteBindings::class,
|
||||
'api..key:' . ApiKey::TYPE_APPLICATION,
|
||||
AuthenticateApplicationUser::class,
|
||||
AuthenticateIPAccess::class,
|
||||
],
|
||||
'client-api' => [
|
||||
SubstituteClientApiBindings::class,
|
||||
SubstituteClientBindings::class,
|
||||
'api..key:' . ApiKey::TYPE_ACCOUNT,
|
||||
AuthenticateIPAccess::class,
|
||||
// This is perhaps a little backwards with the Client API, but logically you'd be unable
|
||||
|
|
|
@ -1,87 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Http\Middleware\Api;
|
||||
|
||||
use Closure;
|
||||
use Pterodactyl\Models\Egg;
|
||||
use Pterodactyl\Models\Nest;
|
||||
use Pterodactyl\Models\Node;
|
||||
use Pterodactyl\Models\User;
|
||||
use Pterodactyl\Models\Server;
|
||||
use Pterodactyl\Models\Database;
|
||||
use Pterodactyl\Models\Location;
|
||||
use Pterodactyl\Models\Allocation;
|
||||
use Illuminate\Routing\Middleware\SubstituteBindings;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
|
||||
class ApiSubstituteBindings extends SubstituteBindings
|
||||
{
|
||||
/**
|
||||
* Mappings to automatically assign route parameters to a model.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $mappings = [
|
||||
'allocation' => Allocation::class,
|
||||
'database' => Database::class,
|
||||
'egg' => Egg::class,
|
||||
'location' => Location::class,
|
||||
'nest' => Nest::class,
|
||||
'node' => Node::class,
|
||||
'server' => Server::class,
|
||||
'user' => User::class,
|
||||
];
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Routing\Router
|
||||
*/
|
||||
protected $router;
|
||||
|
||||
/**
|
||||
* Perform substitution of route parameters without triggering
|
||||
* a 404 error if a model is not found.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
$route = $request->route();
|
||||
|
||||
foreach (self::$mappings as $key => $model) {
|
||||
if (!is_null($this->router->getBindingCallback($key))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->router->model($key, $model, function () use ($request) {
|
||||
$request->attributes->set('is_missing_model', true);
|
||||
});
|
||||
}
|
||||
|
||||
$this->router->substituteBindings($route);
|
||||
|
||||
// Attempt to resolve bindings for this route. If one of the models
|
||||
// cannot be resolved do not immediately return a 404 error. Set a request
|
||||
// attribute that can be checked in the base API request class to only
|
||||
// trigger a 404 after validating that the API key making the request is valid
|
||||
// and even has permission to access the requested resource.
|
||||
try {
|
||||
$this->router->substituteImplicitBindings($route);
|
||||
} catch (ModelNotFoundException $exception) {
|
||||
$request->attributes->set('is_missing_model', true);
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the registered mappings.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getMappings()
|
||||
{
|
||||
return self::$mappings;
|
||||
}
|
||||
}
|
|
@ -1,62 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Http\Middleware\Api\Client;
|
||||
|
||||
use Closure;
|
||||
use Pterodactyl\Models\User;
|
||||
use Pterodactyl\Models\Backup;
|
||||
use Pterodactyl\Models\Database;
|
||||
use Illuminate\Container\Container;
|
||||
use Pterodactyl\Contracts\Extensions\HashidsInterface;
|
||||
use Pterodactyl\Http\Middleware\Api\ApiSubstituteBindings;
|
||||
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
||||
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
||||
|
||||
class SubstituteClientApiBindings extends ApiSubstituteBindings
|
||||
{
|
||||
/**
|
||||
* Perform substitution of route parameters without triggering
|
||||
* a 404 error if a model is not found.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
// Override default behavior of the model binding to use a specific table
|
||||
// column rather than the default 'id'.
|
||||
$this->router->bind('server', function ($value) use ($request) {
|
||||
try {
|
||||
$column = 'uuidShort';
|
||||
if (preg_match('/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i', $value)) {
|
||||
$column = 'uuid';
|
||||
}
|
||||
|
||||
return Container::getInstance()->make(ServerRepositoryInterface::class)->findFirstWhere([
|
||||
[$column, '=', $value],
|
||||
]);
|
||||
} catch (RecordNotFoundException $ex) {
|
||||
$request->attributes->set('is_missing_model', true);
|
||||
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
$this->router->bind('database', function ($value) {
|
||||
$id = Container::getInstance()->make(HashidsInterface::class)->decodeFirst($value);
|
||||
|
||||
return Database::query()->where('id', $id)->firstOrFail();
|
||||
});
|
||||
|
||||
$this->router->bind('backup', function ($value) {
|
||||
return Backup::query()->where('uuid', $value)->firstOrFail();
|
||||
});
|
||||
|
||||
$this->router->bind('user', function ($value) {
|
||||
return User::query()->where('uuid', $value)->firstOrFail();
|
||||
});
|
||||
|
||||
return parent::handle($request, $next);
|
||||
}
|
||||
}
|
36
app/Http/Middleware/Api/Client/SubstituteClientBindings.php
Normal file
36
app/Http/Middleware/Api/Client/SubstituteClientBindings.php
Normal file
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Http\Middleware\Api\Client;
|
||||
|
||||
use Closure;
|
||||
use Pterodactyl\Models\Server;
|
||||
use Illuminate\Routing\Middleware\SubstituteBindings;
|
||||
|
||||
class SubstituteClientBindings extends SubstituteBindings
|
||||
{
|
||||
/**
|
||||
* @param \Illuminate\Http\Request $request
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
// Override default behavior of the model binding to use a specific table
|
||||
// column rather than the default 'id'.
|
||||
$this->router->bind('server', function ($value) {
|
||||
return Server::query()->where(strlen($value) === 8 ? 'uuidShort' : 'uuid', $value)->firstOrFail();
|
||||
});
|
||||
|
||||
$this->router->bind('user', function ($value, $route) {
|
||||
/** @var \Pterodactyl\Models\Subuser $match */
|
||||
$match = $route->parameter('server')
|
||||
->subusers()
|
||||
->whereRelation('user', 'uuid', '=', $value)
|
||||
->firstOrFail();
|
||||
|
||||
return $match->user;
|
||||
});
|
||||
|
||||
return parent::handle($request, $next);
|
||||
}
|
||||
}
|
|
@ -3,6 +3,7 @@
|
|||
namespace Pterodactyl\Http\Middleware\Api;
|
||||
|
||||
use Closure;
|
||||
use JsonException;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
|
||||
|
@ -18,10 +19,10 @@ class IsValidJson
|
|||
public function handle(Request $request, Closure $next)
|
||||
{
|
||||
if ($request->isJson() && !empty($request->getContent())) {
|
||||
json_decode($request->getContent(), true);
|
||||
|
||||
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||
throw new BadRequestHttpException(sprintf('The JSON data passed in the request appears to be malformed. err_code: %d err_message: "%s"', json_last_error(), json_last_error_msg()));
|
||||
try {
|
||||
json_decode($request->getContent(), true, 512, JSON_THROW_ON_ERROR);
|
||||
} catch (JsonException $exception) {
|
||||
throw new BadRequestHttpException('The JSON data passed in the request appears to be malformed: ' . $exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -2,8 +2,6 @@
|
|||
|
||||
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\ApplicationApiRequest;
|
||||
|
||||
|
@ -18,22 +16,4 @@ class DeleteAllocationRequest extends ApplicationApiRequest
|
|||
* @var int
|
||||
*/
|
||||
protected $permission = AdminAcl::WRITE;
|
||||
|
||||
/**
|
||||
* Determine if the requested allocation exists and belongs to the node that
|
||||
* is being passed in the URL.
|
||||
*/
|
||||
public function resourceExists(): bool
|
||||
{
|
||||
$node = $this->route()->parameter('node');
|
||||
$allocation = $this->route()->parameter('allocation');
|
||||
|
||||
if ($node instanceof Node && $node->exists) {
|
||||
if ($allocation instanceof Allocation && $allocation->exists && $allocation->node_id === $node->id) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
namespace Pterodactyl\Http\Requests\Api\Application\Allocations;
|
||||
|
||||
use Pterodactyl\Models\Node;
|
||||
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
||||
use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest;
|
||||
|
||||
|
@ -17,15 +16,4 @@ class GetAllocationsRequest extends ApplicationApiRequest
|
|||
* @var int
|
||||
*/
|
||||
protected $permission = AdminAcl::READ;
|
||||
|
||||
/**
|
||||
* Determine if the node that we are requesting the allocations
|
||||
* for exists on the Panel.
|
||||
*/
|
||||
public function resourceExists(): bool
|
||||
{
|
||||
$node = $this->route()->parameter('node');
|
||||
|
||||
return $node instanceof Node && $node->exists;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,14 +2,12 @@
|
|||
|
||||
namespace Pterodactyl\Http\Requests\Api\Application;
|
||||
|
||||
use Pterodactyl\Models\ApiKey;
|
||||
use Webmozart\Assert\Assert;
|
||||
use Illuminate\Validation\Validator;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Pterodactyl\Exceptions\PterodactylException;
|
||||
use Pterodactyl\Http\Middleware\Api\ApiSubstituteBindings;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\Routing\Exception\InvalidParameterException;
|
||||
|
||||
abstract class ApplicationApiRequest extends FormRequest
|
||||
{
|
||||
|
@ -49,15 +47,7 @@ abstract class ApplicationApiRequest extends FormRequest
|
|||
throw new PterodactylException('An ACL resource must be defined on API requests.');
|
||||
}
|
||||
|
||||
return AdminAcl::check($this->key(), $this->resource, $this->permission);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the requested resource exists on the server.
|
||||
*/
|
||||
public function resourceExists(): bool
|
||||
{
|
||||
return true;
|
||||
return AdminAcl::check($this->attributes->get('api_key'), $this->resource, $this->permission);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -68,35 +58,6 @@ abstract class ApplicationApiRequest extends FormRequest
|
|||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the API key being used for the request.
|
||||
*/
|
||||
public function key(): ApiKey
|
||||
{
|
||||
return $this->attributes->get('api_key');
|
||||
}
|
||||
|
||||
/**
|
||||
* Grab a model from the route parameters. If no model is found in the
|
||||
* binding mappings an exception will be thrown.
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @deprecated
|
||||
*
|
||||
* @throws \Symfony\Component\Routing\Exception\InvalidParameterException
|
||||
*/
|
||||
public function getModel(string $model)
|
||||
{
|
||||
$parameterKey = array_get(array_flip(ApiSubstituteBindings::getMappings()), $model);
|
||||
|
||||
if (is_null($parameterKey)) {
|
||||
throw new InvalidParameterException();
|
||||
}
|
||||
|
||||
return $this->route()->parameter($parameterKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method allowing a developer to easily hook into this logic without having
|
||||
* to remember what the method name is called or where to use it. By default this is
|
||||
|
@ -108,50 +69,26 @@ abstract class ApplicationApiRequest extends FormRequest
|
|||
}
|
||||
|
||||
/**
|
||||
* Validate that the resource exists and can be accessed prior to booting
|
||||
* the validator and attempting to use the data.
|
||||
* Returns the named route parameter and asserts that it is a real model that
|
||||
* exists in the database.
|
||||
*
|
||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||
* @template T of \Illuminate\Database\Eloquent\Model
|
||||
*
|
||||
* @param class-string<T> $expect
|
||||
*
|
||||
* @return T
|
||||
* @noinspection PhpUndefinedClassInspection
|
||||
* @noinspection PhpDocSignatureInspection
|
||||
*/
|
||||
protected function prepareForValidation()
|
||||
public function parameter(string $key, string $expect)
|
||||
{
|
||||
if (!$this->passesAuthorization()) {
|
||||
$this->failedAuthorization();
|
||||
}
|
||||
$value = $this->route()->parameter($key);
|
||||
|
||||
$this->hasValidated = true;
|
||||
}
|
||||
Assert::isInstanceOf($value, $expect);
|
||||
Assert::isInstanceOf($value, Model::class);
|
||||
Assert::true($value->exists);
|
||||
|
||||
/*
|
||||
* Determine if the request passes the authorization check as well
|
||||
* as the exists check.
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
|
||||
*/
|
||||
protected function passesAuthorization()
|
||||
{
|
||||
// If we have already validated we do not need to call this function
|
||||
// again. This is needed to work around Laravel's normal auth validation
|
||||
// that occurs after validating the request params since we are doing auth
|
||||
// validation in the prepareForValidation() function.
|
||||
if ($this->hasValidated) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!parent::passesAuthorization()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Only let the user know that a resource does not exist if they are
|
||||
// authenticated to access the endpoint. This avoids exposing that
|
||||
// an item exists (or does not exist) to the user until they can prove
|
||||
// that they have permission to know about it.
|
||||
if ($this->attributes->get('is_missing_model', false) || !$this->resourceExists()) {
|
||||
throw new NotFoundHttpException(trans('exceptions.api.resource_not_found'));
|
||||
}
|
||||
|
||||
return true;
|
||||
/* @var T $value */
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
namespace Pterodactyl\Http\Requests\Api\Application\Locations;
|
||||
|
||||
use Pterodactyl\Models\Location;
|
||||
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
||||
use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest;
|
||||
|
||||
|
@ -17,14 +16,4 @@ class DeleteLocationRequest extends ApplicationApiRequest
|
|||
* @var int
|
||||
*/
|
||||
protected $permission = AdminAcl::WRITE;
|
||||
|
||||
/**
|
||||
* Determine if the requested location exists on the Panel.
|
||||
*/
|
||||
public function resourceExists(): bool
|
||||
{
|
||||
$location = $this->route()->parameter('location');
|
||||
|
||||
return $location instanceof Location && $location->exists;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,17 +2,6 @@
|
|||
|
||||
namespace Pterodactyl\Http\Requests\Api\Application\Locations;
|
||||
|
||||
use Pterodactyl\Models\Location;
|
||||
|
||||
class GetLocationRequest extends GetLocationsRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the requested location exists on the Panel.
|
||||
*/
|
||||
public function resourceExists(): bool
|
||||
{
|
||||
$location = $this->route()->parameter('location');
|
||||
|
||||
return $location instanceof Location && $location->exists;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,16 +6,6 @@ use Pterodactyl\Models\Location;
|
|||
|
||||
class UpdateLocationRequest extends StoreLocationRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the requested location exists on the Panel.
|
||||
*/
|
||||
public function resourceExists(): bool
|
||||
{
|
||||
$location = $this->route()->parameter('location');
|
||||
|
||||
return $location instanceof Location && $location->exists;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rules to validate this request against.
|
||||
*/
|
||||
|
|
|
@ -2,8 +2,6 @@
|
|||
|
||||
namespace Pterodactyl\Http\Requests\Api\Application\Nests\Eggs;
|
||||
|
||||
use Pterodactyl\Models\Egg;
|
||||
use Pterodactyl\Models\Nest;
|
||||
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
||||
use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest;
|
||||
|
||||
|
@ -18,12 +16,4 @@ class GetEggRequest extends ApplicationApiRequest
|
|||
* @var int
|
||||
*/
|
||||
protected $permission = AdminAcl::READ;
|
||||
|
||||
/**
|
||||
* Determine if the requested egg exists for the selected nest.
|
||||
*/
|
||||
public function resourceExists(): bool
|
||||
{
|
||||
return $this->getModel(Nest::class)->id === $this->getModel(Egg::class)->nest_id;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
namespace Pterodactyl\Http\Requests\Api\Application\Nodes;
|
||||
|
||||
use Pterodactyl\Models\Node;
|
||||
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
||||
use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest;
|
||||
|
||||
|
@ -17,15 +16,4 @@ class DeleteNodeRequest extends ApplicationApiRequest
|
|||
* @var int
|
||||
*/
|
||||
protected $permission = AdminAcl::WRITE;
|
||||
|
||||
/**
|
||||
* Determine if the node being requested for editing exists
|
||||
* on the Panel before validating the data.
|
||||
*/
|
||||
public function resourceExists(): bool
|
||||
{
|
||||
$node = $this->route()->parameter('node');
|
||||
|
||||
return $node instanceof Node && $node->exists;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,17 +2,6 @@
|
|||
|
||||
namespace Pterodactyl\Http\Requests\Api\Application\Nodes;
|
||||
|
||||
use Pterodactyl\Models\Node;
|
||||
|
||||
class GetNodeRequest extends GetNodesRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the requested node exists on the Panel.
|
||||
*/
|
||||
public function resourceExists(): bool
|
||||
{
|
||||
$node = $this->route()->parameter('node');
|
||||
|
||||
return $node instanceof Node && $node->exists;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,8 +12,8 @@ class UpdateNodeRequest extends StoreNodeRequest
|
|||
*/
|
||||
public function rules(array $rules = null): array
|
||||
{
|
||||
$nodeId = $this->getModel(Node::class)->id;
|
||||
$node = $this->route()->parameter('node')->id;
|
||||
|
||||
return parent::rules(Node::getRulesForUpdate($nodeId));
|
||||
return parent::rules(Node::getRulesForUpdate($node));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,15 +16,4 @@ class GetServerDatabaseRequest extends ApplicationApiRequest
|
|||
* @var int
|
||||
*/
|
||||
protected $permission = AdminAcl::READ;
|
||||
|
||||
/**
|
||||
* Determine if the requested server database exists.
|
||||
*/
|
||||
public function resourceExists(): bool
|
||||
{
|
||||
$server = $this->route()->parameter('server');
|
||||
$database = $this->route()->parameter('database');
|
||||
|
||||
return $database->server_id === $server->id;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,19 +2,11 @@
|
|||
|
||||
namespace Pterodactyl\Http\Requests\Api\Application\Servers;
|
||||
|
||||
use Pterodactyl\Models\Server;
|
||||
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
||||
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
||||
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
||||
use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest;
|
||||
|
||||
class GetExternalServerRequest extends ApplicationApiRequest
|
||||
{
|
||||
/**
|
||||
* @var \Pterodactyl\Models\Server
|
||||
*/
|
||||
private $serverModel;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
|
@ -24,30 +16,4 @@ class GetExternalServerRequest extends ApplicationApiRequest
|
|||
* @var int
|
||||
*/
|
||||
protected $permission = AdminAcl::READ;
|
||||
|
||||
/**
|
||||
* Determine if the requested external user exists.
|
||||
*/
|
||||
public function resourceExists(): bool
|
||||
{
|
||||
$repository = $this->container->make(ServerRepositoryInterface::class);
|
||||
|
||||
try {
|
||||
$this->serverModel = $repository->findFirstWhere([
|
||||
['external_id', '=', $this->route()->parameter('external_id')],
|
||||
]);
|
||||
} catch (RecordNotFoundException $exception) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the server model for the requested external server.
|
||||
*/
|
||||
public function getServerModel(): Server
|
||||
{
|
||||
return $this->serverModel;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ class UpdateServerBuildConfigurationRequest extends ServerWriteRequest
|
|||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
$rules = Server::getRulesForUpdate($this->getModel(Server::class));
|
||||
$rules = Server::getRulesForUpdate($this->parameter('server', Server::class));
|
||||
|
||||
return [
|
||||
'allocation' => $rules['allocation_id'],
|
||||
|
|
|
@ -11,7 +11,7 @@ class UpdateServerDetailsRequest extends ServerWriteRequest
|
|||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
$rules = Server::getRulesForUpdate($this->getModel(Server::class));
|
||||
$rules = Server::getRulesForUpdate($this->parameter('server', Server::class));
|
||||
|
||||
return [
|
||||
'external_id' => $rules['external_id'],
|
||||
|
|
|
@ -23,7 +23,7 @@ class UpdateServerStartupRequest extends ApplicationApiRequest
|
|||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
$data = Server::getRulesForUpdate($this->getModel(Server::class));
|
||||
$data = Server::getRulesForUpdate($this->parameter('server', Server::class));
|
||||
|
||||
return [
|
||||
'startup' => $data['startup'],
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
namespace Pterodactyl\Http\Requests\Api\Application\Users;
|
||||
|
||||
use Pterodactyl\Models\User;
|
||||
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
||||
use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest;
|
||||
|
||||
|
@ -17,14 +16,4 @@ class DeleteUserRequest extends ApplicationApiRequest
|
|||
* @var int
|
||||
*/
|
||||
protected $permission = AdminAcl::WRITE;
|
||||
|
||||
/**
|
||||
* Determine if the requested user exists on the Panel.
|
||||
*/
|
||||
public function resourceExists(): bool
|
||||
{
|
||||
$user = $this->route()->parameter('user');
|
||||
|
||||
return $user instanceof User && $user->exists;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,19 +2,11 @@
|
|||
|
||||
namespace Pterodactyl\Http\Requests\Api\Application\Users;
|
||||
|
||||
use Pterodactyl\Models\User;
|
||||
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
||||
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
|
||||
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
||||
use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest;
|
||||
|
||||
class GetExternalUserRequest extends ApplicationApiRequest
|
||||
{
|
||||
/**
|
||||
* @var User
|
||||
*/
|
||||
private $userModel;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
|
@ -24,30 +16,4 @@ class GetExternalUserRequest extends ApplicationApiRequest
|
|||
* @var int
|
||||
*/
|
||||
protected $permission = AdminAcl::READ;
|
||||
|
||||
/**
|
||||
* Determine if the requested external user exists.
|
||||
*/
|
||||
public function resourceExists(): bool
|
||||
{
|
||||
$repository = $this->container->make(UserRepositoryInterface::class);
|
||||
|
||||
try {
|
||||
$this->userModel = $repository->findFirstWhere([
|
||||
['external_id', '=', $this->route()->parameter('external_id')],
|
||||
]);
|
||||
} catch (RecordNotFoundException $exception) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the user model for the requested external user.
|
||||
*/
|
||||
public function getUserModel(): User
|
||||
{
|
||||
return $this->userModel;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ class UpdateUserRequest extends StoreUserRequest
|
|||
*/
|
||||
public function rules(array $rules = null): array
|
||||
{
|
||||
$userId = $this->getModel(User::class)->id;
|
||||
$userId = $this->parameter('user', User::class)->id;
|
||||
|
||||
return parent::rules(User::getRulesForUpdate($userId));
|
||||
}
|
||||
|
|
|
@ -2,8 +2,6 @@
|
|||
|
||||
namespace Pterodactyl\Http\Requests\Api\Client\Servers\Databases;
|
||||
|
||||
use Pterodactyl\Models\Server;
|
||||
use Pterodactyl\Models\Database;
|
||||
use Pterodactyl\Models\Permission;
|
||||
use Pterodactyl\Contracts\Http\ClientPermissionsRequest;
|
||||
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
|
||||
|
@ -14,9 +12,4 @@ class DeleteDatabaseRequest extends ClientApiRequest implements ClientPermission
|
|||
{
|
||||
return Permission::ACTION_DATABASE_DELETE;
|
||||
}
|
||||
|
||||
public function resourceExists(): bool
|
||||
{
|
||||
return $this->getModel(Server::class)->id === $this->getModel(Database::class)->server_id;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,6 +13,6 @@ class DownloadFileRequest extends ClientApiRequest
|
|||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return $this->user()->can('file.read', $this->getModel(Server::class));
|
||||
return $this->user()->can('file.read', $this->parameter('server', Server::class));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue