From 612460ca1632ff7febd2daf0a0ca347b71546094 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Wed, 3 May 2023 12:38:57 +0200 Subject: [PATCH] change/modify/delete/add private keys --- app/Http/Livewire/PrivateKey/Change.php | 39 +++++++++++++++++++ app/Http/Livewire/PrivateKey/Create.php | 27 +++++++++++++ app/Http/Livewire/Server/New/ByIp.php | 16 +------- app/Http/Livewire/Server/PrivateKey.php | 26 +++++++++++++ app/Models/Team.php | 4 ++ resources/views/dashboard.blade.php | 6 +++ .../livewire/private-key/change.blade.php | 14 +++++++ .../livewire/private-key/create.blade.php | 10 +++++ .../views/livewire/server/form.blade.php | 3 +- .../views/livewire/server/new/by-ip.blade.php | 9 +---- .../livewire/server/private-key.blade.php | 7 ++++ resources/views/private-key/new.blade.php | 4 ++ resources/views/private-key/show.blade.php | 4 ++ resources/views/server/private-key.blade.php | 4 ++ resources/views/server/show.blade.php | 4 ++ routes/web.php | 16 ++++++++ 16 files changed, 169 insertions(+), 24 deletions(-) create mode 100644 app/Http/Livewire/PrivateKey/Change.php create mode 100644 app/Http/Livewire/PrivateKey/Create.php create mode 100644 app/Http/Livewire/Server/PrivateKey.php create mode 100644 resources/views/livewire/private-key/change.blade.php create mode 100644 resources/views/livewire/private-key/create.blade.php create mode 100644 resources/views/livewire/server/private-key.blade.php create mode 100644 resources/views/private-key/new.blade.php create mode 100644 resources/views/private-key/show.blade.php create mode 100644 resources/views/server/private-key.blade.php diff --git a/app/Http/Livewire/PrivateKey/Change.php b/app/Http/Livewire/PrivateKey/Change.php new file mode 100644 index 000000000..bede7aca4 --- /dev/null +++ b/app/Http/Livewire/PrivateKey/Change.php @@ -0,0 +1,39 @@ +delete(); + session('currentTeam')->privateKeys = PrivateKey::where('team_id', session('currentTeam')->id)->get(); + redirect()->route('dashboard'); + } + public function changePrivateKey() + { + try { + $this->private_key_value = trim($this->private_key_value); + if (!str_ends_with($this->private_key_value, "\n")) { + $this->private_key_value .= "\n"; + } + PrivateKey::where('uuid', $this->private_key_uuid)->update([ + 'name' => $this->private_key_name, + 'description' => $this->private_key_description, + 'private_key' => $this->private_key_value, + ]); + session('currentTeam')->privateKeys = PrivateKey::where('team_id', session('currentTeam')->id)->get(); + } catch (\Exception $e) { + $this->addError('private_key_value', $e->getMessage()); + } + } +} diff --git a/app/Http/Livewire/PrivateKey/Create.php b/app/Http/Livewire/PrivateKey/Create.php new file mode 100644 index 000000000..e21f02309 --- /dev/null +++ b/app/Http/Livewire/PrivateKey/Create.php @@ -0,0 +1,27 @@ +private_key_value = trim($this->private_key_value); + if (!str_ends_with($this->private_key_value, "\n")) { + $this->private_key_value .= "\n"; + } + PrivateKey::create([ + 'name' => $this->private_key_name, + 'description' => $this->private_key_description, + 'private_key' => $this->private_key_value, + 'team_id' => session('currentTeam')->id + ]); + session('currentTeam')->privateKeys = PrivateKey::where('team_id', session('currentTeam')->id)->get(); + } +} diff --git a/app/Http/Livewire/Server/New/ByIp.php b/app/Http/Livewire/Server/New/ByIp.php index e5fdafa98..baf638a2f 100644 --- a/app/Http/Livewire/Server/New/ByIp.php +++ b/app/Http/Livewire/Server/New/ByIp.php @@ -15,7 +15,7 @@ class ByIp extends Component public $new_private_key_value; public string $name; - public string $description; + public string|null $description = null; public string $ip; public string $user = 'root'; public int $port = 22; @@ -29,20 +29,6 @@ public function setPrivateKey($private_key_id) { $this->private_key_id = $private_key_id; } - public function addPrivateKey() - { - $this->new_private_key_value = trim($this->new_private_key_value); - if (!str_ends_with($this->new_private_key_value, "\n")) { - $this->new_private_key_value .= "\n"; - } - PrivateKey::create([ - 'name' => $this->new_private_key_name, - 'description' => $this->new_private_key_description, - 'private_key' => $this->new_private_key_value, - 'team_id' => session('currentTeam')->id - ]); - session('currentTeam')->privateKeys = $this->private_keys = PrivateKey::where('team_id', session('currentTeam')->id)->get(); - } public function submit() { if (!$this->private_key_id) { diff --git a/app/Http/Livewire/Server/PrivateKey.php b/app/Http/Livewire/Server/PrivateKey.php new file mode 100644 index 000000000..6e57f4ad2 --- /dev/null +++ b/app/Http/Livewire/Server/PrivateKey.php @@ -0,0 +1,26 @@ +parameters['server_uuid'])->update([ + 'private_key_id' => $private_key_id + ]); + return redirect()->route('server.show', $this->parameters['server_uuid']); + } + public function mount() + { + $this->parameters = Route::current()->parameters(); + $this->private_keys = ModelsPrivateKey::where('team_id', session('currentTeam')->id)->get(); + } +} diff --git a/app/Models/Team.php b/app/Models/Team.php index 4c9938c5b..aaadcbb5a 100644 --- a/app/Models/Team.php +++ b/app/Models/Team.php @@ -24,4 +24,8 @@ public function applications() { return $this->hasManyThrough(Application::class, Project::class); } + public function privateKeys() + { + return $this->hasMany(PrivateKey::class); + } } diff --git a/resources/views/dashboard.blade.php b/resources/views/dashboard.blade.php index 22a1b8310..c13cfd80a 100644 --- a/resources/views/dashboard.blade.php +++ b/resources/views/dashboard.blade.php @@ -14,6 +14,12 @@

