Files
pterodactyl-panel/app/Http/Middleware/Api/Client/Server/AuthenticateServerAccess.php
T

70 lines
2.4 KiB
PHP
Raw Normal View History

2018-08-25 15:07:42 -07:00
<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
2018-08-25 15:07:42 -07:00
namespace Pterodactyl\Http\Middleware\Api\Client\Server;
2026-10-05 18:10:18 -04:00
use Closure;
2018-08-25 15:07:42 -07:00
use Illuminate\Http\Request;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Exceptions\Http\Server\ServerStateConflictException;
use Pterodactyl\Http\Concerns\ResolvesRequestContext;
2018-08-25 15:07:42 -07:00
use Pterodactyl\Models\Server;
2026-10-05 18:10:18 -04:00
use Symfony\Component\HttpFoundation\Response;
2018-08-25 15:07:42 -07:00
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class AuthenticateServerAccess
{
2026-10-05 18:10:18 -04:00
use ResolvesRequestContext;
/**
* Routes that this middleware should not apply to if the user is an admin.
2026-10-05 18:10:18 -04:00
*
* @var list<string>
*/
2022-10-14 10:59:20 -06:00
protected array $except = [
'api:client:server.ws',
];
2018-08-25 15:07:42 -07:00
/**
2026-10-05 18:10:18 -04:00
* Authenticate that this server exists and is not suspended or marked as installing.
2018-08-25 15:07:42 -07:00
*/
/**
2026-10-05 18:10:18 -04:00
* @param Closure(Request):Response $next
* @return Response
2018-08-25 15:07:42 -07:00
*/
2026-10-05 18:10:18 -04:00
public function handle(Request $request, Closure $next): mixed
2018-08-25 15:07:42 -07:00
{
2026-10-05 18:10:18 -04:00
$user = $this->authenticatedUser($request);
$server = $this->resolvedRoute($request)->parameter('server');
2018-08-25 15:07:42 -07:00
2026-10-05 18:10:18 -04:00
if (! $server instanceof Server) {
throw new NotFoundHttpException(trans('exceptions.api.resource_not_found'));
2018-08-25 15:07:42 -07:00
}
// At the very least, ensure that the user trying to make this request is the
// server owner, a subuser, or a root admin. We'll leave it up to the controllers
// to authenticate more detailed permissions if needed.
2026-10-05 18:10:18 -04:00
if ($user->id !== $server->owner_id && ! $user->root_admin) {
// Check for subuser status.
2026-10-05 18:10:18 -04:00
if ($server->subusers->doesntContain('user_id', $user->id)) {
throw new NotFoundHttpException(trans('exceptions.api.resource_not_found'));
}
}
try {
$server->validateCurrentState();
2026-10-05 18:10:18 -04:00
} catch (ServerStateConflictException $serverStateConflictException) {
// Still allow users to get information about their server if it is installing or
// being transferred.
2026-10-05 18:10:18 -04:00
if (! $request->routeIs('api:client:server.view')) {
throw_if(($server->isSuspended() || $server->node->isUnderMaintenance()) && ! $request->routeIs('api:client:server.resources'), $serverStateConflictException);
throw_if(! $user->root_admin || ! $request->routeIs($this->except), $serverStateConflictException);
}
2018-08-25 15:07:42 -07:00
}
$request->attributes->set('server', $server);
return $next($request);
}
}