misc_pterodactyl-panel/app/Models/Egg.php

337 lines
9.5 KiB
PHP
Raw Normal View History

2015-12-08 23:33:33 +00:00
<?php
2016-12-07 22:46:38 +00:00
2015-12-08 23:33:33 +00:00
namespace Pterodactyl\Models;
/**
* @property int $id
* @property string $uuid
* @property int $nest_id
* @property string $author
* @property string $name
* @property string|null $description
* @property array|null $features
* @property string $docker_image -- deprecated, use $docker_images
* @property array<string, string> $docker_images
* @property string $update_url
* @property bool $force_outgoing_ip
* @property array|null $file_denylist
* @property string|null $config_files
* @property string|null $config_startup
* @property string|null $config_logs
* @property string|null $config_stop
* @property int|null $config_from
* @property string|null $startup
* @property bool $script_is_privileged
* @property string|null $script_install
* @property string $script_entry
* @property string $script_container
* @property int|null $copy_script_from
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property string|null $copy_script_install
* @property string $copy_script_entry
* @property string $copy_script_container
* @property string|null $inherit_config_files
* @property string|null $inherit_config_startup
* @property string|null $inherit_config_logs
* @property string|null $inherit_config_stop
* @property string $inherit_file_denylist
* @property array|null $inherit_features
* @property \Pterodactyl\Models\Nest $nest
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Server[] $servers
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\EggVariable[] $variables
* @property \Pterodactyl\Models\Egg|null $scriptFrom
* @property \Pterodactyl\Models\Egg|null $configFrom
*/
class Egg extends Model
2015-12-08 23:33:33 +00:00
{
/**
* The resource name for this model when it is transformed into an
* API representation using fractal.
*/
2021-01-23 20:33:34 +00:00
public const RESOURCE_NAME = 'egg';
/**
* Defines the current egg export version.
*/
public const EXPORT_VERSION = 'PTDL_v2';
/**
* Different features that can be enabled on any given egg. These are used internally
* to determine which types of frontend functionality should be shown to the user. Eggs
* will automatically inherit features from a parent egg if they are already configured
* to copy configuration values from said egg.
*
* To skip copying the features, an empty array value should be passed in ("[]") rather
* than leaving it null.
*/
2021-01-23 20:33:34 +00:00
public const FEATURE_EULA_POPUP = 'eula';
public const FEATURE_FASTDL = 'fastdl';
2015-12-08 23:33:33 +00:00
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'eggs';
2015-12-08 23:33:33 +00:00
/**
* Fields that are not mass assignable.
*
* @var array
*/
2017-10-03 03:51:13 +00:00
protected $fillable = [
'name',
'description',
'features',
'docker_images',
'force_outgoing_ip',
'file_denylist',
2017-10-03 03:51:13 +00:00
'config_files',
'config_startup',
'config_logs',
'config_stop',
'config_from',
'startup',
'script_is_privileged',
'script_install',
'script_entry',
'script_container',
'copy_script_from',
];
/**
* Cast values to correct type.
*
* @var array
*/
protected $casts = [
2017-10-07 22:21:41 +00:00
'nest_id' => 'integer',
2017-10-03 03:51:13 +00:00
'config_from' => 'integer',
'script_is_privileged' => 'boolean',
'force_outgoing_ip' => 'boolean',
2017-10-03 03:51:13 +00:00
'copy_script_from' => 'integer',
'features' => 'array',
'docker_images' => 'array',
'file_denylist' => 'array',
];
/**
* @var array
*/
public static $validationRules = [
'nest_id' => 'required|bail|numeric|exists:nests,id',
'uuid' => 'required|string|size:36',
'name' => 'required|string|max:191',
'description' => 'string|nullable',
2020-11-03 04:24:11 +00:00
'features' => 'array|nullable',
'author' => 'required|string|email',
'file_denylist' => 'array|nullable',
'file_denylist.*' => 'string',
'docker_images' => 'required|array|min:1',
'docker_images.*' => 'required|string',
'startup' => 'required|nullable|string',
'config_from' => 'sometimes|bail|nullable|numeric|exists:eggs,id',
'config_stop' => 'required_without:config_from|nullable|string|max:191',
'config_startup' => 'required_without:config_from|nullable|json',
'config_logs' => 'required_without:config_from|nullable|json',
'config_files' => 'required_without:config_from|nullable|json',
'update_url' => 'sometimes|nullable|string',
'force_outgoing_ip' => 'sometimes|boolean',
];
/**
* @var array
*/
protected $attributes = [
'features' => null,
'file_denylist' => null,
'config_stop' => null,
'config_startup' => null,
'config_logs' => null,
'config_files' => null,
'update_url' => null,
];
Refactor to use more laravel logic and improve compatibility with older PHP versions (#206) * Fix @param namespaces for PHPDocs in ServerPolicy * Reduce permission check duplication in ServerPolicy This introduces a new checkPermission method to reduce code duplication when checking for permissions. * Simplify logic to list accessible servers for the user We can directly use the pluck function that laravel collections provide to simplify the logic. * Fix pagination issue when databases/servers exceed 20 Laravels strips out the currently selected tab (or any GET query for that matter) by default when using pagination. the appends() methods helps with keeping that information. * Refactor unnecessary array_merge calls We can just append to the array instead of constantly merging a new copy. * Fix accessing “API Access” on some versions of PHP The “new” word is reserved and should not be used as a method name. http://stackoverflow.com/questions/9575590/why-am-i-getting-an-unexpected-t-new-error-in-php * Fix revoking API keys on older versions of php (5.6) “string” was not a valid function argument type yet, so revoking keys results in an error on older installations. * Fix issues with API due to methods named “list” “list” is yet another reserved keyword in PHP and messes up older installations of PHP (5.6). This renames all methods named “list” to “lists”. The API route names are left untouched (e.g. still called “api.admin.users.list”). * Refactor and shorten some API logic Used laravel collection methods where applicable to directly transform the values instead of converting back and forth. This also removes some dead variables that were never used as well as getting rid of a n+1 problem in the Service API (loading service variables afterwards, not during the model creation). * Return model save status in repositories where applicable * Fix typo in ServicePolicy#powerStart * Apply StyleCI corrections
2016-12-12 19:30:57 +00:00
/**
* Returns the install script for the egg; if egg is copying from another
* it will return the copied script.
*
* @return string
*/
public function getCopyScriptInstallAttribute()
{
2021-01-23 20:33:34 +00:00
if (!is_null($this->script_install) || is_null($this->copy_script_from)) {
2018-03-07 05:07:00 +00:00
return $this->script_install;
}
return $this->scriptFrom->script_install;
}
/**
* Returns the entry command for the egg; if egg is copying from another
* it will return the copied entry command.
*
* @return string
*/
public function getCopyScriptEntryAttribute()
{
2021-01-23 20:33:34 +00:00
if (!is_null($this->script_entry) || is_null($this->copy_script_from)) {
2018-03-07 05:07:00 +00:00
return $this->script_entry;
}
return $this->scriptFrom->script_entry;
}
/**
* Returns the install container for the egg; if egg is copying from another
* it will return the copied install container.
*
* @return string
*/
public function getCopyScriptContainerAttribute()
{
2021-01-23 20:33:34 +00:00
if (!is_null($this->script_container) || is_null($this->copy_script_from)) {
2018-03-07 05:07:00 +00:00
return $this->script_container;
}
return $this->scriptFrom->script_container;
}
/**
* Return the file configuration for an egg.
*
* @return string
*/
public function getInheritConfigFilesAttribute()
{
2021-01-23 20:33:34 +00:00
if (!is_null($this->config_files) || is_null($this->config_from)) {
2018-03-07 05:07:00 +00:00
return $this->config_files;
}
return $this->configFrom->config_files;
}
/**
* Return the startup configuration for an egg.
*
* @return string
*/
public function getInheritConfigStartupAttribute()
{
2021-01-23 20:33:34 +00:00
if (!is_null($this->config_startup) || is_null($this->config_from)) {
2018-03-07 05:07:00 +00:00
return $this->config_startup;
}
return $this->configFrom->config_startup;
}
/**
* Return the log reading configuration for an egg.
*
* @return string
*/
public function getInheritConfigLogsAttribute()
{
2021-01-23 20:33:34 +00:00
if (!is_null($this->config_logs) || is_null($this->config_from)) {
2018-03-07 05:07:00 +00:00
return $this->config_logs;
}
return $this->configFrom->config_logs;
}
/**
* Return the stop command configuration for an egg.
*
* @return string
*/
public function getInheritConfigStopAttribute()
{
2021-01-23 20:33:34 +00:00
if (!is_null($this->config_stop) || is_null($this->config_from)) {
2018-03-07 05:07:00 +00:00
return $this->config_stop;
}
return $this->configFrom->config_stop;
}
/**
* Returns the features available to this egg from the parent configuration if there are
* no features defined for this egg specifically and there is a parent egg configured.
*
* @return array|null
*/
public function getInheritFeaturesAttribute()
{
2021-01-23 20:33:34 +00:00
if (!is_null($this->features) || is_null($this->config_from)) {
return $this->features;
}
return $this->configFrom->features;
}
/**
* Returns the features available to this egg from the parent configuration if there are
* no features defined for this egg specifically and there is a parent egg configured.
*
* @return string[]|null
*/
public function getInheritFileDenylistAttribute()
{
if (is_null($this->config_from)) {
return $this->file_denylist;
}
return $this->configFrom->file_denylist;
}
2017-03-19 23:36:50 +00:00
/**
* Gets nest associated with an egg.
2017-03-19 23:36:50 +00:00
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function nest()
2017-03-19 23:36:50 +00:00
{
return $this->belongsTo(Nest::class);
2017-03-19 23:36:50 +00:00
}
2017-02-05 22:58:17 +00:00
2017-03-19 23:36:50 +00:00
/**
* Gets all servers associated with this egg.
2017-03-19 23:36:50 +00:00
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function servers()
{
return $this->hasMany(Server::class, 'egg_id');
2017-03-19 23:36:50 +00:00
}
2017-02-05 22:58:17 +00:00
2017-03-19 23:36:50 +00:00
/**
* Gets all variables associated with this egg.
2017-03-19 23:36:50 +00:00
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function variables()
{
return $this->hasMany(EggVariable::class, 'egg_id');
2017-03-19 23:36:50 +00:00
}
/**
* Get the parent egg from which to copy scripts.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function scriptFrom()
{
2017-05-02 00:58:36 +00:00
return $this->belongsTo(self::class, 'copy_script_from');
}
/**
* Get the parent egg from which to copy configuration settings.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function configFrom()
{
return $this->belongsTo(self::class, 'config_from');
}
2015-12-08 23:33:33 +00:00
}