2019-09-05 06:00:34 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Models;
|
|
|
|
|
2022-05-07 20:17:10 +02:00
|
|
|
use Illuminate\Support\Arr;
|
2022-05-14 22:03:50 +02:00
|
|
|
use Illuminate\Support\Str;
|
2019-09-05 07:19:57 +02:00
|
|
|
use Illuminate\Validation\Rule;
|
|
|
|
use Illuminate\Container\Container;
|
|
|
|
use Illuminate\Contracts\Validation\Factory;
|
2022-05-29 23:52:14 +02:00
|
|
|
use Illuminate\Validation\ValidationException;
|
2021-01-23 21:09:16 +01:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2020-10-08 06:56:44 +02:00
|
|
|
use Pterodactyl\Exceptions\Model\DataValidationException;
|
2021-01-23 21:09:16 +01:00
|
|
|
use Illuminate\Database\Eloquent\Model as IlluminateModel;
|
2019-09-05 06:00:34 +02:00
|
|
|
|
2020-04-04 08:22:35 +02:00
|
|
|
abstract class Model extends IlluminateModel
|
2019-09-05 06:00:34 +02:00
|
|
|
{
|
2021-01-23 21:09:16 +01:00
|
|
|
use HasFactory;
|
|
|
|
|
2020-04-04 08:22:35 +02:00
|
|
|
/**
|
|
|
|
* Set to true to return immutable Carbon date instances from the model.
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $immutableDates = false;
|
|
|
|
|
2019-09-05 06:00:34 +02:00
|
|
|
/**
|
2019-09-05 07:19:57 +02:00
|
|
|
* Determines if the model should undergo data validation before it is saved
|
|
|
|
* to the database.
|
|
|
|
*
|
|
|
|
* @var bool
|
2019-09-05 06:00:34 +02:00
|
|
|
*/
|
2019-09-05 07:19:57 +02:00
|
|
|
protected $skipValidation = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Validation\Factory
|
|
|
|
*/
|
|
|
|
protected static $validatorFactory;
|
2019-09-05 06:00:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2019-09-05 07:19:57 +02:00
|
|
|
public static $validationRules = [];
|
2019-09-05 06:00:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Listen for the model saving event and fire off the validation
|
|
|
|
* function before it is saved.
|
2019-09-05 07:19:57 +02:00
|
|
|
*
|
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
2019-09-05 06:00:34 +02:00
|
|
|
*/
|
|
|
|
protected static function boot()
|
|
|
|
{
|
|
|
|
parent::boot();
|
|
|
|
|
2019-09-05 07:19:57 +02:00
|
|
|
static::$validatorFactory = Container::getInstance()->make(Factory::class);
|
|
|
|
|
2020-04-04 08:22:35 +02:00
|
|
|
static::saving(function (Model $model) {
|
2022-05-29 23:52:14 +02:00
|
|
|
try {
|
|
|
|
$model->validate();
|
|
|
|
} catch (ValidationException $exception) {
|
2022-06-26 22:31:48 +02:00
|
|
|
throw new DataValidationException($exception->validator, $model);
|
2020-10-08 06:56:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2019-09-05 06:00:34 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-22 20:10:01 +02:00
|
|
|
/**
|
|
|
|
* Returns the model key to use for route model binding. By default we'll
|
|
|
|
* assume every model uses a UUID field for this. If the model does not have
|
|
|
|
* a UUID and is using a different key it should be specified on the model
|
|
|
|
* itself.
|
|
|
|
*
|
|
|
|
* You may also optionally override this on a per-route basis by declaring
|
|
|
|
* the key name in the URL definition, like "{user:id}".
|
|
|
|
*/
|
|
|
|
public function getRouteKeyName(): string
|
|
|
|
{
|
|
|
|
return 'uuid';
|
|
|
|
}
|
|
|
|
|
2019-09-05 06:00:34 +02:00
|
|
|
/**
|
2019-09-05 07:19:57 +02:00
|
|
|
* Set the model to skip validation when saving.
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function skipValidation()
|
|
|
|
{
|
|
|
|
$this->skipValidation = true;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the validator instance used by this model.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Validation\Validator|\Illuminate\Contracts\Validation\Validator
|
|
|
|
*/
|
|
|
|
public function getValidator()
|
|
|
|
{
|
2022-05-29 23:52:14 +02:00
|
|
|
$rules = $this->exists ? static::getRulesForUpdate($this) : static::getRules();
|
2019-09-05 07:19:57 +02:00
|
|
|
|
2022-05-29 23:52:14 +02:00
|
|
|
return static::$validatorFactory->make([], $rules, [], []);
|
2019-09-05 07:19:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-05 07:26:28 +02:00
|
|
|
* Returns the rules associated with this model.
|
|
|
|
*
|
2019-09-05 07:19:57 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getRules()
|
|
|
|
{
|
2019-09-05 07:26:28 +02:00
|
|
|
$rules = static::$validationRules;
|
|
|
|
foreach ($rules as $key => &$rule) {
|
|
|
|
$rule = is_array($rule) ? $rule : explode('|', $rule);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rules;
|
2019-09-05 07:19:57 +02:00
|
|
|
}
|
|
|
|
|
2022-05-07 20:17:10 +02:00
|
|
|
/**
|
|
|
|
* Returns the rules for a specific field. If the field is not found an empty
|
|
|
|
* array is returned.
|
|
|
|
*/
|
|
|
|
public static function getRulesForField(string $field): array
|
|
|
|
{
|
|
|
|
return Arr::get(static::getRules(), $field) ?? [];
|
|
|
|
}
|
|
|
|
|
2019-09-05 07:19:57 +02:00
|
|
|
/**
|
2019-09-05 07:26:28 +02:00
|
|
|
* Returns the rules associated with the model, specifically for updating the given model
|
|
|
|
* rather than just creating it.
|
|
|
|
*
|
2022-05-29 23:52:14 +02:00
|
|
|
* @param \Illuminate\Database\Eloquent\Model|int|string $model
|
2021-01-23 21:33:34 +01:00
|
|
|
*
|
2019-09-05 07:19:57 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
2022-05-29 23:52:14 +02:00
|
|
|
public static function getRulesForUpdate($model, string $column = 'id')
|
2019-09-05 07:19:57 +02:00
|
|
|
{
|
2022-05-29 23:52:14 +02:00
|
|
|
if ($model instanceof Model) {
|
|
|
|
[$id, $column] = [$model->getKey(), $model->getKeyName()];
|
2019-09-05 07:19:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$rules = static::getRules();
|
|
|
|
foreach ($rules as $key => &$data) {
|
|
|
|
// For each rule in a given field, iterate over it and confirm if the rule
|
|
|
|
// is one for a unique field. If that is the case, append the ID of the current
|
|
|
|
// working model so we don't run into errors due to the way that field validation
|
|
|
|
// works.
|
|
|
|
foreach ($data as &$datum) {
|
2021-01-23 21:33:34 +01:00
|
|
|
if (!is_string($datum) || !Str::startsWith($datum, 'unique')) {
|
2019-09-05 07:19:57 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-09-23 21:30:51 +02:00
|
|
|
[, $args] = explode(':', $datum);
|
2019-09-05 07:19:57 +02:00
|
|
|
$args = explode(',', $args);
|
|
|
|
|
2022-05-29 23:52:14 +02:00
|
|
|
$datum = Rule::unique($args[0], $args[1] ?? $key)->ignore($id ?? $model, $column);
|
2019-09-05 07:19:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rules;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if the model is in a valid state or not.
|
2019-09-05 06:00:34 +02:00
|
|
|
*/
|
2022-05-29 23:52:14 +02:00
|
|
|
public function validate(): void
|
2019-09-05 06:00:34 +02:00
|
|
|
{
|
2019-09-05 07:19:57 +02:00
|
|
|
if ($this->skipValidation) {
|
2022-05-29 23:52:14 +02:00
|
|
|
return;
|
2019-09-05 07:19:57 +02:00
|
|
|
}
|
|
|
|
|
2022-05-29 23:52:14 +02:00
|
|
|
$validator = $this->getValidator();
|
|
|
|
$validator->setData(
|
2020-10-08 06:56:44 +02:00
|
|
|
// Trying to do self::toArray() here will leave out keys based on the whitelist/blacklist
|
|
|
|
// for that model. Doing this will return all of the attributes in a format that can
|
|
|
|
// properly be validated.
|
2020-03-29 00:06:36 +01:00
|
|
|
$this->addCastAttributesToArray(
|
2021-01-23 21:33:34 +01:00
|
|
|
$this->getAttributes(),
|
|
|
|
$this->getMutatedAttributes()
|
2020-03-29 00:06:36 +01:00
|
|
|
)
|
2022-05-29 23:52:14 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!$validator->passes()) {
|
|
|
|
throw new ValidationException($validator);
|
|
|
|
}
|
2019-09-05 06:00:34 +02:00
|
|
|
}
|
2020-04-04 08:22:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a timestamp as DateTime object.
|
|
|
|
*
|
|
|
|
* @param mixed $value
|
2021-01-23 21:33:34 +01:00
|
|
|
*
|
2020-04-04 08:22:35 +02:00
|
|
|
* @return \Illuminate\Support\Carbon|\Carbon\CarbonImmutable
|
|
|
|
*/
|
|
|
|
protected function asDateTime($value)
|
|
|
|
{
|
2021-01-23 21:33:34 +01:00
|
|
|
if (!$this->immutableDates) {
|
2020-04-04 08:22:35 +02:00
|
|
|
return parent::asDateTime($value);
|
|
|
|
}
|
|
|
|
|
2020-04-04 20:37:10 +02:00
|
|
|
return parent::asDateTime($value)->toImmutable();
|
2020-04-04 08:22:35 +02:00
|
|
|
}
|
2019-09-05 06:00:34 +02:00
|
|
|
}
|