From 0eb29dac9c449b4abe280606066a43ec3fa6538c Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Thu, 21 May 2020 14:23:12 -0600 Subject: [PATCH] Add mount_server table, fix wrong field type on other many to many tables, add routes for mounting and unmounting mounts on a server, finish server admin mounts page --- .../Admin/Servers/ServerViewController.php | 2 + .../Controllers/Admin/ServersController.php | 30 +++++++++++++ app/Models/Mount.php | 15 ++++++- app/Models/Server.php | 11 +++++ .../2020_05_20_234655_add_mounts_table.php | 4 +- ...20_05_21_192756_add_mount_server_table.php | 33 ++++++++++++++ .../views/admin/servers/view/mounts.blade.php | 44 +++++++++++-------- routes/admin.php | 2 + 8 files changed, 119 insertions(+), 22 deletions(-) create mode 100644 database/migrations/2020_05_21_192756_add_mount_server_table.php diff --git a/app/Http/Controllers/Admin/Servers/ServerViewController.php b/app/Http/Controllers/Admin/Servers/ServerViewController.php index acec1cdba..67531fa5f 100644 --- a/app/Http/Controllers/Admin/Servers/ServerViewController.php +++ b/app/Http/Controllers/Admin/Servers/ServerViewController.php @@ -178,6 +178,8 @@ class ServerViewController extends Controller */ public function mounts(Request $request, Server $server) { + $server->load('mounts'); + return $this->view->make('admin.servers.view.mounts', [ 'mounts' => $this->mountRepository->getMountListForServer($server), 'server' => $server, diff --git a/app/Http/Controllers/Admin/ServersController.php b/app/Http/Controllers/Admin/ServersController.php index bcd0913a1..741a61e46 100644 --- a/app/Http/Controllers/Admin/ServersController.php +++ b/app/Http/Controllers/Admin/ServersController.php @@ -378,4 +378,34 @@ class ServersController extends Controller return response('', 204); } + + /** + * Add a mount to a server. + * + * @param Server $server + * @param int $mount_id + * @return \Illuminate\Http\RedirectResponse + */ + public function addMount(Server $server, int $mount_id) + { + $server->mounts()->attach($mount_id); + + $this->alert->success('Mount was added successfully.')->flash(); + return redirect()->route('admin.servers.view.mounts', $server->id); + } + + /** + * Remove a mount from a server. + * + * @param Server $server + * @param int $mount_id + * @return \Illuminate\Http\RedirectResponse + */ + public function deleteMount(Server $server, int $mount_id) + { + $server->mounts()->detach($mount_id); + + $this->alert->success('Mount was removed successfully.')->flash(); + return redirect()->route('admin.servers.view.mounts', $server->id); + } } diff --git a/app/Models/Mount.php b/app/Models/Mount.php index c580697c5..ed608c56d 100644 --- a/app/Models/Mount.php +++ b/app/Models/Mount.php @@ -12,8 +12,9 @@ namespace Pterodactyl\Models; * @property bool $read_only * @property bool $user_mountable * - * @property \Illuminate\Database\Eloquent\Relations\BelongsToMany $eggs - * @property \Illuminate\Database\Eloquent\Relations\BelongsToMany $nodes + * @property \Pterodactyl\Models\Egg[]|\Illuminate\Database\Eloquent\Collection $eggs + * @property \Pterodactyl\Models\Node[]|\Illuminate\Database\Eloquent\Collection $nodes + * @property \Pterodactyl\Models\Server[]|\Illuminate\Database\Eloquent\Collection $servers */ class Mount extends Model { @@ -94,4 +95,14 @@ class Mount extends Model { return $this->belongsToMany(Node::class); } + + /** + * Returns all servers that have this mount assigned. + * + * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany + */ + public function servers() + { + return $this->belongsToMany(Server::class); + } } diff --git a/app/Models/Server.php b/app/Models/Server.php index d8428cb86..8f15bfcf1 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -53,6 +53,7 @@ use Znck\Eloquent\Traits\BelongsToThrough; * @property \Pterodactyl\Models\DaemonKey[]|\Illuminate\Database\Eloquent\Collection $keys * @property \Pterodactyl\Models\ServerTransfer $transfer * @property \Pterodactyl\Models\Backup[]|\Illuminate\Database\Eloquent\Collection $backups + * @property \Pterodactyl\Models\Mount[]|\Illuminate\Database\Eloquent\Collection $mounts */ class Server extends Model { @@ -351,4 +352,14 @@ class Server extends Model { return $this->hasMany(Backup::class); } + + /** + * Returns all mounts that have this server has mounted. + * + * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany + */ + public function mounts() + { + return $this->belongsToMany(Mount::class); + } } diff --git a/database/migrations/2020_05_20_234655_add_mounts_table.php b/database/migrations/2020_05_20_234655_add_mounts_table.php index e9d169692..09846a0a5 100644 --- a/database/migrations/2020_05_20_234655_add_mounts_table.php +++ b/database/migrations/2020_05_20_234655_add_mounts_table.php @@ -26,14 +26,14 @@ class AddMountsTable extends Migration Schema::create('egg_mount', function (Blueprint $table) { $table->integer('egg_id'); - $table->char('mount_id', 36); + $table->integer('mount_id'); $table->unique(['egg_id', 'mount_id']); }); Schema::create('mount_node', function (Blueprint $table) { $table->integer('node_id'); - $table->char('mount_id', 36); + $table->integer('mount_id'); $table->unique(['node_id', 'mount_id']); }); diff --git a/database/migrations/2020_05_21_192756_add_mount_server_table.php b/database/migrations/2020_05_21_192756_add_mount_server_table.php new file mode 100644 index 000000000..3d79ab3f5 --- /dev/null +++ b/database/migrations/2020_05_21_192756_add_mount_server_table.php @@ -0,0 +1,33 @@ +integer('server_id'); + $table->integer('mount_id'); + + $table->unique(['server_id', 'mount_id']); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('mount_server'); + } +} diff --git a/resources/views/admin/servers/view/mounts.blade.php b/resources/views/admin/servers/view/mounts.blade.php index 174b5ce81..a34823169 100644 --- a/resources/views/admin/servers/view/mounts.blade.php +++ b/resources/views/admin/servers/view/mounts.blade.php @@ -41,21 +41,33 @@ {{ $mount->name }} {{ $mount->source }} {{ $mount->target }} - - @if ($mount->id == 2) - Unmounted - @else - Mounted - @endif - - - @if ($mount->id == 2) - - @else - - @endif - + @if (! in_array($mount->id, $server->mounts->pluck('id')->toArray())) + + Unmounted + + + +
+ {!! csrf_field() !!} + + +
+ + @else + + Mounted + + + +
+ @method('DELETE') + {!! csrf_field() !!} + + +
+ + @endif @endforeach @@ -64,7 +76,3 @@ @endsection - -@section('footer-scripts') - @parent -@endsection diff --git a/routes/admin.php b/routes/admin.php index 6656eac3c..32ce93c83 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -123,6 +123,7 @@ Route::group(['prefix' => 'servers'], function () { Route::post('/view/{server}/build', 'ServersController@updateBuild'); Route::post('/view/{server}/startup', 'ServersController@saveStartup'); Route::post('/view/{server}/database', 'ServersController@newDatabase'); + Route::post('/view/{server}/mounts/{mount}', 'ServersController@addMount')->name('admin.servers.view.mounts.toggle'); Route::post('/view/{server}/manage/toggle', 'ServersController@toggleInstall')->name('admin.servers.view.manage.toggle'); Route::post('/view/{server}/manage/suspension', 'ServersController@manageSuspension')->name('admin.servers.view.manage.suspension'); Route::post('/view/{server}/manage/reinstall', 'ServersController@reinstallServer')->name('admin.servers.view.manage.reinstall'); @@ -133,6 +134,7 @@ Route::group(['prefix' => 'servers'], function () { Route::patch('/view/{server}/database', 'ServersController@resetDatabasePassword'); Route::delete('/view/{server}/database/{database}/delete', 'ServersController@deleteDatabase')->name('admin.servers.view.database.delete'); + Route::delete('/view/{server}/mounts/{mount}', 'ServersController@deleteMount'); }); /*