From 02458c909d3110a56004e3da5ed19afe16077f12 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sun, 5 Feb 2017 19:19:46 -0500 Subject: [PATCH] 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 @@