Destinations

@forelse ($destinations as $destination) {{ data_get($destination, 'name') }} + @empty +

No destinations found.

+ @endforelse +

Private Keys

+ @forelse ($private_keys as $private_key) + {{ data_get($private_key, 'name') }} @empty

No servers found.

@endforelse diff --git a/resources/views/livewire/private-key/change.blade.php b/resources/views/livewire/private-key/change.blade.php new file mode 100644 index 000000000..9513c6c85 --- /dev/null +++ b/resources/views/livewire/private-key/change.blade.php @@ -0,0 +1,14 @@ +
+
+ + + + + + +
diff --git a/resources/views/livewire/private-key/create.blade.php b/resources/views/livewire/private-key/create.blade.php new file mode 100644 index 000000000..040ecf975 --- /dev/null +++ b/resources/views/livewire/private-key/create.blade.php @@ -0,0 +1,10 @@ +
+
+ + + + + +
diff --git a/resources/views/livewire/server/form.blade.php b/resources/views/livewire/server/form.blade.php index 926fcae96..12cf4856e 100644 --- a/resources/views/livewire/server/form.blade.php +++ b/resources/views/livewire/server/form.blade.php @@ -21,7 +21,8 @@ - + diff --git a/resources/views/livewire/server/new/by-ip.blade.php b/resources/views/livewire/server/new/by-ip.blade.php index e4505b04e..9f6827fdb 100644 --- a/resources/views/livewire/server/new/by-ip.blade.php +++ b/resources/views/livewire/server/new/by-ip.blade.php @@ -20,14 +20,7 @@

Add a new One

-
- - - - - +
diff --git a/resources/views/livewire/server/private-key.blade.php b/resources/views/livewire/server/private-key.blade.php new file mode 100644 index 000000000..ab5b30036 --- /dev/null +++ b/resources/views/livewire/server/private-key.blade.php @@ -0,0 +1,7 @@ +
+ @forelse ($private_keys as $private_key) + + @empty +

No private keys found

+ @endforelse +
diff --git a/resources/views/private-key/new.blade.php b/resources/views/private-key/new.blade.php new file mode 100644 index 000000000..631789b86 --- /dev/null +++ b/resources/views/private-key/new.blade.php @@ -0,0 +1,4 @@ + +

New Private Key

+ +
diff --git a/resources/views/private-key/show.blade.php b/resources/views/private-key/show.blade.php new file mode 100644 index 000000000..0e6c01de1 --- /dev/null +++ b/resources/views/private-key/show.blade.php @@ -0,0 +1,4 @@ + +

Private Key

+ +
diff --git a/resources/views/server/private-key.blade.php b/resources/views/server/private-key.blade.php new file mode 100644 index 000000000..5799eb522 --- /dev/null +++ b/resources/views/server/private-key.blade.php @@ -0,0 +1,4 @@ + +

Select a private Key

+ +
diff --git a/resources/views/server/show.blade.php b/resources/views/server/show.blade.php index 6874693f4..757bf32bb 100644 --- a/resources/views/server/show.blade.php +++ b/resources/views/server/show.blade.php @@ -1,6 +1,10 @@

Server

+

Private Key +

+

{{ $server->privateKey->name }}

Destinations

@if ($server->standaloneDockers) @foreach ($server->standaloneDockers as $docker) diff --git a/routes/web.php b/routes/web.php index d50f74e04..291633b4c 100644 --- a/routes/web.php +++ b/routes/web.php @@ -4,6 +4,7 @@ use App\Http\Controllers\HomeController; use App\Http\Controllers\ProjectController; use App\Models\InstanceSettings; +use App\Models\PrivateKey; use App\Models\StandaloneDocker; use App\Models\SwarmDocker; use Illuminate\Support\Facades\Route; @@ -28,10 +29,13 @@ $destinations = $servers->map(function ($server) { return $server->standaloneDockers->merge($server->swarmDockers); })->flatten(); + $private_keys = session('currentTeam')->load(['privateKeys'])->privateKeys; + return view('dashboard', [ 'servers' => $servers->sortBy('name'), 'projects' => $projects->sortBy('name'), 'destinations' => $destinations->sortBy('name'), + 'private_keys' => $private_keys->sortBy('name'), ]); })->name('dashboard'); @@ -60,6 +64,15 @@ })->name('demo'); }); +Route::middleware(['auth'])->group(function () { + Route::get('/private-key/new', fn () => view('private-key.new'))->name('private-key.new'); + Route::get('/private-key/{private_key_uuid}', function () { + $private_key = PrivateKey::where('uuid', request()->private_key_uuid)->first(); + return view('private-key.show', [ + 'private_key' => $private_key, + ]); + })->name('private-key.show'); +}); Route::middleware(['auth'])->group(function () { Route::get('/server/new', fn () => view('server.new'))->name('server.new'); Route::get('/server/{server_uuid}', function () { @@ -71,6 +84,9 @@ 'server' => $server, ]); })->name('server.show'); + Route::get('/server/{server_uuid}/private-key', function () { + return view('server.private-key'); + })->name('server.private-key'); }); Route::middleware(['auth'])->group(function () {