diff --git a/app/Http/Controllers/Admin/MountController.php b/app/Http/Controllers/Admin/MountController.php index 26cc6d36..520b08d1 100644 --- a/app/Http/Controllers/Admin/MountController.php +++ b/app/Http/Controllers/Admin/MountController.php @@ -2,10 +2,12 @@ namespace Pterodactyl\Http\Controllers\Admin; +use Ramsey\Uuid\Uuid; use Illuminate\Http\Request; +use Pterodactyl\Models\Nest; use Pterodactyl\Models\Mount; +use Pterodactyl\Models\Location; use Prologue\Alerts\AlertsMessageBag; -use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Services\Mounts\MountUpdateService; use Pterodactyl\Http\Requests\Admin\MountFormRequest; @@ -37,21 +39,6 @@ class MountController extends Controller */ protected $repository; - /** - * @var \Pterodactyl\Services\Mounts\MountCreationService - */ - protected $creationService; - - /** - * @var \Pterodactyl\Services\Mounts\MountDeletionService - */ - protected $deletionService; - - /** - * @var \Pterodactyl\Services\Mounts\MountUpdateService - */ - protected $updateService; - /** * MountController constructor. * @@ -59,26 +46,17 @@ class MountController extends Controller * @param \Pterodactyl\Contracts\Repository\NestRepositoryInterface $nestRepository * @param \Pterodactyl\Contracts\Repository\LocationRepositoryInterface $locationRepository * @param \Pterodactyl\Repositories\Eloquent\MountRepository $repository - * @param \Pterodactyl\Services\Mounts\MountCreationService $creationService - * @param \Pterodactyl\Services\Mounts\MountDeletionService $deletionService - * @param \Pterodactyl\Services\Mounts\MountUpdateService $updateService */ public function __construct( AlertsMessageBag $alert, NestRepositoryInterface $nestRepository, LocationRepositoryInterface $locationRepository, - MountRepository $repository, - MountCreationService $creationService, - MountDeletionService $deletionService, - MountUpdateService $updateService + MountRepository $repository ) { $this->alert = $alert; $this->nestRepository = $nestRepository; $this->locationRepository = $locationRepository; $this->repository = $repository; - $this->creationService = $creationService; - $this->deletionService = $deletionService; - $this->updateService = $updateService; } /** @@ -103,11 +81,8 @@ class MountController extends Controller */ public function view($id) { - $nests = $this->nestRepository->all(); - $nests->load('eggs'); - - $locations = $this->locationRepository->all(); - $locations->load('nodes'); + $nests = Nest::query()->with('eggs')->get(); + $locations = Location::query()->with('nodes')->get(); return view('admin.mounts.view', [ 'mount' => $this->repository->getWithRelations($id), @@ -126,7 +101,11 @@ class MountController extends Controller */ public function create(MountFormRequest $request) { - $mount = $this->creationService->handle($request->normalize()); + /** @var \Pterodactyl\Models\Mount $mount */ + $mount = Mount::query()->create(array_merge($request->validated(), [ + 'uuid' => Uuid::uuid4()->toString(), + ])); + $this->alert->success('Mount was created successfully.')->flash(); return redirect()->route('admin.mounts.view', $mount->id); @@ -147,7 +126,8 @@ class MountController extends Controller return $this->delete($mount); } - $this->updateService->handle($mount->id, $request->normalize()); + $mount->forceFill($request->validated())->save(); + $this->alert->success('Mount was updated successfully.')->flash(); return redirect()->route('admin.mounts.view', $mount->id); @@ -163,15 +143,9 @@ class MountController extends Controller */ public function delete(Mount $mount) { - try { - $this->deletionService->handle($mount->id); + $mount->delete(); - return redirect()->route('admin.mounts'); - } catch (DisplayException $ex) { - $this->alert->danger($ex->getMessage())->flash(); - } - - return redirect()->route('admin.mounts.view', $mount->id); + return redirect()->route('admin.mounts'); } /** @@ -188,11 +162,12 @@ class MountController extends Controller ]); $eggs = $validatedData['eggs'] ?? []; - if (sizeof($eggs) > 0) { - $mount->eggs()->attach(array_map('intval', $eggs)); - $this->alert->success('Mount was updated successfully.')->flash(); + if (count($eggs) > 0) { + $mount->eggs()->attach($eggs); } + $this->alert->success('Mount was updated successfully.')->flash(); + return redirect()->route('admin.mounts.view', $mount->id); } @@ -205,16 +180,15 @@ class MountController extends Controller */ public function addNodes(Request $request, Mount $mount) { - $validatedData = $request->validate([ - 'nodes' => 'required|exists:nodes,id', - ]); + $data = $request->validate(['nodes' => 'required|exists:nodes,id']); - $nodes = $validatedData['nodes'] ?? []; - if (sizeof($nodes) > 0) { - $mount->nodes()->attach(array_map('intval', $nodes)); - $this->alert->success('Mount was updated successfully.')->flash(); + $nodes = $data['nodes'] ?? []; + if (count($nodes) > 0) { + $mount->nodes()->attach($nodes); } + $this->alert->success('Mount was updated successfully.')->flash(); + return redirect()->route('admin.mounts.view', $mount->id); } diff --git a/app/Http/Controllers/Admin/ServersController.php b/app/Http/Controllers/Admin/ServersController.php index 94df5dc2..400218e0 100644 --- a/app/Http/Controllers/Admin/ServersController.php +++ b/app/Http/Controllers/Admin/ServersController.php @@ -12,6 +12,7 @@ namespace Pterodactyl\Http\Controllers\Admin; use Illuminate\Support\Arr; use Illuminate\Http\Request; use Pterodactyl\Models\User; +use Pterodactyl\Models\Mount; use Pterodactyl\Models\Server; use Prologue\Alerts\AlertsMessageBag; use GuzzleHttp\Exception\RequestException; @@ -412,12 +413,18 @@ class ServersController extends Controller * Add a mount to a server. * * @param Server $server - * @param int $mount_id + * @param \Pterodactyl\Models\Mount $mount * @return \Illuminate\Http\RedirectResponse + * + * @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException + * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException */ - public function addMount(Server $server, int $mount_id) + public function addMount(Server $server, Mount $mount) { - $server->mounts()->attach($mount_id); + $server->mounts()->updateOrCreate([ + 'mount_id' => $mount->id, + 'server_id' => $server->id, + ]); $data = $this->serverConfigurationStructureService->handle($server); @@ -438,15 +445,18 @@ class ServersController extends Controller * Remove a mount from a server. * * @param Server $server - * @param int $mount_id + * @param \Pterodactyl\Models\Mount $mount * @return \Illuminate\Http\RedirectResponse * - * @throws DaemonConnectionException + * @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException */ - public function deleteMount(Server $server, int $mount_id) + public function deleteMount(Server $server, Mount $mount) { - $server->mounts()->detach($mount_id); + $server->mounts() + ->where('mount_id', $mount->id) + ->where('server_id', $server->id) + ->delete(); $data = $this->serverConfigurationStructureService->handle($server); diff --git a/app/Http/Requests/Admin/MountFormRequest.php b/app/Http/Requests/Admin/MountFormRequest.php index b6647b16..bd94a633 100644 --- a/app/Http/Requests/Admin/MountFormRequest.php +++ b/app/Http/Requests/Admin/MountFormRequest.php @@ -1,11 +1,4 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Http\Requests\Admin; diff --git a/app/Models/EggMount.php b/app/Models/EggMount.php new file mode 100644 index 00000000..cd85673c --- /dev/null +++ b/app/Models/EggMount.php @@ -0,0 +1,21 @@ +repository = $repository; - } - - /** - * Create a new mount. - * - * @param array $data - * @return \Pterodactyl\Models\Mount - * - * @throws \Exception - * @throws \Pterodactyl\Exceptions\Model\DataValidationException - */ - public function handle(array $data) - { - return $this->repository->create(array_merge($data, [ - 'uuid' => Uuid::uuid4()->toString(), - ]), true, true); - } -} diff --git a/app/Services/Mounts/MountDeletionService.php b/app/Services/Mounts/MountDeletionService.php deleted file mode 100644 index 0850e685..00000000 --- a/app/Services/Mounts/MountDeletionService.php +++ /dev/null @@ -1,40 +0,0 @@ -repository = $repository; - } - - /** - * Delete an existing location. - * - * @param int|\Pterodactyl\Models\Mount $mount - * @return int|null - */ - public function handle($mount) - { - $mount = ($mount instanceof Mount) ? $mount->id : $mount; - - Assert::integerish($mount, 'First argument passed to handle must be numeric or an instance of ' . Mount::class . ', received %s.'); - - return $this->repository->delete($mount); - } -} diff --git a/app/Services/Mounts/MountUpdateService.php b/app/Services/Mounts/MountUpdateService.php deleted file mode 100644 index a66f1ea9..00000000 --- a/app/Services/Mounts/MountUpdateService.php +++ /dev/null @@ -1,41 +0,0 @@ -repository = $repository; - } - - /** - * Update an existing location. - * - * @param int|\Pterodactyl\Models\Mount $mount - * @param array $data - * @return \Pterodactyl\Models\Mount - * - * @throws \Pterodactyl\Exceptions\Model\DataValidationException - * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException - */ - public function handle($mount, array $data) - { - $mount = ($mount instanceof Mount) ? $mount->id : $mount; - - return $this->repository->update($mount, $data); - } -}