Added validation to variable validation rules to validate that the validation rules are valid
closes #988
This commit is contained in:
parent
7a04a9f169
commit
b96c2d16ee
9 changed files with 246 additions and 32 deletions
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Exceptions\Service\Egg\Variable;
|
||||
|
||||
use Pterodactyl\Exceptions\DisplayException;
|
||||
|
||||
class BadValidationRuleException extends DisplayException
|
||||
{
|
||||
}
|
|
@ -3,24 +3,46 @@
|
|||
namespace Pterodactyl\Services\Eggs\Variables;
|
||||
|
||||
use Pterodactyl\Models\EggVariable;
|
||||
use Illuminate\Contracts\Validation\Factory;
|
||||
use Pterodactyl\Traits\Services\ValidatesValidationRules;
|
||||
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
|
||||
use Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException;
|
||||
|
||||
class VariableCreationService
|
||||
{
|
||||
use ValidatesValidationRules;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface
|
||||
*/
|
||||
protected $repository;
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Contracts\Validation\Factory
|
||||
*/
|
||||
private $validator;
|
||||
|
||||
/**
|
||||
* VariableCreationService constructor.
|
||||
*
|
||||
* @param \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface $repository
|
||||
* @param \Illuminate\Contracts\Validation\Factory $validator
|
||||
*/
|
||||
public function __construct(EggVariableRepositoryInterface $repository)
|
||||
public function __construct(EggVariableRepositoryInterface $repository, Factory $validator)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
$this->validator = $validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the validation factory instance to be used by rule validation
|
||||
* checking in the trait.
|
||||
*
|
||||
* @return \Illuminate\Contracts\Validation\Factory
|
||||
*/
|
||||
protected function getValidator(): Factory
|
||||
{
|
||||
return $this->validator;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -31,6 +53,7 @@ class VariableCreationService
|
|||
* @return \Pterodactyl\Models\EggVariable
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||
* @throws \Pterodactyl\Exceptions\Service\Egg\Variable\BadValidationRuleException
|
||||
* @throws \Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException
|
||||
*/
|
||||
public function handle(int $egg, array $data): EggVariable
|
||||
|
@ -42,6 +65,10 @@ class VariableCreationService
|
|||
));
|
||||
}
|
||||
|
||||
if (! empty($data['rules'] ?? '')) {
|
||||
$this->validateRules($data['rules']);
|
||||
}
|
||||
|
||||
$options = array_get($data, 'options') ?? [];
|
||||
|
||||
return $this->repository->create([
|
||||
|
|
|
@ -3,25 +3,47 @@
|
|||
namespace Pterodactyl\Services\Eggs\Variables;
|
||||
|
||||
use Pterodactyl\Models\EggVariable;
|
||||
use Illuminate\Contracts\Validation\Factory;
|
||||
use Pterodactyl\Exceptions\DisplayException;
|
||||
use Pterodactyl\Traits\Services\ValidatesValidationRules;
|
||||
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
|
||||
use Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException;
|
||||
|
||||
class VariableUpdateService
|
||||
{
|
||||
use ValidatesValidationRules;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface
|
||||
*/
|
||||
protected $repository;
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Contracts\Validation\Factory
|
||||
*/
|
||||
private $validator;
|
||||
|
||||
/**
|
||||
* VariableUpdateService constructor.
|
||||
*
|
||||
* @param \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface $repository
|
||||
* @param \Illuminate\Contracts\Validation\Factory $validator
|
||||
*/
|
||||
public function __construct(EggVariableRepositoryInterface $repository)
|
||||
public function __construct(EggVariableRepositoryInterface $repository, Factory $validator)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
$this->validator = $validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the validation factory instance to be used by rule validation
|
||||
* checking in the trait.
|
||||
*
|
||||
* @return \Illuminate\Contracts\Validation\Factory
|
||||
*/
|
||||
protected function getValidator(): Factory
|
||||
{
|
||||
return $this->validator;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -58,6 +80,10 @@ class VariableUpdateService
|
|||
}
|
||||
}
|
||||
|
||||
if (! empty($data['rules'] ?? '')) {
|
||||
$this->validateRules($data['rules']);
|
||||
}
|
||||
|
||||
$options = array_get($data, 'options') ?? [];
|
||||
|
||||
return $this->repository->withoutFreshModel()->update($variable->id, [
|
||||
|
|
40
app/Traits/Services/ValidatesValidationRules.php
Normal file
40
app/Traits/Services/ValidatesValidationRules.php
Normal file
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Traits\Services;
|
||||
|
||||
use BadMethodCallException;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Contracts\Validation\Factory;
|
||||
use Pterodactyl\Exceptions\Service\Egg\Variable\BadValidationRuleException;
|
||||
|
||||
trait ValidatesValidationRules
|
||||
{
|
||||
/**
|
||||
* @return \Illuminate\Contracts\Validation\Factory
|
||||
*/
|
||||
abstract protected function getValidator(): Factory;
|
||||
|
||||
/**
|
||||
* Validate that the rules being provided are valid for Laravel and can
|
||||
* be resolved.
|
||||
*
|
||||
* @param array|string $rules
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\Service\Egg\Variable\BadValidationRuleException
|
||||
*/
|
||||
public function validateRules($rules)
|
||||
{
|
||||
try {
|
||||
$this->getValidator()->make(['__TEST' => 'test'], ['__TEST' => $rules])->fails();
|
||||
} catch (BadMethodCallException $exception) {
|
||||
$matches = [];
|
||||
if (preg_match('/Method \[(.+)\] does not exist\./', $exception->getMessage(), $matches)) {
|
||||
throw new BadValidationRuleException(trans('exceptions.nest.variables.bad_validation_rule', [
|
||||
'rule' => Str::snake(str_replace('validate', '', array_get($matches, 1, 'unknownRule'))),
|
||||
]), $exception);
|
||||
}
|
||||
|
||||
throw $exception;
|
||||
}
|
||||
}
|
||||
}
|
Reference in a new issue