2018-02-28 03:28:43 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Middleware\Api\Client;
|
|
|
|
|
|
|
|
use Closure;
|
2021-08-05 05:20:43 +00:00
|
|
|
use Illuminate\Support\Str;
|
2021-08-07 22:45:36 +00:00
|
|
|
use Pterodactyl\Models\Task;
|
2021-08-05 05:20:43 +00:00
|
|
|
use Illuminate\Routing\Route;
|
|
|
|
use Pterodactyl\Models\Server;
|
2018-02-28 03:28:43 +00:00
|
|
|
use Illuminate\Container\Container;
|
2021-08-07 22:45:36 +00:00
|
|
|
use Illuminate\Database\Query\JoinClause;
|
2021-08-05 05:20:43 +00:00
|
|
|
use Illuminate\Contracts\Routing\Registrar;
|
2018-08-25 21:43:21 +00:00
|
|
|
use Pterodactyl\Contracts\Extensions\HashidsInterface;
|
2021-08-05 05:20:43 +00:00
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
2018-02-28 03:28:43 +00:00
|
|
|
|
2021-08-05 05:20:43 +00:00
|
|
|
class SubstituteClientApiBindings
|
2018-02-28 03:28:43 +00:00
|
|
|
{
|
2021-08-07 18:55:49 +00:00
|
|
|
protected Registrar $router;
|
2021-08-05 05:20:43 +00:00
|
|
|
|
|
|
|
public function __construct(Registrar $router)
|
|
|
|
{
|
|
|
|
$this->router = $router;
|
|
|
|
}
|
|
|
|
|
2018-02-28 03:28:43 +00:00
|
|
|
/**
|
2021-08-05 05:20:43 +00:00
|
|
|
* Perform substitution of route parameters for the Client API.
|
2021-01-23 20:33:34 +00:00
|
|
|
*
|
2021-10-30 20:41:38 +00:00
|
|
|
* @param \Illuminate\Http\Request $request
|
2021-08-07 23:10:24 +00:00
|
|
|
*
|
2018-02-28 03:28:43 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle($request, Closure $next)
|
|
|
|
{
|
2021-08-05 05:20:43 +00:00
|
|
|
$this->router->bind('server', function ($value) {
|
|
|
|
return Server::query()->where(Str::length($value) === 8 ? 'uuidShort' : 'uuid', $value)->firstOrFail();
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->router->bind('allocation', function ($value, $route) {
|
2021-08-07 23:08:29 +00:00
|
|
|
return $this->server($route)->allocations()->findOrFail($value);
|
2021-08-05 05:20:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
$this->router->bind('schedule', function ($value, $route) {
|
2021-08-07 23:08:29 +00:00
|
|
|
return $this->server($route)->schedule()->findOrFail($value);
|
2018-02-28 03:28:43 +00:00
|
|
|
});
|
|
|
|
|
2021-08-07 22:45:36 +00:00
|
|
|
$this->router->bind('task', function ($value, $route) {
|
|
|
|
return Task::query()
|
|
|
|
->select('tasks.*')
|
|
|
|
->join('schedules', function (JoinClause $join) use ($route) {
|
|
|
|
$join->on('schedules.id', 'tasks.schedule_id')
|
|
|
|
->where('schedules.server_id', $route->parameter('server')->id);
|
|
|
|
})
|
|
|
|
->where('schedules.id', $route->parameter('schedule')->id)
|
2021-08-07 23:08:29 +00:00
|
|
|
->findOrFail($value);
|
2021-08-07 22:45:36 +00:00
|
|
|
});
|
|
|
|
|
2021-08-05 05:20:43 +00:00
|
|
|
$this->router->bind('database', function ($value, $route) {
|
2020-07-10 03:36:08 +00:00
|
|
|
$id = Container::getInstance()->make(HashidsInterface::class)->decodeFirst($value);
|
2018-08-25 21:43:21 +00:00
|
|
|
|
2021-08-07 23:08:29 +00:00
|
|
|
return $this->server($route)->databases()->findOrFail($id);
|
2018-08-25 21:43:21 +00:00
|
|
|
});
|
|
|
|
|
2021-08-05 05:20:43 +00:00
|
|
|
$this->router->bind('backup', function ($value, $route) {
|
|
|
|
return $this->server($route)->backups()->where('uuid', $value)->firstOrFail();
|
2020-04-05 02:54:59 +00:00
|
|
|
});
|
|
|
|
|
2021-08-07 18:15:44 +00:00
|
|
|
$this->router->bind('subuser', function ($value, $route) {
|
2021-08-05 05:20:43 +00:00
|
|
|
return $this->server($route)->subusers()
|
2021-08-07 18:15:44 +00:00
|
|
|
->select('subusers.*')
|
|
|
|
->join('users', 'subusers.user_id', '=', 'users.id')
|
2021-08-05 05:20:43 +00:00
|
|
|
->where('users.uuid', $value)
|
|
|
|
->firstOrFail();
|
2020-08-20 03:21:12 +00:00
|
|
|
});
|
|
|
|
|
2021-08-05 05:20:43 +00:00
|
|
|
try {
|
2021-08-07 23:10:24 +00:00
|
|
|
/* @var \Illuminate\Routing\Route $route */
|
2021-08-05 05:20:43 +00:00
|
|
|
$this->router->substituteBindings($route = $request->route());
|
|
|
|
} catch (ModelNotFoundException $exception) {
|
2021-10-30 20:41:38 +00:00
|
|
|
if (!empty($route) && $route->getMissing()) {
|
2021-08-05 05:20:43 +00:00
|
|
|
$route->getMissing()($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw $exception;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Plucks the server model off the route. If no server model is present a
|
|
|
|
* ModelNotFound exception will be thrown.
|
|
|
|
*
|
|
|
|
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
|
|
|
|
*/
|
|
|
|
private function server(Route $route): Server
|
|
|
|
{
|
|
|
|
$server = $route->parameter('server');
|
|
|
|
if (!$server instanceof Server) {
|
|
|
|
throw (new ModelNotFoundException())->setModel(Server::class, [$route->parameter('server')]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $server;
|
2018-02-28 03:28:43 +00:00
|
|
|
}
|
|
|
|
}
|