diff --git a/app/Models/APILog.php b/app/Models/APILog.php index 1a1772df6..a43624cce 100644 --- a/app/Models/APILog.php +++ b/app/Models/APILog.php @@ -21,10 +21,10 @@ class APILog extends Model */ protected $guarded = ['id', 'created_at', 'updated_at']; - /** - * Cast values to correct type. - */ - protected $casts = [ - 'authorized' => 'boolean', - ]; + protected function casts(): array + { + return [ + 'authorized' => 'boolean', + ]; + } } diff --git a/app/Models/ActivityLog.php b/app/Models/ActivityLog.php index 1f0906838..8eaeb744d 100644 --- a/app/Models/ActivityLog.php +++ b/app/Models/ActivityLog.php @@ -67,11 +67,6 @@ class ActivityLog extends Model 'timestamp', ]; - protected $casts = [ - 'properties' => 'collection', - 'timestamp' => 'datetime', - ]; - protected $with = ['subjects']; public static array $validationRules = [ @@ -82,6 +77,14 @@ class ActivityLog extends Model 'properties' => ['array'], ]; + protected function casts(): array + { + return [ + 'properties' => 'collection', + 'timestamp' => 'datetime', + ]; + } + public function actor(): MorphTo { $morph = $this->morphTo(); diff --git a/app/Models/Allocation.php b/app/Models/Allocation.php index 3430d7537..18d6e269f 100644 --- a/app/Models/Allocation.php +++ b/app/Models/Allocation.php @@ -57,15 +57,6 @@ class Allocation extends Model */ protected $guarded = ['id', 'created_at', 'updated_at']; - /** - * Cast values to correct type. - */ - protected $casts = [ - 'node_id' => 'integer', - 'port' => 'integer', - 'server_id' => 'integer', - ]; - public static array $validationRules = [ 'node_id' => 'required|exists:nodes,id', 'ip' => 'required|ip', @@ -82,9 +73,15 @@ class Allocation extends Model }); } - /** - * {@inheritDoc} - */ + protected function casts(): array + { + return [ + 'node_id' => 'integer', + 'port' => 'integer', + 'server_id' => 'integer', + ]; + } + public function getRouteKeyName(): string { return $this->getKeyName(); diff --git a/app/Models/ApiKey.php b/app/Models/ApiKey.php index 355bd633f..b3a1a1bcb 100644 --- a/app/Models/ApiKey.php +++ b/app/Models/ApiKey.php @@ -88,25 +88,6 @@ class ApiKey extends Model */ protected $table = 'api_keys'; - /** - * Cast values to correct type. - */ - protected $casts = [ - 'allowed_ips' => 'array', - 'user_id' => 'int', - 'last_used_at' => 'datetime', - 'expires_at' => 'datetime', - self::CREATED_AT => 'datetime', - self::UPDATED_AT => 'datetime', - 'r_' . AdminAcl::RESOURCE_USERS => 'int', - 'r_' . AdminAcl::RESOURCE_ALLOCATIONS => 'int', - 'r_' . AdminAcl::RESOURCE_DATABASE_HOSTS => 'int', - 'r_' . AdminAcl::RESOURCE_SERVER_DATABASES => 'int', - 'r_' . AdminAcl::RESOURCE_EGGS => 'int', - 'r_' . AdminAcl::RESOURCE_NODES => 'int', - 'r_' . AdminAcl::RESOURCE_SERVERS => 'int', - ]; - /** * Fields that are mass assignable. */ @@ -147,6 +128,25 @@ class ApiKey extends Model 'r_' . AdminAcl::RESOURCE_SERVERS => 'integer|min:0|max:3', ]; + protected function casts(): array + { + return [ + 'allowed_ips' => 'array', + 'user_id' => 'int', + 'last_used_at' => 'datetime', + 'expires_at' => 'datetime', + self::CREATED_AT => 'datetime', + self::UPDATED_AT => 'datetime', + 'r_' . AdminAcl::RESOURCE_USERS => 'int', + 'r_' . AdminAcl::RESOURCE_ALLOCATIONS => 'int', + 'r_' . AdminAcl::RESOURCE_DATABASE_HOSTS => 'int', + 'r_' . AdminAcl::RESOURCE_SERVER_DATABASES => 'int', + 'r_' . AdminAcl::RESOURCE_EGGS => 'int', + 'r_' . AdminAcl::RESOURCE_NODES => 'int', + 'r_' . AdminAcl::RESOURCE_SERVERS => 'int', + ]; + } + /** * Returns the user this token is assigned to. */ diff --git a/app/Models/AuditLog.php b/app/Models/AuditLog.php index 127838712..aa9d57d6a 100644 --- a/app/Models/AuditLog.php +++ b/app/Models/AuditLog.php @@ -26,18 +26,21 @@ class AuditLog extends Model protected $table = 'audit_logs'; - protected $casts = [ - 'is_system' => 'bool', - 'device' => 'array', - 'metadata' => 'array', - 'created_at' => 'immutable_datetime', - ]; - protected $guarded = [ 'id', 'created_at', ]; + protected function casts(): array + { + return [ + 'is_system' => 'bool', + 'device' => 'array', + 'metadata' => 'array', + 'created_at' => 'immutable_datetime', + ]; + } + public function user(): BelongsTo { return $this->belongsTo(User::class); diff --git a/app/Models/Backup.php b/app/Models/Backup.php index 88ba501a0..f83e46e06 100644 --- a/app/Models/Backup.php +++ b/app/Models/Backup.php @@ -36,18 +36,6 @@ class Backup extends Model protected $table = 'backups'; - protected $casts = [ - 'id' => 'int', - 'is_successful' => 'bool', - 'is_locked' => 'bool', - 'ignored_files' => 'array', - 'bytes' => 'int', - 'completed_at' => 'immutable_datetime', - 'created_at' => 'immutable_datetime', - 'updated_at' => 'immutable_datetime', - 'deleted_at' => 'immutable_datetime', - ]; - protected $attributes = [ 'is_successful' => false, 'is_locked' => false, @@ -71,6 +59,21 @@ class Backup extends Model 'upload_id' => 'nullable|string', ]; + protected function casts(): array + { + return [ + 'id' => 'int', + 'is_successful' => 'bool', + 'is_locked' => 'bool', + 'ignored_files' => 'array', + 'bytes' => 'int', + 'completed_at' => 'immutable_datetime', + 'created_at' => 'immutable_datetime', + 'updated_at' => 'immutable_datetime', + 'deleted_at' => 'immutable_datetime', + ]; + } + public function server(): BelongsTo { return $this->belongsTo(Server::class); diff --git a/app/Models/Database.php b/app/Models/Database.php index d4eed95c5..3af7b9975 100644 --- a/app/Models/Database.php +++ b/app/Models/Database.php @@ -48,15 +48,6 @@ class Database extends Model 'server_id', 'database_host_id', 'database', 'username', 'password', 'remote', 'max_connections', ]; - /** - * Cast values to correct type. - */ - protected $casts = [ - 'server_id' => 'integer', - 'database_host_id' => 'integer', - 'max_connections' => 'integer', - ]; - public static array $validationRules = [ 'server_id' => 'required|numeric|exists:servers,id', 'database_host_id' => 'required|exists:database_hosts,id', @@ -67,9 +58,15 @@ class Database extends Model 'password' => 'string', ]; - /** - * {@inheritDoc} - */ + protected function casts(): array + { + return [ + 'server_id' => 'integer', + 'database_host_id' => 'integer', + 'max_connections' => 'integer', + ]; + } + public function getRouteKeyName(): string { return $this->getKeyName(); diff --git a/app/Models/DatabaseHost.php b/app/Models/DatabaseHost.php index da34dcf29..06c13e1ed 100644 --- a/app/Models/DatabaseHost.php +++ b/app/Models/DatabaseHost.php @@ -42,17 +42,6 @@ class DatabaseHost extends Model 'name', 'host', 'port', 'username', 'password', 'max_databases', 'node_id', ]; - /** - * Cast values to correct type. - */ - protected $casts = [ - 'id' => 'integer', - 'max_databases' => 'integer', - 'node_id' => 'integer', - 'created_at' => 'immutable_datetime', - 'updated_at' => 'immutable_datetime', - ]; - /** * Validation rules to assign to this model. */ @@ -65,6 +54,17 @@ class DatabaseHost extends Model 'node_id' => 'sometimes|nullable|integer|exists:nodes,id', ]; + protected function casts(): array + { + return [ + 'id' => 'integer', + 'max_databases' => 'integer', + 'node_id' => 'integer', + 'created_at' => 'immutable_datetime', + 'updated_at' => 'immutable_datetime', + ]; + } + /** * Gets the node associated with a database host. */ diff --git a/app/Models/Egg.php b/app/Models/Egg.php index 5352dd641..a68d6b63d 100644 --- a/app/Models/Egg.php +++ b/app/Models/Egg.php @@ -99,19 +99,6 @@ class Egg extends Model 'copy_script_from', ]; - /** - * Cast values to correct type. - */ - protected $casts = [ - 'config_from' => 'integer', - 'script_is_privileged' => 'boolean', - 'force_outgoing_ip' => 'boolean', - 'copy_script_from' => 'integer', - 'features' => 'array', - 'docker_images' => 'array', - 'file_denylist' => 'array', - ]; - public static array $validationRules = [ 'uuid' => 'required|string|size:36', 'name' => 'required|string|max:191', @@ -142,6 +129,19 @@ class Egg extends Model 'update_url' => null, ]; + protected function casts(): array + { + return [ + 'config_from' => 'integer', + 'script_is_privileged' => 'boolean', + 'force_outgoing_ip' => 'boolean', + 'copy_script_from' => 'integer', + 'features' => 'array', + 'docker_images' => 'array', + 'file_denylist' => 'array', + ]; + } + protected static function booted(): void { static::deleting(function (self $egg) { diff --git a/app/Models/EggVariable.php b/app/Models/EggVariable.php index 6de71c83b..6df7c53b9 100644 --- a/app/Models/EggVariable.php +++ b/app/Models/EggVariable.php @@ -48,17 +48,6 @@ class EggVariable extends Model */ protected $guarded = ['id', 'created_at', 'updated_at']; - /** - * Cast values to correct type. - */ - protected $casts = [ - 'egg_id' => 'integer', - 'user_viewable' => 'bool', - 'user_editable' => 'bool', - 'created_at' => 'immutable_datetime', - 'updated_at' => 'immutable_datetime', - ]; - public static array $validationRules = [ 'egg_id' => 'exists:eggs,id', 'name' => 'required|string|between:1,191', @@ -75,6 +64,17 @@ class EggVariable extends Model 'user_viewable' => 0, ]; + protected function casts(): array + { + return [ + 'egg_id' => 'integer', + 'user_viewable' => 'bool', + 'user_editable' => 'bool', + 'created_at' => 'immutable_datetime', + 'updated_at' => 'immutable_datetime', + ]; + } + public function getRequiredAttribute(): bool { return in_array('required', explode('|', $this->rules)); diff --git a/app/Models/Mount.php b/app/Models/Mount.php index ca34cd3dd..f372a4989 100644 --- a/app/Models/Mount.php +++ b/app/Models/Mount.php @@ -36,15 +36,6 @@ class Mount extends Model */ protected $guarded = ['id', 'uuid']; - /** - * Default values for specific fields in the database. - */ - protected $casts = [ - 'id' => 'int', - 'read_only' => 'bool', - 'user_mountable' => 'bool', - ]; - /** * Rules verifying that the data being stored matches the expectations of the database. */ @@ -92,6 +83,15 @@ class Mount extends Model '/home/container', ]; + protected function casts(): array + { + return [ + 'id' => 'int', + 'read_only' => 'bool', + 'user_mountable' => 'bool', + ]; + } + /** * Returns all eggs that have this mount assigned. */ diff --git a/app/Models/Node.php b/app/Models/Node.php index 4d9711aa7..a1da6cffa 100644 --- a/app/Models/Node.php +++ b/app/Models/Node.php @@ -60,19 +60,6 @@ class Node extends Model public int $sum_memory; public int $sum_disk; - /** - * Cast values to correct type. - */ - protected $casts = [ - 'memory' => 'integer', - 'disk' => 'integer', - 'daemonListen' => 'integer', - 'daemonSFTP' => 'integer', - 'behind_proxy' => 'boolean', - 'public' => 'boolean', - 'maintenance_mode' => 'boolean', - ]; - /** * Fields that are mass assignable. */ @@ -117,6 +104,19 @@ class Node extends Model 'maintenance_mode' => false, ]; + protected function casts(): array + { + return [ + 'memory' => 'integer', + 'disk' => 'integer', + 'daemonListen' => 'integer', + 'daemonSFTP' => 'integer', + 'behind_proxy' => 'boolean', + 'public' => 'boolean', + 'maintenance_mode' => 'boolean', + ]; + } + /** * Get the connection address to use when making calls to this node. */ diff --git a/app/Models/Permission.php b/app/Models/Permission.php index 22ea7a4c4..ab9fef260 100644 --- a/app/Models/Permission.php +++ b/app/Models/Permission.php @@ -80,13 +80,6 @@ class Permission extends Model */ protected $guarded = ['id', 'created_at', 'updated_at']; - /** - * Cast values to correct type. - */ - protected $casts = [ - 'subuser_id' => 'integer', - ]; - public static array $validationRules = [ 'subuser_id' => 'required|numeric|min:1', 'permission' => 'required|string', @@ -208,6 +201,13 @@ class Permission extends Model ], ]; + protected function casts(): array + { + return [ + 'subuser_id' => 'integer', + ]; + } + /** * Returns all the permissions available on the system for a user to * have when controlling a server. diff --git a/app/Models/Schedule.php b/app/Models/Schedule.php index 7bf43e3f8..8fb3f4167 100644 --- a/app/Models/Schedule.php +++ b/app/Models/Schedule.php @@ -65,16 +65,6 @@ class Schedule extends Model 'next_run_at', ]; - protected $casts = [ - 'id' => 'integer', - 'server_id' => 'integer', - 'is_active' => 'boolean', - 'is_processing' => 'boolean', - 'only_when_online' => 'boolean', - 'last_run_at' => 'datetime', - 'next_run_at' => 'datetime', - ]; - protected $attributes = [ 'name' => null, 'cron_day_of_week' => '*', @@ -102,9 +92,19 @@ class Schedule extends Model 'next_run_at' => 'nullable|date', ]; - /** - * {@inheritDoc} - */ + protected function casts(): array + { + return [ + 'id' => 'integer', + 'server_id' => 'integer', + 'is_active' => 'boolean', + 'is_processing' => 'boolean', + 'only_when_online' => 'boolean', + 'last_run_at' => 'datetime', + 'next_run_at' => 'datetime', + ]; + } + public function getRouteKeyName(): string { return $this->getKeyName(); diff --git a/app/Models/Server.php b/app/Models/Server.php index 1e0f15adc..ad435c596 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -170,29 +170,29 @@ class Server extends Model 'backup_limit' => 'present|nullable|integer|min:0', ]; - /** - * Cast values to correct type. - */ - protected $casts = [ - 'node_id' => 'integer', - 'skip_scripts' => 'boolean', - 'owner_id' => 'integer', - 'memory' => 'integer', - 'swap' => 'integer', - 'disk' => 'integer', - 'io' => 'integer', - 'cpu' => 'integer', - 'oom_disabled' => 'boolean', - 'allocation_id' => 'integer', - 'egg_id' => 'integer', - 'database_limit' => 'integer', - 'allocation_limit' => 'integer', - 'backup_limit' => 'integer', - self::CREATED_AT => 'datetime', - self::UPDATED_AT => 'datetime', - 'deleted_at' => 'datetime', - 'installed_at' => 'datetime', - ]; + protected function casts(): array + { + return [ + 'node_id' => 'integer', + 'skip_scripts' => 'boolean', + 'owner_id' => 'integer', + 'memory' => 'integer', + 'swap' => 'integer', + 'disk' => 'integer', + 'io' => 'integer', + 'cpu' => 'integer', + 'oom_disabled' => 'boolean', + 'allocation_id' => 'integer', + 'egg_id' => 'integer', + 'database_limit' => 'integer', + 'allocation_limit' => 'integer', + 'backup_limit' => 'integer', + self::CREATED_AT => 'datetime', + self::UPDATED_AT => 'datetime', + 'deleted_at' => 'datetime', + 'installed_at' => 'datetime', + ]; + } /** * Returns the format for server allocations when communicating with the Daemon. diff --git a/app/Models/ServerTransfer.php b/app/Models/ServerTransfer.php index 4cc66d951..abfef3eb6 100644 --- a/app/Models/ServerTransfer.php +++ b/app/Models/ServerTransfer.php @@ -40,21 +40,6 @@ class ServerTransfer extends Model */ protected $guarded = ['id', 'created_at', 'updated_at']; - /** - * Cast values to correct type. - */ - protected $casts = [ - 'server_id' => 'int', - 'old_node' => 'int', - 'new_node' => 'int', - 'old_allocation' => 'int', - 'new_allocation' => 'int', - 'old_additional_allocations' => 'array', - 'new_additional_allocations' => 'array', - 'successful' => 'bool', - 'archived' => 'bool', - ]; - public static array $validationRules = [ 'server_id' => 'required|numeric|exists:servers,id', 'old_node' => 'required|numeric', @@ -68,6 +53,21 @@ class ServerTransfer extends Model 'successful' => 'sometimes|nullable|boolean', ]; + protected function casts(): array + { + return [ + 'server_id' => 'int', + 'old_node' => 'int', + 'new_node' => 'int', + 'old_allocation' => 'int', + 'new_allocation' => 'int', + 'old_additional_allocations' => 'array', + 'new_additional_allocations' => 'array', + 'successful' => 'bool', + 'archived' => 'bool', + ]; + } + /** * Gets the server associated with a server transfer. */ diff --git a/app/Models/ServerVariable.php b/app/Models/ServerVariable.php index 2d3023824..0c7eeffc5 100644 --- a/app/Models/ServerVariable.php +++ b/app/Models/ServerVariable.php @@ -26,19 +26,22 @@ class ServerVariable extends Model protected $guarded = ['id', 'created_at', 'updated_at']; - protected $casts = [ - 'server_id' => 'integer', - 'variable_id' => 'integer', - 'created_at' => 'immutable_datetime', - 'updated_at' => 'immutable_datetime', - ]; - public static array $validationRules = [ 'server_id' => 'required|int', 'variable_id' => 'required|int', 'variable_value' => 'string', ]; + protected function casts(): array + { + return [ + 'server_id' => 'integer', + 'variable_id' => 'integer', + 'created_at' => 'immutable_datetime', + 'updated_at' => 'immutable_datetime', + ]; + } + /** * Returns the server this variable is associated with. */ diff --git a/app/Models/Session.php b/app/Models/Session.php index daa9e00d4..e04d98d39 100644 --- a/app/Models/Session.php +++ b/app/Models/Session.php @@ -11,11 +11,11 @@ class Session extends Model */ protected $table = 'sessions'; - /** - * Cast values to correct type. - */ - protected $casts = [ - 'id' => 'string', - 'user_id' => 'integer', - ]; + protected function casts(): array + { + return [ + 'id' => 'string', + 'user_id' => 'integer', + ]; + } } diff --git a/app/Models/Subuser.php b/app/Models/Subuser.php index 3951e44ad..0c1f4d382 100644 --- a/app/Models/Subuser.php +++ b/app/Models/Subuser.php @@ -36,15 +36,6 @@ class Subuser extends Model */ protected $guarded = ['id', 'created_at', 'updated_at']; - /** - * Cast values to correct type. - */ - protected $casts = [ - 'user_id' => 'int', - 'server_id' => 'int', - 'permissions' => 'array', - ]; - public static array $validationRules = [ 'user_id' => 'required|numeric|exists:users,id', 'server_id' => 'required|numeric|exists:servers,id', @@ -52,6 +43,15 @@ class Subuser extends Model 'permissions.*' => 'string', ]; + protected function casts(): array + { + return [ + 'user_id' => 'int', + 'server_id' => 'int', + 'permissions' => 'array', + ]; + } + /** * Return a hashid encoded string to represent the ID of the subuser. */ diff --git a/app/Models/Task.php b/app/Models/Task.php index 0a887977a..c6392b375 100644 --- a/app/Models/Task.php +++ b/app/Models/Task.php @@ -62,18 +62,6 @@ class Task extends Model 'continue_on_failure', ]; - /** - * Cast values to correct type. - */ - protected $casts = [ - 'id' => 'integer', - 'schedule_id' => 'integer', - 'sequence_id' => 'integer', - 'time_offset' => 'integer', - 'is_queued' => 'boolean', - 'continue_on_failure' => 'boolean', - ]; - /** * Default attributes when creating a new model. */ @@ -93,9 +81,18 @@ class Task extends Model 'continue_on_failure' => 'boolean', ]; - /** - * {@inheritDoc} - */ + protected function casts(): array + { + return [ + 'id' => 'integer', + 'schedule_id' => 'integer', + 'sequence_id' => 'integer', + 'time_offset' => 'integer', + 'is_queued' => 'boolean', + 'continue_on_failure' => 'boolean', + ]; + } + public function getRouteKeyName(): string { return $this->getKeyName(); diff --git a/app/Models/TaskLog.php b/app/Models/TaskLog.php index ed5dff7b5..0c272c7f2 100644 --- a/app/Models/TaskLog.php +++ b/app/Models/TaskLog.php @@ -16,15 +16,15 @@ class TaskLog extends Model */ protected $guarded = ['id', 'created_at', 'updated_at']; - /** - * Cast values to correct type. - */ - protected $casts = [ - 'id' => 'integer', - 'task_id' => 'integer', - 'run_status' => 'integer', - 'run_time' => 'datetime', - 'created_at' => 'datetime', - 'updated_at' => 'datetime', - ]; + protected function casts(): array + { + return [ + 'id' => 'integer', + 'task_id' => 'integer', + 'run_status' => 'integer', + 'run_time' => 'datetime', + 'created_at' => 'datetime', + 'updated_at' => 'datetime', + ]; + } } diff --git a/app/Models/User.php b/app/Models/User.php index 0a21c47d8..14b26abaa 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -125,16 +125,6 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac 'root_admin', ]; - /** - * Cast values to correct type. - */ - protected $casts = [ - 'root_admin' => 'boolean', - 'use_totp' => 'boolean', - 'gravatar' => 'boolean', - 'totp_authenticated_at' => 'datetime', - ]; - /** * The attributes excluded from the model's JSON form. */ @@ -168,6 +158,16 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac 'totp_secret' => 'nullable|string', ]; + protected function casts(): array + { + return [ + 'root_admin' => 'boolean', + 'use_totp' => 'boolean', + 'gravatar' => 'boolean', + 'totp_authenticated_at' => 'datetime', + ]; + } + protected static function booted(): void { static::deleting(function (self $user) {