2020-08-23 01:13:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
|
|
|
|
|
|
|
|
use Pterodactyl\Models\Server;
|
2020-08-26 02:22:17 +00:00
|
|
|
use Pterodactyl\Services\Servers\StartupCommandService;
|
2020-08-23 01:13:59 +00:00
|
|
|
use Pterodactyl\Services\Servers\VariableValidatorService;
|
|
|
|
use Pterodactyl\Repositories\Eloquent\ServerVariableRepository;
|
|
|
|
use Pterodactyl\Transformers\Api\Client\EggVariableTransformer;
|
|
|
|
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
|
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
2020-08-26 02:22:17 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Startup\GetStartupRequest;
|
2020-08-23 01:13:59 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Startup\UpdateStartupVariableRequest;
|
|
|
|
|
|
|
|
class StartupController extends ClientApiController
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Servers\VariableValidatorService
|
|
|
|
*/
|
|
|
|
private $service;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\ServerVariableRepository
|
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
2020-08-26 02:22:17 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Servers\StartupCommandService
|
|
|
|
*/
|
|
|
|
private $startupCommandService;
|
|
|
|
|
2020-08-23 01:13:59 +00:00
|
|
|
/**
|
|
|
|
* StartupController constructor.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Services\Servers\VariableValidatorService $service
|
2020-08-26 02:22:17 +00:00
|
|
|
* @param \Pterodactyl\Services\Servers\StartupCommandService $startupCommandService
|
2020-08-23 01:13:59 +00:00
|
|
|
* @param \Pterodactyl\Repositories\Eloquent\ServerVariableRepository $repository
|
|
|
|
*/
|
2020-08-26 02:22:17 +00:00
|
|
|
public function __construct(VariableValidatorService $service, StartupCommandService $startupCommandService, ServerVariableRepository $repository)
|
2020-08-23 01:13:59 +00:00
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->service = $service;
|
|
|
|
$this->repository = $repository;
|
2020-08-26 02:22:17 +00:00
|
|
|
$this->startupCommandService = $startupCommandService;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the startup information for the server including all of the variables.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Startup\GetStartupRequest $request
|
|
|
|
* @param \Pterodactyl\Models\Server $server
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function index(GetStartupRequest $request, Server $server)
|
|
|
|
{
|
|
|
|
$startup = $this->startupCommandService->handle($server, false);
|
|
|
|
|
2020-09-23 04:12:00 +00:00
|
|
|
return $this->fractal->collection(
|
|
|
|
$server->variables()->where('user_viewable', true)->get()
|
|
|
|
)
|
2020-08-26 02:22:17 +00:00
|
|
|
->transformWith($this->getTransformer(EggVariableTransformer::class))
|
|
|
|
->addMeta([
|
|
|
|
'startup_command' => $startup,
|
2020-12-13 19:07:29 +00:00
|
|
|
'docker_images' => $server->egg->docker_images,
|
2020-08-26 02:22:17 +00:00
|
|
|
'raw_startup_command' => $server->startup,
|
|
|
|
])
|
|
|
|
->toArray();
|
2020-08-23 01:13:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates a single variable for a server.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Startup\UpdateStartupVariableRequest $request
|
|
|
|
* @param \Pterodactyl\Models\Server $server
|
|
|
|
* @return array
|
|
|
|
*
|
|
|
|
* @throws \Illuminate\Validation\ValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
*/
|
|
|
|
public function update(UpdateStartupVariableRequest $request, Server $server)
|
|
|
|
{
|
|
|
|
/** @var \Pterodactyl\Models\EggVariable $variable */
|
|
|
|
$variable = $server->variables()->where('env_variable', $request->input('key'))->first();
|
|
|
|
|
2020-09-23 04:12:00 +00:00
|
|
|
if (is_null($variable) || ! $variable->user_viewable) {
|
2020-08-23 01:13:59 +00:00
|
|
|
throw new BadRequestHttpException(
|
2021-01-23 20:09:16 +00:00
|
|
|
'The environment variable you are trying to edit does not exist.'
|
2020-08-23 21:56:05 +00:00
|
|
|
);
|
2021-01-23 20:09:16 +00:00
|
|
|
} elseif (! $variable->user_editable) {
|
2020-08-23 21:56:05 +00:00
|
|
|
throw new BadRequestHttpException(
|
2021-01-23 20:09:16 +00:00
|
|
|
'The environment variable you are trying to edit is read-only.'
|
2020-08-23 01:13:59 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
], [
|
2020-10-10 23:45:24 +00:00
|
|
|
'variable_value' => $request->input('value') ?? '',
|
2020-08-23 01:13:59 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$variable = $variable->refresh();
|
|
|
|
$variable->server_value = $request->input('value');
|
|
|
|
|
2020-08-26 02:22:17 +00:00
|
|
|
$startup = $this->startupCommandService->handle($server, false);
|
|
|
|
|
2020-08-23 01:13:59 +00:00
|
|
|
return $this->fractal->item($variable)
|
|
|
|
->transformWith($this->getTransformer(EggVariableTransformer::class))
|
2020-08-26 02:22:17 +00:00
|
|
|
->addMeta([
|
|
|
|
'startup_command' => $startup,
|
|
|
|
'raw_startup_command' => $server->startup,
|
|
|
|
])
|
2020-08-23 01:13:59 +00:00
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
}
|