From d4bcf0be594e09c187763e9ca56463d7f287037a Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Thu, 2 Feb 2017 18:21:36 -0500 Subject: [PATCH 01/34] Initial implementation of improved sever model and logic --- CHANGELOG.md | 3 + app/Http/Controllers/API/ServerController.php | 2 +- .../Controllers/API/User/InfoController.php | 2 +- .../Controllers/API/User/ServerController.php | 6 +- .../Controllers/Admin/NodesController.php | 4 +- .../Controllers/Admin/ServersController.php | 32 ++++---- .../Controllers/Admin/ServiceController.php | 2 +- app/Http/Controllers/Admin/UserController.php | 2 +- .../Controllers/Remote/RemoteController.php | 4 +- .../Controllers/Server/AjaxController.php | 4 +- .../Controllers/Server/ServerController.php | 35 ++++---- .../Controllers/Server/SubuserController.php | 6 +- .../Controllers/Server/TaskController.php | 4 +- app/Http/Middleware/CheckServer.php | 9 ++- app/Models/Server.php | 81 +++++++------------ app/Models/User.php | 21 +++++ app/Observers/ServerObserver.php | 8 +- app/Policies/ServerPolicy.php | 2 +- app/Repositories/ServerRepository.php | 44 +++++----- app/Repositories/SubuserRepository.php | 14 ++-- app/Services/NotificationService.php | 2 +- ..._08_30_213301_modify_ip_storage_method.php | 4 +- ...6_09_17_194246_add_docker_image_column.php | 2 +- .../2017_02_02_175548_UpdateColumnNames.php | 66 +++++++++++++++ .../themes/pterodactyl/base/index.blade.php | 2 +- .../server/settings/allocation.blade.php | 2 +- resources/views/admin/nodes/view.blade.php | 2 +- resources/views/admin/servers/index.blade.php | 4 +- resources/views/admin/servers/view.blade.php | 8 +- .../admin/services/options/view.blade.php | 2 +- resources/views/admin/users/view.blade.php | 2 +- 31 files changed, 223 insertions(+), 158 deletions(-) create mode 100644 database/migrations/2017_02_02_175548_UpdateColumnNames.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 8922f5620..80b459be5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,9 @@ This project follows [Semantic Versioning](http://semver.org) guidelines. * User model now defines mass assignment fields using `$fillable` rather than `$guarded`. * 2FA checkpoint on login is now its own page, and not an AJAX based call. Improves security on that front. +### Removed +* `Server::getUserDaemonSecret(Server $server)` was removed and replaced with `User::daemonSecret(Server $server)` in order to clean up models. + ## v0.5.6 (Bodacious Boreopterus) ### Added * Added the following languages: Estonian `et`, Dutch `nl`, Norwegian `nb` (partial), Romanian `ro`, and Russian `ru`. Interested in helping us translate the panel into more languages, or improving existing translations? Contact us on Discord and let us know. diff --git a/app/Http/Controllers/API/ServerController.php b/app/Http/Controllers/API/ServerController.php index bc0d04a40..24a2bc00e 100755 --- a/app/Http/Controllers/API/ServerController.php +++ b/app/Http/Controllers/API/ServerController.php @@ -122,7 +122,7 @@ class ServerController extends BaseController 'pack' )->first(); if ($request->input('daemon') === 'true') { - $node = Models\Node::findOrFail($server->node); + $node = Models\Node::findOrFail($server->node_id); $client = Models\Node::guzzleRequest($node->id); $response = $client->request('GET', '/servers', [ diff --git a/app/Http/Controllers/API/User/InfoController.php b/app/Http/Controllers/API/User/InfoController.php index 00923b866..228a66819 100644 --- a/app/Http/Controllers/API/User/InfoController.php +++ b/app/Http/Controllers/API/User/InfoController.php @@ -37,7 +37,7 @@ class InfoController extends BaseController 'id' => $server->uuidShort, 'uuid' => $server->uuid, 'name' => $server->name, - 'node' => $server->nodeName, + 'node' => $server->node_idName, 'ip' => [ 'set' => $server->ip, 'alias' => $server->ip_alias, diff --git a/app/Http/Controllers/API/User/ServerController.php b/app/Http/Controllers/API/User/ServerController.php index c63a482fa..128c5cf68 100644 --- a/app/Http/Controllers/API/User/ServerController.php +++ b/app/Http/Controllers/API/User/ServerController.php @@ -35,7 +35,7 @@ class ServerController extends BaseController public function info(Request $request, $uuid) { $server = Models\Server::getByUUID($uuid); - $node = Models\Node::findOrFail($server->node); + $node = Models\Node::findOrFail($server->node_id); $client = Models\Node::guzzleRequest($node->id); try { @@ -61,7 +61,7 @@ class ServerController extends BaseController $allocations = Models\Allocation::select('id', 'ip', 'port', 'ip_alias as alias')->where('assigned_to', $server->id)->get(); foreach ($allocations as &$allocation) { - $allocation->default = ($allocation->id === $server->allocation); + $allocation->default = ($allocation->id === $server->allocation_id); unset($allocation->id); } @@ -92,7 +92,7 @@ class ServerController extends BaseController public function power(Request $request, $uuid) { $server = Models\Server::getByUUID($uuid); - $client = Models\Node::guzzleRequest($server->node); + $client = Models\Node::guzzleRequest($server->node_id); Auth::user()->can('power-' . $request->input('action'), $server); diff --git a/app/Http/Controllers/Admin/NodesController.php b/app/Http/Controllers/Admin/NodesController.php index 594365f73..506d8434e 100644 --- a/app/Http/Controllers/Admin/NodesController.php +++ b/app/Http/Controllers/Admin/NodesController.php @@ -108,8 +108,8 @@ class NodesController extends Controller return view('admin.nodes.view', [ 'node' => $node, 'servers' => Models\Server::select('servers.*', 'users.email as a_ownerEmail', 'services.name as a_serviceName') - ->join('users', 'users.id', '=', 'servers.owner') - ->join('services', 'services.id', '=', 'servers.service') + ->join('users', 'users.id', '=', 'servers.owner_id') + ->join('services', 'services.id', '=', 'servers.service_id') ->where('node', $id)->paginate(10, ['*'], 'servers'), 'stats' => Models\Server::select(DB::raw('SUM(memory) as memory, SUM(disk) as disk'))->where('node', $node->id)->first(), 'locations' => Models\Location::all(), diff --git a/app/Http/Controllers/Admin/ServersController.php b/app/Http/Controllers/Admin/ServersController.php index 9aece9c4e..f75508139 100644 --- a/app/Http/Controllers/Admin/ServersController.php +++ b/app/Http/Controllers/Admin/ServersController.php @@ -54,9 +54,9 @@ class ServersController extends Controller 'allocations.ip', 'allocations.port', 'allocations.ip_alias' - )->join('nodes', 'servers.node', '=', 'nodes.id') - ->join('users', 'servers.owner', '=', 'users.id') - ->join('allocations', 'servers.allocation', '=', 'allocations.id'); + )->join('nodes', 'servers.node_id', '=', 'nodes.id') + ->join('users', 'servers.owner_id', '=', 'users.id') + ->join('allocations', 'servers.allocation_id', '=', 'allocations.id'); if ($request->input('filter') && ! is_null($request->input('filter'))) { preg_match_all('/[^\s"\']+|"([^"]*)"|\'([^\']*)\'/', urldecode($request->input('filter')), $matches); @@ -96,9 +96,9 @@ class ServersController extends Controller 'allocations.ip', 'allocations.port', 'allocations.ip_alias' - )->join('nodes', 'servers.node', '=', 'nodes.id') - ->join('users', 'servers.owner', '=', 'users.id') - ->join('allocations', 'servers.allocation', '=', 'allocations.id') + )->join('nodes', 'servers.node_id', '=', 'nodes.id') + ->join('users', 'servers.owner_id', '=', 'users.id') + ->join('allocations', 'servers.allocation_id', '=', 'allocations.id') ->paginate(20); } @@ -127,11 +127,11 @@ class ServersController extends Controller 'allocations.ip', 'allocations.port', 'allocations.ip_alias' - )->join('nodes', 'servers.node', '=', 'nodes.id') - ->join('users', 'servers.owner', '=', 'users.id') - ->join('services', 'servers.service', '=', 'services.id') - ->join('service_options', 'servers.option', '=', 'service_options.id') - ->join('allocations', 'servers.allocation', '=', 'allocations.id') + )->join('nodes', 'servers.node_id', '=', 'nodes.id') + ->join('users', 'servers.owner_id', '=', 'users.id') + ->join('services', 'servers.service_id', '=', 'services.id') + ->join('service_options', 'servers.option_id', '=', 'service_options.id') + ->join('allocations', 'servers.allocation_id', '=', 'allocations.id') ->where('servers.id', $id) ->first(); @@ -145,13 +145,13 @@ class ServersController extends Controller 'nodes.*', 'locations.long as a_locationName' )->join('locations', 'nodes.location', '=', 'locations.id') - ->where('nodes.id', $server->node) + ->where('nodes.id', $server->node_id) ->first(), 'assigned' => Models\Allocation::where('assigned_to', $id)->orderBy('ip', 'asc')->orderBy('port', 'asc')->get(), - 'unassigned' => Models\Allocation::where('node', $server->node)->whereNull('assigned_to')->orderBy('ip', 'asc')->orderBy('port', 'asc')->get(), + 'unassigned' => Models\Allocation::where('node', $server->node_id)->whereNull('assigned_to')->orderBy('ip', 'asc')->orderBy('port', 'asc')->get(), 'startup' => Models\ServiceVariables::select('service_variables.*', 'server_variables.variable_value as a_serverValue') ->join('server_variables', 'server_variables.variable_id', '=', 'service_variables.id') - ->where('service_variables.option_id', $server->option) + ->where('service_variables.option_id', $server->option_id) ->where('server_variables.server_id', $server->id) ->get(), 'databases' => Models\Database::select('databases.*', 'database_servers.host as a_host', 'database_servers.port as a_port') @@ -334,8 +334,8 @@ class ServersController extends Controller public function postUpdateServerToggleBuild(Request $request, $id) { $server = Models\Server::findOrFail($id); - $node = Models\Node::findOrFail($server->node); - $client = Models\Node::guzzleRequest($server->node); + $node = Models\Node::findOrFail($server->node_id); + $client = Models\Node::guzzleRequest($server->node_id); try { $res = $client->request('POST', '/server/rebuild', [ diff --git a/app/Http/Controllers/Admin/ServiceController.php b/app/Http/Controllers/Admin/ServiceController.php index 7a70c58da..70de4f88e 100644 --- a/app/Http/Controllers/Admin/ServiceController.php +++ b/app/Http/Controllers/Admin/ServiceController.php @@ -137,7 +137,7 @@ class ServiceController extends Controller 'option' => $opt, 'variables' => Models\ServiceVariables::where('option_id', $option)->get(), 'servers' => Models\Server::select('servers.*', 'users.email as a_ownerEmail') - ->join('users', 'users.id', '=', 'servers.owner') + ->join('users', 'users.id', '=', 'servers.owner_id') ->where('option', $option) ->paginate(10), ]); diff --git a/app/Http/Controllers/Admin/UserController.php b/app/Http/Controllers/Admin/UserController.php index 9a2c6fd04..c2dea5f54 100644 --- a/app/Http/Controllers/Admin/UserController.php +++ b/app/Http/Controllers/Admin/UserController.php @@ -87,7 +87,7 @@ class UserController extends Controller return view('admin.users.view', [ 'user' => User::findOrFail($id), 'servers' => Server::select('servers.*', 'nodes.name as nodeName', 'locations.long as location') - ->join('nodes', 'servers.node', '=', 'nodes.id') + ->join('nodes', 'servers.node_id', '=', 'nodes.id') ->join('locations', 'nodes.location', '=', 'locations.id') ->where('owner', $id) ->get(), diff --git a/app/Http/Controllers/Remote/RemoteController.php b/app/Http/Controllers/Remote/RemoteController.php index b0aa0983e..0f168c04b 100644 --- a/app/Http/Controllers/Remote/RemoteController.php +++ b/app/Http/Controllers/Remote/RemoteController.php @@ -66,7 +66,7 @@ class RemoteController extends Controller ], 422); } - $node = Models\Node::findOrFail($server->node); + $node = Models\Node::findOrFail($server->node_id); $hmac = $request->input('signed'); $status = $request->input('installed'); @@ -93,7 +93,7 @@ class RemoteController extends Controller ], 422); } - $node = Models\Node::findOrFail($server->node); + $node = Models\Node::findOrFail($server->node_id); $hmac = $request->input('signed'); if (base64_decode($hmac) !== hash_hmac('sha256', $server->uuid, $node->daemonSecret, true)) { diff --git a/app/Http/Controllers/Server/AjaxController.php b/app/Http/Controllers/Server/AjaxController.php index da4d058ac..9ee9f4284 100644 --- a/app/Http/Controllers/Server/AjaxController.php +++ b/app/Http/Controllers/Server/AjaxController.php @@ -73,7 +73,7 @@ class AjaxController extends Controller return response()->json([], 404); } - $client = Models\Node::guzzleRequest($server->node); + $client = Models\Node::guzzleRequest($server->node_id); try { $res = $client->request('GET', '/server', [ @@ -178,7 +178,7 @@ class AjaxController extends Controller $server = Models\Server::getByUUID($uuid); $this->authorize('set-connection', $server); - if ((int) $request->input('allocation') === $server->allocation) { + if ((int) $request->input('allocation') === $server->allocation_id) { return response()->json([ 'error' => 'You are already using this as your default connection.', ], 409); diff --git a/app/Http/Controllers/Server/ServerController.php b/app/Http/Controllers/Server/ServerController.php index 593049ca1..7f07f977c 100644 --- a/app/Http/Controllers/Server/ServerController.php +++ b/app/Http/Controllers/Server/ServerController.php @@ -55,14 +55,13 @@ class ServerController extends Controller * @param \Illuminate\Http\Request $request * @return \Illuminate\Contracts\View\View */ - public function getIndex(Request $request) + public function getIndex(Request $request, $uuid) { - $server = Models\Server::getByUUID($request->route()->server); - $node = Models\Node::find($server->node); + $server = Models\Server::byUuid($uuid); Javascript::put([ 'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'daemonSecret', 'username']), - 'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'), + 'node' => collect($server->node_id)->only('fqdn', 'scheme', 'daemonListen'), 'meta' => [ 'saveFile' => route('server.files.save', $server->uuidShort), 'csrfToken' => csrf_token(), @@ -71,7 +70,7 @@ class ServerController extends Controller return view('server.index', [ 'server' => $server, - 'node' => $node, + 'node' => $server->node_id, ]); } @@ -86,7 +85,7 @@ class ServerController extends Controller $server = Models\Server::getByUUID($uuid); $this->authorize('list-files', $server); - $node = Models\Node::find($server->node); + $node = Models\Node::find($server->node_id); Javascript::put([ 'server' => collect($server->makeVisible('daemonSecret'))->only('uuid', 'uuidShort', 'daemonSecret'), @@ -122,7 +121,7 @@ class ServerController extends Controller { $server = Models\Server::getByUUID($uuid); $this->authorize('add-files', $server); - $node = Models\Node::find($server->node); + $node = Models\Node::find($server->node_id); Javascript::put([ 'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']), @@ -148,7 +147,7 @@ class ServerController extends Controller { $server = Models\Server::getByUUID($uuid); $this->authorize('edit-files', $server); - $node = Models\Node::find($server->node); + $node = Models\Node::find($server->node_id); $fileInfo = (object) pathinfo($file); $controller = new FileRepository($uuid); @@ -193,7 +192,7 @@ class ServerController extends Controller public function getDownloadFile(Request $request, $uuid, $file) { $server = Models\Server::getByUUID($uuid); - $node = Models\Node::find($server->node); + $node = Models\Node::find($server->node_id); $this->authorize('download-files', $server); @@ -212,7 +211,7 @@ class ServerController extends Controller { $server = Models\Server::getByUUID($uuid); $this->authorize('view-allocation', $server); - $node = Models\Node::find($server->node); + $node = Models\Node::find($server->node_id); Javascript::put([ 'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']), @@ -230,8 +229,8 @@ class ServerController extends Controller { $server = Models\Server::getByUUID($uuid); $this->authorize('view-startup', $server); - $node = Models\Node::find($server->node); - $allocation = Models\Allocation::findOrFail($server->allocation); + $node = Models\Node::find($server->node_id); + $allocation = Models\Allocation::findOrFail($server->allocation_id); Javascript::put([ 'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']), @@ -242,15 +241,15 @@ class ServerController extends Controller 'service_variables.*', DB::raw('COALESCE(server_variables.variable_value, service_variables.default_value) as a_serverValue') )->leftJoin('server_variables', 'server_variables.variable_id', '=', 'service_variables.id') - ->where('service_variables.option_id', $server->option) + ->where('service_variables.option_id', $server->option_id) ->where('server_variables.server_id', $server->id) ->get(); $service = Models\Service::select( DB::raw('IFNULL(service_options.executable, services.executable) as executable') )->leftJoin('service_options', 'service_options.parent_service', '=', 'services.id') - ->where('service_options.id', $server->option) - ->where('services.id', $server->service) + ->where('service_options.id', $server->option_id) + ->where('services.id', $server->service_id) ->first(); $serverVariables = [ @@ -267,7 +266,7 @@ class ServerController extends Controller return view('server.settings.startup', [ 'server' => $server, - 'node' => Models\Node::find($server->node), + 'node' => Models\Node::find($server->node_id), 'variables' => $variables->where('user_viewable', 1), 'service' => $service, 'processedStartup' => $processed, @@ -278,7 +277,7 @@ class ServerController extends Controller { $server = Models\Server::getByUUID($uuid); $this->authorize('view-databases', $server); - $node = Models\Node::find($server->node); + $node = Models\Node::find($server->node_id); Javascript::put([ 'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']), @@ -299,7 +298,7 @@ class ServerController extends Controller { $server = Models\Server::getByUUID($uuid); $this->authorize('view-sftp', $server); - $node = Models\Node::find($server->node); + $node = Models\Node::find($server->node_id); Javascript::put([ 'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'daemonSecret', 'username']), diff --git a/app/Http/Controllers/Server/SubuserController.php b/app/Http/Controllers/Server/SubuserController.php index a8761c717..1d07b47f7 100644 --- a/app/Http/Controllers/Server/SubuserController.php +++ b/app/Http/Controllers/Server/SubuserController.php @@ -52,7 +52,7 @@ class SubuserController extends Controller { $server = Models\Server::getByUUID($uuid); $this->authorize('list-subusers', $server); - $node = Models\Node::find($server->node); + $node = Models\Node::find($server->node_id); Javascript::put([ 'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']), @@ -73,7 +73,7 @@ class SubuserController extends Controller { $server = Models\Server::getByUUID($uuid); $this->authorize('view-subuser', $server); - $node = Models\Node::find($server->node); + $node = Models\Node::find($server->node_id); Javascript::put([ 'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']), @@ -150,7 +150,7 @@ class SubuserController extends Controller { $server = Models\Server::getByUUID($uuid); $this->authorize('create-subuser', $server); - $node = Models\Node::find($server->node); + $node = Models\Node::find($server->node_id); Javascript::put([ 'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']), diff --git a/app/Http/Controllers/Server/TaskController.php b/app/Http/Controllers/Server/TaskController.php index 8c49ad6bc..51243cc8f 100644 --- a/app/Http/Controllers/Server/TaskController.php +++ b/app/Http/Controllers/Server/TaskController.php @@ -45,7 +45,7 @@ class TaskController extends Controller { $server = Models\Server::getByUUID($uuid); $this->authorize('list-tasks', $server); - $node = Models\Node::find($server->node); + $node = Models\Node::find($server->node_id); Javascript::put([ 'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']), @@ -67,7 +67,7 @@ class TaskController extends Controller { $server = Models\Server::getByUUID($uuid); $this->authorize('create-task', $server); - $node = Models\Node::find($server->node); + $node = Models\Node::find($server->node_id); Javascript::put([ 'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']), diff --git a/app/Http/Middleware/CheckServer.php b/app/Http/Middleware/CheckServer.php index cd83bd9d1..dba9395ac 100644 --- a/app/Http/Middleware/CheckServer.php +++ b/app/Http/Middleware/CheckServer.php @@ -26,6 +26,7 @@ namespace Pterodactyl\Http\Middleware; use Auth; use Closure; +use Illuminate\Http\Request; use Pterodactyl\Models\Server; class CheckServer @@ -37,22 +38,22 @@ class CheckServer * @param \Closure $next * @return mixed */ - public function handle($request, Closure $next) + public function handle(Request $request, Closure $next) { if (! Auth::user()) { return redirect()->guest('auth/login'); } - $server = Server::getByUUID($request->route()->server); + $server = Server::byUuid($request->route()->server); if (! $server) { return response()->view('errors.404', [], 404); } - if ($server->suspended === 1) { + if ($server->suspended) { return response()->view('errors.suspended', [], 403); } - if ($server->installed !== 1) { + if (! $server->installed) { return response()->view('errors.installing', [], 403); } diff --git a/app/Models/Server.php b/app/Models/Server.php index a9f9bd087..32240da44 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -101,28 +101,6 @@ class Server extends Model self::$user = Auth::user(); } - /** - * Determine if we need to change the server's daemonSecret value to - * match that of the user if they are a subuser. - * - * @param Illuminate\Database\Eloquent\Model\Server $server - * @return string - */ - public static function getUserDaemonSecret(Server $server) - { - if (self::$user->id === $server->owner || self::$user->root_admin === 1) { - return $server->daemonSecret; - } - - $subuser = Subuser::where('server_id', $server->id)->where('user_id', self::$user->id)->first(); - - if (is_null($subuser)) { - return null; - } - - return $subuser->daemonSecret; - } - /** * Returns array of all servers owned by the logged in user. * Returns all users servers if user is a root admin. @@ -140,11 +118,11 @@ class Server extends Model 'allocations.port', 'services.name as a_serviceName', 'service_options.name as a_serviceOptionName' - )->join('nodes', 'servers.node', '=', 'nodes.id') + )->join('nodes', 'servers.node_id', '=', 'nodes.id') ->join('locations', 'nodes.location', '=', 'locations.id') - ->join('services', 'servers.service', '=', 'services.id') - ->join('service_options', 'servers.option', '=', 'service_options.id') - ->join('allocations', 'servers.allocation', '=', 'allocations.id'); + ->join('services', 'servers.service_id', '=', 'services.id') + ->join('service_options', 'servers.option_id', '=', 'service_options.id') + ->join('allocations', 'servers.allocation_id', '=', 'allocations.id'); if (self::$user->root_admin !== 1) { $query->whereIn('servers.id', Subuser::accessServers()); @@ -165,30 +143,21 @@ class Server extends Model * @param string $uuid The Short-UUID of the server to return an object about. * @return \Illuminate\Database\Eloquent\Collection */ - public static function getByUUID($uuid) + public static function byUuid($uuid) { - if (array_key_exists($uuid, self::$serverUUIDInstance)) { - return self::$serverUUIDInstance[$uuid]; - } + $query = self::with('service', 'node')->where('uuidShort', $uuid)->orWhere('uuid', $uuid); - $query = self::select('servers.*', 'services.file as a_serviceFile') - ->join('services', 'services.id', '=', 'servers.service') - ->where('uuidShort', $uuid) - ->orWhere('uuid', $uuid); - - if (self::$user->root_admin !== 1) { - $query->whereIn('servers.id', Subuser::accessServers()); + if (! Auth::user()->isRootAdmin()) { + $query->whereIn('id', Subuser::accessServers()); } $result = $query->first(); if (! is_null($result)) { - $result->daemonSecret = self::getUserDaemonSecret($result); + $result->daemonSecret = Auth::user()->daemonToken($result); } - self::$serverUUIDInstance[$uuid] = $result; - - return self::$serverUUIDInstance[$uuid]; + return $result; } /** @@ -197,16 +166,12 @@ class Server extends Model * @param string $uuid * @return array */ - public static function getGuzzleHeaders($uuid) + public function getHeaders() { - if (array_key_exists($uuid, self::$serverUUIDInstance)) { - return [ - 'X-Access-Server' => self::$serverUUIDInstance[$uuid]->uuid, - 'X-Access-Token' => self::$serverUUIDInstance[$uuid]->daemonSecret, - ]; - } - - return []; + return [ + 'X-Access-Server' => $this->uuid, + 'X-Access-Token' => Auth::user()->daemonToken($this), + ]; } /** @@ -226,7 +191,7 @@ class Server extends Model */ public function pack() { - return $this->hasOne(ServicePack::class, 'id', 'pack'); + return $this->hasOne(ServicePack::class, 'id', 'pack_id'); } /** @@ -236,7 +201,7 @@ class Server extends Model */ public function service() { - return $this->hasOne(Service::class, 'id', 'service'); + return $this->hasOne(Service::class, 'id', 'service_id'); } /** @@ -246,7 +211,7 @@ class Server extends Model */ public function option() { - return $this->hasOne(ServiceOptions::class, 'id', 'option'); + return $this->hasOne(ServiceOptions::class, 'id', 'option_id'); } /** @@ -258,4 +223,14 @@ class Server extends Model { return $this->hasMany(ServerVariables::class); } + + /** + * Gets information for the node associated with this server. + * + * @return \Illuminate\Database\Eloquent\Relations\HasOne + */ + public function node() + { + return $this->hasOne(Node::class, 'id', 'node_id'); + } } diff --git a/app/Models/User.php b/app/Models/User.php index 80d9192fa..0de3eb5ae 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -24,6 +24,7 @@ namespace Pterodactyl\Models; +use Auth; use Hash; use Google2FA; use Illuminate\Auth\Authenticatable; @@ -156,4 +157,24 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac { return $this->root_admin === 1; } + + /** + * Returns the user's daemon secret for a given server. + * @param Server $server \Pterodactyl\Models\Server + * @return null|string + */ + public function daemonToken(Server $server) + { + if ($this->id === $server->owner_id || $this->isRootAdmin()) { + return $server->daemonSecret; + } + + $subuser = Subuser::where('server_id', $server->id)->where('user_id', $this->id)->first(); + + if (is_null($subuser)) { + return null; + } + + return $subuser->daemonSecret; + } } diff --git a/app/Observers/ServerObserver.php b/app/Observers/ServerObserver.php index 6506f03f2..cacb9c82f 100644 --- a/app/Observers/ServerObserver.php +++ b/app/Observers/ServerObserver.php @@ -59,12 +59,12 @@ class ServerObserver event(new Events\Server\Created($server)); // Queue Notification Email - $user = Models\User::findOrFail($server->owner); - $node = Models\Node::select('name')->where('id', $server->node)->first(); + $user = Models\User::findOrFail($server->owner_id); + $node = Models\Node::select('name')->where('id', $server->node_id)->first(); $service = Models\Service::select('services.name', 'service_options.name as optionName') ->join('service_options', 'service_options.parent_service', '=', 'services.id') - ->where('services.id', $server->service) - ->where('service_options.id', $server->option) + ->where('services.id', $server->service_id) + ->where('service_options.id', $server->option_id) ->first(); $user->notify((new ServerCreated([ diff --git a/app/Policies/ServerPolicy.php b/app/Policies/ServerPolicy.php index f297fe829..3ef54f436 100644 --- a/app/Policies/ServerPolicy.php +++ b/app/Policies/ServerPolicy.php @@ -48,7 +48,7 @@ class ServerPolicy */ protected function isOwner(User $user, Server $server) { - return $server->owner === $user->id; + return $server->owner_id === $user->id; } /** diff --git a/app/Repositories/ServerRepository.php b/app/Repositories/ServerRepository.php index beb5d94a6..3cd3d32ba 100644 --- a/app/Repositories/ServerRepository.php +++ b/app/Repositories/ServerRepository.php @@ -374,7 +374,7 @@ class ServerRepository try { $server = Models\Server::findOrFail($id); - $owner = Models\User::findOrFail($server->owner); + $owner = Models\User::findOrFail($server->owner_id); // Update daemon secret if it was passed. if ((isset($data['reset_token']) && $data['reset_token'] === true) || (isset($data['owner']) && $data['owner'] !== $owner->email)) { @@ -386,7 +386,7 @@ class ServerRepository // Update Server Owner if it was passed. if (isset($data['owner']) && $data['owner'] !== $owner->email) { $newOwner = Models\User::select('id')->where('email', $data['owner'])->first(); - $server->owner = $newOwner->id; + $server->owner_id = $newOwner->id; } // Update Server Name if it was passed. @@ -405,8 +405,8 @@ class ServerRepository } // If we need to update do it here. - $node = Models\Node::getByID($server->node); - $client = Models\Node::guzzleRequest($server->node); + $node = Models\Node::getByID($server->node_id); + $client = Models\Node::guzzleRequest($server->node_id); $res = $client->request('PATCH', '/server', [ 'headers' => [ @@ -461,8 +461,8 @@ class ServerRepository $server->image = $data['image']; $server->save(); - $node = Models\Node::getByID($server->node); - $client = Models\Node::guzzleRequest($server->node); + $node = Models\Node::getByID($server->node_id); + $client = Models\Node::guzzleRequest($server->node_id); $client->request('PATCH', '/server', [ 'headers' => [ @@ -520,7 +520,7 @@ class ServerRepository try { $server = Models\Server::findOrFail($id); - $allocation = Models\Allocation::findOrFail($server->allocation); + $allocation = Models\Allocation::findOrFail($server->allocation_id); $newBuild = []; @@ -532,14 +532,14 @@ class ServerRepository throw new DisplayException('The requested default connection (' . $ip . ':' . $port . ') is not allocated to this server.'); } - $server->allocation = $selection->id; + $server->allocation_id = $selection->id; $newBuild['default'] = [ 'ip' => $ip, 'port' => (int) $port, ]; // Re-Run to keep updated for rest of function - $allocation = Models\Allocation::findOrFail($server->allocation); + $allocation = Models\Allocation::findOrFail($server->allocation_id); } } @@ -635,8 +635,8 @@ class ServerRepository $server->save(); if (! empty($newBuild)) { - $node = Models\Node::getByID($server->node); - $client = Models\Node::guzzleRequest($server->node); + $node = Models\Node::getByID($server->node_id); + $client = Models\Node::guzzleRequest($server->node_id); $client->request('PATCH', '/server', [ 'headers' => [ @@ -679,7 +679,7 @@ class ServerRepository 'service_variables.*', DB::raw('COALESCE(server_variables.variable_value, service_variables.default_value) as a_currentValue') )->leftJoin('server_variables', 'server_variables.variable_id', '=', 'service_variables.id') - ->where('option_id', $server->option) + ->where('option_id', $server->option_id) ->get(); $variableList = []; @@ -747,8 +747,8 @@ class ServerRepository $model->save(); } - $node = Models\Node::getByID($server->node); - $client = Models\Node::guzzleRequest($server->node); + $node = Models\Node::getByID($server->node_id); + $client = Models\Node::guzzleRequest($server->node_id); $client->request('PATCH', '/server', [ 'headers' => [ @@ -797,7 +797,7 @@ class ServerRepository public function deleteNow($id, $force = false) { $server = Models\Server::withTrashed()->findOrFail($id); - $node = Models\Node::findOrFail($server->node); + $node = Models\Node::findOrFail($server->node_id); // Handle server being restored previously or // an accidental queue. @@ -835,7 +835,7 @@ class ServerRepository $repository->drop($database->id); } - $client = Models\Node::guzzleRequest($server->node); + $client = Models\Node::guzzleRequest($server->node_id); $client->request('DELETE', '/servers', [ 'headers' => [ 'X-Access-Token' => $node->daemonSecret, @@ -888,7 +888,7 @@ class ServerRepository public function suspend($id, $deleted = false) { $server = ($deleted) ? Models\Server::withTrashed()->findOrFail($id) : Models\Server::findOrFail($id); - $node = Models\Node::findOrFail($server->node); + $node = Models\Node::findOrFail($server->node_id); DB::beginTransaction(); @@ -902,7 +902,7 @@ class ServerRepository $server->suspended = 1; $server->save(); - $client = Models\Node::guzzleRequest($server->node); + $client = Models\Node::guzzleRequest($server->node_id); $client->request('POST', '/server/suspend', [ 'headers' => [ 'X-Access-Token' => $node->daemonSecret, @@ -928,7 +928,7 @@ class ServerRepository public function unsuspend($id) { $server = Models\Server::findOrFail($id); - $node = Models\Node::findOrFail($server->node); + $node = Models\Node::findOrFail($server->node_id); DB::beginTransaction(); @@ -942,7 +942,7 @@ class ServerRepository $server->suspended = 0; $server->save(); - $client = Models\Node::guzzleRequest($server->node); + $client = Models\Node::guzzleRequest($server->node_id); $client->request('POST', '/server/unsuspend', [ 'headers' => [ 'X-Access-Token' => $node->daemonSecret, @@ -963,7 +963,7 @@ class ServerRepository public function updateSFTPPassword($id, $password) { $server = Models\Server::findOrFail($id); - $node = Models\Node::findOrFail($server->node); + $node = Models\Node::findOrFail($server->node_id); $validator = Validator::make([ 'password' => $password, @@ -981,7 +981,7 @@ class ServerRepository try { $server->save(); - $client = Models\Node::guzzleRequest($server->node); + $client = Models\Node::guzzleRequest($server->node_id); $client->request('POST', '/server/password', [ 'headers' => [ 'X-Access-Token' => $node->daemonSecret, diff --git a/app/Repositories/SubuserRepository.php b/app/Repositories/SubuserRepository.php index afa27493e..335e6926d 100644 --- a/app/Repositories/SubuserRepository.php +++ b/app/Repositories/SubuserRepository.php @@ -146,7 +146,7 @@ class SubuserRepository } catch (\Exception $ex) { throw $ex; } - } elseif ($server->owner === $user->id) { + } elseif ($server->owner_id === $user->id) { throw new DisplayException('You cannot add the owner of a server as a subuser.'); } elseif (Models\Subuser::select('id')->where('user_id', $user->id)->where('server_id', $server->id)->first()) { throw new DisplayException('A subuser with that email already exists for this server.'); @@ -184,8 +184,8 @@ class SubuserRepository // We contact even if they don't have any daemon permissions to overwrite // if they did have them previously. - $node = Models\Node::getByID($server->node); - $client = Models\Node::guzzleRequest($server->node); + $node = Models\Node::getByID($server->node_id); + $client = Models\Node::guzzleRequest($server->node_id); $res = $client->request('PATCH', '/server', [ 'headers' => [ @@ -240,8 +240,8 @@ class SubuserRepository try { Models\Permission::where('user_id', $subuser->user_id)->where('server_id', $subuser->server_id)->delete(); - $node = Models\Node::getByID($server->node); - $client = Models\Node::guzzleRequest($server->node); + $node = Models\Node::getByID($server->node_id); + $client = Models\Node::guzzleRequest($server->node_id); $res = $client->request('PATCH', '/server', [ 'headers' => [ @@ -318,8 +318,8 @@ class SubuserRepository // Contact Daemon // We contact even if they don't have any daemon permissions to overwrite // if they did have them previously. - $node = Models\Node::getByID($server->node); - $client = Models\Node::guzzleRequest($server->node); + $node = Models\Node::getByID($server->node_id); + $client = Models\Node::guzzleRequest($server->node_id); $res = $client->request('PATCH', '/server', [ 'headers' => [ diff --git a/app/Services/NotificationService.php b/app/Services/NotificationService.php index bb76db9b6..abd2155b1 100644 --- a/app/Services/NotificationService.php +++ b/app/Services/NotificationService.php @@ -48,7 +48,7 @@ class NotificationService public function __construct(Server $server) { $this->server = $server; - $this->user = User::findOrFail($server->owner); + $this->user = User::findOrFail($server->owner_id); } public function pass(array $notification) diff --git a/database/migrations/2016_08_30_213301_modify_ip_storage_method.php b/database/migrations/2016_08_30_213301_modify_ip_storage_method.php index b77ccbea6..768ef0b55 100644 --- a/database/migrations/2016_08_30_213301_modify_ip_storage_method.php +++ b/database/migrations/2016_08_30_213301_modify_ip_storage_method.php @@ -24,7 +24,7 @@ class ModifyIpStorageMethod extends Migration [ 'ip' => $server->ip, 'port' => $server->port, - 'node' => $server->node, + 'node' => $server->node_id, ] ); @@ -61,7 +61,7 @@ class ModifyIpStorageMethod extends Migration // Find the allocations and reset the servers... $servers = DB::select('SELECT id, allocation FROM servers'); foreach ($servers as $server) { - $allocation = DB::select('SELECT * FROM allocations WHERE id = :alocid', ['alocid' => $server->allocation]); + $allocation = DB::select('SELECT * FROM allocations WHERE id = :alocid', ['alocid' => $server->allocation_id]); if (isset($allocation[0])) { DB::update( diff --git a/database/migrations/2016_09_17_194246_add_docker_image_column.php b/database/migrations/2016_09_17_194246_add_docker_image_column.php index 58e4b87a3..ce7d91203 100644 --- a/database/migrations/2016_09_17_194246_add_docker_image_column.php +++ b/database/migrations/2016_09_17_194246_add_docker_image_column.php @@ -22,7 +22,7 @@ class AddDockerImageColumn extends Migration $servers = DB::table('servers')->select( 'servers.id', 'service_options.docker_image as s_optionImage' - )->join('service_options', 'service_options.id', '=', 'servers.option')->get(); + )->join('service_options', 'service_options.id', '=', 'servers.option_id')->get(); foreach ($servers as $server) { $server->image = $server->s_optionImage; diff --git a/database/migrations/2017_02_02_175548_UpdateColumnNames.php b/database/migrations/2017_02_02_175548_UpdateColumnNames.php new file mode 100644 index 000000000..bc49c7d45 --- /dev/null +++ b/database/migrations/2017_02_02_175548_UpdateColumnNames.php @@ -0,0 +1,66 @@ +dropForeign('servers_node_foreign'); + $table->dropForeign('servers_owner_foreign'); + $table->dropForeign('servers_allocation_foreign'); + $table->dropForeign('servers_service_foreign'); + $table->dropForeign('servers_option_foreign'); + + $table->dropIndex('servers_node_foreign'); + $table->dropIndex('servers_owner_foreign'); + $table->dropIndex('servers_allocation_foreign'); + $table->dropIndex('servers_service_foreign'); + $table->dropIndex('servers_option_foreign'); + + $table->renameColumn('node', 'node_id'); + $table->renameColumn('owner', 'owner_id'); + $table->renameColumn('allocation', 'allocation_id'); + $table->renameColumn('service', 'service_id'); + $table->renameColumn('option', 'option_id'); + $table->renameColumn('pack', 'pack_id'); + + $table->foreign('node_id')->references('id')->on('nodes'); + $table->foreign('owner_id')->references('id')->on('users'); + $table->foreign('allocation_id')->references('id')->on('allocations'); + $table->foreign('service_id')->references('id')->on('services'); + $table->foreign('option_id')->references('id')->on('service_options'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('servers', function (Blueprint $table) { + $table->renameColumn('node_id', 'node'); + $table->renameColumn('owner_id', 'owner'); + $table->renameColumn('allocation_id', 'allocation'); + $table->renameColumn('service_id', 'service'); + $table->renameColumn('option_id', 'option'); + $table->renameColumn('pack_id', 'pack'); + + $table->foreign('node')->references('id')->on('nodes'); + $table->foreign('owner')->references('id')->on('users'); + $table->foreign('allocation')->references('id')->on('allocations'); + $table->foreign('service')->references('id')->on('services'); + $table->foreign('option')->references('id')->on('service_options'); + }); + } +} diff --git a/resources/themes/pterodactyl/base/index.blade.php b/resources/themes/pterodactyl/base/index.blade.php index c6066c25a..d029a37a9 100644 --- a/resources/themes/pterodactyl/base/index.blade.php +++ b/resources/themes/pterodactyl/base/index.blade.php @@ -62,7 +62,7 @@ {{ $server->uuidShort }} {{ $server->name }} - {{ $server->nodeName }} + {{ $server->node_idName }} @if(!is_null($server->ip_alias)){{ $server->ip_alias }}@else{{ $server->ip }}@endif:{{ $server->port }} -- / {{ $server->memory === 0 ? '∞' : $server->memory }} MB -- % diff --git a/resources/themes/pterodactyl/server/settings/allocation.blade.php b/resources/themes/pterodactyl/server/settings/allocation.blade.php index f2782a88c..773281e96 100644 --- a/resources/themes/pterodactyl/server/settings/allocation.blade.php +++ b/resources/themes/pterodactyl/server/settings/allocation.blade.php @@ -63,7 +63,7 @@ {{ $allocation->port }} - @if($allocation->id === $server->allocation) + @if($allocation->id === $server->allocation_id) @lang('strings.primary') @else @lang('strings.make_primary') diff --git a/resources/views/admin/nodes/view.blade.php b/resources/views/admin/nodes/view.blade.php index a0ed696e7..8485c9a50 100644 --- a/resources/views/admin/nodes/view.blade.php +++ b/resources/views/admin/nodes/view.blade.php @@ -405,7 +405,7 @@ @foreach($servers as $server) {{ $server->name }} - {{ $server->a_ownerEmail }} + {{ $server->a_ownerEmail }} {{ $server->a_serviceName }} -- / {{ $server->memory === 0 ? '∞' : $server->memory }} MB {{ $server->disk }} MB diff --git a/resources/views/admin/servers/index.blade.php b/resources/views/admin/servers/index.blade.php index 612d2905c..11d852ecf 100644 --- a/resources/views/admin/servers/index.blade.php +++ b/resources/views/admin/servers/index.blade.php @@ -64,8 +64,8 @@ Pending Deletion @endif - {{ $server->a_ownerEmail }} - {{ $server->a_nodeName }} + {{ $server->a_ownerEmail }} + {{ $server->a_nodeName }} {{ $server->username }} @endforeach diff --git a/resources/views/admin/servers/view.blade.php b/resources/views/admin/servers/view.blade.php index abad871d9..7def73236 100644 --- a/resources/views/admin/servers/view.blade.php +++ b/resources/views/admin/servers/view.blade.php @@ -89,7 +89,7 @@ Owner - {{ $server->a_ownerEmail }} + {{ $server->a_ownerEmail }} Location @@ -97,7 +97,7 @@ Node - {{ $node->name }} + {{ $node->name }} Service @@ -304,7 +304,7 @@
@@ -398,7 +398,7 @@ diff --git a/resources/views/admin/services/options/view.blade.php b/resources/views/admin/services/options/view.blade.php index bffb2057e..e3eb61a12 100644 --- a/resources/views/admin/services/options/view.blade.php +++ b/resources/views/admin/services/options/view.blade.php @@ -178,7 +178,7 @@ @foreach ($servers as $server) {{ $server->name }} - {{ $server->a_ownerEmail }} + {{ $server->a_ownerEmail }} {{ $server->updated_at }} @endforeach diff --git a/resources/views/admin/users/view.blade.php b/resources/views/admin/users/view.blade.php index 0a5d8230d..314fc8ba7 100644 --- a/resources/views/admin/users/view.blade.php +++ b/resources/views/admin/users/view.blade.php @@ -117,7 +117,7 @@ {{ $server->uuidShort }} {{ $server->name }} - {{ $server->nodeName }} + {{ $server->node_idName }} {{ $server->username }} @if($server->suspended === 0)Active@elseSuspended@endif From c4b76bf48e05a2d0f0bb7ee687644ee7e518cbe1 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Thu, 2 Feb 2017 18:25:11 -0500 Subject: [PATCH 02/34] Remove FKs correctly --- .../2017_02_02_175548_UpdateColumnNames.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/database/migrations/2017_02_02_175548_UpdateColumnNames.php b/database/migrations/2017_02_02_175548_UpdateColumnNames.php index bc49c7d45..7e281af8e 100644 --- a/database/migrations/2017_02_02_175548_UpdateColumnNames.php +++ b/database/migrations/2017_02_02_175548_UpdateColumnNames.php @@ -49,6 +49,18 @@ class UpdateColumnNames extends Migration public function down() { Schema::table('servers', function (Blueprint $table) { + $table->dropForeign('servers_node_id_foreign'); + $table->dropForeign('servers_owner_id_foreign'); + $table->dropForeign('servers_allocation_id_foreign'); + $table->dropForeign('servers_service_id_foreign'); + $table->dropForeign('servers_option_id_foreign'); + + $table->dropIndex('servers_node_id_foreign'); + $table->dropIndex('servers_owner_id_foreign'); + $table->dropIndex('servers_allocation_id_foreign'); + $table->dropIndex('servers_service_id_foreign'); + $table->dropIndex('servers_option_id_foreign'); + $table->renameColumn('node_id', 'node'); $table->renameColumn('owner_id', 'owner'); $table->renameColumn('allocation_id', 'allocation'); From 644ee85f59fa4bfb6475ef116b69be1ad97d8046 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Thu, 2 Feb 2017 18:25:46 -0500 Subject: [PATCH 03/34] Fix for StyleCI --- app/Models/User.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/Models/User.php b/app/Models/User.php index 0de3eb5ae..7dba0cf0d 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -24,7 +24,6 @@ namespace Pterodactyl\Models; -use Auth; use Hash; use Google2FA; use Illuminate\Auth\Authenticatable; From 3114b7e52a1a078a0d05ec2d8b22fe9073a5bf9b Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Thu, 2 Feb 2017 19:41:38 -0500 Subject: [PATCH 04/34] Complete implementation of new Server model. --- CHANGELOG.md | 5 +- .../Controllers/API/User/ServerController.php | 28 ++--- .../Controllers/Server/AjaxController.php | 24 ++-- .../Controllers/Server/ServerController.php | 103 +++++++----------- .../Controllers/Server/SubuserController.php | 39 +++---- .../Controllers/Server/TaskController.php | 40 +++---- app/Models/Node.php | 16 +++ app/Models/Server.php | 67 +++++++++++- app/Repositories/Daemon/FileRepository.php | 48 ++------ .../server/settings/allocation.blade.php | 2 +- 10 files changed, 170 insertions(+), 202 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 80b459be5..cc093ab76 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,9 +20,12 @@ This project follows [Semantic Versioning](http://semver.org) guidelines. * Admin API and base routes for user management now define the fields that should be passed to repositories rather than passing all fields. * User model now defines mass assignment fields using `$fillable` rather than `$guarded`. * 2FA checkpoint on login is now its own page, and not an AJAX based call. Improves security on that front. +* Updated Server model code to be more efficient, as well as make life easier for backend changes and work. -### Removed +### Deprecated * `Server::getUserDaemonSecret(Server $server)` was removed and replaced with `User::daemonSecret(Server $server)` in order to clean up models. +* `Server::getByUUID()` was replaced with `Server::byUuid()` as well as various other functions through-out the Server model. +* `Server::getHeaders()` was removed and replaced with `Server::getClient()` which returns a Guzzle Client with the correct headers already assigned. ## v0.5.6 (Bodacious Boreopterus) ### Added diff --git a/app/Http/Controllers/API/User/ServerController.php b/app/Http/Controllers/API/User/ServerController.php index 128c5cf68..c6091f0b1 100644 --- a/app/Http/Controllers/API/User/ServerController.php +++ b/app/Http/Controllers/API/User/ServerController.php @@ -34,17 +34,10 @@ class ServerController extends BaseController { public function info(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); - $node = Models\Node::findOrFail($server->node_id); - $client = Models\Node::guzzleRequest($node->id); + $server = Models\Server::byUuid($uuid)->load('allocations'); try { - $response = $client->request('GET', '/server', [ - 'headers' => [ - 'X-Access-Token' => $server->daemonSecret, - 'X-Access-Server' => $server->uuid, - ], - ]); + $response = $server->guzzleClient()->request('GET', '/server'); $json = json_decode($response->getBody()); $daemon = [ @@ -59,8 +52,7 @@ class ServerController extends BaseController Log::error($ex); } - $allocations = Models\Allocation::select('id', 'ip', 'port', 'ip_alias as alias')->where('assigned_to', $server->id)->get(); - foreach ($allocations as &$allocation) { + foreach ($server->allocations as &$allocation) { $allocation->default = ($allocation->id === $server->allocation_id); unset($allocation->id); } @@ -69,7 +61,7 @@ class ServerController extends BaseController 'uuidShort' => $server->uuidShort, 'uuid' => $server->uuid, 'name' => $server->name, - 'node' => $node->name, + 'node' => $server->node->name, 'limits' => [ 'memory' => $server->memory, 'swap' => $server->swap, @@ -78,7 +70,7 @@ class ServerController extends BaseController 'cpu' => $server->cpu, 'oom_disabled' => (bool) $server->oom_disabled, ], - 'allocations' => $allocations, + 'allocations' => $server->allocations, 'sftp' => [ 'username' => (Auth::user()->can('view-sftp', $server)) ? $server->username : null, ], @@ -91,16 +83,10 @@ class ServerController extends BaseController public function power(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); - $client = Models\Node::guzzleRequest($server->node_id); - + $server = Models\Server::byUuid($uuid); Auth::user()->can('power-' . $request->input('action'), $server); - $res = $client->request('PUT', '/server/power', [ - 'headers' => [ - 'X-Access-Server' => $server->uuid, - 'X-Access-Token' => $server->daemonSecret, - ], + $res = $server->guzzleClient()->request('PUT', '/server/power', [ 'exceptions' => false, 'json' => [ 'action' => $request->input('action'), diff --git a/app/Http/Controllers/Server/AjaxController.php b/app/Http/Controllers/Server/AjaxController.php index 9ee9f4284..9493bc0f0 100644 --- a/app/Http/Controllers/Server/AjaxController.php +++ b/app/Http/Controllers/Server/AjaxController.php @@ -67,18 +67,14 @@ class AjaxController extends Controller */ public function getStatus(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); if (! $server) { return response()->json([], 404); } - $client = Models\Node::guzzleRequest($server->node_id); - try { - $res = $client->request('GET', '/server', [ - 'headers' => Models\Server::getGuzzleHeaders($uuid), - ]); + $res = $server->guzzleClient()->request('GET', '/server'); if ($res->getStatusCode() === 200) { return response()->json(json_decode($res->getBody())); } @@ -98,10 +94,10 @@ class AjaxController extends Controller */ public function postDirectoryList(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); - $this->directory = '/' . trim(urldecode($request->input('directory', '/')), '/'); + $server = Models\Server::byUuid($uuid); $this->authorize('list-files', $server); + $this->directory = '/' . trim(urldecode($request->input('directory', '/')), '/'); $prevDir = [ 'header' => ($this->directory !== '/') ? $this->directory : '', ]; @@ -149,7 +145,7 @@ class AjaxController extends Controller */ public function postSaveFile(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); $this->authorize('save-files', $server); $controller = new Repositories\Daemon\FileRepository($uuid); @@ -175,7 +171,7 @@ class AjaxController extends Controller */ public function postSetPrimary(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid)->load('allocations'); $this->authorize('set-connection', $server); if ((int) $request->input('allocation') === $server->allocation_id) { @@ -185,7 +181,7 @@ class AjaxController extends Controller } try { - $allocation = Models\Allocation::where('id', $request->input('allocation'))->where('assigned_to', $server->id)->first(); + $allocation = $server->allocations->where('id', $request->input('allocation'))->where('assigned_to', $server->id)->first(); if (! $allocation) { return response()->json([ 'error' => 'No allocation matching your request was found in the system.', @@ -217,10 +213,10 @@ class AjaxController extends Controller public function postResetDatabasePassword(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); - $database = Models\Database::where('id', $request->input('database'))->where('server_id', $server->id)->firstOrFail(); - + $server = Models\Server::byUuid($uuid); $this->authorize('reset-db-password', $server); + + $database = Models\Database::where('id', $request->input('database'))->where('server_id', $server->id)->firstOrFail(); try { $repo = new Repositories\DatabaseRepository; $password = str_random(16); diff --git a/app/Http/Controllers/Server/ServerController.php b/app/Http/Controllers/Server/ServerController.php index 7f07f977c..0b423cc97 100644 --- a/app/Http/Controllers/Server/ServerController.php +++ b/app/Http/Controllers/Server/ServerController.php @@ -59,9 +59,7 @@ class ServerController extends Controller { $server = Models\Server::byUuid($uuid); - Javascript::put([ - 'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'daemonSecret', 'username']), - 'node' => collect($server->node_id)->only('fqdn', 'scheme', 'daemonListen'), + $server->js([ 'meta' => [ 'saveFile' => route('server.files.save', $server->uuidShort), 'csrfToken' => csrf_token(), @@ -70,7 +68,7 @@ class ServerController extends Controller return view('server.index', [ 'server' => $server, - 'node' => $server->node_id, + 'node' => $server->node, ]); } @@ -82,14 +80,10 @@ class ServerController extends Controller */ public function getFiles(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); $this->authorize('list-files', $server); - $node = Models\Node::find($server->node_id); - - Javascript::put([ - 'server' => collect($server->makeVisible('daemonSecret'))->only('uuid', 'uuidShort', 'daemonSecret'), - 'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'), + $server->js([ 'meta' => [ 'directoryList' => route('server.files.directory-list', $server->uuidShort), 'csrftoken' => csrf_token(), @@ -107,7 +101,7 @@ class ServerController extends Controller return view('server.files.index', [ 'server' => $server, - 'node' => $node, + 'node' => $server->node, ]); } @@ -119,18 +113,14 @@ class ServerController extends Controller */ public function getAddFile(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); $this->authorize('add-files', $server); - $node = Models\Node::find($server->node_id); - Javascript::put([ - 'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']), - 'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'), - ]); + $server->js(); return view('server.files.add', [ 'server' => $server, - 'node' => $node, + 'node' => $server->node, 'directory' => (in_array($request->get('dir'), [null, '/', ''])) ? '' : trim($request->get('dir'), '/') . '/', ]); } @@ -145,9 +135,8 @@ class ServerController extends Controller */ public function getEditFile(Request $request, $uuid, $file) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); $this->authorize('edit-files', $server); - $node = Models\Node::find($server->node_id); $fileInfo = (object) pathinfo($file); $controller = new FileRepository($uuid); @@ -165,15 +154,13 @@ class ServerController extends Controller return redirect()->route('server.files.index', $uuid); } - Javascript::put([ - 'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']), - 'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'), + $server->js([ 'stat' => $fileContent['stat'], ]); return view('server.files.edit', [ 'server' => $server, - 'node' => $node, + 'node' => $server->node, 'file' => $file, 'stat' => $fileContent['stat'], 'contents' => $fileContent['file']->content, @@ -191,9 +178,7 @@ class ServerController extends Controller */ public function getDownloadFile(Request $request, $uuid, $file) { - $server = Models\Server::getByUUID($uuid); - $node = Models\Node::find($server->node_id); - + $server = Models\Server::byUuid($uuid); $this->authorize('download-files', $server); $download = new Models\Download; @@ -204,38 +189,31 @@ class ServerController extends Controller $download->save(); - return redirect($node->scheme . '://' . $node->fqdn . ':' . $node->daemonListen . '/server/file/download/' . $download->token); + return redirect($server->node->scheme . '://' . $server->node->fqdn . ':' . $server->node->daemonListen . '/server/file/download/' . $download->token); } public function getAllocation(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); $this->authorize('view-allocation', $server); - $node = Models\Node::find($server->node_id); - - Javascript::put([ - 'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']), - 'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'), - ]); + $server->js(); return view('server.settings.allocation', [ - 'server' => $server, - 'allocations' => Models\Allocation::where('assigned_to', $server->id)->orderBy('ip', 'asc')->orderBy('port', 'asc')->get(), - 'node' => $node, + 'server' => $server->load(['allocations' => function ($query) { + $query->orderBy('ip', 'asc'); + $query->orderBy('port', 'asc'); + }]), + 'node' => $server->node, ]); } public function getStartup(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); + $server->load(['allocations' => function ($query) use ($server) { + $query->where('id', $server->allocation_id); + }]); $this->authorize('view-startup', $server); - $node = Models\Node::find($server->node_id); - $allocation = Models\Allocation::findOrFail($server->allocation_id); - - Javascript::put([ - 'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']), - 'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'), - ]); $variables = Models\ServiceVariables::select( 'service_variables.*', @@ -252,6 +230,7 @@ class ServerController extends Controller ->where('services.id', $server->service_id) ->first(); + $allocation = $server->allocations->pop(); $serverVariables = [ '{{SERVER_MEMORY}}' => $server->memory, '{{SERVER_IP}}' => $allocation->ip, @@ -260,13 +239,15 @@ class ServerController extends Controller $processed = str_replace(array_keys($serverVariables), array_values($serverVariables), $server->startup); foreach ($variables as &$variable) { - $replace = ($variable->user_viewable === 1) ? $variable->a_serverValue : '**'; + $replace = ($variable->user_viewable === 1) ? $variable->a_serverValue : '[hidden]'; $processed = str_replace('{{' . $variable->env_variable . '}}', $replace, $processed); } + $server->js(); + return view('server.settings.startup', [ 'server' => $server, - 'node' => Models\Node::find($server->node_id), + 'node' => $server->node, 'variables' => $variables->where('user_viewable', 1), 'service' => $service, 'processedStartup' => $processed, @@ -275,18 +256,13 @@ class ServerController extends Controller public function getDatabases(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); $this->authorize('view-databases', $server); - $node = Models\Node::find($server->node_id); - - Javascript::put([ - 'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']), - 'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'), - ]); + $server->js(); return view('server.settings.databases', [ 'server' => $server, - 'node' => $node, + 'node' => $server->node, 'databases' => Models\Database::select('databases.*', 'database_servers.host as a_host', 'database_servers.port as a_port') ->where('server_id', $server->id) ->join('database_servers', 'database_servers.id', '=', 'databases.db_server') @@ -296,24 +272,19 @@ class ServerController extends Controller public function getSFTP(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); $this->authorize('view-sftp', $server); - $node = Models\Node::find($server->node_id); - - Javascript::put([ - 'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'daemonSecret', 'username']), - 'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'), - ]); + $server->js(); return view('server.settings.sftp', [ 'server' => $server, - 'node' => $node, + 'node' => $server->node, ]); } public function postSettingsSFTP(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); $this->authorize('reset-sftp', $server); try { @@ -334,7 +305,7 @@ class ServerController extends Controller public function postSettingsStartup(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); $this->authorize('edit-startup', $server); try { diff --git a/app/Http/Controllers/Server/SubuserController.php b/app/Http/Controllers/Server/SubuserController.php index 1d07b47f7..9badfef45 100644 --- a/app/Http/Controllers/Server/SubuserController.php +++ b/app/Http/Controllers/Server/SubuserController.php @@ -50,18 +50,14 @@ class SubuserController extends Controller public function getIndex(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); $this->authorize('list-subusers', $server); - $node = Models\Node::find($server->node_id); - Javascript::put([ - 'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']), - 'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'), - ]); + $server->js(); return view('server.users.index', [ 'server' => $server, - 'node' => $node, + 'node' => $server->node, 'subusers' => Models\Subuser::select('subusers.*', 'users.email', 'users.username', 'users.use_totp') ->join('users', 'users.id', '=', 'subusers.user_id') ->where('server_id', $server->id) @@ -71,14 +67,8 @@ class SubuserController extends Controller public function getView(Request $request, $uuid, $id) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); $this->authorize('view-subuser', $server); - $node = Models\Node::find($server->node_id); - - Javascript::put([ - 'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']), - 'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'), - ]); $subuser = Models\Subuser::select('subusers.*', 'users.email as a_userEmail') ->join('users', 'users.id', '=', 'subusers.user_id') @@ -98,9 +88,11 @@ class SubuserController extends Controller $permissions[$perm->permission] = true; } + $server->js(); + return view('server.users.view', [ 'server' => $server, - 'node' => $node, + 'node' => $server->node, 'subuser' => $subuser, 'permissions' => $permissions, ]); @@ -108,7 +100,7 @@ class SubuserController extends Controller public function postView(Request $request, $uuid, $id) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); $this->authorize('edit-subuser', $server); $subuser = Models\Subuser::where(DB::raw('md5(id)'), $id)->where('server_id', $server->id)->first(); @@ -148,24 +140,19 @@ class SubuserController extends Controller public function getNew(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); $this->authorize('create-subuser', $server); - $node = Models\Node::find($server->node_id); - - Javascript::put([ - 'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']), - 'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'), - ]); + $server->js(); return view('server.users.new', [ 'server' => $server, - 'node' => $node, + 'node' => $server->node, ]); } public function postNew(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); $this->authorize('create-subuser', $server); try { @@ -193,7 +180,7 @@ class SubuserController extends Controller public function deleteSubuser(Request $request, $uuid, $id) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); $this->authorize('delete-subuser', $server); try { diff --git a/app/Http/Controllers/Server/TaskController.php b/app/Http/Controllers/Server/TaskController.php index 51243cc8f..4fb492afc 100644 --- a/app/Http/Controllers/Server/TaskController.php +++ b/app/Http/Controllers/Server/TaskController.php @@ -43,19 +43,14 @@ class TaskController extends Controller public function getIndex(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid)->load('tasks'); $this->authorize('list-tasks', $server); - $node = Models\Node::find($server->node_id); - - Javascript::put([ - 'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']), - 'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'), - ]); + $server->js(); return view('server.tasks.index', [ 'server' => $server, - 'node' => $node, - 'tasks' => Models\Task::where('server', $server->id)->get(), + 'node' => $server->node, + 'tasks' => $server->tasks, 'actions' => [ 'command' => trans('server.tasks.actions.command'), 'power' => trans('server.tasks.actions.power'), @@ -65,24 +60,19 @@ class TaskController extends Controller public function getNew(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); $this->authorize('create-task', $server); - $node = Models\Node::find($server->node_id); - - Javascript::put([ - 'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']), - 'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'), - ]); + $server->js(); return view('server.tasks.new', [ 'server' => $server, - 'node' => $node, + 'node' => $server->node, ]); } public function postNew(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); $this->authorize('create-task', $server); try { @@ -106,12 +96,11 @@ class TaskController extends Controller public function deleteTask(Request $request, $uuid, $id) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid)->load('tasks'); $this->authorize('delete-task', $server); - $task = Models\Task::findOrFail($id); - - if (! $task || $server->id !== $task->server) { + $task = $server->tasks->where('id', $id)->first(); + if (! $task) { return response()->json([ 'error' => 'No task by that ID was found associated with this server.', ], 404); @@ -133,12 +122,11 @@ class TaskController extends Controller public function toggleTask(Request $request, $uuid, $id) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid)->load('tasks'); $this->authorize('toggle-task', $server); - $task = Models\Task::findOrFail($id); - - if (! $task || $server->id !== $task->server) { + $task = $server->tasks->where('id', $id)->first(); + if (! $task) { return response()->json([ 'error' => 'No task by that ID was found associated with this server.', ], 404); diff --git a/app/Models/Node.php b/app/Models/Node.php index a5cbce464..8e82ef935 100644 --- a/app/Models/Node.php +++ b/app/Models/Node.php @@ -96,11 +96,27 @@ class Node extends Model return self::$nodes[$id]; } + /** + * Return an instance of the Guzzle client for this specific node. + * + * @return \GuzzleHttp\Client + */ + public function guzzleClient($headers = []) + { + return new Client([ + 'base_uri' => sprintf('%s://%s:%s/', $this->scheme, $this->fqdn, $this->daemonListen), + 'timeout' => env('GUZZLE_TIMEOUT', 5.0), + 'connect_timeout' => env('GUZZLE_CONNECT_TIMEOUT', 3.0), + 'headers' => $headers, + ]); + } + /** * Returns a Guzzle Client for the node in question. * * @param int $node * @return \GuzzleHttp\Client + * @deprecated */ public static function guzzleRequest($node) { diff --git a/app/Models/Server.php b/app/Models/Server.php index 32240da44..d0d02bc58 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -25,6 +25,7 @@ namespace Pterodactyl\Models; use Auth; +use Javascript; use Illuminate\Database\Eloquent\Model; use Illuminate\Notifications\Notifiable; use Illuminate\Database\Eloquent\SoftDeletes; @@ -67,18 +68,19 @@ class Server extends Model * @var array */ protected $casts = [ - 'node' => 'integer', + 'node_id' => 'integer', 'suspended' => 'integer', - 'owner' => 'integer', + 'owner_id' => 'integer', 'memory' => 'integer', 'swap' => 'integer', 'disk' => 'integer', 'io' => 'integer', 'cpu' => 'integer', 'oom_disabled' => 'integer', - 'port' => 'integer', - 'service' => 'integer', - 'option' => 'integer', + 'allocation_id' => 'integer', + 'service_id' => 'integer', + 'option_id' => 'integer', + 'pack_id' => 'integer', 'installed' => 'integer', ]; @@ -166,7 +168,7 @@ class Server extends Model * @param string $uuid * @return array */ - public function getHeaders() + public function guzzleHeaders() { return [ 'X-Access-Server' => $this->uuid, @@ -174,6 +176,48 @@ class Server extends Model ]; } + /** + * 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', + 'username' + ]), + 'node' => collect($this->node)->only([ + 'fqdn', + 'scheme', + 'daemonListen' + ]), + ]; + + if (is_array($additional)) { + $response = array_merge($response, $additional); + } + + if (is_array($overwrite)) { + $response = $overwrite; + } + + return Javascript::put($response); + } + /** * Gets all allocations associated with this server. * @@ -233,4 +277,15 @@ class Server extends Model { return $this->hasOne(Node::class, 'id', 'node_id'); } + + /** + * 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'); + } } diff --git a/app/Repositories/Daemon/FileRepository.php b/app/Repositories/Daemon/FileRepository.php index d1319cf91..cb5f92a28 100644 --- a/app/Repositories/Daemon/FileRepository.php +++ b/app/Repositories/Daemon/FileRepository.php @@ -40,28 +40,6 @@ class FileRepository */ protected $server; - /** - * The Eloquent Model for the node corresponding with the requested server. - * - * @var \Illuminate\Database\Eloquent\Model - */ - protected $node; - - /** - * The Guzzle Client associated with the requested server and node. - * - * @var \GuzzleHttp\Client - */ - protected $client; - - /** - * The Guzzle Client headers associated with the requested server and node. - * (non-administrative headers). - * - * @var array - */ - protected $headers; - /** * Constructor. * @@ -69,10 +47,7 @@ class FileRepository */ public function __construct($uuid) { - $this->server = Server::getByUUID($uuid); - $this->node = Node::getByID($this->server->node); - $this->client = Node::guzzleRequest($this->server->node); - $this->headers = Server::getGuzzleHeaders($uuid); + $this->server = Server::byUuid($uuid); } /** @@ -88,12 +63,9 @@ class FileRepository } $file = (object) pathinfo($file); - $file->dirname = (in_array($file->dirname, ['.', './', '/'])) ? null : trim($file->dirname, '/') . '/'; - $res = $this->client->request('GET', '/server/file/stat/' . rawurlencode($file->dirname . $file->basename), [ - 'headers' => $this->headers, - ]); + $res = $this->server->guzzleClient()->request('GET', '/server/file/stat/' . rawurlencode($file->dirname . $file->basename)); $stat = json_decode($res->getBody()); if ($res->getStatusCode() !== 200 || ! isset($stat->size)) { @@ -108,9 +80,7 @@ class FileRepository throw new DisplayException('That file is too large to open in the browser, consider using a SFTP client.'); } - $res = $this->client->request('GET', '/server/file/f/' . rawurlencode($file->dirname . $file->basename), [ - 'headers' => $this->headers, - ]); + $res = $this->server->guzzleClient()->request('GET', '/server/file/f/' . rawurlencode($file->dirname . $file->basename)); $json = json_decode($res->getBody()); if ($res->getStatusCode() !== 200 || ! isset($json->content)) { @@ -137,11 +107,9 @@ class FileRepository } $file = (object) pathinfo($file); - $file->dirname = (in_array($file->dirname, ['.', './', '/'])) ? null : trim($file->dirname, '/') . '/'; - $res = $this->client->request('POST', '/server/file/save', [ - 'headers' => $this->headers, + $res = $this->server->guzzleClient()->request('POST', '/server/file/save', [ 'json' => [ 'path' => rawurlencode($file->dirname . $file->basename), 'content' => $content, @@ -167,9 +135,7 @@ class FileRepository throw new Exception('A valid directory must be specified in order to list its contents.'); } - $res = $this->client->request('GET', '/server/directory/' . rawurlencode($directory), [ - 'headers' => $this->headers, - ]); + $res = $this->server->guzzleClient()->request('GET', '/server/directory/' . rawurlencode($directory)); $json = json_decode($res->getBody()); if ($res->getStatusCode() !== 200) { @@ -180,7 +146,7 @@ class FileRepository $files = []; $folders = []; foreach ($json as &$value) { - if ($value->directory === true) { + if ($value->directory) { // @TODO Handle Symlinks $folders[] = [ 'entry' => $value->name, @@ -189,7 +155,7 @@ class FileRepository 'date' => strtotime($value->modified), 'mime' => $value->mime, ]; - } elseif ($value->file === true) { + } elseif ($value->file) { $files[] = [ 'entry' => $value->name, 'directory' => trim($directory, '/'), diff --git a/resources/themes/pterodactyl/server/settings/allocation.blade.php b/resources/themes/pterodactyl/server/settings/allocation.blade.php index 773281e96..a6a0c36ad 100644 --- a/resources/themes/pterodactyl/server/settings/allocation.blade.php +++ b/resources/themes/pterodactyl/server/settings/allocation.blade.php @@ -49,7 +49,7 @@ @lang('strings.port') - @foreach ($allocations as $allocation) + @foreach ($server->allocations as $allocation) {{ $allocation->ip }} From 3baa21a6eb20d53e03154d853d4b8be2ad9aff98 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Thu, 2 Feb 2017 19:43:55 -0500 Subject: [PATCH 05/34] StyleCI fixes. --- app/Http/Controllers/Server/ServerController.php | 1 - app/Http/Controllers/Server/SubuserController.php | 1 - app/Http/Controllers/Server/TaskController.php | 1 - app/Models/Server.php | 4 ++-- app/Repositories/Daemon/FileRepository.php | 1 - 5 files changed, 2 insertions(+), 6 deletions(-) diff --git a/app/Http/Controllers/Server/ServerController.php b/app/Http/Controllers/Server/ServerController.php index 0b423cc97..75378ddc1 100644 --- a/app/Http/Controllers/Server/ServerController.php +++ b/app/Http/Controllers/Server/ServerController.php @@ -28,7 +28,6 @@ use DB; use Log; use Uuid; use Alert; -use Javascript; use Pterodactyl\Models; use Illuminate\Http\Request; use Pterodactyl\Exceptions\DisplayException; diff --git a/app/Http/Controllers/Server/SubuserController.php b/app/Http/Controllers/Server/SubuserController.php index 9badfef45..8c4452457 100644 --- a/app/Http/Controllers/Server/SubuserController.php +++ b/app/Http/Controllers/Server/SubuserController.php @@ -28,7 +28,6 @@ use DB; use Log; use Auth; use Alert; -use Javascript; use Pterodactyl\Models; use Illuminate\Http\Request; use Pterodactyl\Exceptions\DisplayException; diff --git a/app/Http/Controllers/Server/TaskController.php b/app/Http/Controllers/Server/TaskController.php index 4fb492afc..643e70a45 100644 --- a/app/Http/Controllers/Server/TaskController.php +++ b/app/Http/Controllers/Server/TaskController.php @@ -26,7 +26,6 @@ namespace Pterodactyl\Http\Controllers\Server; use Log; use Alert; -use Javascript; use Pterodactyl\Models; use Illuminate\Http\Request; use Pterodactyl\Repositories; diff --git a/app/Models/Server.php b/app/Models/Server.php index d0d02bc58..db8ba6381 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -198,12 +198,12 @@ class Server extends Model 'uuid', 'uuidShort', 'daemonSecret', - 'username' + 'username', ]), 'node' => collect($this->node)->only([ 'fqdn', 'scheme', - 'daemonListen' + 'daemonListen', ]), ]; diff --git a/app/Repositories/Daemon/FileRepository.php b/app/Repositories/Daemon/FileRepository.php index cb5f92a28..5fc2245fb 100644 --- a/app/Repositories/Daemon/FileRepository.php +++ b/app/Repositories/Daemon/FileRepository.php @@ -26,7 +26,6 @@ namespace Pterodactyl\Repositories\Daemon; use Exception; use GuzzleHttp\Client; -use Pterodactyl\Models\Node; use Pterodactyl\Models\Server; use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Repositories\HelperRepository; From 533e2bcafb95f096487aa51e82ed70dba23be825 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Thu, 2 Feb 2017 19:46:46 -0500 Subject: [PATCH 06/34] Fix database column references in ACP --- app/Http/Controllers/Admin/LocationsController.php | 4 ++-- app/Http/Controllers/Admin/NodesController.php | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/Admin/LocationsController.php b/app/Http/Controllers/Admin/LocationsController.php index 889123b43..9ef004bed 100644 --- a/app/Http/Controllers/Admin/LocationsController.php +++ b/app/Http/Controllers/Admin/LocationsController.php @@ -46,7 +46,7 @@ class LocationsController extends Controller 'locations' => Models\Location::select( 'locations.*', DB::raw('(SELECT COUNT(*) FROM nodes WHERE nodes.location = locations.id) as a_nodeCount'), - DB::raw('(SELECT COUNT(*) FROM servers WHERE servers.node IN (SELECT nodes.id FROM nodes WHERE nodes.location = locations.id)) as a_serverCount') + DB::raw('(SELECT COUNT(*) FROM servers WHERE servers.node_id IN (SELECT nodes.id FROM nodes WHERE nodes.location = locations.id)) as a_serverCount') )->paginate(20), ]); } @@ -56,7 +56,7 @@ class LocationsController extends Controller $model = Models\Location::select( 'locations.id', DB::raw('(SELECT COUNT(*) FROM nodes WHERE nodes.location = locations.id) as a_nodeCount'), - DB::raw('(SELECT COUNT(*) FROM servers WHERE servers.node IN (SELECT nodes.id FROM nodes WHERE nodes.location = locations.id)) as a_serverCount') + DB::raw('(SELECT COUNT(*) FROM servers WHERE servers.node_id IN (SELECT nodes.id FROM nodes WHERE nodes.location = locations.id)) as a_serverCount') )->where('id', $id)->first(); if (! $model) { diff --git a/app/Http/Controllers/Admin/NodesController.php b/app/Http/Controllers/Admin/NodesController.php index 506d8434e..600ecb932 100644 --- a/app/Http/Controllers/Admin/NodesController.php +++ b/app/Http/Controllers/Admin/NodesController.php @@ -57,7 +57,7 @@ class NodesController extends Controller 'nodes' => Models\Node::select( 'nodes.*', 'locations.long as a_locationName', - DB::raw('(SELECT COUNT(*) FROM servers WHERE servers.node = nodes.id) as a_serverCount') + DB::raw('(SELECT COUNT(*) FROM servers WHERE servers.node_id = nodes.id) as a_serverCount') )->join('locations', 'nodes.location', '=', 'locations.id')->paginate(20), ]); } @@ -110,8 +110,8 @@ class NodesController extends Controller 'servers' => Models\Server::select('servers.*', 'users.email as a_ownerEmail', 'services.name as a_serviceName') ->join('users', 'users.id', '=', 'servers.owner_id') ->join('services', 'services.id', '=', 'servers.service_id') - ->where('node', $id)->paginate(10, ['*'], 'servers'), - 'stats' => Models\Server::select(DB::raw('SUM(memory) as memory, SUM(disk) as disk'))->where('node', $node->id)->first(), + ->where('node_id', $id)->paginate(10, ['*'], 'servers'), + 'stats' => Models\Server::select(DB::raw('SUM(memory) as memory, SUM(disk) as disk'))->where('node_id', $node->id)->first(), 'locations' => Models\Location::all(), 'allocations' => Models\Allocation::select('allocations.*', 'servers.name as assigned_to_name') ->where('allocations.node', $node->id) From 9c2d34d6e695f2790a394601526021267265deb9 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Thu, 2 Feb 2017 19:48:13 -0500 Subject: [PATCH 07/34] Mo' StyleCI fixes... :crab: --- app/Http/Controllers/Auth/LoginController.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/Http/Controllers/Auth/LoginController.php b/app/Http/Controllers/Auth/LoginController.php index 0be73ddd2..4ce580445 100644 --- a/app/Http/Controllers/Auth/LoginController.php +++ b/app/Http/Controllers/Auth/LoginController.php @@ -156,7 +156,6 @@ class LoginController extends Controller return redirect()->route('auth.login'); } - if (! is_null($request->input('2fa_token')) && $G2FA->verifyKey($user->totp_secret, $request->input('2fa_token'), 1)) { Auth::login($user, $request->has('remember')); @@ -168,5 +167,4 @@ class LoginController extends Controller return redirect()->route('auth.login'); } } - } From 96d3aa767f297d242469319fb16f7f4161fcfd6d Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Fri, 3 Feb 2017 15:19:14 -0500 Subject: [PATCH 08/34] Model updates for Database Management in ACP --- .../Controllers/Admin/DatabaseController.php | 34 ++++++---------- app/Models/Database.php | 22 +++++++++- app/Models/DatabaseServer.php | 10 +++++ app/Models/Node.php | 39 ++++++++++++++++-- .../2017_02_03_140948_UpdateNodesTable.php | 40 +++++++++++++++++++ .../views/admin/databases/index.blade.php | 33 +++++++-------- resources/views/admin/databases/new.blade.php | 2 +- 7 files changed, 137 insertions(+), 43 deletions(-) create mode 100644 database/migrations/2017_02_03_140948_UpdateNodesTable.php diff --git a/app/Http/Controllers/Admin/DatabaseController.php b/app/Http/Controllers/Admin/DatabaseController.php index 4a7fc3631..6cfa73041 100644 --- a/app/Http/Controllers/Admin/DatabaseController.php +++ b/app/Http/Controllers/Admin/DatabaseController.php @@ -47,30 +47,18 @@ class DatabaseController extends Controller public function getIndex(Request $request) { return view('admin.databases.index', [ - 'databases' => Models\Database::select( - 'databases.*', - 'database_servers.host as a_host', - 'database_servers.port as a_port', - 'servers.id as a_serverId', - 'servers.name as a_serverName' - )->join('database_servers', 'database_servers.id', '=', 'databases.db_server') - ->join('servers', 'databases.server_id', '=', 'servers.id') - ->paginate(20), - 'dbh' => Models\DatabaseServer::select( + 'databases' => Models\Database::with('server')->paginate(50), + 'hosts' => Models\DatabaseServer::select( 'database_servers.*', - 'nodes.name as a_linkedNode', DB::raw('(SELECT COUNT(*) FROM `databases` WHERE `databases`.`db_server` = database_servers.id) as c_databases') - )->leftJoin('nodes', 'nodes.id', '=', 'database_servers.linked_node') - ->paginate(20), + )->with('node')->paginate(20), ]); } public function getNew(Request $request) { return view('admin.databases.new', [ - 'nodes' => Models\Node::select('nodes.id', 'nodes.name', 'locations.long as a_location') - ->join('locations', 'locations.id', '=', 'nodes.location') - ->get(), + 'nodes' => Models\Node::all()->load('location'), ]); } @@ -78,15 +66,17 @@ class DatabaseController extends Controller { try { $repo = new DatabaseRepository; - $repo->add($request->except([ - '_token', + $repo->add($request->only([ + 'name', + 'host', + 'port', + 'username', + 'password', + 'linked_node', ])); - Alert::success('Successfully added a new database server to the system.')->flash(); - return redirect()->route('admin.databases', [ - 'tab' => 'tab_dbservers', - ]); + return redirect()->route('admin.databases', ['tab' => 'tab_dbservers']); } catch (DisplayValidationException $ex) { return redirect()->route('admin.databases.new')->withErrors(json_decode($ex->getMessage()))->withInput(); } catch (\Exception $ex) { diff --git a/app/Models/Database.php b/app/Models/Database.php index 99e768c29..c316d6e0a 100644 --- a/app/Models/Database.php +++ b/app/Models/Database.php @@ -55,7 +55,27 @@ class Database extends Model * @var array */ protected $casts = [ - 'server' => 'integer', + 'server_id' => 'integer', 'db_server' => 'integer', ]; + + /** + * Gets the host database server associated with a database. + * + * @return \Illuminate\Database\Eloquent\Relations\HasOne + */ + public function host() + { + return $this->hasOne(DatabaseServer::class, 'id', 'db_server'); + } + + /** + * Gets the server associated with a database. + * + * @return \Illuminate\Database\Eloquent\Relations\HasOne + */ + public function server() + { + return $this->hasOne(Server::class, 'id', 'server_id'); + } } diff --git a/app/Models/DatabaseServer.php b/app/Models/DatabaseServer.php index 74fd88694..7713f59bf 100644 --- a/app/Models/DatabaseServer.php +++ b/app/Models/DatabaseServer.php @@ -59,4 +59,14 @@ class DatabaseServer extends Model 'server_id' => 'integer', 'db_server' => 'integer', ]; + + /** + * Gets the node associated with a database host. + * + * @return \Illuminate\Database\Eloquent\Relations\HasOne + */ + public function node() + { + return $this->hasOne(Node::class, 'id', 'linked_node'); + } } diff --git a/app/Models/Node.php b/app/Models/Node.php index 8e82ef935..0c4d8fb3c 100644 --- a/app/Models/Node.php +++ b/app/Models/Node.php @@ -53,7 +53,7 @@ class Node extends Model */ protected $casts = [ 'public' => 'integer', - 'location' => 'integer', + 'location_id' => 'integer', 'memory' => 'integer', 'disk' => 'integer', 'daemonListen' => 'integer', @@ -61,11 +61,34 @@ class Node extends Model ]; /** - * Fields that are not mass assignable. + * Fields that are mass assignable. * * @var array */ - protected $guarded = ['id', 'created_at', 'updated_at']; + protected $fillable = [ + 'uuid', + 'uuidShort', + 'node_id', + 'name', + 'suspended', + 'owner_id', + 'memory', + 'swap', + 'disk', + 'io', + 'cpu', + 'oom_disabled', + 'allocation_id', + 'service_id', + 'option_id', + 'pack_id', + 'startup', + 'daemonSecret', + 'image', + 'username', + 'sftp_password', + 'installed', + ]; /** * @var array @@ -193,4 +216,14 @@ class Node extends Model return json_encode($config, $json_options); } + + /** + * Gets the location associated with a node. + * + * @return \Illuminate\Database\Eloquent\Relations\HasOne + */ + public function location() + { + return $this->hasOne(Location::class, 'id', 'location_id'); + } } diff --git a/database/migrations/2017_02_03_140948_UpdateNodesTable.php b/database/migrations/2017_02_03_140948_UpdateNodesTable.php new file mode 100644 index 000000000..4408e612b --- /dev/null +++ b/database/migrations/2017_02_03_140948_UpdateNodesTable.php @@ -0,0 +1,40 @@ +dropForeign('nodes_location_foreign'); + $table->dropIndex('nodes_location_foreign'); + + $table->renameColumn('location', 'location_id'); + $table->foreign('location_id')->references('id')->on('locations'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('nodes', function (Blueprint $table) { + $table->dropForeign('nodes_location_id_foreign'); + $table->dropIndex('nodes_location_id_foreign'); + + $table->renameColumn('location_id', 'location'); + $table->foreign('location')->references('id')->on('locations'); + }); + } +} diff --git a/resources/views/admin/databases/index.blade.php b/resources/views/admin/databases/index.blade.php index f45e70192..bb7be1e3f 100644 --- a/resources/views/admin/databases/index.blade.php +++ b/resources/views/admin/databases/index.blade.php @@ -52,20 +52,21 @@ - @foreach($databases as $db) + @foreach($databases as $database) - {{ $db->a_serverName }} - {{ $db->database }} - {{ $db->username }} ({{ $db->remote }}) - {{ $db->a_host }}:{{ $db->a_port }} - - + {{ $database->server->name }} + {{ $database->database }} + {{ $database->username }} ({{ $database->remote }}) + where('id', $database->db_server)->first(); ?> + {{ $host->host }}:{{ $host->port }} + + @endforeach
- {{ $databases->appends('tab', 'tab_databases')->render() }} + {{ $databases->appends(['tab' => 'tab_databases'])->links() }}
@@ -86,20 +87,20 @@ - @foreach($dbh as $db) + @foreach($hosts as $database) - {{ $db->name }} - {{ $db->host }}:{{ $db->port }} - {{ $db->username }} - {{ $db->c_databases }} - @if(is_null($db->a_linkedNode))unlinked@else{{ $db->a_linkedNode }}@endif - + {{ $database->name }} + {{ $database->host }}:{{ $database->port }} + {{ $database->username }} + {{ $database->c_databases }} + @if(is_null($database->node))unlinked@else{{ $database->node->name }}@endif + @endforeach
- {{ $dbh->appends('tab', 'tab_dbservers')->render() }} + {{ $hosts->appends('tab', 'tab_dbservers')->render() }}
diff --git a/resources/views/admin/databases/new.blade.php b/resources/views/admin/databases/new.blade.php index 24ff27007..c9625cd75 100644 --- a/resources/views/admin/databases/new.blade.php +++ b/resources/views/admin/databases/new.blade.php @@ -47,7 +47,7 @@

A linked node implies that this Database Server is running on that node and it will be auto-selected when adding a database to servers on that node.

From 09d23deed69edf6582e814d459e50682833c6852 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Fri, 3 Feb 2017 16:50:28 -0500 Subject: [PATCH 09/34] New models for node and location admin pages. --- .../Controllers/Admin/LocationsController.php | 16 ++--- .../Controllers/Admin/NodesController.php | 71 +++++++++++-------- app/Models/Allocation.php | 4 +- app/Models/Node.php | 20 ++++++ app/Models/Server.php | 14 +++- app/Repositories/LocationRepository.php | 18 ++--- app/Repositories/NodeRepository.php | 12 +--- .../2017_02_03_155554_RenameColumns.php | 48 +++++++++++++ resources/views/admin/nodes/index.blade.php | 4 +- resources/views/admin/nodes/view.blade.php | 23 +++--- 10 files changed, 152 insertions(+), 78 deletions(-) create mode 100644 database/migrations/2017_02_03_155554_RenameColumns.php diff --git a/app/Http/Controllers/Admin/LocationsController.php b/app/Http/Controllers/Admin/LocationsController.php index 9ef004bed..93a45fb66 100644 --- a/app/Http/Controllers/Admin/LocationsController.php +++ b/app/Http/Controllers/Admin/LocationsController.php @@ -45,8 +45,8 @@ class LocationsController extends Controller return view('admin.locations.index', [ 'locations' => Models\Location::select( 'locations.*', - DB::raw('(SELECT COUNT(*) FROM nodes WHERE nodes.location = locations.id) as a_nodeCount'), - DB::raw('(SELECT COUNT(*) FROM servers WHERE servers.node_id IN (SELECT nodes.id FROM nodes WHERE nodes.location = locations.id)) as a_serverCount') + DB::raw('(SELECT COUNT(*) FROM nodes WHERE nodes.location_id = locations.id) as a_nodeCount'), + DB::raw('(SELECT COUNT(*) FROM servers WHERE servers.node_id IN (SELECT nodes.id FROM nodes WHERE nodes.location_id = locations.id)) as a_serverCount') )->paginate(20), ]); } @@ -55,8 +55,8 @@ class LocationsController extends Controller { $model = Models\Location::select( 'locations.id', - DB::raw('(SELECT COUNT(*) FROM nodes WHERE nodes.location = locations.id) as a_nodeCount'), - DB::raw('(SELECT COUNT(*) FROM servers WHERE servers.node_id IN (SELECT nodes.id FROM nodes WHERE nodes.location = locations.id)) as a_serverCount') + DB::raw('(SELECT COUNT(*) FROM nodes WHERE nodes.location_id = locations.id) as a_nodeCount'), + DB::raw('(SELECT COUNT(*) FROM servers WHERE servers.node_id IN (SELECT nodes.id FROM nodes WHERE nodes.location_id = locations.id)) as a_serverCount') )->where('id', $id)->first(); if (! $model) { @@ -80,12 +80,12 @@ class LocationsController extends Controller { try { $location = new LocationRepository; - $location->edit($id, $request->all()); + $location->edit($id, $request->only(['long', 'short'])); return response('', 204); } catch (DisplayValidationException $ex) { return response()->json([ - 'error' => 'There was a validation error while processing this request. Location descriptions must be between 1 and 255 characters, and the location code must be between 1 and 10 characters with no spaces or special characters.', + 'error' => 'There was a validation error while processing this request. Location descriptions must be between 1 and 255 characters, and the location code must be between 1 and 20 characters with no spaces or special characters.', ], 422); } catch (\Exception $ex) { // This gets caught and processed into JSON anyways. @@ -97,9 +97,7 @@ class LocationsController extends Controller { try { $location = new LocationRepository; - $id = $location->create($request->except([ - '_token', - ])); + $id = $location->create($request->only(['long', 'short'])); Alert::success('New location successfully added.')->flash(); return redirect()->route('admin.locations'); diff --git a/app/Http/Controllers/Admin/NodesController.php b/app/Http/Controllers/Admin/NodesController.php index 600ecb932..9cfac1fde 100644 --- a/app/Http/Controllers/Admin/NodesController.php +++ b/app/Http/Controllers/Admin/NodesController.php @@ -48,17 +48,15 @@ class NodesController extends Controller public function getScript(Request $request, $id) { - return response()->view('admin.nodes.remote.deploy', ['node' => Models\Node::findOrFail($id)])->header('Content-Type', 'text/plain'); + return response()->view('admin.nodes.remote.deploy', [ + 'node' => Models\Node::findOrFail($id), + ])->header('Content-Type', 'text/plain'); } public function getIndex(Request $request) { return view('admin.nodes.index', [ - 'nodes' => Models\Node::select( - 'nodes.*', - 'locations.long as a_locationName', - DB::raw('(SELECT COUNT(*) FROM servers WHERE servers.node_id = nodes.id) as a_serverCount') - )->join('locations', 'nodes.location', '=', 'locations.id')->paginate(20), + 'nodes' => Models\Node::with('location')->withCount('servers')->paginate(20), ]); } @@ -78,15 +76,25 @@ class NodesController extends Controller public function postNew(Request $request) { try { - $node = new NodeRepository; - $new = $node->create($request->except([ - '_token', + $repo = new NodeRepository; + $node = $repo->create($request->only([ + 'name', + 'location', + 'public', + 'fqdn', + 'scheme', + 'memory', + 'memory_overallocate', + 'disk', + 'disk_overallocate', + 'daemonBase', + 'daemonSFTP', + 'daemonListen', ])); - Alert::success('Successfully created new node. Before you can add any servers you need to first assign some IP addresses and ports.')->flash(); - Alert::info('To simplify the node setup you can generate a token on the configuration tab.')->flash(); + Alert::success('Successfully created new node that can be configured automatically on your remote machine by visiting the configuration tab. Before you can add any servers you need to first assign some IP addresses and ports.')->flash(); return redirect()->route('admin.nodes.view', [ - 'id' => $new, + 'id' => $node->id, 'tab' => 'tab_allocation', ]); } catch (DisplayValidationException $e) { @@ -103,26 +111,18 @@ class NodesController extends Controller public function getView(Request $request, $id) { - $node = Models\Node::findOrFail($id); + $node = Models\Node::with( + 'servers.user', + 'servers.service', + 'servers.allocations', + 'location' + )->findOrFail($id); + $node->setRelation('allocations', $node->allocations()->paginate(40)); return view('admin.nodes.view', [ 'node' => $node, - 'servers' => Models\Server::select('servers.*', 'users.email as a_ownerEmail', 'services.name as a_serviceName') - ->join('users', 'users.id', '=', 'servers.owner_id') - ->join('services', 'services.id', '=', 'servers.service_id') - ->where('node_id', $id)->paginate(10, ['*'], 'servers'), 'stats' => Models\Server::select(DB::raw('SUM(memory) as memory, SUM(disk) as disk'))->where('node_id', $node->id)->first(), 'locations' => Models\Location::all(), - 'allocations' => Models\Allocation::select('allocations.*', 'servers.name as assigned_to_name') - ->where('allocations.node', $node->id) - ->leftJoin('servers', 'servers.id', '=', 'allocations.assigned_to') - ->orderBy('allocations.ip', 'asc') - ->orderBy('allocations.port', 'asc') - ->paginate(20, ['*'], 'allocations'), - 'allocation_ips' => Models\Allocation::select('id', 'ip') - ->where('node', $node->id) - ->groupBy('ip') - ->get(), ]); } @@ -130,8 +130,21 @@ class NodesController extends Controller { try { $node = new NodeRepository; - $node->update($id, $request->except([ - '_token', + $node->update($id, $request->only([ + 'name', + 'location', + 'public', + 'fqdn', + 'scheme', + 'memory', + 'memory_overallocate', + 'disk', + 'disk_overallocate', + 'upload_size', + 'daemonBase', + 'daemonSFTP', + 'daemonListen', + 'reset_secret', ])); Alert::success('Successfully update this node\'s information. If you changed any daemon settings you will need to restart it now.')->flash(); diff --git a/app/Models/Allocation.php b/app/Models/Allocation.php index 10308ebdd..34b6baec5 100644 --- a/app/Models/Allocation.php +++ b/app/Models/Allocation.php @@ -48,8 +48,8 @@ class Allocation extends Model * @var array */ protected $casts = [ - 'node' => 'integer', + 'node_id' => 'integer', 'port' => 'integer', - 'assigned_to' => 'integer', + 'server_id' => 'integer', ]; } diff --git a/app/Models/Node.php b/app/Models/Node.php index 0c4d8fb3c..2be35568e 100644 --- a/app/Models/Node.php +++ b/app/Models/Node.php @@ -226,4 +226,24 @@ class Node extends Model { return $this->hasOne(Location::class, 'id', 'location_id'); } + + /** + * Gets the servers associated with a node. + * + * @return \Illuminate\Database\Eloquent\Relations\HasMany + */ + public function servers() + { + return $this->hasMany(Server::class); + } + + /** + * Gets the allocations associated with a node. + * + * @return \Illuminate\Database\Eloquent\Relations\HasMany + */ + public function allocations() + { + return $this->hasMany(Allocation::class); + } } diff --git a/app/Models/Server.php b/app/Models/Server.php index db8ba6381..f7e8bdbed 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -121,7 +121,7 @@ class Server extends Model 'services.name as a_serviceName', 'service_options.name as a_serviceOptionName' )->join('nodes', 'servers.node_id', '=', 'nodes.id') - ->join('locations', 'nodes.location', '=', 'locations.id') + ->join('locations', 'nodes.location_id', '=', 'locations.id') ->join('services', 'servers.service_id', '=', 'services.id') ->join('service_options', 'servers.option_id', '=', 'service_options.id') ->join('allocations', 'servers.allocation_id', '=', 'allocations.id'); @@ -218,6 +218,16 @@ class Server extends Model return Javascript::put($response); } + /** + * Gets the user who owns the server. + * + * @return \Illuminate\Database\Eloquent\Relations\HasOne + */ + public function user() + { + return $this->hasOne(User::class, 'id', 'owner_id'); + } + /** * Gets all allocations associated with this server. * @@ -225,7 +235,7 @@ class Server extends Model */ public function allocations() { - return $this->hasMany(Allocation::class, 'assigned_to'); + return $this->hasMany(Allocation::class, 'server_id'); } /** diff --git a/app/Repositories/LocationRepository.php b/app/Repositories/LocationRepository.php index 79196ff35..e25446d31 100644 --- a/app/Repositories/LocationRepository.php +++ b/app/Repositories/LocationRepository.php @@ -44,7 +44,7 @@ class LocationRepository public function create(array $data) { $validator = Validator::make($data, [ - 'short' => 'required|regex:/^[a-z0-9_.-]{1,10}$/i|unique:locations,short', + 'short' => 'required|regex:/^[\w.-]{1,20}$/i|unique:locations,short', 'long' => 'required|string|min:1|max:255', ]); @@ -73,9 +73,11 @@ class LocationRepository */ public function edit($id, array $data) { + $location = Models\Location::findOrFail($id); + $validator = Validator::make($data, [ - 'short' => 'regex:/^[a-z0-9_.-]{1,10}$/i', - 'long' => 'string|min:1|max:255', + 'short' => 'required|regex:/^[\w.-]{1,20}$/i|unique:locations,short,' . $location->id, + 'long' => 'required|string|min:1|max:255', ]); // Run validator, throw catchable and displayable exception if it fails. @@ -84,15 +86,7 @@ class LocationRepository throw new DisplayValidationException($validator->errors()); } - $location = Models\Location::findOrFail($id); - - if (isset($data['short'])) { - $location->short = $data['short']; - } - - if (isset($data['long'])) { - $location->long = $data['long']; - } + $location->fill($data); return $location->save(); } diff --git a/app/Repositories/NodeRepository.php b/app/Repositories/NodeRepository.php index d55446196..b9ba82e4b 100644 --- a/app/Repositories/NodeRepository.php +++ b/app/Repositories/NodeRepository.php @@ -65,7 +65,7 @@ class NodeRepository // Verify the FQDN if using SSL if (filter_var($data['fqdn'], FILTER_VALIDATE_IP) && $data['scheme'] === 'https') { - throw new DisplayException('A fully qualified domain name is required to use secure comunication on this node.'); + throw new DisplayException('A fully qualified domain name is required to use a secure comunication method on this node.'); } // Verify FQDN is resolvable, or if not using SSL that the IP is valid. @@ -86,7 +86,7 @@ class NodeRepository $node->fill($data); $node->save(); - return $node->id; + return $node; } public function update($id, array $data) @@ -152,18 +152,12 @@ class NodeRepository $oldDaemonKey = $node->daemonSecret; $node->update($data); try { - $client = Models\Node::guzzleRequest($node->id); - $client->request('PATCH', '/config', [ - 'headers' => [ - 'X-Access-Token' => $oldDaemonKey, - ], + $node->guzzleClient(['X-Access-Token' => $oldDaemonKey])->request('PATCH', '/config', [ 'json' => [ 'web' => [ 'listen' => $node->daemonListen, 'ssl' => [ 'enabled' => ($node->scheme === 'https'), - 'certificate' => '/etc/letsencrypt/live/' . $node->fqdn . '/fullchain.pem', - 'key' => '/etc/letsencrypt/live/' . $node->fqdn . '/privkey.pem', ], ], 'sftp' => [ diff --git a/database/migrations/2017_02_03_155554_RenameColumns.php b/database/migrations/2017_02_03_155554_RenameColumns.php new file mode 100644 index 000000000..519ccc826 --- /dev/null +++ b/database/migrations/2017_02_03_155554_RenameColumns.php @@ -0,0 +1,48 @@ +dropForeign('allocations_node_foreign'); + $table->dropForeign('allocations_assigned_to_foreign'); + $table->dropIndex('allocations_node_foreign'); + $table->dropIndex('allocations_assigned_to_foreign'); + + $table->renameColumn('node', 'node_id'); + $table->renameColumn('assigned_to', 'server_id'); + $table->foreign('node_id')->references('id')->on('nodes'); + $table->foreign('server_id')->references('id')->on('servers'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('allocations', function (Blueprint $table) { + $table->dropForeign('allocations_node_id_foreign'); + $table->dropForeign('allocations_server_id_foreign'); + $table->dropIndex('allocations_node_id_foreign'); + $table->dropIndex('allocations_server_id_foreign'); + + $table->renameColumn('node_id', 'node'); + $table->renameColumn('server_id', 'assigned_to'); + $table->foreign('node')->references('id')->on('nodes'); + $table->foreign('assigned_to')->references('id')->on('servers'); + }); + } +} diff --git a/resources/views/admin/nodes/index.blade.php b/resources/views/admin/nodes/index.blade.php index 29546d4ea..de1687087 100644 --- a/resources/views/admin/nodes/index.blade.php +++ b/resources/views/admin/nodes/index.blade.php @@ -53,10 +53,10 @@ {{ $node->name }} - {{ $node->a_locationName }} + {{ $node->location->short }} {{ $node->memory }} MB {{ $node->disk }} MB - {{ $node->a_serverCount }} + {{ $node->servers_count }} diff --git a/resources/views/admin/nodes/view.blade.php b/resources/views/admin/nodes/view.blade.php index 8485c9a50..5fa054703 100644 --- a/resources/views/admin/nodes/view.blade.php +++ b/resources/views/admin/nodes/view.blade.php @@ -60,7 +60,7 @@
  • Configuration
  • Allocation
  • Servers
  • - @if(count($servers) === 0)
  • Delete
  • @endif + @if(count($node->servers) === 0)
  • Delete
  • @endif
    @@ -83,7 +83,7 @@ Total Servers - {{ count($servers) }} + {{ count($node->servers) }} Memory Allocated @@ -309,7 +309,7 @@
    @@ -355,7 +355,7 @@ - @foreach($allocations as $allocation) + @foreach($node->allocations as $allocation) {{ $allocation->ip }} @@ -376,7 +376,7 @@
    - {{ $allocations->appends(['tab' => 'tab_allocation'])->links() }} + {{ $node->allocations->appends(['tab' => 'tab_allocation'])->render() }}
    @@ -402,11 +402,11 @@ - @foreach($servers as $server) + @foreach($node->servers as $server) {{ $server->name }} - {{ $server->a_ownerEmail }} - {{ $server->a_serviceName }} + {{ $server->user->email }} + {{ $server->service->name }} -- / {{ $server->memory === 0 ? '∞' : $server->memory }} MB {{ $server->disk }} MB -- % @@ -415,13 +415,10 @@ @endforeach -
    -
    {!! $servers->appends(['tab' => 'tab_servers'])->render() !!}
    -
    - @if(count($servers) === 0) + @if(count($node->servers) === 0)
    @@ -459,7 +456,7 @@
    From 323f1d943f17250b7ed2ab1506869b4bc479c93e Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sun, 5 Feb 2017 17:58:17 -0500 Subject: [PATCH 10/34] Completed model updates for Services --- .../Controllers/API/ServiceController.php | 2 +- app/Http/Controllers/Admin/PackController.php | 80 ++++++------------- .../Controllers/Admin/ServersController.php | 4 +- .../Controllers/Admin/ServiceController.php | 71 ++++++++-------- .../Controllers/Server/ServerController.php | 2 +- app/Models/Service.php | 35 +++++++- app/Models/ServiceOptions.php | 24 +++++- app/Models/ServicePack.php | 10 +++ app/Models/ServiceVariables.php | 5 ++ app/Observers/ServerObserver.php | 2 +- app/Repositories/ServerRepository.php | 4 +- app/Repositories/ServiceRepository/Option.php | 2 +- app/Repositories/ServiceRepository/Pack.php | 31 ++++--- .../ServiceRepository/Service.php | 15 ++-- .../ServiceRepository/Variable.php | 28 +++++-- .../2017_02_05_164123_AdjustColumnNames.php | 40 ++++++++++ ...64516_AdjustColumnNamesForServicePacks.php | 40 ++++++++++ .../views/admin/services/index.blade.php | 2 +- .../admin/services/options/variable.blade.php | 6 +- .../admin/services/options/view.blade.php | 23 +++--- .../admin/services/packs/byoption.blade.php | 4 +- .../admin/services/packs/byservice.blade.php | 4 +- .../views/admin/services/packs/edit.blade.php | 12 +-- .../views/admin/services/packs/new.blade.php | 8 +- .../admin/services/packs/upload.blade.php | 8 +- resources/views/admin/services/view.blade.php | 4 +- 26 files changed, 299 insertions(+), 167 deletions(-) create mode 100644 database/migrations/2017_02_05_164123_AdjustColumnNames.php create mode 100644 database/migrations/2017_02_05_164516_AdjustColumnNamesForServicePacks.php diff --git a/app/Http/Controllers/API/ServiceController.php b/app/Http/Controllers/API/ServiceController.php index 257ace766..81d5a955d 100755 --- a/app/Http/Controllers/API/ServiceController.php +++ b/app/Http/Controllers/API/ServiceController.php @@ -53,7 +53,7 @@ class ServiceController extends BaseController return [ 'service' => $service, 'options' => Models\ServiceOptions::select('id', 'name', 'description', 'tag', 'docker_image') - ->where('parent_service', $service->id) + ->where('service_id', $service->id) ->with('variables') ->with('packs') ->get(), diff --git a/app/Http/Controllers/Admin/PackController.php b/app/Http/Controllers/Admin/PackController.php index 628cd54b8..3e6685f28 100644 --- a/app/Http/Controllers/Admin/PackController.php +++ b/app/Http/Controllers/Admin/PackController.php @@ -42,64 +42,29 @@ class PackController extends Controller // } - protected function formatServices() - { - $options = Models\ServiceOptions::select( - 'services.name AS p_service', - 'service_options.id', - 'service_options.name' - )->join('services', 'services.id', '=', 'service_options.parent_service')->get(); - - $array = []; - foreach ($options as &$option) { - if (! array_key_exists($option->p_service, $array)) { - $array[$option->p_service] = []; - } - - $array[$option->p_service] = array_merge($array[$option->p_service], [[ - 'id' => $option->id, - 'name' => $option->name, - ]]); - } - - return $array; - } - public function listAll(Request $request) { - return view('admin.services.packs.index', [ - 'services' => Models\Service::all(), - ]); + return view('admin.services.packs.index', ['services' => Models\Service::all()]); } public function listByOption(Request $request, $id) { - $option = Models\ServiceOptions::findOrFail($id); - return view('admin.services.packs.byoption', [ - 'packs' => Models\ServicePack::where('option', $option->id)->get(), - 'service' => Models\Service::findOrFail($option->parent_service), - 'option' => $option, + 'option' => Models\ServiceOptions::with('service', 'packs')->findOrFail($id) ]); } public function listByService(Request $request, $id) { return view('admin.services.packs.byservice', [ - 'service' => Models\Service::findOrFail($id), - 'options' => Models\ServiceOptions::select( - 'service_options.id', - 'service_options.name', - DB::raw('(SELECT COUNT(id) FROM service_packs WHERE service_packs.option = service_options.id) AS p_count') - )->where('parent_service', $id)->get(), + 'service' => Models\Service::with('options', 'options.packs')->findOrFail($id), ]); } public function new(Request $request, $opt = null) { return view('admin.services.packs.new', [ - 'services' => $this->formatServices(), - 'packFor' => $opt, + 'services' => Models\Service::with('options')->get(), ]); } @@ -107,12 +72,18 @@ class PackController extends Controller { try { $repo = new Pack; - $id = $repo->create($request->except([ - '_token', + $pack = $repo->create($request->only([ + 'name', + 'version', + 'description', + 'option', + 'selectable', + 'visible', + 'file_upload', ])); Alert::success('Successfully created new service!')->flash(); - return redirect()->route('admin.services.packs.edit', $id)->withInput(); + return redirect()->route('admin.services.packs.edit', $pack->id)->withInput(); } catch (DisplayValidationException $ex) { return redirect()->route('admin.services.packs.new', $request->input('option'))->withErrors(json_decode($ex->getMessage()))->withInput(); } catch (DisplayException $ex) { @@ -127,15 +98,12 @@ class PackController extends Controller public function edit(Request $request, $id) { - $pack = Models\ServicePack::findOrFail($id); - $option = Models\ServiceOptions::select('id', 'parent_service', 'name')->where('id', $pack->option)->first(); + $pack = Models\ServicePack::with('option.service')->findOrFail($id); return view('admin.services.packs.edit', [ 'pack' => $pack, - 'services' => $this->formatServices(), + 'services' => Models\Service::all()->load('options'), 'files' => Storage::files('packs/' . $pack->uuid), - 'service' => Models\Service::findOrFail($option->parent_service), - 'option' => $option, ]); } @@ -159,8 +127,13 @@ class PackController extends Controller } else { try { $repo = new Pack; - $repo->update($id, $request->except([ - '_token', + $repo->update($id, $request->only([ + 'name', + 'version', + 'description', + 'option', + 'selectable', + 'visible', ])); Alert::success('Service pack has been successfully updated.')->flash(); } catch (DisplayValidationException $ex) { @@ -215,8 +188,7 @@ class PackController extends Controller public function uploadForm(Request $request, $for = null) { return view('admin.services.packs.upload', [ - 'services' => $this->formatServices(), - 'for' => $for, + 'services' => Models\Service::all()->load('options'), ]); } @@ -224,12 +196,10 @@ class PackController extends Controller { try { $repo = new Pack; - $id = $repo->createWithTemplate($request->except([ - '_token', - ])); + $pack = $repo->createWithTemplate($request->only(['option', 'file_upload'])); Alert::success('Successfully created new service!')->flash(); - return redirect()->route('admin.services.packs.edit', $id)->withInput(); + return redirect()->route('admin.services.packs.edit', $pack->id)->withInput(); } catch (DisplayValidationException $ex) { return redirect()->back()->withErrors(json_decode($ex->getMessage()))->withInput(); } catch (DisplayException $ex) { diff --git a/app/Http/Controllers/Admin/ServersController.php b/app/Http/Controllers/Admin/ServersController.php index f75508139..eee134d3e 100644 --- a/app/Http/Controllers/Admin/ServersController.php +++ b/app/Http/Controllers/Admin/ServersController.php @@ -244,7 +244,7 @@ class ServersController extends Controller $service = Models\Service::select('executable', 'startup')->where('id', $request->input('service'))->first(); - return response()->json(Models\ServiceOptions::select('id', 'name', 'docker_image')->where('parent_service', $request->input('service'))->orderBy('name', 'asc')->get()); + return response()->json(Models\ServiceOptions::select('id', 'name', 'docker_image')->where('service_id', $request->input('service'))->orderBy('name', 'asc')->get()); } /** @@ -264,7 +264,7 @@ class ServersController extends Controller $option = Models\ServiceOptions::select( DB::raw('COALESCE(service_options.executable, services.executable) as executable'), DB::raw('COALESCE(service_options.startup, services.startup) as startup') - )->leftJoin('services', 'services.id', '=', 'service_options.parent_service') + )->leftJoin('services', 'services.id', '=', 'service_options.service_id') ->where('service_options.id', $request->input('option')) ->first(); diff --git a/app/Http/Controllers/Admin/ServiceController.php b/app/Http/Controllers/Admin/ServiceController.php index 70de4f88e..f8951993a 100644 --- a/app/Http/Controllers/Admin/ServiceController.php +++ b/app/Http/Controllers/Admin/ServiceController.php @@ -45,10 +45,7 @@ class ServiceController extends Controller public function getIndex(Request $request) { return view('admin.services.index', [ - 'services' => Models\Service::select( - 'services.*', - DB::raw('(SELECT COUNT(*) FROM servers WHERE servers.service = services.id) as c_servers') - )->get(), + 'services' => Models\Service::withCount('servers')->get(), ]); } @@ -61,12 +58,16 @@ class ServiceController extends Controller { try { $repo = new ServiceRepository\Service; - $id = $repo->create($request->except([ - '_token', + $service = $repo->create($request->only([ + 'name', + 'description', + 'file', + 'executable', + 'startup', ])); Alert::success('Successfully created new service!')->flash(); - return redirect()->route('admin.services.service', $id); + return redirect()->route('admin.services.service', $service->id); } catch (DisplayValidationException $ex) { return redirect()->route('admin.services.new')->withErrors(json_decode($ex->getMessage()))->withInput(); } catch (DisplayException $ex) { @@ -82,11 +83,7 @@ class ServiceController extends Controller public function getService(Request $request, $service) { return view('admin.services.view', [ - 'service' => Models\Service::findOrFail($service), - 'options' => Models\ServiceOptions::select( - 'service_options.*', - DB::raw('(SELECT COUNT(*) FROM servers WHERE servers.option = service_options.id) as c_servers') - )->where('parent_service', $service)->get(), + 'service' => Models\Service::with('options', 'options.servers')->findOrFail($service), ]); } @@ -94,8 +91,12 @@ class ServiceController extends Controller { try { $repo = new ServiceRepository\Service; - $repo->update($service, $request->except([ - '_token', + $repo->update($service, $request->only([ + 'name', + 'description', + 'file', + 'executable', + 'startup', ])); Alert::success('Successfully updated this service.')->flash(); } catch (DisplayValidationException $ex) { @@ -130,16 +131,11 @@ class ServiceController extends Controller public function getOption(Request $request, $service, $option) { - $opt = Models\ServiceOptions::findOrFail($option); + $option = Models\ServiceOptions::with('service', 'variables')->findOrFail($option); + $option->setRelation('servers', $option->servers()->with('user')->paginate(25)); return view('admin.services.options.view', [ - 'service' => Models\Service::findOrFail($opt->parent_service), - 'option' => $opt, - 'variables' => Models\ServiceVariables::where('option_id', $option)->get(), - 'servers' => Models\Server::select('servers.*', 'users.email as a_ownerEmail') - ->join('users', 'users.id', '=', 'servers.owner_id') - ->where('option', $option) - ->paginate(10), + 'option' => $option, ]); } @@ -147,8 +143,13 @@ class ServiceController extends Controller { try { $repo = new ServiceRepository\Option; - $repo->update($option, $request->except([ - '_token', + $repo->update($option, $request->only([ + 'name', + 'description', + 'tag', + 'executable', + 'docker_image', + 'startup', ])); Alert::success('Option settings successfully updated.')->flash(); } catch (DisplayValidationException $ex) { @@ -164,13 +165,12 @@ class ServiceController extends Controller public function deleteOption(Request $request, $service, $option) { try { - $service = Models\ServiceOptions::select('parent_service')->where('id', $option)->first(); $repo = new ServiceRepository\Option; $repo->delete($option); Alert::success('Successfully deleted that option.')->flash(); - return redirect()->route('admin.services.service', $service->parent_service); + return redirect()->route('admin.services.service', $service); } catch (DisplayException $ex) { Alert::danger($ex->getMessage())->flash(); } catch (\Exception $ex) { @@ -218,8 +218,7 @@ class ServiceController extends Controller public function getNewVariable(Request $request, $service, $option) { return view('admin.services.options.variable', [ - 'service' => Models\Service::findOrFail($service), - 'option' => Models\ServiceOptions::where('parent_service', $service)->where('id', $option)->firstOrFail(), + 'option' => Models\ServiceOptions::with('service')->findOrFail($option), ]); } @@ -227,8 +226,15 @@ class ServiceController extends Controller { try { $repo = new ServiceRepository\Variable; - $repo->create($option, $request->except([ - '_token', + $repo->create($option, $request->only([ + 'name', + 'description', + 'env_variable', + 'default_value', + 'user_viewable', + 'user_editable', + 'required', + 'regex', ])); Alert::success('Successfully added new variable to this option.')->flash(); @@ -305,8 +311,9 @@ class ServiceController extends Controller { try { $repo = new ServiceRepository\Service; - $repo->updateFile($serviceId, $request->except([ - '_token', + $repo->updateFile($serviceId, $request->only([ + 'file', + 'contents', ])); return response('', 204); diff --git a/app/Http/Controllers/Server/ServerController.php b/app/Http/Controllers/Server/ServerController.php index 75378ddc1..12fb41ad1 100644 --- a/app/Http/Controllers/Server/ServerController.php +++ b/app/Http/Controllers/Server/ServerController.php @@ -224,7 +224,7 @@ class ServerController extends Controller $service = Models\Service::select( DB::raw('IFNULL(service_options.executable, services.executable) as executable') - )->leftJoin('service_options', 'service_options.parent_service', '=', 'services.id') + )->leftJoin('service_options', 'service_options.service_id', '=', 'services.id') ->where('service_options.id', $server->option_id) ->where('services.id', $server->service_id) ->first(); diff --git a/app/Models/Service.php b/app/Models/Service.php index 8c0f27790..d7900b632 100644 --- a/app/Models/Service.php +++ b/app/Models/Service.php @@ -40,5 +40,38 @@ class Service extends Model * * @var array */ - protected $guarded = ['id', 'created_at', 'updated_at']; + protected $fillable = ['id', 'created_at', 'updated_at']; + + /** + * Gets all service options associated with this service. + * + * @return \Illuminate\Database\Eloquent\Relations\HasMany + */ + public function options() + { + return $this->hasMany(ServiceOptions::class); + } + + /** + * Returns all of the packs associated with a service, regardless of the service option. + * + * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough + */ + public function packs() + { + return $this->hasManyThrough( + 'Pterodactyl\Models\ServicePack', 'Pterodactyl\Models\ServiceOptions', + 'service_id', 'option_id' + ); + } + + /** + * Gets all servers associated with this service. + * + * @return \Illuminate\Database\Eloquent\Relations\HasMany + */ + public function servers() + { + return $this->hasMany(Server::class); + } } diff --git a/app/Models/ServiceOptions.php b/app/Models/ServiceOptions.php index 9b4a3a7ae..9cd61a702 100644 --- a/app/Models/ServiceOptions.php +++ b/app/Models/ServiceOptions.php @@ -48,9 +48,29 @@ class ServiceOptions extends Model * @var array */ protected $casts = [ - 'parent_service' => 'integer', + 'service_id' => 'integer', ]; + /** + * Gets service associated with a service option. + * + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo + */ + public function service() + { + return $this->belongsTo(Service::class); + } + + /** + * Gets all servers associated with this service option. + * + * @return \Illuminate\Database\Eloquent\Relations\HasMany + */ + public function servers() + { + return $this->hasMany(Server::class, 'option_id'); + } + /** * Gets all variables associated with this service. * @@ -68,6 +88,6 @@ class ServiceOptions extends Model */ public function packs() { - return $this->hasMany(ServicePack::class, 'option'); + return $this->hasMany(ServicePack::class, 'option_id'); } } diff --git a/app/Models/ServicePack.php b/app/Models/ServicePack.php index cefcc0a32..7c9a624ff 100644 --- a/app/Models/ServicePack.php +++ b/app/Models/ServicePack.php @@ -56,4 +56,14 @@ class ServicePack extends Model 'selectable' => 'boolean', 'visible' => 'boolean', ]; + + /** + * Gets option associated with a service pack. + * + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo + */ + public function option() + { + return $this->belongsTo(ServiceOptions::class); + } } diff --git a/app/Models/ServiceVariables.php b/app/Models/ServiceVariables.php index 5082d714e..f088202e7 100644 --- a/app/Models/ServiceVariables.php +++ b/app/Models/ServiceVariables.php @@ -53,4 +53,9 @@ class ServiceVariables extends Model 'user_editable' => 'integer', 'required' => 'integer', ]; + + public function serverVariables() + { + return $this->hasMany(ServerVariables::class, 'variable_id'); + } } diff --git a/app/Observers/ServerObserver.php b/app/Observers/ServerObserver.php index cacb9c82f..626847aa4 100644 --- a/app/Observers/ServerObserver.php +++ b/app/Observers/ServerObserver.php @@ -62,7 +62,7 @@ class ServerObserver $user = Models\User::findOrFail($server->owner_id); $node = Models\Node::select('name')->where('id', $server->node_id)->first(); $service = Models\Service::select('services.name', 'service_options.name as optionName') - ->join('service_options', 'service_options.parent_service', '=', 'services.id') + ->join('service_options', 'service_options.service_id', '=', 'services.id') ->where('services.id', $server->service_id) ->where('service_options.id', $server->option_id) ->first(); diff --git a/app/Repositories/ServerRepository.php b/app/Repositories/ServerRepository.php index 3cd3d32ba..fe2a2f840 100644 --- a/app/Repositories/ServerRepository.php +++ b/app/Repositories/ServerRepository.php @@ -163,7 +163,7 @@ class ServerRepository // We know the service and option exists because of the validation. // We need to verify that the option exists for the service, and then check for // any required variable fields. (fields are labeled env_) - $option = Models\ServiceOptions::where('id', $data['option'])->where('parent_service', $data['service'])->first(); + $option = Models\ServiceOptions::where('id', $data['option'])->where('service_id', $data['service'])->first(); if (! $option) { throw new DisplayException('The requested service option does not exist for the specified service.'); } @@ -181,7 +181,7 @@ class ServerRepository } // Load up the Service Information - $service = Models\Service::find($option->parent_service); + $service = Models\Service::find($option->service_id); // Check those Variables $variables = Models\ServiceVariables::where('option_id', $data['option'])->get(); diff --git a/app/Repositories/ServiceRepository/Option.php b/app/Repositories/ServiceRepository/Option.php index 8ddcdc006..ddac543d6 100644 --- a/app/Repositories/ServiceRepository/Option.php +++ b/app/Repositories/ServiceRepository/Option.php @@ -63,7 +63,7 @@ class Option } $option = new Models\ServiceOptions; - $option->parent_service = $service->id; + $option->service_id = $service->id; $option->fill($data); $option->save(); diff --git a/app/Repositories/ServiceRepository/Pack.php b/app/Repositories/ServiceRepository/Pack.php index 03858ed67..c3cc2dc9c 100644 --- a/app/Repositories/ServiceRepository/Pack.php +++ b/app/Repositories/ServiceRepository/Pack.php @@ -69,8 +69,8 @@ class Pack try { $uuid = new UuidService; $pack = Models\ServicePack::create([ - 'option' => $data['option'], - 'uuid' => $uuid->generate('servers', 'uuid'), + 'option_id' => $data['option'], + 'uuid' => $uuid->generate('service_packs', 'uuid'), 'name' => $data['name'], 'version' => $data['version'], 'description' => (empty($data['description'])) ? null : $data['description'], @@ -89,7 +89,7 @@ class Pack throw $ex; } - return $pack->id; + return $pack; } public function createWithTemplate(array $data) @@ -123,7 +123,7 @@ class Pack } $json = json_decode($zip->getFromName('import.json')); - $id = $this->create([ + $pack = $this->create([ 'name' => $json->name, 'version' => $json->version, 'description' => $json->description, @@ -132,7 +132,6 @@ class Pack 'visible' => $json->visible, ]); - $pack = Models\ServicePack::findOrFail($id); if (! $zip->extractTo(storage_path('app/packs/' . $pack->uuid), 'archive.tar.gz')) { $pack->delete(); throw new DisplayException('Unable to extract the archive file to the correct location.'); @@ -140,7 +139,7 @@ class Pack $zip->close(); - return $pack->id; + return $pack; } else { $json = json_decode(file_get_contents($data['file_upload']->path())); @@ -170,18 +169,16 @@ class Pack throw new DisplayValidationException($validator->errors()); } - DB::transaction(function () use ($id, $data) { - Models\ServicePack::findOrFail($id)->update([ - 'option' => $data['option'], - 'name' => $data['name'], - 'version' => $data['version'], - 'description' => (empty($data['description'])) ? null : $data['description'], - 'selectable' => isset($data['selectable']), - 'visible' => isset($data['visible']), - ]); + Models\ServicePack::findOrFail($id)->update([ + 'option_id' => $data['option'], + 'name' => $data['name'], + 'version' => $data['version'], + 'description' => (empty($data['description'])) ? null : $data['description'], + 'selectable' => isset($data['selectable']), + 'visible' => isset($data['visible']), + ]); - return true; - }); + return; } public function delete($id) diff --git a/app/Repositories/ServiceRepository/Service.php b/app/Repositories/ServiceRepository/Service.php index eb41a9fee..fd6ffe629 100644 --- a/app/Repositories/ServiceRepository/Service.php +++ b/app/Repositories/ServiceRepository/Service.php @@ -55,23 +55,18 @@ class Service $data['author'] = env('SERVICE_AUTHOR', (string) Uuid::generate(4)); - $service = new Models\Service; DB::beginTransaction(); - try { - $service->fill($data); - $service->save(); - - Storage::put('services/' . $data['file'] . '/main.json', '{}'); - Storage::copy('services/.templates/index.js', 'services/' . $data['file'] . '/index.js'); - + $service = Models\Service::create($data); + Storage::put('services/' . $service->file . '/main.json', '{}'); + Storage::copy('services/.templates/index.js', 'services/' . $service->file . '/index.js'); DB::commit(); } catch (\Exception $ex) { DB::rollBack(); throw $ex; } - return $service->id; + return $service; } public function update($id, array $data) @@ -99,7 +94,7 @@ class Service { $service = Models\Service::findOrFail($id); $servers = Models\Server::where('service', $service->id)->get(); - $options = Models\ServiceOptions::select('id')->where('parent_service', $service->id); + $options = Models\ServiceOptions::select('id')->where('service_id', $service->id); if (count($servers) !== 0) { throw new DisplayException('You cannot delete a service that has servers associated with it.'); diff --git a/app/Repositories/ServiceRepository/Variable.php b/app/Repositories/ServiceRepository/Variable.php index fbd258444..bc7ab4c13 100644 --- a/app/Repositories/ServiceRepository/Variable.php +++ b/app/Repositories/ServiceRepository/Variable.php @@ -39,7 +39,7 @@ class Variable public function create($id, array $data) { - $option = Models\ServiceOptions::findOrFail($id); + $option = Models\ServiceOptions::select('id')->findOrFail($id); $validator = Validator::make($data, [ 'name' => 'required|string|min:1|max:255', @@ -67,21 +67,22 @@ class Variable $data['user_viewable'] = (isset($data['user_viewable']) && in_array((int) $data['user_viewable'], [0, 1])) ? $data['user_viewable'] : 0; $data['user_editable'] = (isset($data['user_editable']) && in_array((int) $data['user_editable'], [0, 1])) ? $data['user_editable'] : 0; $data['required'] = (isset($data['required']) && in_array((int) $data['required'], [0, 1])) ? $data['required'] : 0; + $data['option_id'] = $option->id; - $variable = new Models\ServiceVariables; - $variable->option_id = $option->id; - $variable->fill($data); + $variable = Models\ServiceVariables::create($data); - return $variable->save(); + return $variable; } public function delete($id) { - $variable = Models\ServiceVariables::findOrFail($id); + $variable = Models\ServiceVariables::with('serverVariables')->findOrFail($id); DB::beginTransaction(); try { - Models\ServerVariables::where('variable_id', $variable->id)->delete(); + foreach($variable->serverVariables as $svar) { + $svar->delete(); + } $variable->delete(); DB::commit(); @@ -125,7 +126,18 @@ class Variable $data['user_editable'] = (isset($data['user_editable']) && in_array((int) $data['user_editable'], [0, 1])) ? $data['user_editable'] : $variable->user_editable; $data['required'] = (isset($data['required']) && in_array((int) $data['required'], [0, 1])) ? $data['required'] : $variable->required; - $variable->fill($data); + // Not using $data because the function that passes into this function + // can't do $requst->only() due to the page setup. + $variable->fill([ + 'name' => $data['name'], + 'description' => $data['description'], + 'env_variable' => $data['env_variable'], + 'default_value' => $data['default_value'], + 'user_viewable' => $data['user_viewable'], + 'user_editable' => $data['user_editable'], + 'required' => $data['required'], + 'regex' => $data['regex'], + ]); return $variable->save(); } diff --git a/database/migrations/2017_02_05_164123_AdjustColumnNames.php b/database/migrations/2017_02_05_164123_AdjustColumnNames.php new file mode 100644 index 000000000..ddb37b891 --- /dev/null +++ b/database/migrations/2017_02_05_164123_AdjustColumnNames.php @@ -0,0 +1,40 @@ +dropForeign('service_options_parent_service_foreign'); + $table->dropIndex('service_options_parent_service_foreign'); + + $table->renameColumn('parent_service', 'service_id'); + $table->foreign('service_id')->references('id')->on('services'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('service_options', function (Blueprint $table) { + $table->dropForeign('service_options_service_id_foreign'); + $table->dropIndex('service_options_service_id_foreign'); + + $table->renameColumn('service_id', 'parent_service'); + $table->foreign('parent_service')->references('id')->on('services'); + }); + } +} diff --git a/database/migrations/2017_02_05_164516_AdjustColumnNamesForServicePacks.php b/database/migrations/2017_02_05_164516_AdjustColumnNamesForServicePacks.php new file mode 100644 index 000000000..5e57ffef3 --- /dev/null +++ b/database/migrations/2017_02_05_164516_AdjustColumnNamesForServicePacks.php @@ -0,0 +1,40 @@ +dropForeign('service_packs_option_foreign'); + $table->dropIndex('service_packs_option_foreign'); + + $table->renameColumn('option', 'option_id'); + $table->foreign('option_id')->references('id')->on('service_options'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('service_packs', function (Blueprint $table) { + $table->dropForeign('service_packs_option_id_foreign'); + $table->dropIndex('service_packs_option_id_foreign'); + + $table->renameColumn('option_id', 'option'); + $table->foreign('option')->references('id')->on('service_options'); + }); + } +} diff --git a/resources/views/admin/services/index.blade.php b/resources/views/admin/services/index.blade.php index 453f1582f..c97df4cf1 100644 --- a/resources/views/admin/services/index.blade.php +++ b/resources/views/admin/services/index.blade.php @@ -44,7 +44,7 @@ {{ $service->name }} {!! $service->description !!} - {{ $service->c_servers }} + {{ $service->servers_count }} @endforeach diff --git a/resources/views/admin/services/options/variable.blade.php b/resources/views/admin/services/options/variable.blade.php index 2c8ff8957..5ed9540c3 100644 --- a/resources/views/admin/services/options/variable.blade.php +++ b/resources/views/admin/services/options/variable.blade.php @@ -28,12 +28,12 @@

    New Option Variable


    -
    +
    diff --git a/resources/views/admin/services/options/view.blade.php b/resources/views/admin/services/options/view.blade.php index e3eb61a12..f40bc8154 100644 --- a/resources/views/admin/services/options/view.blade.php +++ b/resources/views/admin/services/options/view.blade.php @@ -28,12 +28,12 @@
    Warning! This page contains advanced settings that the panel and daemon use to control servers. Modifying information on this page is not recommended unless you are absolutely sure of what you are doing.

    Settings


    - +
    @@ -74,7 +74,7 @@
    - +

    To use the default startup of the parent service simply leave this field blank.

    @@ -88,9 +88,9 @@
    -

    Variables


    - @foreach($variables as $variable) -
    +

    Variables


    + @foreach($option->variables as $variable) +
    @@ -158,7 +158,7 @@
    {!! csrf_field() !!} - +
    @@ -175,16 +175,19 @@ - @foreach ($servers as $server) + @foreach ($option->servers as $server) {{ $server->name }} - {{ $server->a_ownerEmail }} + {{ $server->user->email }} {{ $server->updated_at }} @endforeach - +
    + {!! $option->servers->render() !!} +
    +
    diff --git a/resources/views/admin/services/packs/byoption.blade.php b/resources/views/admin/services/packs/byoption.blade.php index 23dbf2951..f781a3a3c 100644 --- a/resources/views/admin/services/packs/byoption.blade.php +++ b/resources/views/admin/services/packs/byoption.blade.php @@ -29,7 +29,7 @@
  • Admin Control
  • Services
  • Packs
  • -
  • {{ $service->name }}
  • +
  • {{ $option->service->name }}
  • {{ $option->name }}
  • Service Packs


    @@ -44,7 +44,7 @@ - @foreach ($packs as $pack) + @foreach ($option->packs as $pack) {{ $pack->name }} {{ $pack->version }} diff --git a/resources/views/admin/services/packs/byservice.blade.php b/resources/views/admin/services/packs/byservice.blade.php index 8475de289..f36ce4e4c 100644 --- a/resources/views/admin/services/packs/byservice.blade.php +++ b/resources/views/admin/services/packs/byservice.blade.php @@ -40,10 +40,10 @@ - @foreach ($options as $option) + @foreach ($service->options as $option) {{ $option->name }} - {{ $option->p_count }} + {{ $option->packs->count() }} @endforeach diff --git a/resources/views/admin/services/packs/edit.blade.php b/resources/views/admin/services/packs/edit.blade.php index dcb67d5df..cc6470b3b 100644 --- a/resources/views/admin/services/packs/edit.blade.php +++ b/resources/views/admin/services/packs/edit.blade.php @@ -29,8 +29,8 @@
  • Admin Control
  • Services
  • Packs
  • -
  • {{ $service->name }}
  • -
  • {{ $option->name }}
  • +
  • {{ $pack->option->service->name }}
  • +
  • {{ $pack->option->name }}
  • {{ $pack->name }} ({{ $pack->version }})
  • Manage Service Pack


    @@ -62,10 +62,10 @@
    diff --git a/resources/views/admin/services/packs/new.blade.php b/resources/views/admin/services/packs/new.blade.php index 82601bc62..d5b0440da 100644 --- a/resources/views/admin/services/packs/new.blade.php +++ b/resources/views/admin/services/packs/new.blade.php @@ -59,10 +59,10 @@
    diff --git a/resources/views/admin/services/packs/upload.blade.php b/resources/views/admin/services/packs/upload.blade.php index e9ca020fa..cb5e548d8 100644 --- a/resources/views/admin/services/packs/upload.blade.php +++ b/resources/views/admin/services/packs/upload.blade.php @@ -12,10 +12,10 @@
    diff --git a/resources/views/admin/services/view.blade.php b/resources/views/admin/services/view.blade.php index 887478aca..6b660a5ac 100644 --- a/resources/views/admin/services/view.blade.php +++ b/resources/views/admin/services/view.blade.php @@ -41,12 +41,12 @@ - @foreach($options as $option) + @foreach($service->options as $option) {{ $option->name }} {!! $option->description !!} {{ $option->tag }} - {{ $option->c_servers }} + {{ $option->servers->count() }} @endforeach From b1389262e2c333d740e889e0d96baf3651133db1 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sun, 5 Feb 2017 18:00:39 -0500 Subject: [PATCH 11/34] Use belongsTo versus hasOne when more logical. --- app/Models/Database.php | 8 ++++---- app/Models/DatabaseServer.php | 4 ++-- app/Models/Node.php | 4 ++-- app/Models/Server.php | 16 ++++++++-------- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/app/Models/Database.php b/app/Models/Database.php index c316d6e0a..b5e9d39ad 100644 --- a/app/Models/Database.php +++ b/app/Models/Database.php @@ -62,20 +62,20 @@ class Database extends Model /** * Gets the host database server associated with a database. * - * @return \Illuminate\Database\Eloquent\Relations\HasOne + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function host() { - return $this->hasOne(DatabaseServer::class, 'id', 'db_server'); + return $this->belongsTo(DatabaseServer::class, 'db_server'); } /** * Gets the server associated with a database. * - * @return \Illuminate\Database\Eloquent\Relations\HasOne + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function server() { - return $this->hasOne(Server::class, 'id', 'server_id'); + return $this->belongsTo(Server::class); } } diff --git a/app/Models/DatabaseServer.php b/app/Models/DatabaseServer.php index 7713f59bf..c71d1b8d7 100644 --- a/app/Models/DatabaseServer.php +++ b/app/Models/DatabaseServer.php @@ -63,10 +63,10 @@ class DatabaseServer extends Model /** * Gets the node associated with a database host. * - * @return \Illuminate\Database\Eloquent\Relations\HasOne + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function node() { - return $this->hasOne(Node::class, 'id', 'linked_node'); + return $this->belongsTo(Node::class, 'linked_node'); } } diff --git a/app/Models/Node.php b/app/Models/Node.php index 2be35568e..936682644 100644 --- a/app/Models/Node.php +++ b/app/Models/Node.php @@ -220,11 +220,11 @@ class Node extends Model /** * Gets the location associated with a node. * - * @return \Illuminate\Database\Eloquent\Relations\HasOne + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function location() { - return $this->hasOne(Location::class, 'id', 'location_id'); + return $this->belongsTo(Location::class); } /** diff --git a/app/Models/Server.php b/app/Models/Server.php index f7e8bdbed..abe512e69 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -221,11 +221,11 @@ class Server extends Model /** * Gets the user who owns the server. * - * @return \Illuminate\Database\Eloquent\Relations\HasOne + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function user() { - return $this->hasOne(User::class, 'id', 'owner_id'); + return $this->belongsTo(User::class, 'owner_id'); } /** @@ -251,21 +251,21 @@ class Server extends Model /** * Gets information for the service associated with this server. * - * @return \Illuminate\Database\Eloquent\Relations\HasOne + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function service() { - return $this->hasOne(Service::class, 'id', 'service_id'); + return $this->belongsTo(Service::class); } /** * Gets information for the service option associated with this server. * - * @return \Illuminate\Database\Eloquent\Relations\HasOne + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function option() { - return $this->hasOne(ServiceOptions::class, 'id', 'option_id'); + return $this->belongsTo(ServiceOptions::class); } /** @@ -281,11 +281,11 @@ class Server extends Model /** * Gets information for the node associated with this server. * - * @return \Illuminate\Database\Eloquent\Relations\HasOne + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function node() { - return $this->hasOne(Node::class, 'id', 'node_id'); + return $this->belongsTo(Node::class); } /** From 02458c909d3110a56004e3da5ed19afe16077f12 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sun, 5 Feb 2017 19:19:46 -0500 Subject: [PATCH 12/34] Improves server model and cleans up model code calls. --- .../Controllers/API/User/InfoController.php | 2 +- app/Http/Controllers/Base/IndexController.php | 2 +- app/Models/Allocation.php | 11 +++++ app/Models/Server.php | 46 +++++------------ app/Models/Subuser.php | 33 ++----------- app/Models/User.php | 49 +++++++++++++++---- .../themes/pterodactyl/base/index.blade.php | 4 +- .../pterodactyl/layouts/master.blade.php | 4 +- 8 files changed, 71 insertions(+), 80 deletions(-) diff --git a/app/Http/Controllers/API/User/InfoController.php b/app/Http/Controllers/API/User/InfoController.php index 228a66819..844e493dc 100644 --- a/app/Http/Controllers/API/User/InfoController.php +++ b/app/Http/Controllers/API/User/InfoController.php @@ -32,7 +32,7 @@ class InfoController extends BaseController { public function me(Request $request) { - return Models\Server::getUserServers()->map(function ($server) { + return $request->user()->serverAccessCollection()->map(function ($server) { return [ 'id' => $server->uuidShort, 'uuid' => $server->uuid, diff --git a/app/Http/Controllers/Base/IndexController.php b/app/Http/Controllers/Base/IndexController.php index 160bc67e6..e59ceb068 100644 --- a/app/Http/Controllers/Base/IndexController.php +++ b/app/Http/Controllers/Base/IndexController.php @@ -48,7 +48,7 @@ class IndexController extends Controller public function getIndex(Request $request) { return view('base.index', [ - 'servers' => Server::getUserServers(10), + 'servers' => $request->user()->serverAccessCollection(10)->load('node', 'allocation'), ]); } diff --git a/app/Models/Allocation.php b/app/Models/Allocation.php index 34b6baec5..2df21976f 100644 --- a/app/Models/Allocation.php +++ b/app/Models/Allocation.php @@ -52,4 +52,15 @@ class Allocation extends Model 'port' => 'integer', 'server_id' => 'integer', ]; + + /** + * Accessor to automatically provide the IP alias if defined. + * + * @param null|string $value + * @return string + */ + public function getAliasAttribute($value) + { + return (is_null($this->ip_alias)) ? $this->ip : $this->ip_alias; + } } diff --git a/app/Models/Server.php b/app/Models/Server.php index abe512e69..ff63af0e5 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -103,40 +103,6 @@ class Server extends Model self::$user = Auth::user(); } - /** - * Returns array of all servers owned by the logged in user. - * Returns all users servers if user is a root admin. - * - * @return \Illuminate\Database\Eloquent\Collection - */ - public static function getUserServers($paginate = null) - { - $query = self::select( - 'servers.*', - 'nodes.name as nodeName', - 'locations.short as a_locationShort', - 'allocations.ip', - 'allocations.ip_alias', - 'allocations.port', - 'services.name as a_serviceName', - 'service_options.name as a_serviceOptionName' - )->join('nodes', 'servers.node_id', '=', 'nodes.id') - ->join('locations', 'nodes.location_id', '=', 'locations.id') - ->join('services', 'servers.service_id', '=', 'services.id') - ->join('service_options', 'servers.option_id', '=', 'service_options.id') - ->join('allocations', 'servers.allocation_id', '=', 'allocations.id'); - - if (self::$user->root_admin !== 1) { - $query->whereIn('servers.id', Subuser::accessServers()); - } - - if (is_numeric($paginate)) { - return $query->paginate($paginate); - } - - return $query->get(); - } - /** * Returns a single server specified by UUID. * DO NOT USE THIS TO MODIFY SERVER DETAILS OR SAVE THOSE DETAILS. @@ -150,7 +116,7 @@ class Server extends Model $query = self::with('service', 'node')->where('uuidShort', $uuid)->orWhere('uuid', $uuid); if (! Auth::user()->isRootAdmin()) { - $query->whereIn('id', Subuser::accessServers()); + $query->whereIn('id', Auth::user()->serverAccessArray()); } $result = $query->first(); @@ -228,6 +194,16 @@ class Server extends Model return $this->belongsTo(User::class, 'owner_id'); } + /** + * 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. * diff --git a/app/Models/Subuser.php b/app/Models/Subuser.php index 8d4a5b133..ea855d671 100644 --- a/app/Models/Subuser.php +++ b/app/Models/Subuser.php @@ -55,33 +55,8 @@ class Subuser extends Model * * @var array */ - protected $casts = [ - 'user_id' => 'integer', - 'server_id' => 'integer', - ]; - - /** - * @var mixed - */ - protected static $user; - - /** - * Constructor. - */ - public function __construct() - { - self::$user = Auth::user(); - } - - /** - * Returns an array of each server ID that the user has access to. - * - * @return array - */ - public static function accessServers() - { - $union = self::select('server_id')->where('user_id', self::$user->id); - - return Server::select('id')->where('owner', self::$user->id)->union($union)->pluck('id'); - } + protected $casts = [ + 'user_id' => 'integer', + 'server_id' => 'integer', + ]; } diff --git a/app/Models/User.php b/app/Models/User.php index 7dba0cf0d..1714dcebf 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -87,16 +87,6 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac */ protected $hidden = ['password', 'remember_token', 'totp_secret']; - /** - * Determines if a user has permissions. - * - * @return bool - */ - public function permissions() - { - return $this->hasMany(Permission::class); - } - /** * Enables or disables TOTP on an account if the token is valid. * @@ -176,4 +166,43 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac return $subuser->daemonSecret; } + + /** + * Returns all permissions that a user has. + * + * @return \Illuminate\Database\Eloquent\Relations\HasMany + */ + public function permissions() + { + return $this->hasMany(Permission::class); + } + + /** + * Returns an array of all servers a user is able to access. + * Note: does not account for user admin status. + * + * @return array + */ + public function serverAccessArray() + { + $union = Subuser::select('server_id')->where('user_id', $this->id); + + return Server::select('id')->where('owner_id', $this->id)->union($union)->pluck('id')->all(); + } + + /** + * Returns an array of all servers a user is able to access. + * Note: does not account for user admin status. + * + * @return Collection + */ + public function serverAccessCollection($paginate = null) + { + $query = Server::with('service', 'node'); + if (! $this->isRootAdmin()) { + $query->whereIn('id', $this->serverAccessArray()); + } + + return (is_numeric($paginate)) ? $query->paginate($paginate) : $query->get(); + } } diff --git a/resources/themes/pterodactyl/base/index.blade.php b/resources/themes/pterodactyl/base/index.blade.php index d029a37a9..71efda93f 100644 --- a/resources/themes/pterodactyl/base/index.blade.php +++ b/resources/themes/pterodactyl/base/index.blade.php @@ -62,8 +62,8 @@ {{ $server->uuidShort }} {{ $server->name }} - {{ $server->node_idName }} - @if(!is_null($server->ip_alias)){{ $server->ip_alias }}@else{{ $server->ip }}@endif:{{ $server->port }} + {{ $server->node->name }} + {{ $server->allocation->alias }}:{{ $server->allocation->port }} -- / {{ $server->memory === 0 ? '∞' : $server->memory }} MB -- % diff --git a/resources/themes/pterodactyl/layouts/master.blade.php b/resources/themes/pterodactyl/layouts/master.blade.php index 57c7383e0..d478bd372 100644 --- a/resources/themes/pterodactyl/layouts/master.blade.php +++ b/resources/themes/pterodactyl/layouts/master.blade.php @@ -245,7 +245,7 @@
    - @if(count($databases) > 0) + @if(count($server->databases) > 0)
    @@ -426,12 +432,12 @@ - @foreach($databases as $database) + @foreach($server->databases as $database) - + @endforeach @@ -568,7 +574,7 @@ $(document).ready(function () { 'X-Access-Token': '{{ $server->daemonSecret }}', 'X-Access-Server': '{{ $server->uuid }}' }, - url: '{{ $node->scheme }}://{{ $node->fqdn }}:{{ $node->daemonListen }}/server', + url: '{{ $server->node->scheme }}://{{ $server->node->fqdn }}:{{ $server->node->daemonListen }}/server', dataType: 'json', timeout: 5000, }).done(function (data) { diff --git a/resources/views/admin/users/view.blade.php b/resources/views/admin/users/view.blade.php index 314fc8ba7..3ba9ec035 100644 --- a/resources/views/admin/users/view.blade.php +++ b/resources/views/admin/users/view.blade.php @@ -39,7 +39,7 @@
    - +
    @@ -64,7 +64,7 @@
    {!! csrf_field() !!} - +
    @@ -72,7 +72,7 @@
    - +
    @@ -83,7 +83,7 @@
    - +
    {{ $database->database }} {{ $database->username }} ({{ $database->remote }}) {{ Crypt::decrypt($database->password) }} {{ $database->a_host }}:{{ $database->a_port }}{{ $database->host->host }}:{{ $database->host->port }}
    @@ -112,12 +112,12 @@ - @foreach($servers as $server) + @foreach($user->servers as $server) - + @@ -127,7 +127,7 @@ @else
    There are no servers associated with this account.
    @endif - +
    From c8f6c93d26445ef8bc366612c82d84514bc650b8 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Thu, 9 Feb 2017 18:44:07 -0500 Subject: [PATCH 15/34] Modify subusers model setup --- app/Models/Permission.php | 33 ++++++--- app/Models/Subuser.php | 10 +++ app/Models/User.php | 4 +- app/Policies/ServerPolicy.php | 34 ++++----- ...2_09_174834_SetupPermissionsPivotTable.php | 72 +++++++++++++++++++ 5 files changed, 124 insertions(+), 29 deletions(-) create mode 100644 database/migrations/2017_02_09_174834_SetupPermissionsPivotTable.php diff --git a/app/Models/Permission.php b/app/Models/Permission.php index 008473f3c..391f5e8d2 100644 --- a/app/Models/Permission.php +++ b/app/Models/Permission.php @@ -42,22 +42,35 @@ class Permission extends Model */ protected $guarded = ['id', 'created_at', 'updated_at']; - /** - * Cast values to correct type. - * - * @var array - */ - protected $casts = [ - 'user_id' => 'integer', - 'server_id' => 'integer', - ]; + /** + * Cast values to correct type. + * + * @var array + */ + protected $casts = [ + 'subuser_id' => 'integer', + ]; + /** + * Find permission by permission node. + * + * @param \Illuminate\Database\Query\Builder $query + * @param string $permission + * @return \Illuminate\Database\Query\Builder + */ public function scopePermission($query, $permission) { return $query->where('permission', $permission); } - public function scopeServer($query, $server) + /** + * Filter permission by server. + * + * @param \Illuminate\Database\Query\Builder $query + * @param \Pterodactyl\Models\Server $server + * @return \Illuminate\Database\Query\Builder + */ + public function scopeServer($query, Server $server) { return $query->where('server_id', $server->id); } diff --git a/app/Models/Subuser.php b/app/Models/Subuser.php index 826995d0c..6d390df95 100644 --- a/app/Models/Subuser.php +++ b/app/Models/Subuser.php @@ -79,4 +79,14 @@ class Subuser extends Model { return $this->belongsTo(User::class); } + + /** + * Gets the permissions associated with a subuser. + * + * @return \Illuminate\Database\Eloquent\Relations\HasMany + */ + public function permissions() + { + return $this->hasMany(Pemission::class); + } } diff --git a/app/Models/User.php b/app/Models/User.php index 6d1bfa19b..abcddf227 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -199,11 +199,11 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac /** * Returns all permissions that a user has. * - * @return \Illuminate\Database\Eloquent\Relations\HasMany + * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough */ public function permissions() { - return $this->hasMany(Permission::class); + return $this->hasManyThrough(Permission::class, Subuser::class); } /** diff --git a/app/Policies/ServerPolicy.php b/app/Policies/ServerPolicy.php index 3ef54f436..d67f3aced 100644 --- a/app/Policies/ServerPolicy.php +++ b/app/Policies/ServerPolicy.php @@ -39,6 +39,23 @@ class ServerPolicy // } + /** + * Checks if the user has the given permission on/for the server. + * + * @param \Pterodactyl\Models\User $user + * @param \Pterodactyl\Models\Server $server + * @param $permission + * @return bool + */ + private function checkPermission(User $user, Server $server, $permission) + { + if ($this->isOwner($user, $server)) { + return true; + } + + return $user->permissions()->server($server)->permission($permission)->exists(); + } + /** * Determine if current user is the owner of a server. * @@ -521,21 +538,4 @@ class ServerPolicy { return $this->checkPermission($user, $server, 'set-allocation'); } - - /** - * Checks if the user has the given permission on/for the server. - * - * @param \Pterodactyl\Models\User $user - * @param \Pterodactyl\Models\Server $server - * @param $permission - * @return bool - */ - private function checkPermission(User $user, Server $server, $permission) - { - if ($this->isOwner($user, $server)) { - return true; - } - - return $user->permissions()->server($server)->permission($permission)->exists(); - } } diff --git a/database/migrations/2017_02_09_174834_SetupPermissionsPivotTable.php b/database/migrations/2017_02_09_174834_SetupPermissionsPivotTable.php new file mode 100644 index 000000000..fe87c4f77 --- /dev/null +++ b/database/migrations/2017_02_09_174834_SetupPermissionsPivotTable.php @@ -0,0 +1,72 @@ +unsignedInteger('subuser_id')->after('id'); + }); + + DB::transaction(function () { + foreach(Subuser::all() as &$subuser) { + Permission::where('user_id', $subuser->user_id)->where('server_id', $subuser->server_id)->update([ + 'subuser_id' => $subuser->id, + ]); + } + }); + + Schema::table('permissions', function (Blueprint $table) { + $table->dropForeign('permissions_server_id_foreign'); + $table->dropIndex('permissions_server_id_foreign'); + $table->dropForeign('permissions_user_id_foreign'); + $table->dropIndex('permissions_user_id_foreign'); + + $table->dropColumn('server_id'); + $table->dropColumn('user_id'); + $table->foreign('subuser_id')->references('id')->on('subusers'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('permissions', function (Blueprint $table) { + $table->unsignedInteger('server_id')->after('subuser_id'); + $table->unsignedInteger('user_id')->after('server_id'); + }); + + DB::transaction(function () { + foreach(Subuser::all() as &$subuser) { + Permission::where('subuser_id', $subuser->id)->update([ + 'user_id' => $subuser->user_id, + 'server_id' => $subuser->server_id, + ]); + } + }); + + Schema::table('permissions', function (Blueprint $table) { + $table->dropForeign('permissions_subuser_id_foreign'); + $table->dropIndex('permissions_subuser_id_foreign'); + $table->dropColumn('subuser_id'); + + $table->foreign('server_id')->references('id')->on('servers'); + $table->foreign('user_id')->references('id')->on('users'); + }); + } +} From 99812b040731b90854598ce8c48c3a2fc0572b97 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Thu, 9 Feb 2017 19:38:54 -0500 Subject: [PATCH 16/34] More subuser Model updates. --- .../Controllers/Server/SubuserController.php | 45 +++++-------------- app/Models/Permission.php | 8 ++++ app/Models/Server.php | 10 +++++ app/Models/Subuser.php | 2 +- app/Repositories/SubuserRepository.php | 21 +++++---- ...2_09_174834_SetupPermissionsPivotTable.php | 3 ++ .../pterodactyl/server/users/index.blade.php | 16 +++---- .../pterodactyl/server/users/view.blade.php | 4 +- 8 files changed, 54 insertions(+), 55 deletions(-) diff --git a/app/Http/Controllers/Server/SubuserController.php b/app/Http/Controllers/Server/SubuserController.php index ed25d4a2c..76735e351 100644 --- a/app/Http/Controllers/Server/SubuserController.php +++ b/app/Http/Controllers/Server/SubuserController.php @@ -49,7 +49,7 @@ class SubuserController extends Controller public function getIndex(Request $request, $uuid) { - $server = Models\Server::byUuid($uuid); + $server = Models\Server::byUuid($uuid)->load('subusers.user'); $this->authorize('list-subusers', $server); $server->js(); @@ -57,35 +57,17 @@ class SubuserController extends Controller return view('server.users.index', [ 'server' => $server, 'node' => $server->node, - 'subusers' => Models\Subuser::select('subusers.*', 'users.email', 'users.username', 'users.use_totp') - ->join('users', 'users.id', '=', 'subusers.user_id') - ->where('server_id', $server->id) - ->get(), + 'subusers' => $server->subusers, ]); } public function getView(Request $request, $uuid, $id) { - $server = Models\Server::byUuid($uuid); + $server = Models\Server::byUuid($uuid)->load('node'); $this->authorize('view-subuser', $server); - $subuser = Models\Subuser::select('subusers.*', 'users.email as a_userEmail') - ->join('users', 'users.id', '=', 'subusers.user_id') - ->where(DB::raw('md5(subusers.id)'), $id)->where('subusers.server_id', $server->id) - ->first(); - - if (! $subuser) { - abort(404); - } - - $permissions = []; - $modelPermissions = Models\Permission::select('permission') - ->where('user_id', $subuser->user_id)->where('server_id', $server->id) - ->get(); - - foreach ($modelPermissions as &$perm) { - $permissions[$perm->permission] = true; - } + $subuser = Models\Subuser::with('permissions', 'user') + ->where('server_id', $server->id)->findOrFail($id); $server->js(); @@ -93,7 +75,9 @@ class SubuserController extends Controller 'server' => $server, 'node' => $server->node, 'subuser' => $subuser, - 'permissions' => $permissions, + 'permissions' => $subuser->permissions->mapWithKeys(function ($item, $key) { + return [$item->permission => true]; + }), ]); } @@ -102,12 +86,10 @@ class SubuserController extends Controller $server = Models\Server::byUuid($uuid); $this->authorize('edit-subuser', $server); - $subuser = Models\Subuser::where(DB::raw('md5(id)'), $id)->where('server_id', $server->id)->first(); + $subuser = Models\Subuser::where('server_id', $server->id)->findOrFail($id); try { - if (! $subuser) { - throw new DisplayException('Unable to locate a subuser by that ID.'); - } elseif ($subuser->user_id === Auth::user()->id) { + if ($subuser->user_id === Auth::user()->id) { throw new DisplayException('You are not authorized to edit you own account.'); } @@ -163,7 +145,7 @@ class SubuserController extends Controller return redirect()->route('server.subusers.view', [ 'uuid' => $uuid, - 'id' => md5($subuser->id), + 'id' => $subuser->id, ]); } catch (DisplayValidationException $ex) { return redirect()->route('server.subusers.new', $uuid)->withErrors(json_decode($ex->getMessage()))->withInput(); @@ -183,10 +165,7 @@ class SubuserController extends Controller $this->authorize('delete-subuser', $server); try { - $subuser = Models\Subuser::select('id')->where(DB::raw('md5(id)'), $id)->where('server_id', $server->id)->first(); - if (! $subuser) { - throw new DisplayException('No subuser by that ID was found on the system.'); - } + $subuser = Models\Subuser::where('server_id', $server->id)->findOrFail($id); $repo = new SubuserRepository; $repo->delete($subuser->id); diff --git a/app/Models/Permission.php b/app/Models/Permission.php index 391f5e8d2..fe7221719 100644 --- a/app/Models/Permission.php +++ b/app/Models/Permission.php @@ -28,6 +28,14 @@ use Illuminate\Database\Eloquent\Model; class Permission extends Model { + + /** + * Should timestamps be used on this model. + * + * @var boolean + */ + public $timestamps = false; + /** * The table associated with the model. * diff --git a/app/Models/Server.php b/app/Models/Server.php index 3ea72e2b9..1ce041ca7 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -196,6 +196,16 @@ class Server extends Model return $this->belongsTo(User::class, 'owner_id'); } + /** + * 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. * diff --git a/app/Models/Subuser.php b/app/Models/Subuser.php index 6d390df95..25a82f615 100644 --- a/app/Models/Subuser.php +++ b/app/Models/Subuser.php @@ -87,6 +87,6 @@ class Subuser extends Model */ public function permissions() { - return $this->hasMany(Pemission::class); + return $this->hasMany(Permission::class); } } diff --git a/app/Repositories/SubuserRepository.php b/app/Repositories/SubuserRepository.php index 94a8ccecb..4e4691dda 100644 --- a/app/Repositories/SubuserRepository.php +++ b/app/Repositories/SubuserRepository.php @@ -167,8 +167,7 @@ class SubuserRepository } Models\Permission::create([ - 'user_id' => $user->id, - 'server_id' => $server->id, + 'subuser_id' => $subuser->id, 'permission' => $permission, ]); } @@ -213,14 +212,12 @@ class SubuserRepository */ public function delete($id) { - $subuser = Models\Subuser::with('server.node', 'permissions')->findOrFail($id); + $subuser = Models\Subuser::with('server.node')->findOrFail($id); $server = $subuser->server; DB::beginTransaction(); try { - Models\Permission::where('user_id', $subuser->user_id)->where('server_id', $subuser->server_id)->delete(); - $server->node->guzzleClient([ 'X-Access-Server' => $server->uuid, 'X-Access-Token' => $server->node->daemonSecret, @@ -232,6 +229,9 @@ class SubuserRepository ], ]); + foreach($subuser->permissions as &$permission) { + $permission->delete(); + } $subuser->delete(); DB::commit(); @@ -273,7 +273,9 @@ class SubuserRepository DB::beginTransaction(); try { - Models\Permission::where('user_id', $subuser->user_id)->where('server_id', $subuser->server_id)->delete(); + foreach($subuser->permissions as &$permission) { + $permission->delete(); + } $daemonPermissions = $this->coreDaemonPermissions; foreach ($data['permissions'] as $permission) { @@ -282,13 +284,10 @@ class SubuserRepository if (! is_null($this->permissions[$permission])) { array_push($daemonPermissions, $this->permissions[$permission]); } - $model = new Models\Permission; - $model->fill([ - 'user_id' => $data['user'], - 'server_id' => $data['server'], + Models\Permission::create([ + 'subuser_id' => $subuser->id, 'permission' => $permission, ]); - $model->save(); } } diff --git a/database/migrations/2017_02_09_174834_SetupPermissionsPivotTable.php b/database/migrations/2017_02_09_174834_SetupPermissionsPivotTable.php index fe87c4f77..ce5e3ae89 100644 --- a/database/migrations/2017_02_09_174834_SetupPermissionsPivotTable.php +++ b/database/migrations/2017_02_09_174834_SetupPermissionsPivotTable.php @@ -35,6 +35,8 @@ class SetupPermissionsPivotTable extends Migration $table->dropColumn('server_id'); $table->dropColumn('user_id'); + $table->dropColumn('created_at'); + $table->dropColumn('updated_at'); $table->foreign('subuser_id')->references('id')->on('subusers'); }); } @@ -49,6 +51,7 @@ class SetupPermissionsPivotTable extends Migration Schema::table('permissions', function (Blueprint $table) { $table->unsignedInteger('server_id')->after('subuser_id'); $table->unsignedInteger('user_id')->after('server_id'); + $table->timestamps(); }); DB::transaction(function () { diff --git a/resources/themes/pterodactyl/server/users/index.blade.php b/resources/themes/pterodactyl/server/users/index.blade.php index f44849758..daffcb611 100644 --- a/resources/themes/pterodactyl/server/users/index.blade.php +++ b/resources/themes/pterodactyl/server/users/index.blade.php @@ -51,29 +51,29 @@ @can('view-subuser', $server)
    @endcan @can('delete-subuser', $server)@endcan - @foreach($subusers as $user) + @foreach($subusers as $subuser) - - + + - + @can('view-subuser', $server) @endcan @can('delete-subuser', $server) diff --git a/resources/themes/pterodactyl/server/users/view.blade.php b/resources/themes/pterodactyl/server/users/view.blade.php index b697edc34..925fbdf05 100644 --- a/resources/themes/pterodactyl/server/users/view.blade.php +++ b/resources/themes/pterodactyl/server/users/view.blade.php @@ -35,7 +35,7 @@ @section('content') @can('edit-subuser', $server) - + @endcan
    @@ -45,7 +45,7 @@
    {!! csrf_field() !!} - +
    From 0720bfe62f4fad89158b00f0fecab3111b92e365 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Fri, 10 Feb 2017 17:03:58 -0500 Subject: [PATCH 17/34] Cleanup database query code. --- app/Http/Controllers/Admin/DatabaseController.php | 5 +---- resources/views/admin/databases/index.blade.php | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/Admin/DatabaseController.php b/app/Http/Controllers/Admin/DatabaseController.php index 6cfa73041..1b7dcb5a2 100644 --- a/app/Http/Controllers/Admin/DatabaseController.php +++ b/app/Http/Controllers/Admin/DatabaseController.php @@ -48,10 +48,7 @@ class DatabaseController extends Controller { return view('admin.databases.index', [ 'databases' => Models\Database::with('server')->paginate(50), - 'hosts' => Models\DatabaseServer::select( - 'database_servers.*', - DB::raw('(SELECT COUNT(*) FROM `databases` WHERE `databases`.`db_server` = database_servers.id) as c_databases') - )->with('node')->paginate(20), + 'hosts' => Models\DatabaseServer::withCount('databases')->with('node')->paginate(20), ]); } diff --git a/resources/views/admin/databases/index.blade.php b/resources/views/admin/databases/index.blade.php index bb7be1e3f..f1c2a4aac 100644 --- a/resources/views/admin/databases/index.blade.php +++ b/resources/views/admin/databases/index.blade.php @@ -92,7 +92,7 @@ - + From ba175e6b55348e0bf9b5bc81d979e6f247aa9019 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Fri, 10 Feb 2017 17:09:56 -0500 Subject: [PATCH 18/34] Cleanup location model and controller for Admin. --- .../Controllers/Admin/LocationsController.php | 24 ++++--------------- app/Models/Location.php | 20 ++++++++++++++++ .../views/admin/locations/index.blade.php | 4 ++-- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/app/Http/Controllers/Admin/LocationsController.php b/app/Http/Controllers/Admin/LocationsController.php index d74f05d18..4f6343e5f 100644 --- a/app/Http/Controllers/Admin/LocationsController.php +++ b/app/Http/Controllers/Admin/LocationsController.php @@ -43,35 +43,21 @@ class LocationsController extends Controller public function getIndex(Request $request) { return view('admin.locations.index', [ - 'locations' => Models\Location::select( - 'locations.*', - DB::raw('(SELECT COUNT(*) FROM nodes WHERE nodes.location_id = locations.id) as a_nodeCount'), - DB::raw('(SELECT COUNT(*) FROM servers WHERE servers.node_id IN (SELECT nodes.id FROM nodes WHERE nodes.location_id = locations.id)) as a_serverCount') - )->paginate(20), + 'locations' => Models\Location::withCount('nodes', 'servers')->paginate(20), ]); } public function deleteLocation(Request $request, $id) { - $model = Models\Location::select( - 'locations.id', - DB::raw('(SELECT COUNT(*) FROM nodes WHERE nodes.location_id = locations.id) as a_nodeCount'), - DB::raw('(SELECT COUNT(*) FROM servers WHERE servers.node_id IN (SELECT nodes.id FROM nodes WHERE nodes.location_id = locations.id)) as a_serverCount') - )->where('id', $id)->first(); + $location = Models\Location::withCount('nodes')->findOrFail($id); - if (! $model) { + if ($location->nodes_count > 0) { return response()->json([ - 'error' => 'No location with that ID exists on the system.', - ], 404); - } - - if ($model->a_nodeCount > 0 || $model->a_serverCount > 0) { - return response()->json([ - 'error' => 'You cannot remove a location that is currently assigned to a node or server.', + 'error' => 'You cannot remove a location that is currently assigned to a node.', ], 422); } - $model->delete(); + $location->delete(); return response('', 204); } diff --git a/app/Models/Location.php b/app/Models/Location.php index 5c8feb9fa..f9ceec767 100644 --- a/app/Models/Location.php +++ b/app/Models/Location.php @@ -41,4 +41,24 @@ class Location extends Model * @var array */ protected $guarded = ['id', 'created_at', 'updated_at']; + + /** + * Gets the nodes in a specificed location. + * + * @return \Illuminate\Database\Eloquent\Relations\HasMany + */ + public function nodes() + { + return $this->hasMany(Node::class); + } + + /** + * Gets the servers within a given location. + * + * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough + */ + public function servers() + { + return $this->hasManyThrough(Server::class, Node::class); + } } diff --git a/resources/views/admin/locations/index.blade.php b/resources/views/admin/locations/index.blade.php index 9aaf49faa..2fbd4b05a 100644 --- a/resources/views/admin/locations/index.blade.php +++ b/resources/views/admin/locations/index.blade.php @@ -46,8 +46,8 @@ - - + + From efef3568706e77f71214c5ec4160c0e4d00ba6b4 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Fri, 10 Feb 2017 17:18:46 -0500 Subject: [PATCH 19/34] Assorted model code updates. --- .../Controllers/Admin/NodesController.php | 26 ++++-------- app/Http/Controllers/Admin/PackController.php | 16 ++----- .../Controllers/Admin/ServersController.php | 14 ++----- .../Controllers/Admin/ServiceController.php | 42 +++++-------------- app/Http/Controllers/Admin/UserController.php | 16 ++----- 5 files changed, 31 insertions(+), 83 deletions(-) diff --git a/app/Http/Controllers/Admin/NodesController.php b/app/Http/Controllers/Admin/NodesController.php index 9cfac1fde..fc32f3929 100644 --- a/app/Http/Controllers/Admin/NodesController.php +++ b/app/Http/Controllers/Admin/NodesController.php @@ -112,10 +112,8 @@ class NodesController extends Controller public function getView(Request $request, $id) { $node = Models\Node::with( - 'servers.user', - 'servers.service', - 'servers.allocations', - 'location' + 'servers.user', 'servers.service', + 'servers.allocations', 'location' )->findOrFail($id); $node->setRelation('allocations', $node->allocations()->paginate(40)); @@ -131,20 +129,12 @@ class NodesController extends Controller try { $node = new NodeRepository; $node->update($id, $request->only([ - 'name', - 'location', - 'public', - 'fqdn', - 'scheme', - 'memory', - 'memory_overallocate', - 'disk', - 'disk_overallocate', - 'upload_size', - 'daemonBase', - 'daemonSFTP', - 'daemonListen', - 'reset_secret', + 'name', 'location', 'public', + 'fqdn', 'scheme', 'memory', + 'memory_overallocate', 'disk', + 'disk_overallocate', 'upload_size', + 'daemonBase', 'daemonSFTP', + 'daemonListen', 'reset_secret', ])); Alert::success('Successfully update this node\'s information. If you changed any daemon settings you will need to restart it now.')->flash(); diff --git a/app/Http/Controllers/Admin/PackController.php b/app/Http/Controllers/Admin/PackController.php index 3e6685f28..c86e2ada4 100644 --- a/app/Http/Controllers/Admin/PackController.php +++ b/app/Http/Controllers/Admin/PackController.php @@ -73,12 +73,8 @@ class PackController extends Controller try { $repo = new Pack; $pack = $repo->create($request->only([ - 'name', - 'version', - 'description', - 'option', - 'selectable', - 'visible', + 'name', 'version', 'description', + 'option', 'selectable', 'visible', 'file_upload', ])); Alert::success('Successfully created new service!')->flash(); @@ -128,12 +124,8 @@ class PackController extends Controller try { $repo = new Pack; $repo->update($id, $request->only([ - 'name', - 'version', - 'description', - 'option', - 'selectable', - 'visible', + 'name', 'version', 'description', + 'option', 'selectable', 'visible', ])); Alert::success('Service pack has been successfully updated.')->flash(); } catch (DisplayValidationException $ex) { diff --git a/app/Http/Controllers/Admin/ServersController.php b/app/Http/Controllers/Admin/ServersController.php index d287e7819..b68f8d01c 100644 --- a/app/Http/Controllers/Admin/ServersController.php +++ b/app/Http/Controllers/Admin/ServersController.php @@ -279,13 +279,9 @@ class ServersController extends Controller try { $server = new ServerRepository; $server->changeBuild($id, $request->only([ - 'default', - 'add_additional', - 'remove_additional', - 'memory', - 'swap', - 'io', - 'cpu', + 'default', 'add_additional', + 'remove_additional', 'memory', + 'swap', 'io', 'cpu', ])); Alert::success('Server details were successfully updated.')->flash(); } catch (DisplayValidationException $ex) { @@ -377,9 +373,7 @@ class ServersController extends Controller try { $repo = new DatabaseRepository; $repo->create($id, $request->only([ - 'db_server', - 'database', - 'remote', + 'db_server', 'database', 'remote', ])); Alert::success('Added new database to this server.')->flash(); } catch (DisplayValidationException $ex) { diff --git a/app/Http/Controllers/Admin/ServiceController.php b/app/Http/Controllers/Admin/ServiceController.php index f8951993a..5aad76fd3 100644 --- a/app/Http/Controllers/Admin/ServiceController.php +++ b/app/Http/Controllers/Admin/ServiceController.php @@ -59,11 +59,8 @@ class ServiceController extends Controller try { $repo = new ServiceRepository\Service; $service = $repo->create($request->only([ - 'name', - 'description', - 'file', - 'executable', - 'startup', + 'name', 'description', 'file', + 'executable', 'startup', ])); Alert::success('Successfully created new service!')->flash(); @@ -92,11 +89,8 @@ class ServiceController extends Controller try { $repo = new ServiceRepository\Service; $repo->update($service, $request->only([ - 'name', - 'description', - 'file', - 'executable', - 'startup', + 'name', 'description', 'file', + 'executable', 'startup', ])); Alert::success('Successfully updated this service.')->flash(); } catch (DisplayValidationException $ex) { @@ -134,9 +128,7 @@ class ServiceController extends Controller $option = Models\ServiceOptions::with('service', 'variables')->findOrFail($option); $option->setRelation('servers', $option->servers()->with('user')->paginate(25)); - return view('admin.services.options.view', [ - 'option' => $option, - ]); + return view('admin.services.options.view', ['option' => $option]); } public function postOption(Request $request, $service, $option) @@ -144,12 +136,8 @@ class ServiceController extends Controller try { $repo = new ServiceRepository\Option; $repo->update($option, $request->only([ - 'name', - 'description', - 'tag', - 'executable', - 'docker_image', - 'startup', + 'name', 'description', 'tag', + 'executable', 'docker_image', 'startup', ])); Alert::success('Option settings successfully updated.')->flash(); } catch (DisplayValidationException $ex) { @@ -227,14 +215,9 @@ class ServiceController extends Controller try { $repo = new ServiceRepository\Variable; $repo->create($option, $request->only([ - 'name', - 'description', - 'env_variable', - 'default_value', - 'user_viewable', - 'user_editable', - 'required', - 'regex', + 'name', 'description', 'env_variable', + 'default_value', 'user_viewable', + 'user_editable', 'required', 'regex', ])); Alert::success('Successfully added new variable to this option.')->flash(); @@ -311,10 +294,7 @@ class ServiceController extends Controller { try { $repo = new ServiceRepository\Service; - $repo->updateFile($serviceId, $request->only([ - 'file', - 'contents', - ])); + $repo->updateFile($serviceId, $request->only(['file', 'contents'])); return response('', 204); } catch (DisplayException $ex) { diff --git a/app/Http/Controllers/Admin/UserController.php b/app/Http/Controllers/Admin/UserController.php index eef57a895..5ebce1b42 100644 --- a/app/Http/Controllers/Admin/UserController.php +++ b/app/Http/Controllers/Admin/UserController.php @@ -88,12 +88,8 @@ class UserController extends Controller try { $user = new UserRepository; $userid = $user->create($request->only([ - 'email', - 'password', - 'name_first', - 'name_last', - 'username', - 'root_admin', + 'email', 'password', 'name_first', + 'name_last', 'username', 'root_admin', ])); Alert::success('Account has been successfully created.')->flash(); @@ -113,12 +109,8 @@ class UserController extends Controller try { $repo = new UserRepository; $repo->update($user, $request->only([ - 'email', - 'password', - 'name_first', - 'name_last', - 'username', - 'root_admin', + 'email', 'password', 'name_first', + 'name_last', 'username', 'root_admin', ])); Alert::success('User account was successfully updated.')->flash(); } catch (DisplayValidationException $ex) { From 3b3002b77a73375aee5f230d25e401632aeb2e46 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Fri, 10 Feb 2017 17:29:10 -0500 Subject: [PATCH 20/34] API Model updates. --- app/Http/Controllers/Base/APIController.php | 14 +++---- app/Models/APIKey.php | 10 +++++ app/Repositories/APIRepository.php | 4 +- ...7_02_10_171858_UpdateAPIKeyColumnNames.php | 38 +++++++++++++++++++ 4 files changed, 56 insertions(+), 10 deletions(-) create mode 100644 database/migrations/2017_02_10_171858_UpdateAPIKeyColumnNames.php diff --git a/app/Http/Controllers/Base/APIController.php b/app/Http/Controllers/Base/APIController.php index 611638c95..2130ee760 100644 --- a/app/Http/Controllers/Base/APIController.php +++ b/app/Http/Controllers/Base/APIController.php @@ -38,13 +38,8 @@ class APIController extends Controller { public function index(Request $request) { - $keys = Models\APIKey::where('user', $request->user()->id)->get(); - foreach ($keys as &$key) { - $key->permissions = Models\APIPermission::where('key_id', $key->id)->get(); - } - return view('base.api.index', [ - 'keys' => $keys, + 'keys' => Models\APIKey::where('user_id', $request->user()->id)->get(), ]); } @@ -57,8 +52,11 @@ class APIController extends Controller { try { $repo = new APIRepository($request->user()); - $secret = $repo->create($request->except(['_token'])); - Alert::success('An API Keypair has successfully been generated. The API secret for this public key is shown below and will not be shown again.

    ' . $secret . '')->flash(); + $secret = $repo->create($request->only([ + 'memo', 'allowed_ips', + 'adminPermissions', 'permissions', + ])); + Alert::success('An API Key-Pair has successfully been generated. The API secret for this public key is shown below and will not be shown again.

    ' . $secret . '')->flash(); return redirect()->route('account.api'); } catch (DisplayValidationException $ex) { diff --git a/app/Models/APIKey.php b/app/Models/APIKey.php index 4b94b6782..68e481712 100644 --- a/app/Models/APIKey.php +++ b/app/Models/APIKey.php @@ -48,4 +48,14 @@ class APIKey extends Model * @var array */ protected $guarded = ['id', 'created_at', 'updated_at']; + + /** + * Gets the permissions associated with a key. + * + * @return \Illuminate\Database\Eloquent\Relations\HasMany + */ + public function permissions() + { + return $this->hasMany(APIPermission::class, 'key_id'); + } } diff --git a/app/Repositories/APIRepository.php b/app/Repositories/APIRepository.php index 7ce94f34b..382e6a274 100644 --- a/app/Repositories/APIRepository.php +++ b/app/Repositories/APIRepository.php @@ -102,7 +102,7 @@ class APIRepository { $this->user = is_null($user) ? Auth::user() : $user; if (is_null($this->user)) { - throw new \Exception('Cannot access API Repository without passing a user to __construct().'); + throw new \Exception('Cannot access API Repository without passing a user to constructor.'); } } @@ -178,7 +178,7 @@ class APIRepository } } - if ($this->user->root_admin === 1 && isset($data['adminPermissions'])) { + if ($this->user->isRootAdmin() && isset($data['adminPermissions'])) { foreach ($data['adminPermissions'] as $permNode) { if (! strpos($permNode, ':')) { continue; diff --git a/database/migrations/2017_02_10_171858_UpdateAPIKeyColumnNames.php b/database/migrations/2017_02_10_171858_UpdateAPIKeyColumnNames.php new file mode 100644 index 000000000..358f9938d --- /dev/null +++ b/database/migrations/2017_02_10_171858_UpdateAPIKeyColumnNames.php @@ -0,0 +1,38 @@ +dropForeign('api_keys_user_foreign')->dropIndex('api_keys_user_foreign'); + + $table->renameColumn('user', 'user_id'); + $table->foreign('user_id')->references('id')->on('users'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('api_keys', function (Blueprint $table) { + $table->dropForeign('api_keys_user_id_foreign')->dropIndex('api_keys_user_id_foreign'); + + $table->renameColumn('user_id', 'user'); + $table->foreign('user')->references('id')->on('users'); + }); + } +} From c70d31c08f1f2d6c0bb95d7db790c8b064c6f0d1 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Fri, 10 Feb 2017 17:36:58 -0500 Subject: [PATCH 21/34] More model updates. --- .../Controllers/Base/SecurityController.php | 3 +-- .../Controllers/Remote/RemoteController.php | 17 ++++++----------- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/app/Http/Controllers/Base/SecurityController.php b/app/Http/Controllers/Base/SecurityController.php index cf8bf2dc1..23b6f0d68 100644 --- a/app/Http/Controllers/Base/SecurityController.php +++ b/app/Http/Controllers/Base/SecurityController.php @@ -118,8 +118,7 @@ class SecurityController extends Controller public function revoke(Request $request, $id) { - $session = Session::where('id', $id)->where('user_id', $request->user()->id)->firstOrFail(); - $session->delete(); + Session::where('user_id', $request->user()->id)->findOrFail($id)->delete(); return redirect()->route('account.security'); } diff --git a/app/Http/Controllers/Remote/RemoteController.php b/app/Http/Controllers/Remote/RemoteController.php index 0f168c04b..95ff6ab3e 100644 --- a/app/Http/Controllers/Remote/RemoteController.php +++ b/app/Http/Controllers/Remote/RemoteController.php @@ -28,7 +28,6 @@ use Carbon\Carbon; use Pterodactyl\Models; use Illuminate\Http\Request; use Pterodactyl\Http\Controllers\Controller; -use Pterodactyl\Services\NotificationService; class RemoteController extends Controller { @@ -42,7 +41,7 @@ class RemoteController extends Controller public function postDownload(Request $request) { - $download = Models\Download::where('token', $request->input('token', '00'))->first(); + $download = Models\Download::where('token', $request->input('token'))->first(); if (! $download) { return response()->json([ 'error' => 'An invalid request token was recieved with this request.', @@ -59,18 +58,17 @@ class RemoteController extends Controller public function postInstall(Request $request) { - $server = Models\Server::where('uuid', $request->input('server'))->first(); + $server = Models\Server::where('uuid', $request->input('server'))->with('node')->first(); if (! $server) { return response()->json([ 'error' => 'No server by that ID was found on the system.', ], 422); } - $node = Models\Node::findOrFail($server->node_id); $hmac = $request->input('signed'); $status = $request->input('installed'); - if (base64_decode($hmac) !== hash_hmac('sha256', $server->uuid, $node->daemonSecret, true)) { + if (base64_decode($hmac) !== hash_hmac('sha256', $server->uuid, $server->node->daemonSecret, true)) { return response()->json([ 'error' => 'Signed HMAC was invalid.', ], 403); @@ -86,17 +84,15 @@ class RemoteController extends Controller public function event(Request $request) { - $server = Models\Server::where('uuid', $request->input('server'))->first(); + $server = Models\Server::where('uuid', $request->input('server'))->with('node')->first(); if (! $server) { return response()->json([ 'error' => 'No server by that ID was found on the system.', ], 422); } - $node = Models\Node::findOrFail($server->node_id); - $hmac = $request->input('signed'); - if (base64_decode($hmac) !== hash_hmac('sha256', $server->uuid, $node->daemonSecret, true)) { + if (base64_decode($hmac) !== hash_hmac('sha256', $server->uuid, $server->node->daemonSecret, true)) { return response()->json([ 'error' => 'Signed HMAC was invalid.', ], 403); @@ -130,7 +126,6 @@ class RemoteController extends Controller $token->delete(); // Manually as getConfigurationAsJson() returns it in correct format already - return response($node->getConfigurationAsJson(), 200) - ->header('Content-Type', 'application/json'); + return response()->json($node->getConfigurationAsJson(), 200); } } From 8dc1f41b73c7211b39e1dd8bbeacee01f1700641 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Fri, 10 Feb 2017 17:41:56 -0500 Subject: [PATCH 22/34] More code cleanup. --- app/Repositories/APIRepository.php | 2 +- app/Services/NotificationService.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/app/Repositories/APIRepository.php b/app/Repositories/APIRepository.php index 382e6a274..14348b2db 100644 --- a/app/Repositories/APIRepository.php +++ b/app/Repositories/APIRepository.php @@ -149,7 +149,7 @@ class APIRepository $secretKey = str_random(16) . '.' . str_random(7) . '.' . str_random(7); $key = new Models\APIKey; $key->fill([ - 'user' => $this->user->id, + 'user_id' => $this->user->id, 'public' => str_random(16), 'secret' => Crypt::encrypt($secretKey), 'allowed_ips' => empty($this->allowed) ? null : json_encode($this->allowed), diff --git a/app/Services/NotificationService.php b/app/Services/NotificationService.php index abd2155b1..53a6e0ff9 100644 --- a/app/Services/NotificationService.php +++ b/app/Services/NotificationService.php @@ -48,7 +48,6 @@ class NotificationService public function __construct(Server $server) { $this->server = $server; - $this->user = User::findOrFail($server->owner_id); } public function pass(array $notification) From 32a1dc17eddd8e1d69180607cbdad8ceb2670c62 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Fri, 10 Feb 2017 20:26:38 -0500 Subject: [PATCH 23/34] API model updates, as well as general model updates and code fixes. --- .../Controllers/API/LocationController.php | 13 +- app/Http/Controllers/API/NodeController.php | 118 +++++------------- app/Http/Controllers/API/ServerController.php | 81 +++++------- .../Controllers/API/ServiceController.php | 11 +- .../Controllers/API/User/InfoController.php | 15 +-- .../Controllers/API/User/ServerController.php | 21 ++-- app/Http/Controllers/API/UserController.php | 44 +++---- .../Controllers/Admin/ServersController.php | 13 +- app/Http/Middleware/APISecretToken.php | 2 +- app/Models/Node.php | 28 +---- app/Repositories/APIRepository.php | 2 +- app/Repositories/NodeRepository.php | 14 +-- app/Repositories/ServerRepository.php | 48 +++---- resources/views/admin/nodes/new.blade.php | 4 +- resources/views/admin/nodes/view.blade.php | 4 +- resources/views/admin/servers/new.blade.php | 16 +-- 16 files changed, 162 insertions(+), 272 deletions(-) diff --git a/app/Http/Controllers/API/LocationController.php b/app/Http/Controllers/API/LocationController.php index 3bae975d8..94ec5fe7d 100755 --- a/app/Http/Controllers/API/LocationController.php +++ b/app/Http/Controllers/API/LocationController.php @@ -49,11 +49,12 @@ class LocationController extends BaseController */ public function lists(Request $request) { - return Location::select('locations.*', DB::raw('GROUP_CONCAT(nodes.id) as nodes')) - ->join('nodes', 'locations.id', '=', 'nodes.location') - ->groupBy('locations.id') - ->get()->each(function ($location) { - $location->nodes = explode(',', $location->nodes); - })->all(); + return Location::with('nodes')->get()->map(function ($item) { + $item->nodes->transform(function ($item) { + return collect($item)->only(['id', 'name', 'fqdn', 'scheme', 'daemonListen', 'daemonSFTP']); + }); + + return $item; + })->toArray(); } } diff --git a/app/Http/Controllers/API/NodeController.php b/app/Http/Controllers/API/NodeController.php index 0eda949f4..fd87770f7 100755 --- a/app/Http/Controllers/API/NodeController.php +++ b/app/Http/Controllers/API/NodeController.php @@ -24,6 +24,7 @@ namespace Pterodactyl\Http\Controllers\API; +use Log; use Pterodactyl\Models; use Illuminate\Http\Request; use Dingo\Api\Exception\ResourceException; @@ -96,15 +97,21 @@ class NodeController extends BaseController public function create(Request $request) { try { - $node = new NodeRepository; - $new = $node->create($request->all()); + $repo = new NodeRepository; + $node = $repo->create($request->only([ + 'name', 'location_id', 'public', 'fqdn', + 'scheme', 'memory', 'memory_overallocate', + 'disk', 'disk_overallocate', 'daemonBase', + 'daemonSFTP', 'daemonListen', + ])); - return ['id' => $new]; + return ['id' => $repo->id]; } catch (DisplayValidationException $ex) { throw new ResourceException('A validation error occured.', json_decode($ex->getMessage(), true)); } catch (DisplayException $ex) { throw new ResourceException($ex->getMessage()); - } catch (\Exception $e) { + } catch (\Exception $ex) { + Log::error($ex); throw new BadRequestHttpException('There was an error while attempting to add this node to the system.'); } } @@ -124,88 +131,35 @@ class NodeController extends BaseController */ public function view(Request $request, $id, $fields = null) { - $node = Models\Node::where('id', $id); + $node = Models\Node::with('allocations')->where('id', $id)->first(); + if (! $node) { + throw new NotFoundHttpException('No node by that ID was found.'); + } + + $node->allocations->transform(function ($item) { + return collect($item)->only([ + 'id', 'ip', 'ip_alias', 'port', 'server_id' + ]); + }); if (! is_null($request->input('fields'))) { - foreach (explode(',', $request->input('fields')) as $field) { - if (! empty($field)) { - $node->addSelect($field); - } + $fields = explode(',', $request->input('fields')); + if (! empty($fields) && is_array($fields)) { + return collect($node)->only($fields); } } - try { - if (! $node->first()) { - throw new NotFoundHttpException('No node by that ID was found.'); - } - - return [ - 'node' => $node->first(), - 'allocations' => [ - 'assigned' => Models\Allocation::where('node', $id)->whereNotNull('assigned_to')->get(), - 'unassigned' => Models\Allocation::where('node', $id)->whereNull('assigned_to')->get(), - ], - ]; - } catch (NotFoundHttpException $ex) { - throw $ex; - } catch (\Exception $ex) { - throw new BadRequestHttpException('There was an issue with the fields passed in the request.'); - } + return $node; } public function config(Request $request, $id) { - if (! $request->secure()) { - throw new BadRequestHttpException('This API route can only be accessed using a secure connection.'); - } - $node = Models\Node::where('id', $id)->first(); if (! $node) { throw new NotFoundHttpException('No node by that ID was found.'); } - return [ - 'web' => [ - 'listen' => $node->daemonListen, - 'host' => '0.0.0.0', - 'ssl' => [ - 'enabled' => ($node->scheme === 'https'), - 'certificate' => '/etc/certs/' . $node->fqdn . '/fullchain.pem', - 'key' => '/etc/certs/' . $node->fqdn . '/privkey.pem', - ], - ], - 'docker' => [ - 'socket' => '/var/run/docker.sock', - 'autoupdate_images' => true, - ], - 'sftp' => [ - 'path' => $node->daemonBase, - 'port' => (int) $node->daemonSFTP, - 'container' => 'ptdl-sftp', - ], - 'query' => [ - 'kill_on_fail' => true, - 'fail_limit' => 5, - ], - 'logger' => [ - 'path' => 'logs/', - 'src' => false, - 'level' => 'info', - 'period' => '1d', - 'count' => 3, - ], - 'remote' => [ - 'base' => config('app.url'), - 'download' => route('remote.download'), - 'installed' => route('remote.install'), - ], - 'uploads' => [ - 'size_limit' => $node->upload_size, - ], - 'keys' => [ - $node->daemonSecret, - ], - ]; + return $node->getConfigurationAsJson(); } /** @@ -219,12 +173,7 @@ class NodeController extends BaseController */ public function allocations(Request $request) { - $allocations = Models\Allocation::all(); - if ($allocations->count() < 1) { - throw new NotFoundHttpException('No allocations have been created.'); - } - - return $allocations; + return Models\Allocation::all()->toArray(); } /** @@ -238,18 +187,7 @@ class NodeController extends BaseController */ public function allocationsView(Request $request, $id) { - $query = Models\Allocation::where('assigned_to', $id)->get(); - try { - if (empty($query)) { - throw new NotFoundHttpException('No allocations for that server were found.'); - } - - return $query; - } catch (NotFoundHttpException $ex) { - throw $ex; - } catch (\Exception $ex) { - throw new BadRequestHttpException('There was an issue with the fields passed in the request.'); - } + return Models\Allocation::where('assigned_to', $id)->get()->toArray(); } /** diff --git a/app/Http/Controllers/API/ServerController.php b/app/Http/Controllers/API/ServerController.php index 24a2bc00e..e6b2add2c 100755 --- a/app/Http/Controllers/API/ServerController.php +++ b/app/Http/Controllers/API/ServerController.php @@ -72,10 +72,10 @@ class ServerController extends BaseController public function create(Request $request) { try { - $server = new ServerRepository; - $new = $server->create($request->all()); + $repo = new ServerRepository; + $server = $repo->create($request->all()); - return ['id' => $new]; + return ['id' => $server->id]; } catch (DisplayValidationException $ex) { throw new ResourceException('A validation error occured.', json_decode($ex->getMessage(), true)); } catch (DisplayException $ex) { @@ -101,58 +101,38 @@ class ServerController extends BaseController */ public function view(Request $request, $id) { - $query = Models\Server::where('id', $id); + $server = Models\Server::with('node', 'allocations', 'pack')->where('id', $id)->first(); + if (! $server) { + throw new NotFoundHttpException('No server by that ID was found.'); + } if (! is_null($request->input('fields'))) { - foreach (explode(',', $request->input('fields')) as $field) { - if (! empty($field)) { - $query->addSelect($field); - } + $fields = explode(',', $request->input('fields')); + if (! empty($fields) && is_array($fields)) { + return collect($server)->only($fields); } } - try { - if (! $query->first()) { - throw new NotFoundHttpException('No server by that ID was found.'); - } + if ($request->input('daemon') === 'true') { + try { + $response = $server->node->guzzleClient([ + 'X-Access-Token' => $server->node->daemonSecret, + ])->request('GET', '/servers'); - // Requested Daemon Stats - $server = $query->with( - 'allocations', - 'pack' - )->first(); - if ($request->input('daemon') === 'true') { - $node = Models\Node::findOrFail($server->node_id); - $client = Models\Node::guzzleRequest($node->id); - - $response = $client->request('GET', '/servers', [ - 'headers' => [ - 'X-Access-Token' => $node->daemonSecret, - ], - ]); - - // Only return the daemon token if the request is using HTTPS - if ($request->secure()) { - $server->daemon_token = $server->daemonSecret; - } $server->daemon = json_decode($response->getBody())->{$server->uuid}; - - return $server->toArray(); + } catch (\GuzzleHttp\Exception\TransferException $ex) { + // Couldn't hit the daemon, return what we have though. + $server->daemon = [ + 'error' => 'There was an error encountered while attempting to connect to the remote daemon.', + ]; } - - return $server->toArray(); - } catch (NotFoundHttpException $ex) { - throw $ex; - } catch (\GuzzleHttp\Exception\TransferException $ex) { - // Couldn't hit the daemon, return what we have though. - $server->daemon = [ - 'error' => 'There was an error encountered while attempting to connect to the remote daemon.', - ]; - - return $server->toArray(); - } catch (\Exception $ex) { - throw new BadRequestHttpException('There was an issue with the fields passed in the request.'); } + + $server->allocations->transform(function ($item) { + return collect($item)->except(['created_at', 'updated_at']); + }); + + return $server->toArray(); } /** @@ -179,7 +159,9 @@ class ServerController extends BaseController { try { $server = new ServerRepository; - $server->updateDetails($id, $request->all()); + $server->updateDetails($id, $request->only([ + 'owner', 'name', 'reset_token', + ])); return Models\Server::findOrFail($id); } catch (DisplayValidationException $ex) { @@ -224,7 +206,10 @@ class ServerController extends BaseController { try { $server = new ServerRepository; - $server->changeBuild($id, $request->all()); + $server->changeBuild($id, $request->only([ + 'default', 'add_additional', 'remove_additional', + 'memory', 'swap', 'io', 'cpu', 'disk', + ])); return Models\Server::findOrFail($id); } catch (DisplayValidationException $ex) { diff --git a/app/Http/Controllers/API/ServiceController.php b/app/Http/Controllers/API/ServiceController.php index 81d5a955d..b1c600e27 100755 --- a/app/Http/Controllers/API/ServiceController.php +++ b/app/Http/Controllers/API/ServiceController.php @@ -45,18 +45,11 @@ class ServiceController extends BaseController public function view(Request $request, $id) { - $service = Models\Service::find($id); + $service = Models\Service::with('options.variables', 'options.packs')->find($id); if (! $service) { throw new NotFoundHttpException('No service by that ID was found.'); } - return [ - 'service' => $service, - 'options' => Models\ServiceOptions::select('id', 'name', 'description', 'tag', 'docker_image') - ->where('service_id', $service->id) - ->with('variables') - ->with('packs') - ->get(), - ]; + return $service->toArray(); } } diff --git a/app/Http/Controllers/API/User/InfoController.php b/app/Http/Controllers/API/User/InfoController.php index 844e493dc..6ef84ce50 100644 --- a/app/Http/Controllers/API/User/InfoController.php +++ b/app/Http/Controllers/API/User/InfoController.php @@ -32,19 +32,16 @@ class InfoController extends BaseController { public function me(Request $request) { - return $request->user()->serverAccessCollection()->map(function ($server) { + return $request->user()->serverAccessCollection()->load('allocation', 'option')->map(function ($server) { return [ 'id' => $server->uuidShort, 'uuid' => $server->uuid, 'name' => $server->name, - 'node' => $server->node_idName, - 'ip' => [ - 'set' => $server->ip, - 'alias' => $server->ip_alias, - ], - 'port' => $server->port, - 'service' => $server->a_serviceName, - 'option' => $server->a_serviceOptionName, + 'node' => $server->node->name, + 'ip' => $server->allocation->alias, + 'port' => $server->allocation->port, + 'service' => $server->service->name, + 'option' => $server->option->name, ]; })->all(); } diff --git a/app/Http/Controllers/API/User/ServerController.php b/app/Http/Controllers/API/User/ServerController.php index c6091f0b1..465c7a345 100644 --- a/app/Http/Controllers/API/User/ServerController.php +++ b/app/Http/Controllers/API/User/ServerController.php @@ -25,7 +25,6 @@ namespace Pterodactyl\Http\Controllers\API\User; use Log; -use Auth; use Pterodactyl\Models; use Illuminate\Http\Request; use Pterodactyl\Http\Controllers\API\BaseController; @@ -43,20 +42,14 @@ class ServerController extends BaseController $daemon = [ 'status' => $json->status, 'stats' => $json->proc, - 'query' => $json->query, ]; } catch (\Exception $ex) { $daemon = [ - 'error' => 'An error was encountered while trying to connect to the daemon to collece information. It might be offline.', + 'error' => 'An error was encountered while trying to connect to the daemon to collect information. It might be offline.', ]; Log::error($ex); } - foreach ($server->allocations as &$allocation) { - $allocation->default = ($allocation->id === $server->allocation_id); - unset($allocation->id); - } - return [ 'uuidShort' => $server->uuidShort, 'uuid' => $server->uuid, @@ -70,12 +63,18 @@ class ServerController extends BaseController 'cpu' => $server->cpu, 'oom_disabled' => (bool) $server->oom_disabled, ], - 'allocations' => $server->allocations, + 'allocations' => $server->allocations->map(function ($item) use ($server) { + return [ + 'ip' => $item->alias, + 'port' => $item->port, + 'default' => ($item->id === $server->allocation_id), + ]; + }), 'sftp' => [ - 'username' => (Auth::user()->can('view-sftp', $server)) ? $server->username : null, + 'username' => ($request->user()->can('view-sftp', $server)) ? $server->username : null, ], 'daemon' => [ - 'token' => ($request->secure()) ? $server->daemonSecret : false, + 'token' => $server->daemonSecret, 'response' => $daemon, ], ]; diff --git a/app/Http/Controllers/API/UserController.php b/app/Http/Controllers/API/UserController.php index a330ef61c..14c212f77 100755 --- a/app/Http/Controllers/API/UserController.php +++ b/app/Http/Controllers/API/UserController.php @@ -75,31 +75,27 @@ class UserController extends BaseController */ public function view(Request $request, $id) { - $query = Models\User::where((is_numeric($id) ? 'id' : 'email'), $id); + $user = Models\User::with('servers')->where((is_numeric($id) ? 'id' : 'email'), $id)->first(); + if (! $user->first()) { + throw new NotFoundHttpException('No user by that ID was found.'); + } + + $user->servers->transform(function ($item) { + return collect($item)->only([ + 'id', 'node_id', 'uuidShort', + 'uuid', 'name', 'suspended', + 'owner_id', + ]); + }); if (! is_null($request->input('fields'))) { - foreach (explode(',', $request->input('fields')) as $field) { - if (! empty($field)) { - $query->addSelect($field); - } + $fields = explode(',', $request->input('fields')); + if (! empty($fields) && is_array($fields)) { + return collect($user)->only($fields); } } - try { - if (! $query->first()) { - throw new NotFoundHttpException('No user by that ID was found.'); - } - - $user = $query->first(); - $userArray = $user->toArray(); - $userArray['servers'] = Models\Server::select('id', 'uuid', 'node', 'suspended')->where('owner', $user->id)->get(); - - return $userArray; - } catch (NotFoundHttpException $ex) { - throw $ex; - } catch (\Exception $ex) { - throw new BadRequestHttpException('There was an issue with the fields passed in the request.'); - } + return $user->toArray(); } /** @@ -123,7 +119,9 @@ class UserController extends BaseController try { $user = new UserRepository; $create = $user->create($request->only([ - 'email', 'username', 'name_first', 'name_last', 'password', 'root_admin', 'custom_id', + 'email', 'username', 'name_first', + 'name_last', 'password', + 'root_admin', 'custom_id', ])); $create = $user->create($request->input('email'), $request->input('password'), $request->input('admin'), $request->input('custom_id')); @@ -160,7 +158,9 @@ class UserController extends BaseController try { $user = new UserRepository; $user->update($id, $request->only([ - 'username', 'email', 'name_first', 'name_last', 'password', 'root_admin', 'language', + 'username', 'email', 'name_first', + 'name_last', 'password', + 'root_admin', 'language', ])); return Models\User::findOrFail($id); diff --git a/app/Http/Controllers/Admin/ServersController.php b/app/Http/Controllers/Admin/ServersController.php index b68f8d01c..ab73937c4 100644 --- a/app/Http/Controllers/Admin/ServersController.php +++ b/app/Http/Controllers/Admin/ServersController.php @@ -86,16 +86,9 @@ class ServersController extends Controller { try { $server = new ServerRepository; - $response = $server->create($request->only([ - 'owner', 'name', 'memory', 'swap', - 'node', 'ip', 'port', 'allocation', - 'cpu', 'disk', 'service', - 'option', 'location', 'pack', - 'startup', 'custom_image_name', - 'auto_deploy', 'custom_id', - ])); + $response = $server->create($request->except('_token')); - return redirect()->route('admin.servers.view', ['id' => $response]); + return redirect()->route('admin.servers.view', ['id' => $response->id]); } catch (DisplayValidationException $ex) { return redirect()->route('admin.servers.new')->withErrors(json_decode($ex->getMessage()))->withInput(); } catch (DisplayException $ex) { @@ -188,7 +181,7 @@ class ServersController extends Controller ], 500); } - $option = Models\ServiceOptions::with('variables', ['packs' => function ($query) { + $option = Models\ServiceOptions::with('variables')->with(['packs' => function ($query) { $query->where('selectable', true); }])->findOrFail($request->input('option')); diff --git a/app/Http/Middleware/APISecretToken.php b/app/Http/Middleware/APISecretToken.php index a1c203c9d..25bf891ba 100755 --- a/app/Http/Middleware/APISecretToken.php +++ b/app/Http/Middleware/APISecretToken.php @@ -121,7 +121,7 @@ class APISecretToken extends Authorization // Log the Route Access APILogService::log($request, null, true); - return Auth::loginUsingId($key->user); + return Auth::loginUsingId($key->user_id); } protected function _generateHMAC($body, $key) diff --git a/app/Models/Node.php b/app/Models/Node.php index 3da24531c..78e0828f7 100644 --- a/app/Models/Node.php +++ b/app/Models/Node.php @@ -66,28 +66,12 @@ class Node extends Model * @var array */ protected $fillable = [ - 'uuid', - 'uuidShort', - 'node_id', - 'name', - 'suspended', - 'owner_id', - 'memory', - 'swap', - 'disk', - 'io', - 'cpu', - 'oom_disabled', - 'allocation_id', - 'service_id', - 'option_id', - 'pack_id', - 'startup', - 'daemonSecret', - 'image', - 'username', - 'sftp_password', - 'installed', + 'public', 'name', 'location_id', + 'fqdn', 'scheme', 'memory', + 'memory_overallocate', 'disk', + 'disk_overallocate', 'upload_size', + 'daemonSecret', 'daemonBase', + 'daemonSFTP', 'daemonListen', ]; /** diff --git a/app/Repositories/APIRepository.php b/app/Repositories/APIRepository.php index 14348b2db..71fbb88b5 100644 --- a/app/Repositories/APIRepository.php +++ b/app/Repositories/APIRepository.php @@ -62,8 +62,8 @@ class APIRepository // Node Management Routes 'nodes.list', + 'nodes.view', 'nodes.create', - 'nodes.list', 'nodes.allocations', 'nodes.delete', diff --git a/app/Repositories/NodeRepository.php b/app/Repositories/NodeRepository.php index e409a011a..1af316b23 100644 --- a/app/Repositories/NodeRepository.php +++ b/app/Repositories/NodeRepository.php @@ -44,7 +44,7 @@ class NodeRepository // Validate Fields $validator = Validator::make($data, [ 'name' => 'required|regex:/^([\w .-]{1,100})$/', - 'location' => 'required|numeric|min:1|exists:locations,id', + 'location_id' => 'required|numeric|min:1|exists:locations,id', 'public' => 'required|numeric|between:0,1', 'fqdn' => 'required|string|unique:nodes,fqdn', 'scheme' => 'required|regex:/^(http(s)?)$/', @@ -91,7 +91,7 @@ class NodeRepository // Validate Fields $validator = $validator = Validator::make($data, [ 'name' => 'regex:/^([\w .-]{1,100})$/', - 'location' => 'numeric|min:1|exists:locations,id', + 'location_id' => 'numeric|min:1|exists:locations,id', 'public' => 'numeric|between:0,1', 'fqdn' => 'string|unique:nodes,fqdn,' . $id, 'scheme' => 'regex:/^(http(s)?)$/', @@ -210,13 +210,13 @@ class NodeRepository foreach ($portBlock as $assignPort) { $alloc = Models\Allocation::firstOrNew([ - 'node' => $node->id, + 'node_id' => $node->id, 'ip' => $ip, 'port' => $assignPort, ]); if (! $alloc->exists) { $alloc->fill([ - 'node' => $node->id, + 'node_id' => $node->id, 'ip' => $ip, 'port' => $assignPort, 'ip_alias' => $setAlias, @@ -227,13 +227,13 @@ class NodeRepository } } else { $alloc = Models\Allocation::firstOrNew([ - 'node' => $node->id, + 'node_id' => $node->id, 'ip' => $ip, 'port' => $port, ]); if (! $alloc->exists) { $alloc->fill([ - 'node' => $node->id, + 'node_id' => $node->id, 'ip' => $ip, 'port' => $port, 'ip_alias' => $setAlias, @@ -269,7 +269,7 @@ class NodeRepository ]); // Delete Allocations - Models\Allocation::where('node', $node->id)->delete(); + Models\Allocation::where('node_id', $node->id)->delete(); // Delete configure tokens Models\NodeConfigurationToken::where('node', $node->id)->delete(); diff --git a/app/Repositories/ServerRepository.php b/app/Repositories/ServerRepository.php index 13caf0a5c..6e441666a 100644 --- a/app/Repositories/ServerRepository.php +++ b/app/Repositories/ServerRepository.php @@ -90,17 +90,17 @@ class ServerRepository 'io' => 'required|numeric|min:10|max:1000', 'cpu' => 'required|numeric|min:0', 'disk' => 'required|numeric|min:0', - 'service' => 'required|numeric|min:1|exists:services,id', - 'option' => 'required|numeric|min:1|exists:service_options,id', - 'location' => 'required|numeric|min:1|exists:locations,id', - 'pack' => 'sometimes|nullable|numeric|min:0', + 'service_id' => 'required|numeric|min:1|exists:services,id', + 'option_id' => 'required|numeric|min:1|exists:service_options,id', + 'location_id' => 'required|numeric|min:1|exists:locations,id', + 'pack_id' => 'sometimes|nullable|numeric|min:0', 'startup' => 'string', 'custom_image_name' => 'required_if:use_custom_image,on', 'auto_deploy' => 'sometimes|boolean', 'custom_id' => 'sometimes|required|numeric|unique:servers,id', ]); - $validator->sometimes('node', 'bail|required|numeric|min:1|exists:nodes,id', function ($input) { + $validator->sometimes('node_id', 'bail|required|numeric|min:1|exists:nodes,id', function ($input) { return ! ($input->auto_deploy); }); @@ -112,7 +112,7 @@ class ServerRepository return ! $input->auto_deploy && ! $input->allocation; }); - $validator->sometimes('allocation', 'numeric|exists:allocations,id', function ($input) { + $validator->sometimes('allocation_id', 'numeric|exists:allocations,id', function ($input) { return ! ($input->auto_deploy || ($input->port && $input->ip)); }); @@ -131,19 +131,19 @@ class ServerRepository if (isset($data['auto_deploy']) && in_array($data['auto_deploy'], [true, 1, '1'])) { // This is an auto-deployment situation // Ignore any other passed node data - unset($data['node'], $data['ip'], $data['port'], $data['allocation']); + unset($data['node_id'], $data['ip'], $data['port'], $data['allocation_id']); $autoDeployed = true; - $node = DeploymentService::smartRandomNode($data['memory'], $data['disk'], $data['location']); + $node = DeploymentService::smartRandomNode($data['memory'], $data['disk'], $data['location_id']); $allocation = DeploymentService::randomAllocation($node->id); } else { - $node = Models\Node::findOrFail($data['node']); + $node = Models\Node::findOrFail($data['node_id']); } // Verify IP & Port are a.) free and b.) assigned to the node. // We know the node exists because of 'exists:nodes,id' in the validation if (! $autoDeployed) { - if (! isset($data['allocation'])) { + if (! isset($data['allocation_id'])) { $allocation = Models\Allocation::where('ip', $data['ip'])->where('port', $data['port'])->where('node', $data['node'])->whereNull('assigned_to')->first(); } else { $allocation = Models\Allocation::where('id', $data['allocation'])->where('node', $data['node'])->whereNull('assigned_to')->first(); @@ -165,12 +165,12 @@ class ServerRepository } // Validate the Pack - if ($data['pack'] == 0) { - $data['pack'] = null; + if ($data['pack_id'] == 0) { + $data['pack_id'] = null; } - if (! is_null($data['pack'])) { - $pack = Models\ServicePack::where('id', $data['pack'])->where('option', $data['option'])->first(); + if (! is_null($data['pack_id'])) { + $pack = Models\ServicePack::where('id', $data['pack_id'])->where('option', $data['option_id'])->first(); if (! $pack) { throw new DisplayException('The requested service pack does not seem to exist for this combination.'); } @@ -180,7 +180,7 @@ class ServerRepository $service = Models\Service::find($option->service_id); // Check those Variables - $variables = Models\ServiceVariables::where('option_id', $data['option'])->get(); + $variables = Models\ServiceVariables::where('option_id', $data['option_id'])->get(); $variableList = []; if ($variables) { foreach ($variables as $variable) { @@ -254,10 +254,10 @@ class ServerRepository $server->fill([ 'uuid' => $genUuid, 'uuidShort' => $genShortUuid, - 'node' => $node->id, + 'node_id' => $node->id, 'name' => $data['name'], 'suspended' => 0, - 'owner' => $user->id, + 'owner_id' => $user->id, 'memory' => $data['memory'], 'swap' => $data['swap'], 'disk' => $data['disk'], @@ -265,9 +265,9 @@ class ServerRepository 'cpu' => $data['cpu'], 'oom_disabled' => (isset($data['oom_disabled'])) ? true : false, 'allocation' => $allocation->id, - 'service' => $data['service'], - 'option' => $data['option'], - 'pack' => $data['pack'], + 'service_id' => $data['service_id'], + 'option_id' => $data['option_id'], + 'pack_id' => $data['pack_id'], 'startup' => $data['startup'], 'daemonSecret' => $uuid->generate('servers', 'daemonSecret'), 'image' => (isset($data['custom_image_name'])) ? $data['custom_image_name'] : $option->docker_image, @@ -277,7 +277,7 @@ class ServerRepository $server->save(); // Mark Allocation in Use - $allocation->assigned_to = $server->id; + $allocation->server_id = $server->id; $allocation->save(); // Add Variables @@ -329,7 +329,7 @@ class ServerRepository DB::commit(); - return $server->id; + return $server; } catch (TransferException $ex) { DB::rollBack(); throw new DisplayException('There was an error while attempting to connect to the daemon to add this server.', $ex); @@ -782,8 +782,8 @@ class ServerRepository DB::beginTransaction(); try { // Unassign Allocations - Models\Allocation::where('assigned_to', $server->id)->update([ - 'assigned_to' => null, + Models\Allocation::where('server_id', $server->id)->update([ + 'server_id' => null, ]); // Remove Variables diff --git a/resources/views/admin/nodes/new.blade.php b/resources/views/admin/nodes/new.blade.php index 07227daa7..37add9812 100644 --- a/resources/views/admin/nodes/new.blade.php +++ b/resources/views/admin/nodes/new.blade.php @@ -44,9 +44,9 @@
    - @foreach($locations as $location) - + @endforeach
    diff --git a/resources/views/admin/nodes/view.blade.php b/resources/views/admin/nodes/view.blade.php index 5fa054703..9b569d5c8 100644 --- a/resources/views/admin/nodes/view.blade.php +++ b/resources/views/admin/nodes/view.blade.php @@ -138,9 +138,9 @@
    - @foreach($locations as $location) - + @endforeach
    diff --git a/resources/views/admin/servers/new.blade.php b/resources/views/admin/servers/new.blade.php index 2c3d655a2..2fe12b1dd 100644 --- a/resources/views/admin/servers/new.blade.php +++ b/resources/views/admin/servers/new.blade.php @@ -63,7 +63,7 @@
    - @foreach($locations as $location) @@ -75,7 +75,7 @@
    {{ $server->uuidShort }} {{ $server->name }}{{ $server->node_idName }}{{ $server->node->name }} {{ $server->username }} @if($server->suspended === 0)Active@elseSuspended@endif
    User Image{{ $user->username }} - {{ $user->email }}User Image{{ $subuser->user->username }} + {{ $subuser->user->email }} - @if($user->use_totp) + @if($subuser->user->use_totp) @else @endif - + - +
    {{ $database->name }} {{ $database->host }}:{{ $database->port }} {{ $database->username }}{{ $database->c_databases }}{{ $database->databases_count }} @if(is_null($database->node))unlinked@else{{ $database->node->name }}@endif
    {{ $location->short }} {{ $location->long }}{{ $location->a_nodeCount }}{{ $location->a_serverCount }}{{ $location->nodes_count }}{{ $location->servers_count }}