2018-02-25 15:30:56 -06:00
<? php
2026-10-05 18:10:18 -04:00
declare ( strict_types = 1 );
2018-02-25 15:30:56 -06:00
namespace Pterodactyl\Http\Controllers\Api\Client ;
2026-10-05 18:10:18 -04:00
use Knuckles\Scribe\Attributes\Endpoint ;
use Knuckles\Scribe\Attributes\Group ;
use Knuckles\Scribe\Attributes\QueryParam ;
use Knuckles\Scribe\Attributes\Response as ScribeResponse ;
use Knuckles\Scribe\Attributes\Subgroup ;
use League\Fractal\Pagination\IlluminatePaginatorAdapter ;
use Pterodactyl\Extensions\Scribe\Attributes\ResponseFromTransformer ;
use Pterodactyl\Facades\Fractal ;
use Pterodactyl\Http\Requests\Api\Client\GetServersRequest ;
2020-10-15 21:21:38 -07:00
use Pterodactyl\Models\Filters\MultiFieldServerFilter ;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\Server ;
use Pterodactyl\Services\Servers\SubuserPermissionCatalog ;
2018-02-27 21:28:43 -06:00
use Pterodactyl\Transformers\Api\Client\ServerTransformer ;
2026-10-05 18:10:18 -04:00
use Spatie\QueryBuilder\AllowedFilter ;
use Spatie\QueryBuilder\QueryBuilder ;
2018-02-25 15:30:56 -06:00
2026-10-05 18:10:18 -04:00
#[ Group ( 'Client API' , 'Endpoints authenticated as a panel user using a client API token.' )]
#[ Subgroup ( 'Client Overview' , 'Discover servers and permissions available to the authenticated user.' )]
2018-02-25 15:30:56 -06:00
class ClientController extends ClientApiController
{
2026-10-05 18:10:18 -04:00
private const array PERMISSIONS_EXAMPLE = [
'object' => 'system_permissions' ,
'attributes' => [
'permissions' => [
'websocket' => [
'description' => 'Allows the user to connect to the server websocket, giving them access to view console output and realtime server stats.' ,
'keys' => [
'connect' => 'Allows a user to connect to the websocket instance for a server to stream the console.' ,
],
],
'control' => [
'description' => "Permissions that control a user's ability to control the power state of a server, or send commands." ,
'keys' => [
'console' => 'Allows a user to send commands to the server instance via the console.' ,
'start' => 'Allows a user to start the server if it is stopped.' ,
'stop' => 'Allows a user to stop a server if it is running.' ,
'restart' => 'Allows a user to perform a server restart.' ,
],
],
],
],
];
2018-02-27 21:28:43 -06:00
/**
2022-10-14 10:59:20 -06:00
* Return all the servers available to the client making the API
2018-02-27 21:28:43 -06:00
* request, including servers the user has access to as a subuser.
2026-10-05 18:10:18 -04:00
*
* @return ApiPayload
2018-02-27 21:28:43 -06:00
*/
2026-10-05 18:10:18 -04:00
#[ Endpoint ( 'List client servers' , 'Returns a paginated list of servers the authenticated user can access as an owner, subuser, or administrator.' )]
#[ QueryParam ( 'page' , 'integer' , 'Page number to return.' , required : false , example : 1 )]
#[ QueryParam ( 'per_page' , 'integer' , 'Number of servers to return per page. The maximum is 100.' , required : false , example : 50 )]
#[ QueryParam ( 'type' , 'string' , 'Scope of servers to return. "owner" returns owned servers, "admin" returns admin-accessible servers excluding owned or subuser servers, and "admin-all" returns all servers for root administrators.' , required : false , example : 'owner' , enum : [ 'owner' , 'admin' , 'admin-all' ])]
#[ QueryParam ( 'filter[uuid]' , 'string' , 'Filter servers by UUID.' , required : false , example : '4fcb1f44-0f90-4a1a-a8bf-7cc8a14d0f26' )]
#[ QueryParam ( 'filter[name]' , 'string' , 'Filter servers by name.' , required : false , example : 'Minecraft Server' )]
#[ QueryParam ( 'filter[description]' , 'string' , 'Filter servers by description.' , required : false , example : 'Production' )]
#[ QueryParam ( 'filter[external_id]' , 'string' , 'Filter servers by external identifier.' , required : false , example : 'billing-server-42' )]
#[ QueryParam ( 'filter[*]' , 'string' , 'Search across server name, UUID, short UUID, external identifier, or allocation address and port.' , required : false , example : '192.0.2.10:25565' )]
#[ ResponseFromTransformer ( ServerTransformer :: class , Server :: class , description : 'Accessible servers returned.' , collection : true , factoryStates : [ 'withRelationships' ], resourceKey : 'server' , paginate : [ IlluminatePaginatorAdapter :: class , 50 ])]
2018-02-27 21:28:43 -06:00
public function index ( GetServersRequest $request ) : array
{
2020-07-06 21:25:00 -07:00
$user = $request -> user ();
$transformer = $this -> getTransformer ( ServerTransformer :: class );
// Start the query builder and ensure we eager load any requested relationships from the request.
2020-07-26 10:43:46 -07:00
$builder = QueryBuilder :: for (
Server :: query () -> with ( $this -> getIncludesForTransformer ( $transformer , [ 'node' ]))
2020-10-15 21:21:38 -07:00
) -> allowedFilters ([
'uuid' ,
'name' ,
2022-06-26 13:26:12 -04:00
'description' ,
2020-10-15 21:21:38 -07:00
'external_id' ,
2026-10-05 18:10:18 -04:00
AllowedFilter :: custom ( '*' , new MultiFieldServerFilter ),
2020-10-15 21:21:38 -07:00
]);
2020-07-06 21:25:00 -07:00
2026-10-05 18:10:18 -04:00
$type = $request -> validated ( 'type' );
2022-10-14 10:59:20 -06:00
// Either return all the servers the user has access to because they are an admin `?type=admin` or
// just return all the servers the user has access to because they are the owner or a subuser of the
2020-10-15 21:21:38 -07:00
// server. If ?type=admin-all is passed all servers on the system will be returned to the user, rather
// than only servers they can see because they are an admin.
if ( in_array ( $type , [ 'admin' , 'admin-all' ])) {
// If they aren't an admin but want all the admin servers don't fail the request, just
// make it a query that will never return any results back.
2026-10-05 18:10:18 -04:00
if ( ! $user -> root_admin ) {
2020-10-15 21:21:38 -07:00
$builder -> whereRaw ( '1 = 2' );
} else {
$builder = $type === 'admin-all'
? $builder
: $builder -> whereNotIn ( 'servers.id' , $user -> accessibleServers () -> pluck ( 'id' ) -> all ());
}
2021-01-23 12:09:16 -08:00
} elseif ( $type === 'owner' ) {
2020-10-15 21:21:38 -07:00
$builder = $builder -> where ( 'servers.owner_id' , $user -> id );
2020-07-26 10:43:46 -07:00
} else {
2020-10-15 21:21:38 -07:00
$builder = $builder -> whereIn ( 'servers.id' , $user -> accessibleServers () -> pluck ( 'id' ) -> all ());
2019-08-03 12:44:15 -07:00
}
2026-10-05 18:10:18 -04:00
$servers = $builder -> paginate ( min ( $request -> integer ( 'per_page' , 50 ), 100 )) -> appends ( $request -> query ());
2018-02-27 21:28:43 -06:00
2026-10-05 18:10:18 -04:00
return Fractal :: transformWith ( $transformer ) -> collection ( $servers ) -> toResponseArray ();
2018-02-27 21:28:43 -06:00
}
2019-11-03 17:37:06 -08:00
/**
2026-10-05 18:10:18 -04:00
* Returns all the subuser permissions available on the system, including those
* registered by enabled extensions.
*
* @return array{object: string, attributes: array<string, mixed>}
2019-11-03 17:37:06 -08:00
*/
2026-10-05 18:10:18 -04:00
#[ Endpoint ( 'List server permissions' , 'Returns the permission keys that can be granted to server subusers, including those registered by enabled extensions under `ext.<extension id>`.' )]
#[ ScribeResponse ( self :: PERMISSIONS_EXAMPLE , description : 'System permission definitions returned.' )]
public function permissions ( SubuserPermissionCatalog $catalog ) : array
2019-11-03 17:37:06 -08:00
{
return [
'object' => 'system_permissions' ,
'attributes' => [
2026-10-05 18:10:18 -04:00
'permissions' => $catalog -> groups (),
2019-11-03 17:37:06 -08:00
],
];
}
2018-02-25 15:30:56 -06:00
}