-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added button to generate ssh keys
- Loading branch information
1 parent
da44d72
commit 689d1f1
Showing
4 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace App\Livewire\Other; | ||
|
||
use Illuminate\Contracts\View\View; | ||
use Illuminate\Support\Facades\Artisan; | ||
use Illuminate\Support\Facades\Auth; | ||
use Illuminate\Support\Facades\Log; | ||
use Livewire\Component; | ||
use Masmerise\Toaster\Toaster; | ||
|
||
class GenerateSSHKeysButton extends Component | ||
{ | ||
public function generateKeys(): void | ||
{ | ||
if (! $this->checkAdmin()) { | ||
return; | ||
} | ||
|
||
// All the key generation is handled in this command. | ||
// We're providing a button as a wrapper to help. | ||
Artisan::call('vanguard:generate-ssh-key'); | ||
|
||
Log::info('SSH key generation initiated via button.', ['user_id' => Auth::id()]); | ||
|
||
Toaster::success(__('SSH key generation started. Please reload the page.')); | ||
} | ||
|
||
public function render(): View | ||
{ | ||
return view('livewire.other.generate-ssh-keys-button'); | ||
} | ||
|
||
private function checkAdmin(): bool | ||
{ | ||
if (! Auth::user()?->isAdmin()) { | ||
Toaster::error(__('You are not authorized to generate SSH keys.')); | ||
Log::error('Non-admin user attempted to generate SSH keys.', ['user_id' => Auth::id()]); | ||
|
||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
resources/views/livewire/other/generate-ssh-keys-button.blade.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<div class="inline-flex"> | ||
<button wire:click="generateKeys" class="ml-2 bg-red-400/65 hover:bg-red-400/90 p-2 px-6 text-sm rounded-[.70rem] focus:outline-none focus:ring-2 focus:ring-red-600 focus:ring-offset-2 transition ease-in-out duration-150"> | ||
@svg('heroicon-o-play', ['class' => 'h-4 -w-4 -mt-1 inline']) | ||
{{ __('Generate SSH Keys') }} | ||
</button> | ||
</div> |
45 changes: 45 additions & 0 deletions
45
tests/Feature/Livewire/Other/GenerateSSHKeysButtonTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
use App\Livewire\Other\GenerateSSHKeysButton; | ||
use App\Models\User; | ||
|
||
test('the button can be rendered', function (): void { | ||
|
||
Livewire::test(GenerateSSHKeysButton::class) | ||
->assertStatus(200); | ||
}); | ||
|
||
test('an administrator can click the button', function (): void { | ||
Toaster::fake(); | ||
Config::set('auth.admin_email_addresses', ['[email protected]']); | ||
|
||
$user = User::factory()->create(['email' => '[email protected]']); | ||
|
||
$this->actingAs($user); | ||
|
||
Artisan::shouldReceive('call') | ||
->once() | ||
->with('vanguard:generate-ssh-key') | ||
->andReturn(0); | ||
|
||
Livewire::test(GenerateSSHKeysButton::class) | ||
->call('generateKeys'); | ||
|
||
$this->assertTrue($user->isAdmin()); | ||
|
||
Toaster::assertDispatched(__('SSH key generation started. Please reload the page.')); | ||
}); | ||
|
||
test('a regular user cannot click the button', function (): void { | ||
Toaster::fake(); | ||
|
||
$user = User::factory()->create(); | ||
|
||
$this->actingAs($user); | ||
|
||
Livewire::test(GenerateSSHKeysButton::class) | ||
->call('generateKeys'); | ||
|
||
Toaster::assertDispatched(__('You are not authorized to generate SSH keys.')); | ||
$this->assertFalse($user->isAdmin()); | ||
}); |