2015-12-06 19:58:49 +01:00
|
|
|
<?php
|
2016-01-20 01:10:39 +01:00
|
|
|
/**
|
2016-01-20 22:05:16 +01:00
|
|
|
* Pterodactyl - Panel
|
2017-01-24 23:57:08 +01:00
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
2016-01-20 01:10:39 +01:00
|
|
|
*
|
2016-01-20 21:56:40 +01: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 01:10:39 +01:00
|
|
|
*
|
2016-01-20 21:56:40 +01:00
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
2016-01-20 01:10:39 +01:00
|
|
|
*
|
2016-01-20 21:56:40 +01: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 01:10:39 +01:00
|
|
|
*/
|
2016-12-07 23:46:38 +01:00
|
|
|
|
2015-12-06 19:58:49 +01:00
|
|
|
namespace Pterodactyl\Models;
|
|
|
|
|
|
|
|
use Auth;
|
2017-02-03 01:41:38 +01:00
|
|
|
use Javascript;
|
2015-12-06 19:58:49 +01:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2017-01-25 01:15:03 +01:00
|
|
|
use Illuminate\Notifications\Notifiable;
|
2016-10-28 02:05:29 +02:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2015-12-06 19:58:49 +01:00
|
|
|
|
|
|
|
class Server extends Model
|
|
|
|
{
|
2017-01-25 01:15:03 +01:00
|
|
|
use Notifiable, SoftDeletes;
|
2016-10-28 02:05:29 +02:00
|
|
|
|
2015-12-06 19:58:49 +01:00
|
|
|
/**
|
|
|
|
* The table associated with the model.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $table = 'servers';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The attributes excluded from the model's JSON form.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-10-28 02:05:29 +02:00
|
|
|
protected $hidden = ['daemonSecret', 'sftp_password'];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The attributes that should be mutated to dates.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $dates = ['deleted_at'];
|
2015-12-06 19:58:49 +01:00
|
|
|
|
2015-12-15 21:08:41 +01:00
|
|
|
/**
|
|
|
|
* Fields that are not mass assignable.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-10-28 02:05:29 +02:00
|
|
|
protected $guarded = ['id', 'installed', 'created_at', 'updated_at', 'deleted_at'];
|
2015-12-15 21:08:41 +01:00
|
|
|
|
2016-12-07 23:46:38 +01:00
|
|
|
/**
|
|
|
|
* Cast values to correct type.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-01-27 04:17:51 +01:00
|
|
|
protected $casts = [
|
2017-02-03 01:41:38 +01:00
|
|
|
'node_id' => 'integer',
|
2016-09-02 03:21:01 +02:00
|
|
|
'suspended' => 'integer',
|
2017-02-03 01:41:38 +01:00
|
|
|
'owner_id' => 'integer',
|
2016-01-27 04:17:51 +01:00
|
|
|
'memory' => 'integer',
|
|
|
|
'swap' => 'integer',
|
|
|
|
'disk' => 'integer',
|
|
|
|
'io' => 'integer',
|
|
|
|
'cpu' => 'integer',
|
|
|
|
'oom_disabled' => 'integer',
|
2017-02-03 01:41:38 +01:00
|
|
|
'allocation_id' => 'integer',
|
|
|
|
'service_id' => 'integer',
|
|
|
|
'option_id' => 'integer',
|
|
|
|
'pack_id' => 'integer',
|
2016-01-27 04:17:51 +01:00
|
|
|
'installed' => 'integer',
|
|
|
|
];
|
|
|
|
|
2015-12-06 19:58:49 +01:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $serverUUIDInstance = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
protected static $user;
|
|
|
|
|
|
|
|
/**
|
2016-12-07 23:46:38 +01:00
|
|
|
* Constructor.
|
2015-12-06 19:58:49 +01:00
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2016-10-28 02:05:29 +02:00
|
|
|
parent::__construct();
|
2016-10-20 22:48:37 +02:00
|
|
|
self::$user = Auth::user();
|
2015-12-06 19:58:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-23 03:43:50 +01:00
|
|
|
* Returns a single server specified by UUID.
|
|
|
|
* DO NOT USE THIS TO MODIFY SERVER DETAILS OR SAVE THOSE DETAILS.
|
|
|
|
* YOU WILL OVERWRITE THE SECRET KEY AND BREAK THINGS.
|
2015-12-06 19:58:49 +01:00
|
|
|
*
|
|
|
|
* @param string $uuid The Short-UUID of the server to return an object about.
|
|
|
|
* @return \Illuminate\Database\Eloquent\Collection
|
|
|
|
*/
|
2017-02-03 00:21:36 +01:00
|
|
|
public static function byUuid($uuid)
|
2015-12-06 19:58:49 +01:00
|
|
|
{
|
2017-02-06 01:59:16 +01:00
|
|
|
$query = self::with('service', 'node')->where(function ($q) use ($uuid) {
|
|
|
|
$q->where('uuidShort', $uuid)->orWhere('uuid', $uuid);
|
|
|
|
});
|
2015-12-06 19:58:49 +01:00
|
|
|
|
2017-02-03 00:21:36 +01:00
|
|
|
if (! Auth::user()->isRootAdmin()) {
|
2017-02-06 01:19:46 +01:00
|
|
|
$query->whereIn('id', Auth::user()->serverAccessArray());
|
2015-12-06 19:58:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$result = $query->first();
|
|
|
|
|
2016-12-07 23:46:38 +01:00
|
|
|
if (! is_null($result)) {
|
2017-02-03 00:21:36 +01:00
|
|
|
$result->daemonSecret = Auth::user()->daemonToken($result);
|
2015-12-06 19:58:49 +01:00
|
|
|
}
|
|
|
|
|
2017-02-03 00:21:36 +01:00
|
|
|
return $result;
|
2015-12-06 19:58:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-07 23:46:38 +01:00
|
|
|
* Returns non-administrative headers for accessing a server on the daemon.
|
2015-12-06 19:58:49 +01:00
|
|
|
*
|
|
|
|
* @param string $uuid
|
|
|
|
* @return array
|
|
|
|
*/
|
2017-02-03 01:41:38 +01:00
|
|
|
public function guzzleHeaders()
|
2015-12-06 19:58:49 +01:00
|
|
|
{
|
2017-02-03 00:21:36 +01:00
|
|
|
return [
|
|
|
|
'X-Access-Server' => $this->uuid,
|
|
|
|
'X-Access-Token' => Auth::user()->daemonToken($this),
|
|
|
|
];
|
2015-12-06 19:58:49 +01:00
|
|
|
}
|
2017-02-02 22:24:08 +01:00
|
|
|
|
2017-02-03 01:41:38 +01:00
|
|
|
/**
|
|
|
|
* Return an instance of the Guzzle client for this specific server using defined access token.
|
|
|
|
*
|
|
|
|
* @return \GuzzleHttp\Client
|
|
|
|
*/
|
|
|
|
public function guzzleClient()
|
|
|
|
{
|
|
|
|
return $this->node->guzzleClient($this->guzzleHeaders());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns javascript object to be embedded on server view pages with relevant information.
|
|
|
|
*
|
|
|
|
* @return \Laracasts\Utilities\JavaScript\JavaScriptFacade
|
|
|
|
*/
|
|
|
|
public function js($additional = null, $overwrite = null)
|
|
|
|
{
|
|
|
|
$response = [
|
|
|
|
'server' => collect($this->makeVisible('daemonSecret'))->only([
|
|
|
|
'uuid',
|
|
|
|
'uuidShort',
|
|
|
|
'daemonSecret',
|
2017-02-03 01:43:55 +01:00
|
|
|
'username',
|
2017-02-03 01:41:38 +01:00
|
|
|
]),
|
|
|
|
'node' => collect($this->node)->only([
|
|
|
|
'fqdn',
|
|
|
|
'scheme',
|
2017-02-03 01:43:55 +01:00
|
|
|
'daemonListen',
|
2017-02-03 01:41:38 +01:00
|
|
|
]),
|
|
|
|
];
|
|
|
|
|
|
|
|
if (is_array($additional)) {
|
|
|
|
$response = array_merge($response, $additional);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($overwrite)) {
|
|
|
|
$response = $overwrite;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Javascript::put($response);
|
|
|
|
}
|
|
|
|
|
2017-02-03 22:50:28 +01:00
|
|
|
/**
|
|
|
|
* Gets the user who owns the server.
|
|
|
|
*
|
2017-02-06 00:00:39 +01:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2017-02-03 22:50:28 +01:00
|
|
|
*/
|
|
|
|
public function user()
|
|
|
|
{
|
2017-02-06 00:00:39 +01:00
|
|
|
return $this->belongsTo(User::class, 'owner_id');
|
2017-02-03 22:50:28 +01:00
|
|
|
}
|
|
|
|
|
2017-02-06 01:19:46 +01:00
|
|
|
/**
|
|
|
|
* Gets the default allocation for a server.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne
|
|
|
|
*/
|
|
|
|
public function allocation()
|
|
|
|
{
|
|
|
|
return $this->hasOne(Allocation::class, 'id', 'allocation_id');
|
|
|
|
}
|
|
|
|
|
2017-02-02 22:24:08 +01:00
|
|
|
/**
|
|
|
|
* Gets all allocations associated with this server.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
|
|
|
public function allocations()
|
|
|
|
{
|
2017-02-03 22:50:28 +01:00
|
|
|
return $this->hasMany(Allocation::class, 'server_id');
|
2017-02-02 22:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets information for the pack associated with this server.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne
|
|
|
|
*/
|
|
|
|
public function pack()
|
|
|
|
{
|
2017-02-03 00:21:36 +01:00
|
|
|
return $this->hasOne(ServicePack::class, 'id', 'pack_id');
|
2017-02-02 22:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets information for the service associated with this server.
|
|
|
|
*
|
2017-02-06 00:00:39 +01:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2017-02-02 22:24:08 +01:00
|
|
|
*/
|
|
|
|
public function service()
|
|
|
|
{
|
2017-02-06 00:00:39 +01:00
|
|
|
return $this->belongsTo(Service::class);
|
2017-02-02 22:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets information for the service option associated with this server.
|
|
|
|
*
|
2017-02-06 00:00:39 +01:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2017-02-02 22:24:08 +01:00
|
|
|
*/
|
|
|
|
public function option()
|
|
|
|
{
|
2017-02-06 00:00:39 +01:00
|
|
|
return $this->belongsTo(ServiceOptions::class);
|
2017-02-02 22:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets information for the service variables associated with this server.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
|
|
|
public function variables()
|
|
|
|
{
|
|
|
|
return $this->hasMany(ServerVariables::class);
|
|
|
|
}
|
2017-02-03 00:21:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets information for the node associated with this server.
|
|
|
|
*
|
2017-02-06 00:00:39 +01:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2017-02-03 00:21:36 +01:00
|
|
|
*/
|
|
|
|
public function node()
|
|
|
|
{
|
2017-02-06 00:00:39 +01:00
|
|
|
return $this->belongsTo(Node::class);
|
2017-02-03 00:21:36 +01:00
|
|
|
}
|
2017-02-03 01:41:38 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets information for the tasks associated with this server.
|
|
|
|
*
|
|
|
|
* @TODO adjust server column in tasks to be server_id
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
|
|
|
public function tasks()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Task::class, 'server', 'id');
|
|
|
|
}
|
2017-02-09 23:43:54 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets all databases associated with a server.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
|
|
|
public function databases()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Database::class);
|
|
|
|
}
|
2015-12-06 19:58:49 +01:00
|
|
|
}
|