2017-07-03 02:29:58 +00:00
|
|
|
<?php
|
2017-09-26 02:43:01 +00:00
|
|
|
/**
|
2017-07-03 02:29:58 +00:00
|
|
|
* Pterodactyl - Panel
|
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
|
|
|
*
|
2017-09-26 02:43:01 +00:00
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* https://opensource.org/licenses/MIT
|
2017-07-03 02:29:58 +00:00
|
|
|
*/
|
|
|
|
|
2017-07-15 16:52:34 +00:00
|
|
|
namespace Pterodactyl\Repositories\Eloquent;
|
2017-07-03 02:29:58 +00:00
|
|
|
|
2017-08-31 02:14:20 +00:00
|
|
|
use Webmozart\Assert\Assert;
|
2017-07-15 16:52:34 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2017-08-19 03:19:06 +00:00
|
|
|
use Pterodactyl\Repositories\Concerns\Searchable;
|
2017-07-22 02:17:42 +00:00
|
|
|
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
2017-07-15 16:52:34 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
|
|
|
|
2017-08-19 03:19:06 +00:00
|
|
|
class ServerRepository extends EloquentRepository implements ServerRepositoryInterface
|
2017-07-03 02:29:58 +00:00
|
|
|
{
|
2017-08-19 03:19:06 +00:00
|
|
|
use Searchable;
|
|
|
|
|
2017-07-08 19:07:51 +00:00
|
|
|
/**
|
2017-07-15 16:52:34 +00:00
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function model()
|
|
|
|
{
|
|
|
|
return Server::class;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
2017-07-08 19:07:51 +00:00
|
|
|
*/
|
2017-07-15 16:52:34 +00:00
|
|
|
public function getAllServers($paginate = 25)
|
|
|
|
{
|
2017-09-20 03:10:14 +00:00
|
|
|
Assert::nullOrIntegerish($paginate, 'First argument passed to getAllServers must be integer or null, received %s.');
|
2017-07-15 16:52:34 +00:00
|
|
|
|
2017-09-20 03:10:14 +00:00
|
|
|
$instance = $this->getBuilder()->with('node', 'user', 'allocation')->search($this->searchTerm);
|
|
|
|
|
|
|
|
return is_null($paginate) ? $instance->get($this->getColumns()) : $instance->paginate($paginate, $this->getColumns());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getDataForRebuild($server = null, $node = null)
|
|
|
|
{
|
|
|
|
Assert::nullOrIntegerish($server, 'First argument passed to getDataForRebuild must be null or integer, received %s.');
|
|
|
|
Assert::nullOrIntegerish($node, 'Second argument passed to getDataForRebuild must be null or integer, received %s.');
|
|
|
|
|
2017-10-07 04:57:53 +00:00
|
|
|
$instance = $this->getBuilder()->with('allocation', 'allocations', 'pack', 'egg', 'node');
|
2017-09-20 03:10:14 +00:00
|
|
|
|
|
|
|
if (! is_null($server) && is_null($node)) {
|
|
|
|
$instance = $instance->where('id', '=', $server);
|
|
|
|
} elseif (is_null($server) && ! is_null($node)) {
|
|
|
|
$instance = $instance->where('node_id', '=', $node);
|
2017-07-15 16:52:34 +00:00
|
|
|
}
|
|
|
|
|
2017-09-20 03:10:14 +00:00
|
|
|
return $instance->get($this->getColumns());
|
2017-07-15 16:52:34 +00:00
|
|
|
}
|
2017-07-20 01:49:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
* @return \Illuminate\Database\Eloquent\Model
|
|
|
|
*/
|
|
|
|
public function findWithVariables($id)
|
|
|
|
{
|
2017-09-20 03:10:14 +00:00
|
|
|
Assert::integerish($id, 'First argument passed to findWithVariables must be integer, received %s.');
|
|
|
|
|
2017-10-07 04:57:53 +00:00
|
|
|
$instance = $this->getBuilder()->with('egg.variables', 'variables')
|
2017-10-26 03:33:28 +00:00
|
|
|
->where($this->getModel()->getKeyName(), '=', $id)
|
|
|
|
->first($this->getColumns());
|
2017-07-20 01:49:41 +00:00
|
|
|
|
|
|
|
if (is_null($instance)) {
|
|
|
|
throw new RecordNotFoundException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
2017-10-26 03:33:28 +00:00
|
|
|
/**
|
|
|
|
* Get the primary allocation for a given server. If a model is passed into
|
|
|
|
* the function, load the allocation relationship onto it. Otherwise, find and
|
|
|
|
* return the server from the database.
|
|
|
|
*
|
|
|
|
* @param int|\Pterodactyl\Models\Server $server
|
|
|
|
* @param bool $refresh
|
|
|
|
* @return \Pterodactyl\Models\Server
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
*/
|
|
|
|
public function getPrimaryAllocation($server, bool $refresh = false): Server
|
|
|
|
{
|
|
|
|
$instance = $server;
|
|
|
|
if (! $instance instanceof Server) {
|
|
|
|
Assert::integerish($server, 'First argument passed to getPrimaryAllocation must be instance of \Pterodactyl\Models\Server or integer, received %s.');
|
|
|
|
$instance = $this->getBuilder()->find($server, $this->getColumns());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $instance) {
|
|
|
|
throw new RecordNotFoundException;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $instance->relationLoaded('allocation') || $refresh) {
|
|
|
|
$instance->load('allocation');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
2017-07-20 01:49:41 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2017-07-22 02:17:42 +00:00
|
|
|
public function getVariablesWithValues($id, $returnWithObject = false)
|
2017-07-20 01:49:41 +00:00
|
|
|
{
|
2017-10-07 04:57:53 +00:00
|
|
|
$instance = $this->getBuilder()->with('variables', 'egg.variables')
|
2017-07-22 02:17:42 +00:00
|
|
|
->find($id, $this->getColumns());
|
2017-07-20 01:49:41 +00:00
|
|
|
|
|
|
|
if (! $instance) {
|
|
|
|
throw new RecordNotFoundException();
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = [];
|
2017-10-07 04:57:53 +00:00
|
|
|
$instance->egg->variables->each(function ($item) use (&$data, $instance) {
|
2017-07-20 01:49:41 +00:00
|
|
|
$display = $instance->variables->where('variable_id', $item->id)->pluck('variable_value')->first();
|
|
|
|
|
|
|
|
$data[$item->env_variable] = $display ?? $item->default_value;
|
|
|
|
});
|
|
|
|
|
2017-07-22 02:17:42 +00:00
|
|
|
if ($returnWithObject) {
|
|
|
|
return (object) [
|
|
|
|
'data' => $data,
|
|
|
|
'server' => $instance,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-07-20 01:49:41 +00:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2017-07-22 02:17:42 +00:00
|
|
|
/**
|
2017-10-27 04:49:54 +00:00
|
|
|
* Return enough data to be used for the creation of a server via the daemon.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Models\Server $server
|
|
|
|
* @param bool $refresh
|
|
|
|
* @return \Pterodactyl\Models\Server
|
2017-07-22 02:17:42 +00:00
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
public function getDataForCreation(Server $server, bool $refresh = false): Server
|
2017-07-20 01:49:41 +00:00
|
|
|
{
|
2017-10-27 04:49:54 +00:00
|
|
|
foreach (['allocation', 'allocations', 'pack', 'egg'] as $relation) {
|
|
|
|
if (! $server->relationLoaded($relation) || $refresh) {
|
|
|
|
$server->load($relation);
|
|
|
|
}
|
2017-07-22 02:17:42 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 04:49:54 +00:00
|
|
|
return $server;
|
2017-07-22 02:17:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getWithDatabases($id)
|
|
|
|
{
|
|
|
|
$instance = $this->getBuilder()->with('databases.host')
|
|
|
|
->where('installed', 1)
|
|
|
|
->find($id, $this->getColumns());
|
2017-07-20 01:49:41 +00:00
|
|
|
|
|
|
|
if (! $instance) {
|
|
|
|
throw new RecordNotFoundException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $instance;
|
|
|
|
}
|
2017-07-25 02:34:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getDaemonServiceData($id)
|
|
|
|
{
|
2017-10-07 22:21:41 +00:00
|
|
|
Assert::integerish($id, 'First argument passed to getDaemonServiceData must be integer, received %s.');
|
2017-07-25 02:34:10 +00:00
|
|
|
|
2017-10-07 22:21:41 +00:00
|
|
|
$instance = $this->getBuilder()->with('egg.nest', 'pack')->find($id, $this->getColumns());
|
2017-07-25 02:34:10 +00:00
|
|
|
if (! $instance) {
|
|
|
|
throw new RecordNotFoundException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
2017-10-07 04:57:53 +00:00
|
|
|
'type' => $instance->egg->nest->folder,
|
|
|
|
'option' => $instance->egg->tag,
|
2017-07-25 02:34:10 +00:00
|
|
|
'pack' => (! is_null($instance->pack_id)) ? $instance->pack->uuid : null,
|
|
|
|
];
|
|
|
|
}
|
2017-08-31 02:11:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getUserAccessServers($user)
|
|
|
|
{
|
|
|
|
Assert::numeric($user, 'First argument passed to getUserAccessServers must be numeric, received %s.');
|
|
|
|
|
|
|
|
$subuser = $this->app->make(SubuserRepository::class);
|
|
|
|
|
|
|
|
return $this->getBuilder()->select('id')->where('owner_id', $user)->union(
|
|
|
|
$subuser->getBuilder()->select('server_id')->where('user_id', $user)
|
|
|
|
)->pluck('id')->all();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function filterUserAccessServers($user, $admin = false, $level = 'all', array $relations = [])
|
|
|
|
{
|
|
|
|
Assert::numeric($user, 'First argument passed to filterUserAccessServers must be numeric, received %s.');
|
|
|
|
Assert::boolean($admin, 'Second argument passed to filterUserAccessServers must be boolean, received %s.');
|
|
|
|
Assert::stringNotEmpty($level, 'Third argument passed to filterUserAccessServers must be a non-empty string, received %s.');
|
|
|
|
|
|
|
|
$instance = $this->getBuilder()->with($relations);
|
|
|
|
|
|
|
|
// If access level is set to owner, only display servers
|
|
|
|
// that the user owns.
|
|
|
|
if ($level === 'owner') {
|
|
|
|
$instance->where('owner_id', $user);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If set to all, display all servers they can access, including
|
|
|
|
// those they access as an admin.
|
|
|
|
//
|
|
|
|
// If set to subuser, only return the servers they can access because
|
|
|
|
// they are owner, or marked as a subuser of the server.
|
|
|
|
if (($level === 'all' && ! $admin) || $level === 'subuser') {
|
|
|
|
$instance->whereIn('id', $this->getUserAccessServers($user));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If set to admin, only display the servers a user can access
|
|
|
|
// as an administrator (leaves out owned and subuser of).
|
|
|
|
if ($level === 'admin' && $admin) {
|
|
|
|
$instance->whereIn('id', $this->getUserAccessServers($user));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $instance->search($this->searchTerm)->paginate(
|
|
|
|
$this->app->make('config')->get('pterodactyl.paginate.frontend.servers')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getByUuid($uuid)
|
|
|
|
{
|
|
|
|
Assert::stringNotEmpty($uuid, 'First argument passed to getByUuid must be a non-empty string, received %s.');
|
|
|
|
|
2017-10-07 04:57:53 +00:00
|
|
|
$instance = $this->getBuilder()->with('nest', 'node')->where(function ($query) use ($uuid) {
|
2017-08-31 02:11:14 +00:00
|
|
|
$query->where('uuidShort', $uuid)->orWhere('uuid', $uuid);
|
|
|
|
})->first($this->getColumns());
|
|
|
|
|
|
|
|
if (! $instance) {
|
|
|
|
throw new RecordNotFoundException;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $instance;
|
|
|
|
}
|
2017-07-03 02:29:58 +00:00
|
|
|
}
|