Skip to content

Commit

Permalink
feat: added button to generate ssh keys
Browse files Browse the repository at this point in the history
  • Loading branch information
lewislarsen committed Jul 7, 2024
1 parent da44d72 commit 689d1f1
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
45 changes: 45 additions & 0 deletions app/Livewire/Other/GenerateSSHKeysButton.php
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;
}
}
1 change: 1 addition & 0 deletions resources/views/layouts/app.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
</code>
{{ __('to create your SSH key.') }}
</span>
@livewire('other.generate-ssh-keys-button')
</div>
</div>
@endif
Expand Down
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 tests/Feature/Livewire/Other/GenerateSSHKeysButtonTest.php
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());
});

0 comments on commit 689d1f1

Please sign in to comment.