From 9a21584c42e2dcd11f2ba3476bab01493fc6810e Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sun, 13 Sep 2020 09:59:54 -0700 Subject: [PATCH] Cleanup mount code; automatically include the mount in the configuration --- .../Repository/PackRepositoryInterface.php | 3 +-- app/Http/Controllers/Admin/PackController.php | 3 ++- app/Models/MountNode.php | 23 +++++++++++++++++++ app/Models/MountServer.php | 23 +++++++++++++++++++ app/Models/Node.php | 10 ++++++++ app/Models/Server.php | 4 ++-- app/Repositories/Eloquent/PackRepository.php | 1 - .../ServerConfigurationStructureService.php | 22 +++++++----------- 8 files changed, 69 insertions(+), 20 deletions(-) create mode 100644 app/Models/MountNode.php create mode 100644 app/Models/MountServer.php diff --git a/app/Contracts/Repository/PackRepositoryInterface.php b/app/Contracts/Repository/PackRepositoryInterface.php index f3ac27f96..a0e9f7a5d 100644 --- a/app/Contracts/Repository/PackRepositoryInterface.php +++ b/app/Contracts/Repository/PackRepositoryInterface.php @@ -4,9 +4,8 @@ namespace Pterodactyl\Contracts\Repository; use Pterodactyl\Models\Pack; use Illuminate\Contracts\Pagination\LengthAwarePaginator; -use Pterodactyl\Contracts\Repository\Attributes\SearchableInterface; -interface PackRepositoryInterface extends RepositoryInterface, SearchableInterface +interface PackRepositoryInterface extends RepositoryInterface { /** * Return a pack with the associated server models attached to it. diff --git a/app/Http/Controllers/Admin/PackController.php b/app/Http/Controllers/Admin/PackController.php index 98191222e..454d6a55d 100644 --- a/app/Http/Controllers/Admin/PackController.php +++ b/app/Http/Controllers/Admin/PackController.php @@ -12,6 +12,7 @@ namespace Pterodactyl\Http\Controllers\Admin; use Illuminate\Http\Request; use Pterodactyl\Models\Pack; use Prologue\Alerts\AlertsMessageBag; +use Spatie\QueryBuilder\QueryBuilder; use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Services\Packs\ExportPackService; use Pterodactyl\Services\Packs\PackUpdateService; @@ -114,7 +115,7 @@ class PackController extends Controller public function index(Request $request) { return view('admin.packs.index', [ - 'packs' => $this->repository->setSearchTerm($request->input('query'))->paginateWithEggAndServerCount(), + 'packs' => $this->repository->paginateWithEggAndServerCount(), ]); } diff --git a/app/Models/MountNode.php b/app/Models/MountNode.php new file mode 100644 index 000000000..a897dd6dd --- /dev/null +++ b/app/Models/MountNode.php @@ -0,0 +1,23 @@ + $this->daemonSFTP, ], ], + 'allowed_mounts' => $this->mounts->pluck('source')->toArray(), 'remote' => route('index'), ]; } @@ -219,6 +221,14 @@ class Node extends Model ); } + /** + * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough + */ + public function mounts() + { + return $this->hasManyThrough(Mount::class, MountNode::class, 'node_id', 'id', 'id', 'mount_id'); + } + /** * Gets the location associated with a node. * diff --git a/app/Models/Server.php b/app/Models/Server.php index 91ba9621a..225a04be1 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -367,10 +367,10 @@ class Server extends Model /** * Returns all mounts that have this server has mounted. * - * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany + * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough */ public function mounts() { - return $this->belongsToMany(Mount::class); + return $this->hasManyThrough(Mount::class, MountServer::class, 'server_id', 'id', 'id', 'mount_id'); } } diff --git a/app/Repositories/Eloquent/PackRepository.php b/app/Repositories/Eloquent/PackRepository.php index bdd5eca6a..6cf3996bd 100644 --- a/app/Repositories/Eloquent/PackRepository.php +++ b/app/Repositories/Eloquent/PackRepository.php @@ -47,7 +47,6 @@ class PackRepository extends EloquentRepository implements PackRepositoryInterfa public function paginateWithEggAndServerCount(): LengthAwarePaginator { return $this->getBuilder()->with('egg')->withCount('servers') - ->search($this->getSearchTerm()) ->paginate(50, $this->getColumns()); } } diff --git a/app/Services/Servers/ServerConfigurationStructureService.php b/app/Services/Servers/ServerConfigurationStructureService.php index f975f7e8f..60630d064 100644 --- a/app/Services/Servers/ServerConfigurationStructureService.php +++ b/app/Services/Servers/ServerConfigurationStructureService.php @@ -9,6 +9,7 @@ namespace Pterodactyl\Services\Servers; +use Pterodactyl\Models\Mount; use Pterodactyl\Models\Server; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; @@ -66,22 +67,9 @@ class ServerConfigurationStructureService * * @param \Pterodactyl\Models\Server $server * @return array - * - * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException */ protected function returnCurrentFormat(Server $server) { - $mounts = $server->mounts; - foreach ($mounts as $mount) { - unset($mount->id); - unset($mount->uuid); - unset($mount->name); - unset($mount->description); - $mount->read_only = $mount->read_only == 1; - unset($mount->user_mountable); - unset($mount->pivot); - } - return [ 'uuid' => $server->uuid, 'suspended' => (bool) $server->suspended, @@ -108,7 +96,13 @@ class ServerConfigurationStructureService ], 'mappings' => $server->getAllocationMappings(), ], - 'mounts' => $mounts, + 'mounts' => $server->mounts->map(function (Mount $mount) { + return [ + 'source' => $mount->source, + 'target' => $mount->target, + 'read_only' => $mount->read_only, + ]; + }), ]; }