2026-10-05 18:10:18 -04:00
<? php
declare ( strict_types = 1 );
namespace Pterodactyl\Http\Controllers\Api\Admin\Servers ;
use Illuminate\Http\Response ;
use Knuckles\Scribe\Attributes\Endpoint ;
use Knuckles\Scribe\Attributes\Group ;
use Knuckles\Scribe\Attributes\Response as ScribeResponse ;
use Knuckles\Scribe\Attributes\Subgroup ;
use Pterodactyl\Contracts\Servers\AttachesMountsToServers ;
use Pterodactyl\Contracts\Servers\DetachesMountsFromServers ;
use Pterodactyl\Facades\Activity ;
use Pterodactyl\Http\Controllers\Api\Admin\AdminApiController ;
use Pterodactyl\Http\Controllers\Api\Admin\Mounts\MountController ;
use Pterodactyl\Http\Requests\Api\Admin\Servers\DeleteServerMountRequest ;
use Pterodactyl\Http\Requests\Api\Admin\Servers\GetServerMountsRequest ;
use Pterodactyl\Http\Requests\Api\Admin\Servers\StoreServerMountRequest ;
2026-10-11 21:40:18 -04:00
use Pterodactyl\Http\Resources\Api\Admin\MountResource ;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\Mount ;
use Pterodactyl\Models\Server ;
use Symfony\Component\HttpKernel\Exception\HttpException ;
#[ Group ( 'Admin API' , 'Root administrator endpoints for managing panel configuration and resources.' )]
#[ Subgroup ( 'Server Mounts' , 'Attach and detach eligible mount definitions for a server.' )]
class ServerMountController extends AdminApiController
{
private const array SERVER_MOUNT_EXAMPLE = [
'object' => 'mount' ,
'attributes' => [
... MountController :: MOUNT_EXAMPLE [ 'attributes' ],
'mounted' => true ,
],
];
private const array SERVER_MOUNT_LIST_EXAMPLE = [
'object' => 'list' ,
'data' => [
self :: SERVER_MOUNT_EXAMPLE ,
],
];
private const array INSTALL_STATE_ERROR = [
'errors' => [
[
'code' => 'HttpException' ,
'status' => '403' ,
'detail' => 'Access to this resource is not allowed due to the current installation state.' ,
],
],
];
/**
* List server mounts.
*
* @return array{object: string, data: list<array{object: string, attributes: array<string, mixed>}>}
*/
#[ Endpoint ( 'List server mounts' , 'Returns mount definitions that are eligible for a server, including whether each mount is attached.' )]
#[ ScribeResponse ( self :: SERVER_MOUNT_LIST_EXAMPLE , description : 'Eligible server mounts returned.' )]
#[ ScribeResponse ( self :: INSTALL_STATE_ERROR , status : 403 , description : 'The server is not installed.' )]
2026-10-08 19:17:30 -04:00
public function index ( GetServerMountsRequest $request , Server $server ) : array
2026-10-05 18:10:18 -04:00
{
$this -> assertServerInstalled ( $server );
2026-10-08 19:17:30 -04:00
$mounts = Mount :: query () -> availableToServer ( $server ) -> get ();
2026-10-05 18:10:18 -04:00
$mountedIds = $server -> mounts -> pluck ( 'id' ) -> all ();
2026-10-11 21:40:18 -04:00
$data = array_values ( $mounts -> map ( fn ( Mount $mount ) : array => array_merge ( MountResource :: make ( $mount ) -> toArray ( $request ), [
2026-10-05 18:10:18 -04:00
'mounted' => in_array ( $mount -> id , $mountedIds , true ),
])) -> all ());
return [
'object' => 'list' ,
'data' => array_map ( fn ( array $attributes ) : array => [
'object' => Mount :: RESOURCE_NAME ,
'attributes' => $attributes ,
], $data ),
];
}
/**
* Create server mount.
*/
#[ Endpoint ( 'Attach server mount' , 'Attaches an eligible mount definition to a server.' )]
#[ ScribeResponse ( status : 204 , description : 'Mount attached.' )]
#[ ScribeResponse ( self :: INSTALL_STATE_ERROR , status : 403 , description : 'The server is not installed.' )]
public function store ( StoreServerMountRequest $request , AttachesMountsToServers $attach , Server $server ) : Response
{
$this -> assertServerInstalled ( $server );
2026-10-11 07:09:19 -04:00
$mount = $attach -> attach ( $server , $request -> safe () -> integer ( 'mount_id' ));
2026-10-05 18:10:18 -04:00
Activity :: event ( 'admin:server.mount' )
-> subject ( $server , $mount )
-> property ( 'name' , $mount -> name )
-> log ();
return $this -> returnNoContent ();
}
/**
* Delete server mount.
*/
#[ Endpoint ( 'Detach server mount' , 'Detaches a mount definition from a server.' )]
#[ ScribeResponse ( status : 204 , description : 'Mount detached.' )]
#[ ScribeResponse ( self :: INSTALL_STATE_ERROR , status : 403 , description : 'The server is not installed.' )]
public function destroy ( DeleteServerMountRequest $request , DetachesMountsFromServers $detach , Server $server , Mount $mount ) : Response
{
$this -> assertServerInstalled ( $server );
$detach -> detach ( $server , $mount );
Activity :: event ( 'admin:server.unmount' )
-> subject ( $server , $mount )
-> property ( 'name' , $mount -> name )
-> log ();
return $this -> returnNoContent ();
}
private function assertServerInstalled ( Server $server ) : void
{
throw_unless ( $server -> isInstalled (), HttpException :: class , Response :: HTTP_FORBIDDEN , 'Access to this resource is not allowed due to the current installation state.' );
}
}