Skip to content

Commit

Permalink
Merge pull request #25 from mailcarrierapp/feat/token-dashboard
Browse files Browse the repository at this point in the history
[2.x] Add tokens dashboard
  • Loading branch information
danilopolani authored Mar 4, 2024
2 parents c19ff32 + d1cc265 commit 4a1d0ee
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/Actions/Auth/GenerateToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@

namespace MailCarrier\Actions\Auth;

use Carbon\Carbon;
use MailCarrier\Actions\Action;

class GenerateToken extends Action
{
/**
* Ensure that the Auth Manager user exists.
*/
public function run(string $name = 'Unnamed'): string
public function run(string $name = 'Unnamed', array $abilities = ['*'], ?string $expiresAt = null): string
{
$authUser = (new EnsureAuthManagerExists)->run();

return $authUser->createToken($name)->plainTextToken;
if ($expiresAt) {
$expiresAt = Carbon::parse($expiresAt);
}

return $authUser->createToken($name, $abilities, $expiresAt)->plainTextToken;
}
}
2 changes: 2 additions & 0 deletions src/Providers/Filament/MailCarrierPanelProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Illuminate\Session\Middleware\StartSession;
use Illuminate\View\Middleware\ShareErrorsFromSession;
use MailCarrier\Pages\Login;
use MailCarrier\Resources\ApiTokenResource;
use MailCarrier\Resources\LayoutResource;
use MailCarrier\Resources\LogResource;
use MailCarrier\Resources\TemplateResource;
Expand Down Expand Up @@ -47,6 +48,7 @@ public function panel(Panel $panel): Panel
LogResource::class,
LayoutResource::class,
TemplateResource::class,
ApiTokenResource::class,
])
->discoverPages(in: app_path('Filament/Pages'), for: 'MailCarrier\\Pages')
->pages([
Expand Down
53 changes: 53 additions & 0 deletions src/Resources/ApiTokenResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace MailCarrier\Resources;

use Filament\Resources\Resource;
use Filament\Tables;
use Laravel\Sanctum\PersonalAccessToken;
use MailCarrier\Resources\ApiTokenResource\Pages;

class ApiTokenResource extends Resource
{
protected static ?string $model = PersonalAccessToken::class;

protected static ?string $modelLabel = 'API Tokens';

protected static ?string $navigationIcon = 'heroicon-o-key';

protected static ?string $navigationGroup = 'Management';

/**
* List all the records.
*/
public static function table(Tables\Table $table): Tables\Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')
->placeholder('No name provided'),

Tables\Columns\TextColumn::make('expires_at')
->label('Expiration date (UTC)')
->dateTime()
->placeholder('Never expires'),

Tables\Columns\TextColumn::make('last_used_at')
->dateTime()
->placeholder('Never used'),
])
->actions([
Tables\Actions\DeleteAction::make(),
]);
}

/**
* Get Filament CRUD pages.
*/
public static function getPages(): array
{
return [
'index' => Pages\ListApiTokens::route('/'),
];
}
}
84 changes: 84 additions & 0 deletions src/Resources/ApiTokenResource/Actions/CreateAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace MailCarrier\Resources\ApiTokenResource\Actions;

use Carbon\Carbon;
use Filament\Actions\Action;
use Filament\Actions\Concerns\CanCustomizeProcess;
use Filament\Forms;
use Filament\Support\Enums\Alignment;
use Filament\Support\Enums\MaxWidth;
use MailCarrier\Actions\Auth\GenerateToken;
use MailCarrier\Resources\ApiTokenResource;

class CreateAction extends Action
{
use CanCustomizeProcess;

const GENERATED_TOKEN_FIELD_NAME = 'generated_token';

public static function getDefaultName(): ?string
{
return 'create';
}

protected function setUp(): void
{
parent::setUp();

$this->label(fn (): string => __('filament-actions::create.single.label', [
'label' => 'API Token',
]));

$this->authorize(fn (): bool => ApiTokenResource::canCreate());

$this->modalHeading(fn (): string => __('filament-actions::create.single.modal.heading', [
'label' => 'API Token',
]));

$this->modalSubmitActionLabel(__('filament-actions::create.single.modal.actions.create.label'));

$this->successNotificationTitle(__('filament-actions::create.single.notifications.created.title'));

$this->modalFooterActionsAlignment(Alignment::End);

$this->modalWidth(MaxWidth::Large);

$this->record(null);

$this->form([
Forms\Components\TextInput::make('name')
->hidden(fn (Forms\Get $get) => !is_null($get(static::GENERATED_TOKEN_FIELD_NAME)))
->required(),

Forms\Components\DateTimePicker::make('expires_at')
->minDate(Carbon::now())
->label('Expiration date (UTC)')
->hidden(fn (Forms\Get $get) => !is_null($get(static::GENERATED_TOKEN_FIELD_NAME)))
->helperText('Leave empty to never expire'),

Forms\Components\TextInput::make('generated_token')
->readOnly()
->dehydrated(false)
->helperText('Copy it to a safe place, it won\'t be shown anymore')
->extraInputAttributes([
'onClick' => 'this.select()',
])
->visible(fn (Forms\Get $get) => !is_null($get(static::GENERATED_TOKEN_FIELD_NAME))),
]);

$this->action(function (array $data, Forms\Form $form): void {
$form->fill([
static::GENERATED_TOKEN_FIELD_NAME => GenerateToken::resolve()->run(
$data['name'],
expiresAt: $data['expires_at'],
),
]);

$this->modalSubmitAction(false);
$this->modalCancelActionLabel('Close');
$this->sendSuccessNotification();
$this->halt();
});
}
}
19 changes: 19 additions & 0 deletions src/Resources/ApiTokenResource/Pages/ListApiTokens.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace MailCarrier\Resources\ApiTokenResource\Pages;

use Filament\Resources\Pages\ListRecords;
use MailCarrier\Resources\ApiTokenResource;
use MailCarrier\Resources\ApiTokenResource\Actions\CreateAction;

class ListApiTokens extends ListRecords
{
protected static string $resource = ApiTokenResource::class;

protected function getHeaderActions(): array
{
return [
CreateAction::make(),
];
}
}

0 comments on commit 4a1d0ee

Please sign in to comment.