Files
panel-pelican-dev/app/Models/Mount.php

142 lines
4.2 KiB
PHP
Raw Permalink Normal View History

2020-05-20 17:29:03 -06:00
<?php
2024-03-12 22:39:16 -04:00
namespace App\Models;
2020-05-20 17:29:03 -06:00
use App\Contracts\Validatable;
use App\Traits\HasValidation;
2025-09-24 13:34:19 +02:00
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
2025-05-09 19:18:20 +02:00
use Illuminate\Database\Eloquent\Relations\MorphToMany;
2020-05-20 17:29:03 -06:00
/**
* @property int $id
* @property string $uuid
* @property string $name
* @property string|null $description
* @property string $source
* @property string $target
* @property bool $read_only
* @property bool $user_mountable
* @property-read Collection<int, Egg> $eggs
* @property-read int|null $eggs_count
* @property-read Collection<int, Node> $nodes
* @property-read int|null $nodes_count
* @property-read Collection<int, Server> $servers
* @property-read int|null $servers_count
*
* @method static \Illuminate\Database\Eloquent\Builder<static>|Mount newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder<static>|Mount newQuery()
* @method static \Illuminate\Database\Eloquent\Builder<static>|Mount query()
* @method static \Illuminate\Database\Eloquent\Builder<static>|Mount whereDescription($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Mount whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Mount whereName($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Mount whereReadOnly($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Mount whereSource($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Mount whereTarget($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Mount whereUserMountable($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Mount whereUuid($value)
2020-05-20 17:29:03 -06:00
*/
class Mount extends Model implements Validatable
2020-05-20 17:29:03 -06:00
{
use HasValidation { getRules as getValidationRules; }
2020-05-20 17:29:03 -06:00
/**
* The resource name for this model when it is transformed into an
* API representation using fractal.
*/
2021-01-23 12:33:34 -08:00
public const RESOURCE_NAME = 'mount';
2020-05-20 17:29:03 -06:00
/**
* Fields that are not mass assignable.
*
* @var string[]
2020-05-20 17:29:03 -06:00
*/
2024-05-11 22:08:23 -04:00
protected $guarded = ['id'];
/**
* Rules verifying that the data being stored matches the expectations of the database.
*
* @var array<array-key, string[]>
*/
public static array $validationRules = [
'name' => ['required', 'string', 'min:2', 'max:64', 'unique:mounts,name'],
'description' => ['nullable', 'string', 'max:255'],
'source' => ['required', 'string'],
'target' => ['required', 'string'],
'read_only' => ['sometimes', 'boolean'],
'user_mountable' => ['sometimes', 'boolean'],
];
/**
* Implement language verification by overriding Eloquence's gather rules function.
*
* @return array<array-key, string[]>
*/
public static function getRules(): array
{
$rules = self::getValidationRules();
$rules['source'][] = 'not_in:' . implode(',', Mount::$invalidSourcePaths);
$rules['target'][] = 'not_in:' . implode(',', Mount::$invalidTargetPaths);
return $rules;
}
2020-05-20 18:55:59 -06:00
/**
* Disable timestamps on this model.
*/
public $timestamps = false;
2020-10-17 14:37:18 -06:00
/**
* Blacklisted source paths.
*
* @var string[]
2020-10-17 14:37:18 -06:00
*/
2024-10-19 21:00:11 -04:00
public static array $invalidSourcePaths = [
2024-04-12 12:52:00 -04:00
'/etc/pelican',
'/var/lib/pelican/volumes',
2020-10-17 14:37:18 -06:00
'/srv/daemon-data',
];
/**
* Blacklisted target paths.
*
* @var string[]
2020-10-17 14:37:18 -06:00
*/
2024-10-19 21:00:11 -04:00
public static array $invalidTargetPaths = [
2020-10-17 14:37:18 -06:00
'/home/container',
];
2024-03-19 21:08:49 -04:00
protected function casts(): array
{
return [
'id' => 'int',
'read_only' => 'bool',
'user_mountable' => 'bool',
];
}
/**
* Returns all eggs that have this mount assigned.
*/
2025-05-09 19:18:20 +02:00
public function eggs(): MorphToMany
{
2025-05-09 19:18:20 +02:00
return $this->morphedByMany(Egg::class, 'mountable');
}
/**
* Returns all nodes that have this mount assigned.
*/
2025-05-09 19:18:20 +02:00
public function nodes(): MorphToMany
{
2025-05-09 19:18:20 +02:00
return $this->morphedByMany(Node::class, 'mountable');
}
/**
* Returns all servers that have this mount assigned.
*/
2025-05-09 19:18:20 +02:00
public function servers(): MorphToMany
{
2025-05-09 19:18:20 +02:00
return $this->morphedByMany(Server::class, 'mountable');
}
2020-05-20 17:29:03 -06:00
}