2018-01-20 21:33:04 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Middleware\Api;
|
|
|
|
|
|
|
|
use Closure;
|
2018-01-27 18:38:56 +00:00
|
|
|
use Pterodactyl\Models\Egg;
|
|
|
|
use Pterodactyl\Models\Nest;
|
|
|
|
use Pterodactyl\Models\Node;
|
2018-02-04 21:38:38 +00:00
|
|
|
use Pterodactyl\Models\User;
|
2018-01-27 18:38:56 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
|
|
|
use Pterodactyl\Models\Database;
|
|
|
|
use Pterodactyl\Models\Location;
|
|
|
|
use Pterodactyl\Models\Allocation;
|
2018-01-20 21:33:04 +00:00
|
|
|
use Illuminate\Routing\Middleware\SubstituteBindings;
|
2018-01-26 04:34:53 +00:00
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
2018-01-20 21:33:04 +00:00
|
|
|
|
|
|
|
class ApiSubstituteBindings extends SubstituteBindings
|
|
|
|
{
|
2018-01-27 18:38:56 +00:00
|
|
|
/**
|
|
|
|
* 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,
|
2018-02-04 21:38:38 +00:00
|
|
|
'user' => User::class,
|
2018-01-27 18:38:56 +00:00
|
|
|
];
|
|
|
|
|
2018-02-28 03:28:43 +00:00
|
|
|
/**
|
|
|
|
* @var \Illuminate\Routing\Router
|
|
|
|
*/
|
|
|
|
protected $router;
|
|
|
|
|
2018-01-20 21:33:04 +00:00
|
|
|
/**
|
|
|
|
* Perform substitution of route parameters without triggering
|
|
|
|
* a 404 error if a model is not found.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Closure $next
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle($request, Closure $next)
|
|
|
|
{
|
|
|
|
$route = $request->route();
|
|
|
|
|
2018-01-27 18:38:56 +00:00
|
|
|
foreach (self::$mappings as $key => $model) {
|
2018-02-28 03:28:43 +00:00
|
|
|
if (! is_null($this->router->getBindingCallback($key))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-02-28 03:04:04 +00:00
|
|
|
$this->router->model($key, $model, function () use ($request) {
|
|
|
|
$request->attributes->set('is_missing_model', true);
|
|
|
|
});
|
2018-01-27 18:38:56 +00:00
|
|
|
}
|
|
|
|
|
2018-01-20 21:33:04 +00:00
|
|
|
$this->router->substituteBindings($route);
|
|
|
|
|
2018-01-26 04:34:53 +00:00
|
|
|
// 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);
|
2018-01-20 21:33:04 +00:00
|
|
|
}
|
2018-01-26 04:34:53 +00:00
|
|
|
|
|
|
|
return $next($request);
|
2018-01-20 21:33:04 +00:00
|
|
|
}
|
2018-01-27 18:38:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the registered mappings.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getMappings()
|
|
|
|
{
|
|
|
|
return self::$mappings;
|
|
|
|
}
|
2018-01-20 21:33:04 +00:00
|
|
|
}
|