misc_pterodactyl-panel/app/Http/Controllers/Api/Application/Nodes/AllocationController.php

98 lines
3.6 KiB
PHP
Raw Normal View History

<?php
2018-01-20 01:58:57 +00:00
namespace Pterodactyl\Http\Controllers\Api\Application\Nodes;
2018-01-13 20:08:19 +00:00
use Pterodactyl\Models\Node;
2020-07-07 03:05:42 +00:00
use Illuminate\Http\JsonResponse;
use Pterodactyl\Models\Allocation;
use Pterodactyl\Services\Allocations\AssignmentService;
use Pterodactyl\Services\Allocations\AllocationDeletionService;
use Pterodactyl\Transformers\Api\Application\AllocationTransformer;
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
2018-01-20 01:58:57 +00:00
use Pterodactyl\Http\Requests\Api\Application\Allocations\GetAllocationsRequest;
use Pterodactyl\Http\Requests\Api\Application\Allocations\StoreAllocationRequest;
use Pterodactyl\Http\Requests\Api\Application\Allocations\DeleteAllocationRequest;
class AllocationController extends ApplicationApiController
{
/**
* @var \Pterodactyl\Services\Allocations\AssignmentService
*/
private $assignmentService;
/**
* @var \Pterodactyl\Services\Allocations\AllocationDeletionService
*/
private $deletionService;
/**
* AllocationController constructor.
*
2019-09-06 04:32:57 +00:00
* @param \Pterodactyl\Services\Allocations\AssignmentService $assignmentService
* @param \Pterodactyl\Services\Allocations\AllocationDeletionService $deletionService
*/
public function __construct(
AssignmentService $assignmentService,
2020-07-07 03:05:42 +00:00
AllocationDeletionService $deletionService
) {
parent::__construct();
$this->assignmentService = $assignmentService;
$this->deletionService = $deletionService;
}
/**
* Return all of the allocations that exist for a given node.
*
2018-01-20 01:58:57 +00:00
* @param \Pterodactyl\Http\Requests\Api\Application\Allocations\GetAllocationsRequest $request
2020-07-07 03:05:42 +00:00
* @param \Pterodactyl\Models\Node $node
*
* @return array
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
2020-07-07 03:05:42 +00:00
public function index(GetAllocationsRequest $request, Node $node): array
{
2020-07-07 03:05:42 +00:00
$allocations = $node->allocations()->paginate(50);
return $this->fractal->collection($allocations)
->transformWith($this->getTransformer(AllocationTransformer::class))
->toArray();
}
/**
* Store new allocations for a given node.
*
* @param \Pterodactyl\Http\Requests\Api\Application\Allocations\StoreAllocationRequest $request
2020-07-07 03:05:42 +00:00
* @param \Pterodactyl\Models\Node $node
* @return \Illuminate\Http\JsonResponse
*
* @throws \Pterodactyl\Exceptions\Service\Allocation\CidrOutOfRangeException
* @throws \Pterodactyl\Exceptions\Service\Allocation\InvalidPortMappingException
* @throws \Pterodactyl\Exceptions\Service\Allocation\PortOutOfRangeException
* @throws \Pterodactyl\Exceptions\Service\Allocation\TooManyPortsInRangeException
*/
2020-07-07 03:05:42 +00:00
public function store(StoreAllocationRequest $request, Node $node): JsonResponse
{
2020-07-07 03:05:42 +00:00
$this->assignmentService->handle($node, $request->validated());
2020-07-07 03:05:42 +00:00
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
}
/**
* Delete a specific allocation from the Panel.
*
* @param \Pterodactyl\Http\Requests\Api\Application\Allocations\DeleteAllocationRequest $request
2020-07-07 03:05:42 +00:00
* @param \Pterodactyl\Models\Node $node
* @param \Pterodactyl\Models\Allocation $allocation
* @return \Illuminate\Http\JsonResponse
*
* @throws \Pterodactyl\Exceptions\Service\Allocation\ServerUsingAllocationException
*/
2020-07-07 03:05:42 +00:00
public function delete(DeleteAllocationRequest $request, Node $node, Allocation $allocation): JsonResponse
{
2020-07-07 03:05:42 +00:00
$this->deletionService->handle($allocation);
2020-07-07 03:05:42 +00:00
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
}
}