2019-03-24 00:47:20 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
|
|
|
|
|
|
|
|
use Pterodactyl\Models\Server;
|
2020-07-10 03:36:08 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2022-05-29 23:26:28 +00:00
|
|
|
use Pterodactyl\Facades\Activity;
|
2020-07-10 03:36:08 +00:00
|
|
|
use Pterodactyl\Models\Allocation;
|
2020-07-10 02:56:46 +00:00
|
|
|
use Pterodactyl\Exceptions\DisplayException;
|
|
|
|
use Pterodactyl\Repositories\Eloquent\ServerRepository;
|
2019-09-06 04:41:20 +00:00
|
|
|
use Pterodactyl\Repositories\Eloquent\AllocationRepository;
|
2019-03-24 00:47:20 +00:00
|
|
|
use Pterodactyl\Transformers\Api\Client\AllocationTransformer;
|
|
|
|
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
|
2020-10-31 21:58:15 +00:00
|
|
|
use Pterodactyl\Services\Allocations\FindAssignableAllocationService;
|
2019-03-24 00:47:20 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\GetNetworkRequest;
|
2020-10-31 21:58:15 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\NewAllocationRequest;
|
2020-07-10 03:36:08 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\DeleteAllocationRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\UpdateAllocationRequest;
|
2020-07-10 02:56:46 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\SetPrimaryAllocationRequest;
|
2019-03-24 00:47:20 +00:00
|
|
|
|
2020-07-10 03:36:08 +00:00
|
|
|
class NetworkAllocationController extends ClientApiController
|
2019-03-24 00:47:20 +00:00
|
|
|
{
|
|
|
|
/**
|
2019-09-06 04:41:20 +00:00
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\AllocationRepository
|
2019-03-24 00:47:20 +00:00
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
2020-07-10 02:56:46 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\ServerRepository
|
|
|
|
*/
|
|
|
|
private $serverRepository;
|
|
|
|
|
2020-09-28 15:50:34 +00:00
|
|
|
/**
|
2020-10-31 21:58:15 +00:00
|
|
|
* @var \Pterodactyl\Services\Allocations\FindAssignableAllocationService
|
2020-09-28 15:50:34 +00:00
|
|
|
*/
|
2020-10-31 21:58:15 +00:00
|
|
|
private $assignableAllocationService;
|
2020-09-28 15:50:34 +00:00
|
|
|
|
2019-03-24 00:47:20 +00:00
|
|
|
/**
|
|
|
|
* NetworkController constructor.
|
2020-09-28 20:14:14 +00:00
|
|
|
*/
|
2020-07-10 02:56:46 +00:00
|
|
|
public function __construct(
|
|
|
|
AllocationRepository $repository,
|
2020-09-28 15:50:34 +00:00
|
|
|
ServerRepository $serverRepository,
|
2020-10-31 21:58:15 +00:00
|
|
|
FindAssignableAllocationService $assignableAllocationService
|
2020-07-10 02:56:46 +00:00
|
|
|
) {
|
2019-03-24 00:47:20 +00:00
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->repository = $repository;
|
2020-07-10 02:56:46 +00:00
|
|
|
$this->serverRepository = $serverRepository;
|
2020-10-31 21:58:15 +00:00
|
|
|
$this->assignableAllocationService = $assignableAllocationService;
|
2019-03-24 00:47:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lists all of the allocations available to a server and wether or
|
|
|
|
* not they are currently assigned as the primary for this server.
|
|
|
|
*/
|
2019-09-06 04:41:20 +00:00
|
|
|
public function index(GetNetworkRequest $request, Server $server): array
|
2019-03-24 00:47:20 +00:00
|
|
|
{
|
2020-07-10 02:56:46 +00:00
|
|
|
return $this->fractal->collection($server->allocations)
|
|
|
|
->transformWith($this->getTransformer(AllocationTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
2020-07-10 03:36:08 +00:00
|
|
|
/**
|
|
|
|
* Set the primary allocation for a server.
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
*/
|
|
|
|
public function update(UpdateAllocationRequest $request, Server $server, Allocation $allocation): array
|
|
|
|
{
|
2022-05-29 23:26:28 +00:00
|
|
|
$original = $allocation->notes;
|
|
|
|
|
|
|
|
$allocation->forceFill(['notes' => $request->input('notes')])->save();
|
|
|
|
|
|
|
|
if ($original !== $allocation->notes) {
|
|
|
|
Activity::event('server:allocation.notes')
|
|
|
|
->subject($allocation)
|
|
|
|
->property(['allocation' => $allocation->toString(), 'old' => $original, 'new' => $allocation->notes])
|
|
|
|
->log();
|
|
|
|
}
|
2020-07-10 03:36:08 +00:00
|
|
|
|
|
|
|
return $this->fractal->item($allocation)
|
|
|
|
->transformWith($this->getTransformer(AllocationTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
2020-07-10 02:56:46 +00:00
|
|
|
/**
|
|
|
|
* Set the primary allocation for a server.
|
|
|
|
*
|
2020-07-10 03:36:08 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
2020-07-10 02:56:46 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
2020-07-10 03:36:08 +00:00
|
|
|
*/
|
|
|
|
public function setPrimary(SetPrimaryAllocationRequest $request, Server $server, Allocation $allocation): array
|
|
|
|
{
|
|
|
|
$this->serverRepository->update($server->id, ['allocation_id' => $allocation->id]);
|
|
|
|
|
2022-05-29 23:26:28 +00:00
|
|
|
Activity::event('server:allocation.primary')
|
|
|
|
->subject($allocation)
|
|
|
|
->property('allocation', $allocation->toString())
|
|
|
|
->log();
|
|
|
|
|
2020-09-28 15:50:34 +00:00
|
|
|
return $this->fractal->item($allocation)
|
|
|
|
->transformWith($this->getTransformer(AllocationTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the notes for the allocation for a server.
|
2021-01-23 20:09:16 +00:00
|
|
|
*s.
|
2020-10-31 21:58:15 +00:00
|
|
|
*
|
2020-09-28 15:50:34 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
|
|
|
*/
|
2020-10-31 21:58:15 +00:00
|
|
|
public function store(NewAllocationRequest $request, Server $server): array
|
2020-09-28 15:50:34 +00:00
|
|
|
{
|
2020-11-01 04:57:27 +00:00
|
|
|
if ($server->allocations()->count() >= $server->allocation_limit) {
|
2021-01-23 20:33:34 +00:00
|
|
|
throw new DisplayException('Cannot assign additional allocations to this server: limit has been reached.');
|
2020-11-01 04:57:27 +00:00
|
|
|
}
|
|
|
|
|
2020-10-31 21:58:15 +00:00
|
|
|
$allocation = $this->assignableAllocationService->handle($server);
|
2020-09-28 15:50:34 +00:00
|
|
|
|
2022-05-29 23:26:28 +00:00
|
|
|
Activity::event('server:allocation.create')
|
|
|
|
->subject($allocation)
|
|
|
|
->property('allocation', $allocation->toString())
|
|
|
|
->log();
|
|
|
|
|
2020-07-10 03:36:08 +00:00
|
|
|
return $this->fractal->item($allocation)
|
|
|
|
->transformWith($this->getTransformer(AllocationTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete an allocation from a server.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
2020-07-10 02:56:46 +00:00
|
|
|
*/
|
2020-07-10 03:36:08 +00:00
|
|
|
public function delete(DeleteAllocationRequest $request, Server $server, Allocation $allocation)
|
2020-07-10 02:56:46 +00:00
|
|
|
{
|
2022-05-07 19:05:28 +00:00
|
|
|
// Don't allow the deletion of allocations if the server does not have an
|
|
|
|
// allocation limit set.
|
|
|
|
if (empty($server->allocation_limit)) {
|
|
|
|
throw new DisplayException('You cannot delete allocations for this server: no allocation limit is set.');
|
|
|
|
}
|
|
|
|
|
2020-07-10 03:36:08 +00:00
|
|
|
if ($allocation->id === $server->allocation_id) {
|
2021-01-23 20:33:34 +00:00
|
|
|
throw new DisplayException('You cannot delete the primary allocation for this server.');
|
2020-07-10 02:56:46 +00:00
|
|
|
}
|
|
|
|
|
2020-11-01 04:22:44 +00:00
|
|
|
Allocation::query()->where('id', $allocation->id)->update([
|
|
|
|
'notes' => null,
|
|
|
|
'server_id' => null,
|
|
|
|
]);
|
2019-03-24 00:47:20 +00:00
|
|
|
|
2022-05-29 23:26:28 +00:00
|
|
|
Activity::event('server:allocation.delete')
|
|
|
|
->subject($allocation)
|
|
|
|
->property('allocation', $allocation->toString())
|
|
|
|
->log();
|
|
|
|
|
2020-07-10 03:36:08 +00:00
|
|
|
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
|
2019-03-24 00:47:20 +00:00
|
|
|
}
|
|
|
|
}
|