Path: blob/1.0-develop/app/Http/Middleware/Api/Client/Server/AuthenticateServerAccess.php
10280 views
<?php12namespace Pterodactyl\Http\Middleware\Api\Client\Server;34use Illuminate\Http\Request;5use Pterodactyl\Models\Server;6use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;7use Pterodactyl\Exceptions\Http\Server\ServerStateConflictException;89class AuthenticateServerAccess10{11/**12* Routes that this middleware should not apply to if the user is an admin.13*/14protected array $except = [15'api:client:server.ws',16];1718/**19* AuthenticateServerAccess constructor.20*/21public function __construct()22{23}2425/**26* Authenticate that this server exists and is not suspended or marked as installing.27*/28public function handle(Request $request, \Closure $next): mixed29{30/** @var \Pterodactyl\Models\User $user */31$user = $request->user();32$server = $request->route()->parameter('server');3334if (!$server instanceof Server) {35throw new NotFoundHttpException(trans('exceptions.api.resource_not_found'));36}3738// At the very least, ensure that the user trying to make this request is the39// server owner, a subuser, or a root admin. We'll leave it up to the controllers40// to authenticate more detailed permissions if needed.41if ($user->id !== $server->owner_id && !$user->root_admin) {42// Check for subuser status.43if (!$server->subusers->contains('user_id', $user->id)) {44throw new NotFoundHttpException(trans('exceptions.api.resource_not_found'));45}46}4748try {49$server->validateCurrentState();50} catch (ServerStateConflictException $exception) {51// Still allow users to get information about their server if it is installing or52// being transferred.53if (!$request->routeIs('api:client:server.view')) {54if (($server->isSuspended() || $server->node->isUnderMaintenance()) && !$request->routeIs('api:client:server.resources')) {55throw $exception;56}57if (!$user->root_admin || !$request->routeIs($this->except)) {58throw $exception;59}60}61}6263$request->attributes->set('server', $server);6465return $next($request);66}67}686970