misc_pterodactyl-panel/app/Http/Controllers/Api/Client/Servers/StartupController.php

93 lines
3.5 KiB
PHP
Raw Normal View History

<?php
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
use Pterodactyl\Models\Server;
use Pterodactyl\Services\Servers\StartupCommandService;
use Pterodactyl\Repositories\Eloquent\ServerVariableRepository;
use Pterodactyl\Transformers\Api\Client\EggVariableTransformer;
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Pterodactyl\Http\Requests\Api\Client\Servers\Startup\GetStartupRequest;
use Pterodactyl\Http\Requests\Api\Client\Servers\Startup\UpdateStartupVariableRequest;
class StartupController extends ClientApiController
{
2021-03-05 17:03:12 +00:00
private ServerVariableRepository $repository;
private StartupCommandService $startupCommandService;
/**
* StartupController constructor.
*/
2022-02-14 00:04:11 +00:00
public function __construct(StartupCommandService $startupCommandService, ServerVariableRepository $repository)
{
parent::__construct();
$this->repository = $repository;
$this->startupCommandService = $startupCommandService;
}
/**
* Returns the startup information for the server including all of the variables.
*
2021-03-05 17:03:12 +00:00
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
2021-03-05 17:03:12 +00:00
public function index(GetStartupRequest $request, Server $server): array
{
$startup = $this->startupCommandService->handle($server, false);
return $this->fractal->collection(
$server->variables()->where('user_viewable', true)->get()
)
2021-08-07 21:32:40 +00:00
->transformWith(EggVariableTransformer::class)
->addMeta([
'startup_command' => $startup,
'docker_images' => $server->egg->docker_images,
'raw_startup_command' => $server->startup,
])
->toArray();
}
/**
* Updates a single variable for a server.
*
* @throws \Illuminate\Validation\ValidationException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
2021-03-05 17:03:12 +00:00
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
2021-03-05 17:03:12 +00:00
public function update(UpdateStartupVariableRequest $request, Server $server): array
{
$variable = $server->variables()->where('env_variable', $request->input('key'))->first();
2021-01-23 20:33:34 +00:00
if (is_null($variable) || !$variable->user_viewable) {
throw new BadRequestHttpException('The environment variable you are trying to edit does not exist.');
} elseif (!$variable->user_editable) {
throw new BadRequestHttpException('The environment variable you are trying to edit is read-only.');
}
// Revalidate the variable value using the egg variable specific validation rules for it.
$this->validate($request, ['value' => $variable->rules]);
$this->repository->updateOrCreate([
'server_id' => $server->id,
'variable_id' => $variable->id,
], [
'variable_value' => $request->input('value') ?? '',
]);
$variable = $variable->refresh();
$variable->server_value = $request->input('value');
$startup = $this->startupCommandService->handle($server, false);
return $this->fractal->item($variable)
2021-08-07 21:32:40 +00:00
->transformWith(EggVariableTransformer::class)
->addMeta([
'startup_command' => $startup,
'raw_startup_command' => $server->startup,
])
->toArray();
}
}