Add support for changing the server default allocation as a normal user
This commit is contained in:
parent
5a3428f0a0
commit
d50ea18598
14 changed files with 308 additions and 68 deletions
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Exceptions\Service\Allocation;
|
||||
|
||||
use Pterodactyl\Exceptions\PterodactylException;
|
||||
|
||||
class AllocationDoesNotBelongToServerException extends PterodactylException
|
||||
{
|
||||
}
|
|
@ -41,11 +41,14 @@ class DatabaseController extends Controller
|
|||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\View\View
|
||||
*
|
||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||
*/
|
||||
public function index(Request $request): View
|
||||
{
|
||||
$server = $request->attributes->get('server');
|
||||
$this->injectJavascript();
|
||||
$this->authorize('view-databases', $server);
|
||||
$this->setRequest($request)->injectJavascript();
|
||||
|
||||
return view('server.databases.index', [
|
||||
'databases' => $this->repository->getDatabasesForServer($server->id),
|
||||
|
@ -58,11 +61,14 @@ class DatabaseController extends Controller
|
|||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*
|
||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
*/
|
||||
public function update(Request $request): JsonResponse
|
||||
{
|
||||
$this->authorize('reset-db-password', $request->attributes->get('server'));
|
||||
|
||||
$password = str_random(20);
|
||||
$this->passwordService->handle($request->attributes->get('database'), $password);
|
||||
|
||||
|
|
|
@ -86,28 +86,6 @@ class ServerController extends Controller
|
|||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the database overview for a server.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param string $uuid
|
||||
* @return \Illuminate\View\View
|
||||
*/
|
||||
public function getDatabases(Request $request, $uuid)
|
||||
{
|
||||
$server = Models\Server::byUuid($uuid);
|
||||
$this->authorize('view-databases', $server);
|
||||
|
||||
$server->load('node', 'databases.host');
|
||||
$server->js();
|
||||
|
||||
return view('server.settings.databases', [
|
||||
'server' => $server,
|
||||
'node' => $server->node,
|
||||
'databases' => $server->databases,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the SFTP overview for a server.
|
||||
*
|
||||
|
|
|
@ -0,0 +1,97 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Http\Controllers\Server\Settings;
|
||||
|
||||
use Illuminate\View\View;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Pterodactyl\Http\Controllers\Controller;
|
||||
use Pterodactyl\Contracts\Extensions\HashidsInterface;
|
||||
use Pterodactyl\Traits\Controllers\JavascriptInjection;
|
||||
use Pterodactyl\Services\Allocations\SetDefaultAllocationService;
|
||||
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
|
||||
use Pterodactyl\Exceptions\Service\Allocation\AllocationDoesNotBelongToServerException;
|
||||
|
||||
class AllocationController extends Controller
|
||||
{
|
||||
use JavascriptInjection;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Allocations\SetDefaultAllocationService
|
||||
*/
|
||||
private $defaultAllocationService;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Extensions\HashidsInterface
|
||||
*/
|
||||
private $hashids;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface
|
||||
*/
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* AllocationController constructor.
|
||||
*
|
||||
* @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $repository
|
||||
* @param \Pterodactyl\Contracts\Extensions\HashidsInterface $hashids
|
||||
* @param \Pterodactyl\Services\Allocations\SetDefaultAllocationService $defaultAllocationService
|
||||
*/
|
||||
public function __construct(
|
||||
AllocationRepositoryInterface $repository,
|
||||
HashidsInterface $hashids,
|
||||
SetDefaultAllocationService $defaultAllocationService
|
||||
) {
|
||||
$this->defaultAllocationService = $defaultAllocationService;
|
||||
$this->hashids = $hashids;
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the allocation management overview page for a server.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\View\View
|
||||
*
|
||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||
*/
|
||||
public function index(Request $request): View
|
||||
{
|
||||
$server = $request->attributes->get('server');
|
||||
$this->authorize('view-allocations', $server);
|
||||
$this->setRequest($request)->injectJavascript();
|
||||
|
||||
return view('server.settings.allocation', [
|
||||
'allocations' => $this->repository->findWhere([['server_id', '=', $server->id]]),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the default allocation for a server.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*
|
||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
*/
|
||||
public function update(Request $request): JsonResponse
|
||||
{
|
||||
$server = $request->attributes->get('server');
|
||||
$this->authorize('edit-allocation', $server);
|
||||
|
||||
$allocation = $this->hashids->decodeFirst($request->input('allocation'), 0);
|
||||
|
||||
try {
|
||||
$this->defaultAllocationService->handle($server->id, $allocation);
|
||||
} catch (AllocationDoesNotBelongToServerException $exception) {
|
||||
return response()->json(['error' => 'No matching allocation was located for this server.'], 404);
|
||||
}
|
||||
|
||||
return response()->json();
|
||||
}
|
||||
}
|
|
@ -64,6 +64,16 @@ class Allocation extends Model implements CleansAttributes, ValidableContract
|
|||
'server_id' => 'nullable|exists:servers,id',
|
||||
];
|
||||
|
||||
/**
|
||||
* Return a hashid encoded string to represent the ID of the allocation.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getHashidAttribute()
|
||||
{
|
||||
return app()->make('hashids')->encode($this->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Accessor to automatically provide the IP alias if defined.
|
||||
*
|
||||
|
|
|
@ -86,7 +86,8 @@ class Permission extends Model implements CleansAttributes, ValidableContract
|
|||
'delete-subuser' => null,
|
||||
],
|
||||
'server' => [
|
||||
'set-connection' => null,
|
||||
'view-allocations' => null,
|
||||
'edit-allocation' => null,
|
||||
'view-startup' => null,
|
||||
'edit-startup' => null,
|
||||
],
|
||||
|
|
110
app/Services/Allocations/SetDefaultAllocationService.php
Normal file
110
app/Services/Allocations/SetDefaultAllocationService.php
Normal file
|
@ -0,0 +1,110 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Services\Allocations;
|
||||
|
||||
use Pterodactyl\Models\Server;
|
||||
use Pterodactyl\Models\Allocation;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
use Illuminate\Database\ConnectionInterface;
|
||||
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
|
||||
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
||||
use Pterodactyl\Exceptions\Service\Allocation\AllocationDoesNotBelongToServerException;
|
||||
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonRepositoryInterface;
|
||||
|
||||
class SetDefaultAllocationService
|
||||
{
|
||||
/**
|
||||
* @var \Illuminate\Database\ConnectionInterface
|
||||
*/
|
||||
private $connection;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
|
||||
*/
|
||||
private $daemonRepository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface
|
||||
*/
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
|
||||
*/
|
||||
private $serverRepository;
|
||||
|
||||
/**
|
||||
* SetDefaultAllocationService constructor.
|
||||
*
|
||||
* @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $repository
|
||||
* @param \Illuminate\Database\ConnectionInterface $connection
|
||||
* @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonRepository
|
||||
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $serverRepository
|
||||
*/
|
||||
public function __construct(
|
||||
AllocationRepositoryInterface $repository,
|
||||
ConnectionInterface $connection,
|
||||
DaemonRepositoryInterface $daemonRepository,
|
||||
ServerRepositoryInterface $serverRepository
|
||||
) {
|
||||
$this->connection = $connection;
|
||||
$this->daemonRepository = $daemonRepository;
|
||||
$this->repository = $repository;
|
||||
$this->serverRepository = $serverRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the default allocation for a server only if that allocation is currently
|
||||
* assigned to the specified server.
|
||||
*
|
||||
* @param int|\Pterodactyl\Models\Server $server
|
||||
* @param int $allocation
|
||||
* @return \Pterodactyl\Models\Allocation
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
* @throws \Pterodactyl\Exceptions\Service\Allocation\AllocationDoesNotBelongToServerException
|
||||
*/
|
||||
public function handle($server, int $allocation): Allocation
|
||||
{
|
||||
if (! $server instanceof Server) {
|
||||
$server = $this->serverRepository->find($server);
|
||||
}
|
||||
|
||||
$allocations = $this->repository->findWhere([['server_id', '=', $server->id]]);
|
||||
$model = $allocations->filter(function ($model) use ($allocation) {
|
||||
return $model->id === $allocation;
|
||||
})->first();
|
||||
|
||||
if (! $model instanceof Allocation) {
|
||||
throw new AllocationDoesNotBelongToServerException;
|
||||
}
|
||||
|
||||
$this->connection->beginTransaction();
|
||||
$this->serverRepository->withoutFresh()->update($server->id, ['allocation_id' => $model->id]);
|
||||
|
||||
// Update on the daemon.
|
||||
try {
|
||||
$this->daemonRepository->setAccessServer($server->uuid)->setNode($server->node_id)->update([
|
||||
'build' => [
|
||||
'default' => [
|
||||
'ip' => $model->ip,
|
||||
'port' => $model->port,
|
||||
],
|
||||
'ports|overwrite' => $allocations->groupBy('ip')->map(function ($item) {
|
||||
return $item->pluck('port');
|
||||
})->toArray(),
|
||||
],
|
||||
]);
|
||||
|
||||
$this->connection->commit();
|
||||
} catch (RequestException $exception) {
|
||||
$this->connection->rollBack();
|
||||
throw new DaemonConnectionException($exception);
|
||||
}
|
||||
|
||||
return $model;
|
||||
}
|
||||
}
|
|
@ -14,17 +14,34 @@ use Illuminate\Http\Request;
|
|||
|
||||
trait JavascriptInjection
|
||||
{
|
||||
/**
|
||||
* @var \Illuminate\Http\Request
|
||||
*/
|
||||
private $request;
|
||||
|
||||
/**
|
||||
* Set the request object to use when injecting JS.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return $this
|
||||
*/
|
||||
public function setRequest(Request $request)
|
||||
{
|
||||
$this->request = $request;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Injects server javascript into the page to be used by other services.
|
||||
*
|
||||
* @param array $args
|
||||
* @param bool $overwrite
|
||||
* @param \Illuminate\Http\Request|null $request
|
||||
* @param array $args
|
||||
* @param bool $overwrite
|
||||
* @return array
|
||||
*/
|
||||
public function injectJavascript($args = [], $overwrite = false, Request $request = null)
|
||||
public function injectJavascript($args = [], $overwrite = false)
|
||||
{
|
||||
$request = $request ?? app()->make(Request::class);
|
||||
$request = $this->request ?? app()->make(Request::class);
|
||||
$server = $request->attributes->get('server');
|
||||
$token = $request->attributes->get('server_token');
|
||||
|
||||
|
|
Reference in a new issue