mirror of
				https://github.com/pelican-dev/panel.git
				synced 2025-11-04 04:06:51 +01:00 
			
		
		
		
	* Fix `serverVariables` not saving due to `join` * Remove deprecated `viewableServerVariables`
		
			
				
	
	
		
			108 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			108 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
namespace App\Http\Controllers\Api\Client\Servers;
 | 
						|
 | 
						|
use App\Models\Server;
 | 
						|
use App\Facades\Activity;
 | 
						|
use App\Models\ServerVariable;
 | 
						|
use App\Services\Servers\StartupCommandService;
 | 
						|
use App\Transformers\Api\Client\EggVariableTransformer;
 | 
						|
use App\Http\Controllers\Api\Client\ClientApiController;
 | 
						|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
 | 
						|
use App\Http\Requests\Api\Client\Servers\Startup\GetStartupRequest;
 | 
						|
use App\Http\Requests\Api\Client\Servers\Startup\UpdateStartupVariableRequest;
 | 
						|
use Dedoc\Scramble\Attributes\Group;
 | 
						|
 | 
						|
#[Group('Server - Startup')]
 | 
						|
class StartupController extends ClientApiController
 | 
						|
{
 | 
						|
    /**
 | 
						|
     * StartupController constructor.
 | 
						|
     */
 | 
						|
    public function __construct(
 | 
						|
        private StartupCommandService $startupCommandService,
 | 
						|
    ) {
 | 
						|
        parent::__construct();
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * List startup variables
 | 
						|
     *
 | 
						|
     * Returns the startup information for the server including all the variables.
 | 
						|
     *
 | 
						|
     * @return array<array-key, mixed>
 | 
						|
     */
 | 
						|
    public function index(GetStartupRequest $request, Server $server): array
 | 
						|
    {
 | 
						|
        $startup = $this->startupCommandService->handle($server);
 | 
						|
 | 
						|
        return $this->fractal->collection(
 | 
						|
            $server->variables()->where('user_viewable', true)->orderBy('sort')->get()
 | 
						|
        )
 | 
						|
            ->transformWith($this->getTransformer(EggVariableTransformer::class))
 | 
						|
            ->addMeta([
 | 
						|
                'startup_command' => $startup,
 | 
						|
                'docker_images' => $server->egg->docker_images,
 | 
						|
                'raw_startup_command' => $server->startup,
 | 
						|
            ])
 | 
						|
            ->toArray();
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Update startup variable
 | 
						|
     *
 | 
						|
     * Updates a single variable for a server.
 | 
						|
     *
 | 
						|
     * @return array<array-key, mixed>
 | 
						|
     *
 | 
						|
     * @throws \Illuminate\Validation\ValidationException
 | 
						|
     * @throws \App\Exceptions\Model\DataValidationException
 | 
						|
     */
 | 
						|
    public function update(UpdateStartupVariableRequest $request, Server $server): array
 | 
						|
    {
 | 
						|
        $variable = $server->variables()->where('env_variable', $request->input('key'))->first();
 | 
						|
 | 
						|
        if (!$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.');
 | 
						|
        }
 | 
						|
 | 
						|
        $original = $variable->server_value;
 | 
						|
 | 
						|
        // Revalidate the variable value using the egg variable specific validation rules for it.
 | 
						|
        $request->validate(['value' => $variable->rules]);
 | 
						|
 | 
						|
        ServerVariable::query()->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);
 | 
						|
 | 
						|
        if ($variable->env_variable !== $request->input('value')) {
 | 
						|
            Activity::event('server:startup.edit')
 | 
						|
                ->subject($variable)
 | 
						|
                ->property([
 | 
						|
                    'variable' => $variable->env_variable,
 | 
						|
                    'old' => $original,
 | 
						|
                    'new' => $request->input('value'),
 | 
						|
                ])
 | 
						|
                ->log();
 | 
						|
        }
 | 
						|
 | 
						|
        return $this->fractal->item($variable)
 | 
						|
            ->transformWith($this->getTransformer(EggVariableTransformer::class))
 | 
						|
            ->addMeta([
 | 
						|
                'startup_command' => $startup,
 | 
						|
                'raw_startup_command' => $server->startup,
 | 
						|
            ])
 | 
						|
            ->toArray();
 | 
						|
    }
 | 
						|
}
 |