Files
pterodactyl-panel/app/Services/Servers/VariableValidatorService.php
T

63 lines
2.2 KiB
PHP
Raw Normal View History

<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
namespace Pterodactyl\Services\Servers;
2026-10-05 18:10:18 -04:00
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Validation\ValidationException;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Data\ValidatedEggVariable;
use Pterodactyl\Models\Egg;
use Pterodactyl\Models\EggVariable;
use Pterodactyl\Models\User;
use Pterodactyl\Traits\Services\HasUserLevels;
class VariableValidatorService
{
use HasUserLevels;
2017-07-22 20:15:01 -05:00
/**
* VariableValidatorService constructor.
*/
2026-10-05 18:10:18 -04:00
public function __construct(private ValidationFactory $validator) {}
/**
2018-05-13 10:50:56 -04:00
* Validate all of the passed data against the given service option variables.
*
2026-10-05 18:10:18 -04:00
* @param array<string, ApiScalar> $fields environment variable name => submitted value
* @return Collection<int, ValidatedEggVariable>
*
2024-10-21 19:18:20 -06:00
* @throws ValidationException
*/
2026-10-05 18:10:18 -04:00
public function handle(Egg $egg, array $fields = []): Collection
{
2026-10-05 18:10:18 -04:00
$query = $egg->variables();
if (! $this->isUserLevel(User::USER_LEVEL_ADMIN)) {
2022-10-14 10:59:20 -06:00
// Don't attempt to validate variables if they aren't user editable,
// and we're not running this at an admin level.
2020-10-10 16:46:56 -07:00
$query = $query->where('user_editable', true)->where('user_viewable', true);
}
$variables = $query->get();
2026-10-05 18:10:18 -04:00
$data = [];
$rules = [];
$customAttributes = [];
2020-10-10 16:46:56 -07:00
foreach ($variables as $variable) {
2026-10-05 18:10:18 -04:00
$data['environment'][$variable->env_variable] = Arr::get($fields, $variable->env_variable);
$rules['environment.'.$variable->env_variable] = $variable->rules;
$customAttributes['environment.'.$variable->env_variable] = trans('validation.internal.variable_value', ['env' => $variable->name]);
2018-01-28 17:14:14 -06:00
}
2018-01-28 17:14:14 -06:00
$validator = $this->validator->make($data, $rules, [], $customAttributes);
2026-10-05 18:10:18 -04:00
throw_if($validator->fails(), ValidationException::class, $validator);
2026-10-05 18:10:18 -04:00
return $variables->map(fn (EggVariable $item): ValidatedEggVariable => new ValidatedEggVariable(
$item->id,
$item->env_variable,
$fields[$item->env_variable] ?? null,
));
}
}