|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pterodactyl\Http\Controllers\Api\Client\Servers; |
| 4 | + |
| 5 | +use Carbon\CarbonImmutable; |
| 6 | +use Pterodactyl\Models\Server; |
| 7 | +use Illuminate\Http\JsonResponse; |
| 8 | +use Pterodactyl\Services\Servers\VariableValidatorService; |
| 9 | +use Pterodactyl\Repositories\Eloquent\ServerVariableRepository; |
| 10 | +use Pterodactyl\Transformers\Api\Client\EggVariableTransformer; |
| 11 | +use Pterodactyl\Http\Controllers\Api\Client\ClientApiController; |
| 12 | +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
| 13 | +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
| 14 | +use Pterodactyl\Http\Requests\Api\Client\Servers\Startup\UpdateStartupVariableRequest; |
| 15 | + |
| 16 | +class StartupController extends ClientApiController |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @var \Pterodactyl\Services\Servers\VariableValidatorService |
| 20 | + */ |
| 21 | + private $service; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var \Pterodactyl\Repositories\Eloquent\ServerVariableRepository |
| 25 | + */ |
| 26 | + private $repository; |
| 27 | + |
| 28 | + /** |
| 29 | + * StartupController constructor. |
| 30 | + * |
| 31 | + * @param \Pterodactyl\Services\Servers\VariableValidatorService $service |
| 32 | + * @param \Pterodactyl\Repositories\Eloquent\ServerVariableRepository $repository |
| 33 | + */ |
| 34 | + public function __construct(VariableValidatorService $service, ServerVariableRepository $repository) |
| 35 | + { |
| 36 | + parent::__construct(); |
| 37 | + |
| 38 | + $this->service = $service; |
| 39 | + $this->repository = $repository; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Updates a single variable for a server. |
| 44 | + * |
| 45 | + * @param \Pterodactyl\Http\Requests\Api\Client\Servers\Startup\UpdateStartupVariableRequest $request |
| 46 | + * @param \Pterodactyl\Models\Server $server |
| 47 | + * @return array |
| 48 | + * |
| 49 | + * @throws \Illuminate\Validation\ValidationException |
| 50 | + * @throws \Pterodactyl\Exceptions\Model\DataValidationException |
| 51 | + * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException |
| 52 | + */ |
| 53 | + public function update(UpdateStartupVariableRequest $request, Server $server) |
| 54 | + { |
| 55 | + /** @var \Pterodactyl\Models\EggVariable $variable */ |
| 56 | + $variable = $server->variables()->where('env_variable', $request->input('key'))->first(); |
| 57 | + |
| 58 | + if (is_null($variable) || !$variable->user_viewable || !$variable->user_editable) { |
| 59 | + throw new BadRequestHttpException( |
| 60 | + "The environment variable you are trying to edit [\"{$request->input('key')}\"] does not exist." |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + // Revalidate the variable value using the egg variable specific validation rules for it. |
| 65 | + $this->validate($request, ['value' => $variable->rules]); |
| 66 | + |
| 67 | + $this->repository->updateOrCreate([ |
| 68 | + 'server_id' => $server->id, |
| 69 | + 'variable_id' => $variable->id, |
| 70 | + ], [ |
| 71 | + 'variable_value' => $request->input('value'), |
| 72 | + ]); |
| 73 | + |
| 74 | + $variable = $variable->refresh(); |
| 75 | + $variable->server_value = $request->input('value'); |
| 76 | + |
| 77 | + return $this->fractal->item($variable) |
| 78 | + ->transformWith($this->getTransformer(EggVariableTransformer::class)) |
| 79 | + ->toArray(); |
| 80 | + } |
| 81 | +} |
0 commit comments