Update and test variable validator logic
This commit is contained in:
parent
d8228f2da8
commit
69f27ed807
3 changed files with 152 additions and 226 deletions
|
@ -11,32 +11,15 @@ namespace Pterodactyl\Services\Servers;
|
|||
|
||||
use Pterodactyl\Models\User;
|
||||
use Illuminate\Support\Collection;
|
||||
use Pterodactyl\Models\EggVariable;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Pterodactyl\Traits\Services\HasUserLevels;
|
||||
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
||||
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
|
||||
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface;
|
||||
|
||||
class VariableValidatorService
|
||||
{
|
||||
use HasUserLevels;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface
|
||||
*/
|
||||
private $optionVariableRepository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
|
||||
*/
|
||||
private $serverRepository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface
|
||||
*/
|
||||
private $serverVariableRepository;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Contracts\Validation\Factory
|
||||
*/
|
||||
|
@ -45,20 +28,10 @@ class VariableValidatorService
|
|||
/**
|
||||
* VariableValidatorService constructor.
|
||||
*
|
||||
* @param \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface $optionVariableRepository
|
||||
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $serverRepository
|
||||
* @param \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface $serverVariableRepository
|
||||
* @param \Illuminate\Contracts\Validation\Factory $validator
|
||||
*/
|
||||
public function __construct(
|
||||
EggVariableRepositoryInterface $optionVariableRepository,
|
||||
ServerRepositoryInterface $serverRepository,
|
||||
ServerVariableRepositoryInterface $serverVariableRepository,
|
||||
ValidationFactory $validator
|
||||
) {
|
||||
$this->optionVariableRepository = $optionVariableRepository;
|
||||
$this->serverRepository = $serverRepository;
|
||||
$this->serverVariableRepository = $serverVariableRepository;
|
||||
public function __construct(ValidationFactory $validator)
|
||||
{
|
||||
$this->validator = $validator;
|
||||
}
|
||||
|
||||
|
@ -72,16 +45,18 @@ class VariableValidatorService
|
|||
*/
|
||||
public function handle(int $egg, array $fields = []): Collection
|
||||
{
|
||||
$variables = $this->optionVariableRepository->findWhere([['egg_id', '=', $egg]]);
|
||||
$query = EggVariable::query()->where('egg_id', $egg);
|
||||
if (! $this->isUserLevel(User::USER_LEVEL_ADMIN)) {
|
||||
// Don't attempt to validate variables if they aren't user editable
|
||||
// and we're not running this at an admin level.
|
||||
$query = $query->where('user_editable', true)->where('user_viewable', true);
|
||||
}
|
||||
|
||||
/** @var \Pterodactyl\Models\EggVariable[] $variables */
|
||||
$variables = $query->get();
|
||||
|
||||
$data = $rules = $customAttributes = [];
|
||||
foreach ($variables as $variable) {
|
||||
// Don't attempt to validate variables if they aren't user editable
|
||||
// and we're not running this at an admin level.
|
||||
if (! $variable->user_editable && ! $this->isUserLevel(User::USER_LEVEL_ADMIN)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$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]);
|
||||
|
@ -92,23 +67,12 @@ class VariableValidatorService
|
|||
throw new ValidationException($validator);
|
||||
}
|
||||
|
||||
$response = $variables->filter(function ($item) {
|
||||
// Skip doing anything if user is not an admin and variable is not user viewable or editable.
|
||||
if (! $this->isUserLevel(User::USER_LEVEL_ADMIN) && (! $item->user_editable || ! $item->user_viewable)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
})->map(function ($item) use ($fields) {
|
||||
return (object) [
|
||||
return Collection::make($variables)->map(function ($item) use ($fields) {
|
||||
return (object)[
|
||||
'id' => $item->id,
|
||||
'key' => $item->env_variable,
|
||||
'value' => array_get($fields, $item->env_variable),
|
||||
'value' => $fields[$item->env_variable] ?? null,
|
||||
];
|
||||
})->filter(function ($item) {
|
||||
return is_object($item);
|
||||
});
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue