misc_pterodactyl-panel/app/Models/Server.php

304 lines
8.1 KiB
PHP
Raw Normal View History

<?php
2016-01-20 00:10:39 +00:00
/**
2016-01-20 21:05:16 +00:00
* Pterodactyl - Panel
2017-01-24 22:57:08 +00:00
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
2016-01-20 00:10:39 +00:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
2016-01-20 00:10:39 +00:00
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
2016-01-20 00:10:39 +00:00
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
2016-01-20 00:10:39 +00:00
*/
2016-12-07 22:46:38 +00:00
namespace Pterodactyl\Models;
use Schema;
use Sofa\Eloquence\Eloquence;
use Sofa\Eloquence\Validable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Znck\Eloquent\Traits\BelongsToThrough;
use Sofa\Eloquence\Contracts\CleansAttributes;
use Sofa\Eloquence\Contracts\Validable as ValidableContract;
class Server extends Model implements CleansAttributes, ValidableContract
{
use BelongsToThrough, Eloquence, Notifiable, Validable;
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'servers';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['sftp_password'];
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['deleted_at'];
/**
* Fields that are not mass assignable.
*
* @var array
*/
protected $guarded = ['id', 'installed', 'created_at', 'updated_at', 'deleted_at'];
/**
* @var array
*/
protected static $applicationRules = [
'owner_id' => 'required',
'name' => 'required',
'memory' => 'required',
'swap' => 'required',
'io' => 'required',
'cpu' => 'required',
'disk' => 'required',
'service_id' => 'required',
'option_id' => 'required',
'node_id' => 'required',
'allocation_id' => 'required',
'pack_id' => 'sometimes',
'auto_deploy' => 'sometimes',
'custom_id' => 'sometimes',
'skip_scripts' => 'sometimes',
];
/**
* @var array
*/
protected static $dataIntegrityRules = [
'owner_id' => 'exists:users,id',
'name' => 'regex:/^([\w .-]{1,200})$/',
'node_id' => 'exists:nodes,id',
'description' => 'nullable|string',
'memory' => 'numeric|min:0',
'swap' => 'numeric|min:-1',
'io' => 'numeric|between:10,1000',
'cpu' => 'numeric|min:0',
'disk' => 'numeric|min:0',
'allocation_id' => 'exists:allocations,id',
'service_id' => 'exists:services,id',
'option_id' => 'exists:service_options,id',
'pack_id' => 'nullable|numeric|min:0',
'custom_container' => 'nullable|string',
'startup' => 'nullable|string',
'auto_deploy' => 'accepted',
'custom_id' => 'numeric|unique:servers,id',
'skip_scripts' => 'boolean',
];
/**
* Cast values to correct type.
*
* @var array
*/
protected $casts = [
'node_id' => 'integer',
'skip_scripts' => 'boolean',
'suspended' => 'integer',
'owner_id' => 'integer',
'memory' => 'integer',
'swap' => 'integer',
'disk' => 'integer',
'io' => 'integer',
'cpu' => 'integer',
'oom_disabled' => 'integer',
'allocation_id' => 'integer',
'service_id' => 'integer',
'option_id' => 'integer',
'pack_id' => 'integer',
'installed' => 'integer',
];
2016-01-27 03:17:51 +00:00
/**
* Parameters for search querying.
*
* @var array
*/
protected $searchableColumns = [
'name' => 10,
'username' => 10,
'uuidShort' => 9,
'uuid' => 8,
'pack.name' => 7,
'user.email' => 6,
'user.username' => 6,
'node.name' => 2,
];
2017-02-19 00:31:44 +00:00
/**
* Return the columns available for this table.
*
* @return array
*/
2017-04-09 23:16:39 +00:00
public function getTableColumns()
{
return Schema::getColumnListing($this->getTable());
}
/**
* Gets the user who owns the server.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo(User::class, 'owner_id');
}
2017-02-10 00:38:54 +00:00
/**
* Gets the subusers associated with a server.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function subusers()
{
return $this->hasMany(Subuser::class);
}
/**
* Gets the default allocation for a server.
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function allocation()
{
return $this->hasOne(Allocation::class, 'id', 'allocation_id');
}
/**
* Gets all allocations associated with this server.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function allocations()
{
return $this->hasMany(Allocation::class, 'server_id');
}
/**
* Gets information for the pack associated with this server.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function pack()
{
return $this->belongsTo(Pack::class);
}
/**
* Gets information for the service associated with this server.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function service()
{
return $this->belongsTo(Service::class);
}
/**
* Gets information for the service option associated with this server.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function option()
{
2017-02-12 21:02:23 +00:00
return $this->belongsTo(ServiceOption::class);
}
/**
* Gets information for the service variables associated with this server.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function variables()
{
return $this->hasMany(ServerVariable::class);
}
/**
* Gets information for the node associated with this server.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function node()
{
return $this->belongsTo(Node::class);
}
/**
* Gets information for the tasks associated with this server.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function schedule()
{
return $this->hasMany(Schedule::class);
}
/**
* Gets all databases associated with a server.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function databases()
{
return $this->hasMany(Database::class);
}
/**
* Returns the location that a server belongs to.
*
* @return \Znck\Eloquent\Relations\BelongsToThrough
*
* @throws \Exception
*/
public function location()
{
return $this->belongsToThrough(Location::class, Node::class);
}
/**
* Return the key belonging to the server owner.
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function ownerKey()
{
return $this->hasOne(DaemonKey::class, 'user_id', 'owner_id');
}
/**
* Returns all of the daemon keys belonging to this server.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function keys()
{
return $this->hasMany(DaemonKey::class);
}
}