2019-03-23 17:47:20 -07:00
<? php
2026-10-05 18:10:18 -04:00
declare ( strict_types = 1 );
2019-03-23 17:47:20 -07:00
namespace Pterodactyl\Http\Controllers\Api\Client\Servers ;
2020-07-09 20:36:08 -07:00
use Illuminate\Http\JsonResponse ;
2026-10-05 18:10:18 -04:00
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\Allocations\AssignsAllocations ;
use Pterodactyl\Contracts\Allocations\SetsPrimaryAllocations ;
use Pterodactyl\Contracts\Allocations\UnassignsAllocations ;
use Pterodactyl\Contracts\Allocations\UpdatesAllocationNotes ;
2020-07-09 19:56:46 -07:00
use Pterodactyl\Exceptions\DisplayException ;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Extensions\Scribe\Attributes\ResponseFromTransformer ;
use Pterodactyl\Facades\Activity ;
use Pterodactyl\Facades\Fractal ;
2019-03-23 17:47:20 -07:00
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController ;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\DeleteAllocationRequest ;
2019-03-23 17:47:20 -07:00
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\GetNetworkRequest ;
2020-10-31 14:58:15 -07:00
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\NewAllocationRequest ;
2020-07-09 19:56:46 -07:00
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\SetPrimaryAllocationRequest ;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\UpdateAllocationRequest ;
use Pterodactyl\Models\Allocation ;
use Pterodactyl\Models\Server ;
use Pterodactyl\Support\JsonValueGuard ;
use Pterodactyl\Transformers\Api\Client\AllocationTransformer ;
2019-03-23 17:47:20 -07:00
2026-10-05 18:10:18 -04:00
#[ Group ( 'Client API' , 'Endpoints authenticated as a panel user using a client API token.' )]
#[ Subgroup ( 'Server Network' , 'View and manage network allocations for an accessible server.' )]
2020-07-09 20:36:08 -07:00
class NetworkAllocationController extends ClientApiController
2019-03-23 17:47:20 -07:00
{
2026-10-05 18:10:18 -04:00
private const array LIMIT_ERROR = [
'errors' => [
[
'code' => 'DisplayException' ,
'status' => '400' ,
'detail' => 'Cannot assign additional allocations to this server: limit has been reached.' ,
],
],
];
private const array DELETE_PRIMARY_ERROR = [
'errors' => [
[
'code' => 'DisplayException' ,
'status' => '400' ,
'detail' => 'You cannot delete the primary allocation for this server.' ,
],
],
];
2019-03-23 17:47:20 -07:00
/**
2022-10-14 10:59:20 -06:00
* Lists all the allocations available to a server and whether
* they are currently assigned as the primary for this server.
2026-10-05 18:10:18 -04:00
*
* @return ApiPayload
2019-03-23 17:47:20 -07:00
*/
2026-10-05 18:10:18 -04:00
#[ Endpoint ( 'List server allocations' , 'Returns all network allocations assigned to the server.' )]
#[ ResponseFromTransformer ( AllocationTransformer :: class , Allocation :: class , description : 'Server allocations returned.' , collection : true , factoryStates : [ 'withServer' ], resourceKey : 'allocation' )]
2019-09-05 21:41:20 -07:00
public function index ( GetNetworkRequest $request , Server $server ) : array
2019-03-23 17:47:20 -07:00
{
2026-10-05 18:10:18 -04:00
return Fractal :: collection ( $server -> allocations )
2020-07-09 19:56:46 -07:00
-> transformWith ( $this -> getTransformer ( AllocationTransformer :: class ))
2026-10-05 18:10:18 -04:00
-> toResponseArray ();
2020-07-09 19:56:46 -07:00
}
2020-07-09 20:36:08 -07:00
/**
2026-10-05 18:10:18 -04:00
* @return ApiPayload
2020-07-09 20:36:08 -07:00
*/
2026-10-05 18:10:18 -04:00
#[ Endpoint ( 'Update allocation notes' , 'Updates notes attached to a server allocation.' )]
#[ ResponseFromTransformer ( AllocationTransformer :: class , Allocation :: class , description : 'Allocation notes updated.' , factoryStates : [ 'withServer' ], resourceKey : 'allocation' )]
public function update ( UpdateAllocationRequest $request , UpdatesAllocationNotes $notes , Server $server , Allocation $allocation ) : array
2020-07-09 20:36:08 -07:00
{
2022-05-29 19:26:28 -04:00
$original = $allocation -> notes ;
2026-10-05 18:10:18 -04:00
$allocation = $notes -> update ( $allocation , JsonValueGuard :: nullableString ( $request -> validated ( 'notes' )));
2022-05-29 19:26:28 -04:00
if ( $original !== $allocation -> notes ) {
Activity :: event ( 'server:allocation.notes' )
-> subject ( $allocation )
-> property ([ 'allocation' => $allocation -> toString (), 'old' => $original , 'new' => $allocation -> notes ])
-> log ();
}
2020-07-09 20:36:08 -07:00
2026-10-05 18:10:18 -04:00
return Fractal :: item ( $allocation )
2020-07-09 20:36:08 -07:00
-> transformWith ( $this -> getTransformer ( AllocationTransformer :: class ))
2026-10-05 18:10:18 -04:00
-> toResponseArray ();
2020-07-09 20:36:08 -07:00
}
2020-07-09 19:56:46 -07:00
/**
* Set the primary allocation for a server.
*
2026-10-05 18:10:18 -04:00
* @return ApiPayload
2020-07-09 20:36:08 -07:00
*/
2026-10-05 18:10:18 -04:00
#[ Endpoint ( 'Set primary allocation' , 'Sets the primary network allocation for the server.' )]
#[ ResponseFromTransformer ( AllocationTransformer :: class , Allocation :: class , description : 'Primary allocation updated.' , factoryStates : [ 'withServer' ], resourceKey : 'allocation' )]
public function setPrimary ( SetPrimaryAllocationRequest $request , SetsPrimaryAllocations $allocations , Server $server , Allocation $allocation ) : array
2020-07-09 20:36:08 -07:00
{
2026-10-05 18:10:18 -04:00
$allocation = $allocations -> setPrimary ( $server , $allocation );
2020-07-09 20:36:08 -07:00
2022-05-29 19:26:28 -04:00
Activity :: event ( 'server:allocation.primary' )
-> subject ( $allocation )
-> property ( 'allocation' , $allocation -> toString ())
-> log ();
2026-10-05 18:10:18 -04:00
return Fractal :: item ( $allocation )
2020-09-28 11:50:34 -04:00
-> transformWith ( $this -> getTransformer ( AllocationTransformer :: class ))
2026-10-05 18:10:18 -04:00
-> toResponseArray ();
2020-09-28 11:50:34 -04:00
}
/**
2026-10-05 18:10:18 -04:00
* @return ApiPayload
2020-10-31 14:58:15 -07:00
*
2024-10-21 19:18:20 -06:00
* @throws DisplayException
2020-09-28 11:50:34 -04:00
*/
2026-10-05 18:10:18 -04:00
#[ Endpoint ( 'Create server allocation' , 'Assigns an available allocation to the server.' )]
#[ ResponseFromTransformer ( AllocationTransformer :: class , Allocation :: class , description : 'Allocation assigned.' , factoryStates : [ 'withServer' ], resourceKey : 'allocation' )]
#[ ScribeResponse ( self :: LIMIT_ERROR , status : 400 , description : 'The server has reached its configured allocation limit or no allocation is available.' )]
public function store ( NewAllocationRequest $request , AssignsAllocations $allocations , Server $server ) : array
2020-09-28 11:50:34 -04:00
{
2026-10-05 18:10:18 -04:00
$allocation = $allocations -> assign ( $server );
2020-10-31 21:57:27 -07:00
2026-10-05 18:10:18 -04:00
Activity :: event ( 'server:allocation.create' ) -> subject ( $allocation ) -> property ( 'allocation' , $allocation -> toString ()) -> log ();
2020-09-28 11:50:34 -04:00
2026-10-05 18:10:18 -04:00
return Fractal :: item ( $allocation )
2020-07-09 20:36:08 -07:00
-> transformWith ( $this -> getTransformer ( AllocationTransformer :: class ))
2026-10-05 18:10:18 -04:00
-> toResponseArray ();
2020-07-09 20:36:08 -07:00
}
/**
* Delete an allocation from a server.
*
2024-10-21 19:18:20 -06:00
* @throws DisplayException
2020-07-09 19:56:46 -07:00
*/
2026-10-05 18:10:18 -04:00
#[ Endpoint ( 'Delete server allocation' , 'Removes a non-primary allocation from the server and returns it to the available pool.' )]
#[ ScribeResponse ( status : 204 , description : 'Allocation removed.' )]
#[ ScribeResponse ( self :: DELETE_PRIMARY_ERROR , status : 400 , description : 'The allocation cannot be removed because it is primary or the server has no allocation limit.' )]
public function delete ( DeleteAllocationRequest $request , UnassignsAllocations $allocations , Server $server , Allocation $allocation ) : JsonResponse
2020-07-09 19:56:46 -07:00
{
2026-10-05 18:10:18 -04:00
$allocation = $allocations -> unassign ( $server , $allocation );
2019-03-23 17:47:20 -07:00
2022-05-29 19:26:28 -04:00
Activity :: event ( 'server:allocation.delete' )
-> subject ( $allocation )
-> property ( 'allocation' , $allocation -> toString ())
-> log ();
2020-07-09 20:36:08 -07:00
return new JsonResponse ([], JsonResponse :: HTTP_NO_CONTENT );
2019-03-23 17:47:20 -07:00
}
}