2017-07-19 20:49:41 -05:00
|
|
|
<?php
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2017-07-19 20:49:41 -05:00
|
|
|
namespace Pterodactyl\Services\Servers;
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
|
|
|
|
|
use Illuminate\Support\Arr;
|
2017-10-26 23:49:54 -05:00
|
|
|
use Illuminate\Support\Collection;
|
2017-11-25 13:15:46 -06:00
|
|
|
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;
|
2017-10-26 23:49:54 -05:00
|
|
|
use Pterodactyl\Traits\Services\HasUserLevels;
|
2017-07-19 20:49:41 -05:00
|
|
|
|
|
|
|
|
class VariableValidatorService
|
|
|
|
|
{
|
2017-10-26 23:49:54 -05:00
|
|
|
use HasUserLevels;
|
2017-07-19 20:49:41 -05:00
|
|
|
|
2017-07-22 20:15:01 -05:00
|
|
|
/**
|
|
|
|
|
* VariableValidatorService constructor.
|
|
|
|
|
*/
|
2026-10-05 18:10:18 -04:00
|
|
|
public function __construct(private ValidationFactory $validator) {}
|
2017-07-19 20:49:41 -05:00
|
|
|
|
|
|
|
|
/**
|
2018-05-13 10:50:56 -04:00
|
|
|
* Validate all of the passed data against the given service option variables.
|
2017-07-19 20:49:41 -05:00
|
|
|
*
|
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
|
2017-07-19 20:49:41 -05:00
|
|
|
*/
|
2026-10-05 18:10:18 -04:00
|
|
|
public function handle(Egg $egg, array $fields = []): Collection
|
2017-07-19 20:49:41 -05:00
|
|
|
{
|
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,
|
2018-02-15 20:58:51 -06:00
|
|
|
// 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
|
|
|
}
|
2017-07-19 20:49:41 -05: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);
|
2017-07-19 20:49:41 -05:00
|
|
|
|
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,
|
|
|
|
|
));
|
2017-07-19 20:49:41 -05:00
|
|
|
}
|
|
|
|
|
}
|