2017-07-19 20:49:41 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Servers;
|
|
|
|
|
2017-10-26 23:49:54 -05:00
|
|
|
use Pterodactyl\Models\User;
|
|
|
|
use Illuminate\Support\Collection;
|
2020-10-10 16:46:56 -07:00
|
|
|
use Pterodactyl\Models\EggVariable;
|
2017-11-25 13:15:46 -06:00
|
|
|
use Illuminate\Validation\ValidationException;
|
2017-10-26 23:49:54 -05:00
|
|
|
use Pterodactyl\Traits\Services\HasUserLevels;
|
2017-07-22 20:15:01 -05:00
|
|
|
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
|
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.
|
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function __construct(private ValidationFactory $validator)
|
2020-10-10 16:46:56 -07:00
|
|
|
{
|
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
|
|
|
*
|
2017-11-25 13:15:46 -06:00
|
|
|
* @throws \Illuminate\Validation\ValidationException
|
2017-07-19 20:49:41 -05:00
|
|
|
*/
|
2017-10-26 23:49:54 -05:00
|
|
|
public function handle(int $egg, array $fields = []): Collection
|
2017-07-19 20:49:41 -05:00
|
|
|
{
|
2020-10-10 16:46:56 -07:00
|
|
|
$query = EggVariable::query()->where('egg_id', $egg);
|
2021-01-23 12:33:34 -08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var \Pterodactyl\Models\EggVariable[] $variables */
|
|
|
|
$variables = $query->get();
|
2018-02-15 20:58:51 -06:00
|
|
|
|
2020-10-10 16:46:56 -07:00
|
|
|
$data = $rules = $customAttributes = [];
|
|
|
|
foreach ($variables as $variable) {
|
2018-01-28 17:14:14 -06:00
|
|
|
$data['environment'][$variable->env_variable] = array_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]);
|
|
|
|
}
|
2017-07-19 20:49:41 -05:00
|
|
|
|
2018-01-28 17:14:14 -06:00
|
|
|
$validator = $this->validator->make($data, $rules, [], $customAttributes);
|
|
|
|
if ($validator->fails()) {
|
|
|
|
throw new ValidationException($validator);
|
|
|
|
}
|
2017-07-19 20:49:41 -05:00
|
|
|
|
2020-10-10 16:46:56 -07:00
|
|
|
return Collection::make($variables)->map(function ($item) use ($fields) {
|
2021-01-23 12:09:16 -08:00
|
|
|
return (object) [
|
2017-07-19 20:49:41 -05:00
|
|
|
'id' => $item->id,
|
|
|
|
'key' => $item->env_variable,
|
2020-10-10 16:46:56 -07:00
|
|
|
'value' => $fields[$item->env_variable] ?? null,
|
2017-07-19 20:49:41 -05:00
|
|
|
];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|